web_translate_it 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72c1ce39a90e605ff2b39b63440181d91049014e83d2eba2506185ffe901c3c1
4
- data.tar.gz: 5051fbc9a0f55f21351c821098aef8bd4111f1be4d735706389eab5d231c7f60
3
+ metadata.gz: d01e00c6188bc51a7520d8a712f988f14125e606fd27a8cd7a8d1576075a433d
4
+ data.tar.gz: 5c1e552315ce4837b6505f2af0e1f911f4a625bdfcc3011f432502766e8c21b3
5
5
  SHA512:
6
- metadata.gz: 830cf16e7dfdd3ece01f3bce72e5c60d750facaade45aa8626035acf8916b6524590ebd88d0015d7874499177b216c340eb7a0c563665883fb2945ab820d9b97
7
- data.tar.gz: b631196b93ffb7a6e4e48b207a958d0e626a6f1e68bcbc825cb5e6db971a727e8cb74405af49f36ad1976275efa2d349b1772ee553a401cbd11df12577e79ff9
6
+ metadata.gz: 6a13defd7a2f234d187d496a4da810b56abd06a98a07458e9110ca81be81aa93388c5d65f7804a5ed7fcacea0d2ddc617d2bc1be66c6baf57d98fa545710bf4f
7
+ data.tar.gz: 888436899b2d2f0e00c734f9bbb20a1692f9b4a3d1aa1cb10ce100215bc3b5d362d19176f2c5855baa70ce312adf5ee5415c489a6ee71ba4ebb0a292266120d4
data/history.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## Version 3.4.0 / 2026-11-04
2
+
3
+ * **The project API key is now sent in an `Authorization: Bearer` header instead of in the URL.** Requests go to `/api/projects/current/...` rather than `/api/projects/<api key>/...`, so the key no longer ends up in proxy, load balancer and server logs. webtranslateit.com accepts the header since 2026-11-04. Earlier `wti` versions keep working: the API still accepts the key in the URL.
4
+ * Caution: `wti --debug` prints the raw HTTP exchange through Net::HTTP, which now includes the `Authorization` header and therefore your API key. Don't paste debug output into tickets or CI logs.
5
+ * `wti init` keeps the API token out of `.wti` when `WTI_PROJECT_API_KEY` is set: the generated file reads `api_key: <%= ENV['WTI_PROJECT_API_KEY'] %>`, so it can be committed safely. Without parameters, `wti init` uses the token from the environment instead of prompting for it. Otherwise `wti init` still writes the token, and now warns when `.wti` is inside a git repository and not ignored. It also warns when `WTI_PROJECT_API_KEY` is set to a different token than the one passed, since the environment takes precedence.
6
+ * Document `WTI_PROJECT_API_KEY` and ERB in `.wti` in the readme.
7
+
1
8
  ## Version 3.3.0 / 2026-09-17
2
9
 
3
10
  * Add `--zip` option to `wti pull`. It downloads the language files through the zip file endpoint: one request per locale instead of one request per file, and a single request when the whole project is pulled. Much faster on projects with many files, for instance when pulling translations in a build. Files already up to date are left alone, only the locales needing an update are requested, and an archive which fails to download fails its own files instead of aborting the pull. #473
@@ -19,7 +19,7 @@ module WebTranslateIt
19
19
 
20
20
  def self.find_all(connection, params = {}) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
21
21
  params = params.transform_keys(&:to_s)
22
- url = "/api/projects/#{connection.api_key}/#{resource_path}"
22
+ url = "#{Connection::PROJECT_PATH}/#{resource_path}"
23
23
  url += "?#{HashUtil.to_params(filter_params(params))}" unless params.empty?
24
24
 
25
25
  Concurrency.with_retries do
@@ -43,7 +43,7 @@ module WebTranslateIt
43
43
 
44
44
  def self.find(connection, id)
45
45
  Concurrency.with_retries do
46
- response = connection.get("/api/projects/#{connection.api_key}/#{resource_path}/#{id}")
46
+ response = connection.get("#{Connection::PROJECT_PATH}/#{resource_path}/#{id}")
47
47
  return nil if response.code.to_i == 404
48
48
 
49
49
  record = new(JSON.parse(response.body), connection: connection)
@@ -66,17 +66,17 @@ module WebTranslateIt
66
66
 
67
67
  def delete
68
68
  Concurrency.with_retries do
69
- HttpResponse.handle_response(connection.delete("/api/projects/#{connection.api_key}/#{self.class.resource_path}/#{id}"))
69
+ HttpResponse.handle_response(connection.delete("#{Connection::PROJECT_PATH}/#{self.class.resource_path}/#{id}"))
70
70
  end
71
71
  end
72
72
 
73
- def translation_for(locale) # rubocop:todo Metrics/AbcSize
73
+ def translation_for(locale)
74
74
  translation = translations.detect { |t| t.locale == locale }
75
75
  return translation if translation
76
76
  return nil if new_record
77
77
 
78
78
  Concurrency.with_retries do
79
- response = HttpResponse.handle_response(connection.get("/api/projects/#{connection.api_key}/#{self.class.resource_path}/#{id}/locales/#{locale}/translations"))
79
+ response = HttpResponse.handle_response(connection.get("#{Connection::PROJECT_PATH}/#{self.class.resource_path}/#{id}/locales/#{locale}/translations"))
80
80
  json = JSON.parse(response)
81
81
  return nil if json.empty?
82
82
 
@@ -106,13 +106,13 @@ module WebTranslateIt
106
106
  end
107
107
 
108
108
  Concurrency.with_retries do
109
- HttpResponse.handle_response(connection.put("/api/projects/#{connection.api_key}/#{self.class.resource_path}/#{id}", body: to_json))
109
+ HttpResponse.handle_response(connection.put("#{Connection::PROJECT_PATH}/#{self.class.resource_path}/#{id}", body: to_json))
110
110
  end
111
111
  end
112
112
 
113
113
  def create
114
114
  Concurrency.with_retries do
115
- raw = connection.post("/api/projects/#{connection.api_key}/#{self.class.resource_path}", body: to_json(with_translations: true))
115
+ raw = connection.post("#{Connection::PROJECT_PATH}/#{self.class.resource_path}", body: to_json(with_translations: true))
116
116
  response = JSON.parse(HttpResponse.handle_response(raw))
117
117
  self.id = response['id']
118
118
  self.new_record = false
@@ -6,12 +6,15 @@ module WebTranslateIt
6
6
 
7
7
  class Init < Base
8
8
 
9
+ ENV_API_KEY = 'WTI_PROJECT_API_KEY'
10
+
9
11
  def call
10
12
  puts '# Initializing project'
11
13
  api_key, path = read_parameters
12
14
  ensure_directory_exists(path)
13
15
  project_info = JSON.parse(WebTranslateIt::Project.fetch_info(api_key))['project']
14
16
  write_configuration(path, api_key, project_info)
17
+ warn_if_key_committable(path) unless key_from_env?(api_key)
15
18
  print_success(project_info)
16
19
  true
17
20
  end
@@ -20,12 +23,46 @@ module WebTranslateIt
20
23
 
21
24
  def read_parameters
22
25
  if parameters.any?
26
+ warn_env_takes_precedence if env_api_key && parameters[0] != env_api_key
23
27
  [parameters[0], '.wti']
28
+ elsif env_api_key
29
+ puts " Using the Project API Key from #{ENV_API_KEY}."
30
+ [env_api_key, Prompt.ask(' Path to configuration file:', '.wti')]
24
31
  else
25
32
  [Prompt.ask(' Project API Key:'), Prompt.ask(' Path to configuration file:', '.wti')]
26
33
  end
27
34
  end
28
35
 
36
+ def env_api_key
37
+ ENV.fetch(ENV_API_KEY, nil).to_s.strip.then { |key| key.empty? ? nil : key }
38
+ end
39
+
40
+ # The key came from the environment, so the configuration file can read it
41
+ # from there too and never hold the secret itself.
42
+ def key_from_env?(api_key)
43
+ !env_api_key.nil? && api_key == env_api_key
44
+ end
45
+
46
+ def warn_env_takes_precedence
47
+ puts StringUtil.important(" Warning: #{ENV_API_KEY} is set and takes precedence over the key in the configuration file.")
48
+ end
49
+
50
+ # A `.wti` holding the raw key is safe on disk but not in git history. Only
51
+ # warn inside a git work tree, where the file is about to be committable.
52
+ def warn_if_key_committable(path)
53
+ return unless git?('rev-parse', '--is-inside-work-tree')
54
+ return if git?('check-ignore', '-q', path)
55
+
56
+ puts ''
57
+ puts StringUtil.important(" Warning: `#{path}` contains your Project API Key and is not ignored by git.")
58
+ puts " Add `#{path}` to .gitignore, or keep the key out of the file: set #{ENV_API_KEY}"
59
+ puts " and use `api_key: <%= ENV['#{ENV_API_KEY}'] %>` in `#{path}`."
60
+ end
61
+
62
+ def git?(*args)
63
+ system('git', *args, out: File::NULL, err: File::NULL) == true
64
+ end
65
+
29
66
  def print_success(project_info)
30
67
  puts ''
31
68
  puts " The project #{project_info['name']} was successfully initialized."
@@ -71,12 +108,17 @@ module WebTranslateIt
71
108
  puts ''
72
109
  end
73
110
 
111
+ def api_key_line(api_key)
112
+ key_from_env?(api_key) ? "<%= ENV['#{ENV_API_KEY}'] %>" : api_key
113
+ end
114
+
74
115
  def generate_configuration(api_key, project_info)
75
116
  <<~FILE
76
117
  # Required - The Project API Token from WebTranslateIt.com
77
118
  # More information: https://github.com/webtranslateit/webtranslateit/wiki#configuration-file
119
+ # The #{ENV_API_KEY} environment variable, when set, takes precedence over this value.
78
120
 
79
- api_key: #{api_key}
121
+ api_key: #{api_key_line(api_key)}
80
122
 
81
123
  # Optional - Locales not to sync with WebTranslateIt.
82
124
  # Takes a string, a symbol, or an array of string or symbol.
@@ -95,7 +95,7 @@ module WebTranslateIt
95
95
 
96
96
  # Convenience method which returns the endpoint for fetching a list of locales for a project.
97
97
  def api_url
98
- "/api/projects/#{api_key}"
98
+ Connection::PROJECT_PATH
99
99
  end
100
100
 
101
101
  # Returns a logger. If RAILS_DEFAULT_LOGGER is defined, use it, else, define a new logger.
@@ -4,6 +4,9 @@ module WebTranslateIt
4
4
 
5
5
  class Connection
6
6
 
7
+ # The API resolves `current` to the project the bearer token belongs to.
8
+ PROJECT_PATH = '/api/projects/current'
9
+
7
10
  @debug = false
8
11
 
9
12
  class << self
@@ -43,6 +46,7 @@ module WebTranslateIt
43
46
  yield self if block_given?
44
47
  end
45
48
 
49
+ # Caution: Net::HTTP's debug stream prints request headers, including Authorization.
46
50
  def self.turn_debug_on
47
51
  @debug = true
48
52
  end
@@ -68,6 +72,7 @@ module WebTranslateIt
68
72
  def api_request(method_class, path, body: nil)
69
73
  request = method_class.new(path)
70
74
  HttpResponse.add_fields(request)
75
+ request['Authorization'] = "Bearer #{api_key}"
71
76
  request.body = body if body
72
77
  yield request if block_given?
73
78
  http_connection.request(request)
@@ -7,13 +7,13 @@ module WebTranslateIt
7
7
  def self.fetch_info(api_key)
8
8
  Concurrency.with_retries do
9
9
  WebTranslateIt::Connection.new(api_key) do |conn|
10
- return HttpResponse.handle_response(conn.get("/api/projects/#{api_key}"))
10
+ return HttpResponse.handle_response(conn.get(Connection::PROJECT_PATH))
11
11
  end
12
12
  end
13
13
  end
14
14
 
15
15
  def self.fetch_stats(api_key, file_id = nil)
16
- url = file_id.nil? ? "/api/projects/#{api_key}/stats" : "/api/projects/#{api_key}/stats?file=#{file_id}"
16
+ url = file_id.nil? ? "#{Connection::PROJECT_PATH}/stats" : "#{Connection::PROJECT_PATH}/stats?file=#{file_id}"
17
17
  Concurrency.with_retries do
18
18
  WebTranslateIt::Connection.new(api_key) do |conn|
19
19
  return HttpResponse.handle_response(conn.get(url))
@@ -24,7 +24,7 @@ module WebTranslateIt
24
24
  # Fetch every language file of a project in a single request, as a zip
25
25
  # archive. Pass a locale to narrow the archive down to that locale.
26
26
  def self.fetch_zip(connection, locale: nil)
27
- url = "/api/projects/#{connection.api_key}/zip_file"
27
+ url = "#{Connection::PROJECT_PATH}/zip_file"
28
28
  url += "?locale=#{URI.encode_www_form_component(locale)}" if locale
29
29
  Concurrency.with_retries do
30
30
  HttpResponse.handle_response(connection.get(url))
@@ -33,14 +33,14 @@ module WebTranslateIt
33
33
 
34
34
  def self.create_locale(connection, locale_code)
35
35
  Concurrency.with_retries do
36
- response = connection.post("/api/projects/#{connection.api_key}/locales") { |req| req.set_form_data({'id' => locale_code}, ';') }
36
+ response = connection.post("#{Connection::PROJECT_PATH}/locales") { |req| req.set_form_data({'id' => locale_code}, ';') }
37
37
  HttpResponse.handle_response(response)
38
38
  end
39
39
  end
40
40
 
41
41
  def self.delete_locale(connection, locale_code)
42
42
  Concurrency.with_retries do
43
- HttpResponse.handle_response(connection.delete("/api/projects/#{connection.api_key}/locales/#{locale_code}"))
43
+ HttpResponse.handle_response(connection.delete("#{Connection::PROJECT_PATH}/locales/#{locale_code}"))
44
44
  end
45
45
  end
46
46
 
@@ -43,7 +43,7 @@ module WebTranslateIt
43
43
 
44
44
  def update
45
45
  Concurrency.with_retries do
46
- HttpResponse.handle_response(connection.put("/api/projects/#{connection.api_key}/terms/#{id}/locales/#{locale}/translations/#{id}", body: to_json))
46
+ HttpResponse.handle_response(connection.put("#{Connection::PROJECT_PATH}/terms/#{id}/locales/#{locale}/translations/#{id}", body: to_json))
47
47
  end
48
48
  end
49
49
 
@@ -30,7 +30,7 @@ module WebTranslateIt
30
30
  def assign_attributes(_params); end
31
31
 
32
32
  def translation_path
33
- "/api/projects/#{connection.api_key}/#{self.class.parent_resource_path}/#{parent_id}/locales/#{locale}/translations"
33
+ "#{Connection::PROJECT_PATH}/#{self.class.parent_resource_path}/#{parent_id}/locales/#{locale}/translations"
34
34
  end
35
35
 
36
36
  end
@@ -188,15 +188,15 @@ module WebTranslateIt
188
188
 
189
189
  # Convenience method which returns the URL of the API endpoint for a locale.
190
190
  def api_url
191
- "/api/projects/#{api_key}/files/#{id}/locales/#{locale}"
191
+ "#{Connection::PROJECT_PATH}/files/#{id}/locales/#{locale}"
192
192
  end
193
193
 
194
194
  def api_url_for_create
195
- "/api/projects/#{api_key}/files"
195
+ "#{Connection::PROJECT_PATH}/files"
196
196
  end
197
197
 
198
198
  def api_url_for_delete
199
- "/api/projects/#{api_key}/files/#{id}"
199
+ "#{Connection::PROJECT_PATH}/files/#{id}"
200
200
  end
201
201
 
202
202
  def local_checksum
data/readme.md CHANGED
@@ -61,6 +61,18 @@ Check `wti --help` for help.
61
61
 
62
62
  If you’d like to specify another path for your configuration file, you can use `wti init`. This command will ask you to enter your project API token and where to save the configuration file (by default it will create a `.wti` in your project root directory).
63
63
 
64
+ ### Keeping the API token out of `.wti`
65
+
66
+ `wti init` writes the API token into `.wti`. That's fine on your machine, but if you commit `.wti`, the token ends up in your repository's history. `wti init` warns you when `.wti` isn't in your `.gitignore`.
67
+
68
+ To commit `.wti` without the token, set the `WTI_PROJECT_API_KEY` environment variable. It always takes precedence over the `api_key` in `.wti`. If it's set when you run `wti init`, the generated file reads the token from the environment instead of storing it:
69
+
70
+ ``` yaml
71
+ api_key: <%= ENV['WTI_PROJECT_API_KEY'] %>
72
+ ```
73
+
74
+ `.wti` is read through ERB, so you can point `api_key` at any environment variable this way. In CI, store the token as a secret and expose it as `WTI_PROJECT_API_KEY`.
75
+
64
76
  Now you’re all set and you can use the `wti` commands on your project.
65
77
 
66
78
  ## Using on multiple projects
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_translate_it
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edouard Briere