Skip to content

Commit 8f598d6

Browse files
committed
Make oauth exchange opt-in for now
1 parent b76e123 commit 8f598d6

File tree

4 files changed

+55
-44
lines changed

4 files changed

+55
-44
lines changed

lib/hex/repo.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ defmodule Hex.Repo do
6464
url: trusted_mirror_url || mirror_url,
6565
public_key: @hexpm_public_key,
6666
auth_key: auth_key,
67+
oauth_exchange: true,
6768
trusted: trusted_mirror_url != nil or mirror_url == nil
6869
}
6970
end
@@ -73,6 +74,7 @@ defmodule Hex.Repo do
7374
url: @hexpm_url,
7475
public_key: @hexpm_public_key,
7576
auth_key: nil,
77+
oauth_exchange: true,
7678
trusted: true
7779
}
7880
end

lib/mix/tasks/hex.repo.ex

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ defmodule Mix.Tasks.Hex.Repo do
4141
4242
* `--fetch-public-key FINGERPRINT` - Download public key from the repository and verify against the fingerprint (optional).
4343
44-
* `--no-oauth-exchange` - Disable OAuth token exchange for API keys. Use the API key directly instead of exchanging it for a short-lived OAuth token (optional).
44+
* `--oauth-exchange` - Enable OAuth token exchange for API keys. Exchange the API key for a short-lived OAuth token
45+
instead of using the API key directly. Defaults to enabled for hexpm, disabled for other repositories.
46+
In the future, this will default to enabled for all repositories (optional).
47+
48+
* `--no-oauth-exchange` - Disable OAuth token exchange for API keys. Use the API key directly instead of exchanging
49+
it for a short-lived OAuth token. Currently a no-op, but will disable OAuth token exchange in the future (optional).
4550
4651
* `--oauth-exchange-url URL` - Custom URL for OAuth token exchange. By default, the API URL is used (optional).
4752
@@ -50,6 +55,7 @@ defmodule Mix.Tasks.Hex.Repo do
5055
$ mix hex.repo set NAME --url URL
5156
$ mix hex.repo set NAME --public-key PATH
5257
$ mix hex.repo set NAME --auth-key KEY
58+
$ mix hex.repo set NAME --oauth-exchange
5359
$ mix hex.repo set NAME --no-oauth-exchange
5460
$ mix hex.repo set NAME --oauth-exchange-url URL
5561
@@ -72,14 +78,14 @@ defmodule Mix.Tasks.Hex.Repo do
7278
public_key: :string,
7379
auth_key: :string,
7480
fetch_public_key: :string,
75-
no_oauth_exchange: :boolean,
81+
oauth_exchange: :boolean,
7682
oauth_exchange_url: :string
7783
]
7884
@set_switches [
7985
url: :string,
8086
public_key: :string,
8187
auth_key: :string,
82-
no_oauth_exchange: :boolean,
88+
oauth_exchange: :boolean,
8389
oauth_exchange_url: :string
8490
]
8591
@show_switches [
@@ -143,15 +149,17 @@ defmodule Mix.Tasks.Hex.Repo do
143149
end
144150

145151
defp add(name, url, opts) do
146-
opts_with_exchange = normalize_oauth_exchange_opt(opts)
152+
# Default oauth_exchange to true for hexpm/repo.hex.pm, false for others
153+
default_oauth_exchange = name == "hexpm"
154+
oauth_exchange = Keyword.get(opts, :oauth_exchange, default_oauth_exchange)
147155

148156
public_key =
149-
read_public_key(opts_with_exchange[:public_key]) ||
157+
read_public_key(opts[:public_key]) ||
150158
fetch_public_key(
151-
opts_with_exchange[:fetch_public_key],
159+
opts[:fetch_public_key],
152160
url,
153-
opts_with_exchange[:auth_key],
154-
opts_with_exchange[:oauth_exchange]
161+
opts[:auth_key],
162+
oauth_exchange
155163
)
156164

157165
repo =
@@ -160,11 +168,11 @@ defmodule Mix.Tasks.Hex.Repo do
160168
public_key: nil,
161169
fetch_public_key: nil,
162170
auth_key: nil,
163-
oauth_exchange: true,
171+
oauth_exchange: oauth_exchange,
164172
oauth_exchange_url: nil,
165173
trusted: true
166174
}
167-
|> Map.merge(Map.new(opts_with_exchange))
175+
|> Map.merge(Map.new(opts))
168176
|> Map.put(:public_key, public_key)
169177

170178
Hex.State.fetch!(:repos)
@@ -180,8 +188,6 @@ defmodule Mix.Tasks.Hex.Repo do
180188
opts
181189
end
182190

183-
opts = normalize_oauth_exchange_opt(opts)
184-
185191
Hex.State.fetch!(:repos)
186192
|> Map.update!(name, &Map.merge(&1, Map.new(opts)))
187193
|> Hex.Config.update_repos()
@@ -299,16 +305,4 @@ defmodule Mix.Tasks.Hex.Repo do
299305
Mix.raise("Config does not contain repo #{name}")
300306
end
301307
end
302-
303-
defp normalize_oauth_exchange_opt(opts) do
304-
if Keyword.has_key?(opts, :no_oauth_exchange) do
305-
oauth_exchange = !opts[:no_oauth_exchange]
306-
307-
opts
308-
|> Keyword.delete(:no_oauth_exchange)
309-
|> Keyword.put(:oauth_exchange, oauth_exchange)
310-
else
311-
opts
312-
end
313-
end
314308
end

test/mix/tasks/hex.repo_test.exs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ defmodule Mix.Tasks.Hex.RepoTest do
7171
"--fetch-public-key",
7272
@public_key_fingerprint,
7373
"--auth-key",
74-
"AAAA",
75-
"--no-oauth-exchange"
74+
"AAAA"
7675
])
7776

7877
assert [
@@ -180,24 +179,24 @@ defmodule Mix.Tasks.Hex.RepoTest do
180179
end
181180

182181
describe "OAuth exchange configuration" do
183-
test "add with --no-oauth-exchange disables OAuth token exchange" do
182+
test "add with --oauth-exchange enables OAuth token exchange" do
184183
in_tmp(fn ->
185184
Hex.State.put(:config_home, File.cwd!())
186185

187186
Mix.Tasks.Hex.Repo.run([
188187
"add",
189188
"reponame",
190189
"http://example.com",
191-
"--no-oauth-exchange"
190+
"--oauth-exchange"
192191
])
193192

194193
config = Hex.Config.read()
195194
repo = config[:"$repos"]["reponame"]
196-
assert repo.oauth_exchange == false
195+
assert repo.oauth_exchange == true
197196
end)
198197
end
199198

200-
test "add without --no-oauth-exchange enables OAuth token exchange by default" do
199+
test "add without --oauth-exchange disables OAuth token exchange by default for non-hexpm repos" do
201200
in_tmp(fn ->
202201
Hex.State.put(:config_home, File.cwd!())
203202

@@ -209,7 +208,7 @@ defmodule Mix.Tasks.Hex.RepoTest do
209208

210209
config = Hex.Config.read()
211210
repo = config[:"$repos"]["reponame"]
212-
assert repo.oauth_exchange == true
211+
assert repo.oauth_exchange == false
213212
end)
214213
end
215214

@@ -231,36 +230,36 @@ defmodule Mix.Tasks.Hex.RepoTest do
231230
end)
232231
end
233232

234-
test "add with both --no-oauth-exchange and --oauth-exchange-url" do
233+
test "add with both --oauth-exchange and --oauth-exchange-url" do
235234
in_tmp(fn ->
236235
Hex.State.put(:config_home, File.cwd!())
237236

238237
Mix.Tasks.Hex.Repo.run([
239238
"add",
240239
"reponame",
241240
"http://example.com",
242-
"--no-oauth-exchange",
241+
"--oauth-exchange",
243242
"--oauth-exchange-url",
244243
"http://custom-oauth.com"
245244
])
246245

247246
config = Hex.Config.read()
248247
repo = config[:"$repos"]["reponame"]
249-
assert repo.oauth_exchange == false
248+
assert repo.oauth_exchange == true
250249
assert repo.oauth_exchange_url == "http://custom-oauth.com"
251250
end)
252251
end
253252

254-
test "set with --no-oauth-exchange disables OAuth token exchange" do
253+
test "set with --oauth-exchange enables OAuth token exchange" do
255254
in_tmp(fn ->
256255
Hex.State.put(:config_home, File.cwd!())
257256

258257
Mix.Tasks.Hex.Repo.run(["add", "reponame", "http://example.com"])
259-
Mix.Tasks.Hex.Repo.run(["set", "reponame", "--no-oauth-exchange"])
258+
Mix.Tasks.Hex.Repo.run(["set", "reponame", "--oauth-exchange"])
260259

261260
config = Hex.Config.read()
262261
repo = config[:"$repos"]["reponame"]
263-
assert repo.oauth_exchange == false
262+
assert repo.oauth_exchange == true
264263
end)
265264
end
266265

@@ -290,8 +289,7 @@ defmodule Mix.Tasks.Hex.RepoTest do
290289
Mix.Tasks.Hex.Repo.run([
291290
"add",
292291
"reponame",
293-
"http://example.com",
294-
"--no-oauth-exchange"
292+
"http://example.com"
295293
])
296294

297295
Mix.Tasks.Hex.Repo.run(["show", "reponame", "--oauth-exchange"])
@@ -318,7 +316,7 @@ defmodule Mix.Tasks.Hex.RepoTest do
318316
end)
319317
end
320318

321-
test "add with auth key and --no-oauth-exchange" do
319+
test "add with auth key defaults to no oauth exchange for non-hexpm repos" do
322320
in_tmp(fn ->
323321
Hex.State.put(:config_home, File.cwd!())
324322

@@ -327,8 +325,7 @@ defmodule Mix.Tasks.Hex.RepoTest do
327325
"reponame",
328326
"http://example.com",
329327
"--auth-key",
330-
"my-api-key",
331-
"--no-oauth-exchange"
328+
"my-api-key"
332329
])
333330

334331
config = Hex.Config.read()
@@ -338,15 +335,15 @@ defmodule Mix.Tasks.Hex.RepoTest do
338335
end)
339336
end
340337

341-
test "list displays repos with OAuth exchange disabled" do
338+
test "list displays repos with different OAuth exchange settings" do
342339
in_tmp(fn ->
343340
Hex.State.put(:config_home, File.cwd!())
344341

345342
Mix.Tasks.Hex.Repo.run([
346343
"add",
347344
"repo1",
348345
"http://example1.com",
349-
"--no-oauth-exchange"
346+
"--oauth-exchange"
350347
])
351348

352349
Mix.Tasks.Hex.Repo.run(["add", "repo2", "http://example2.com"])
@@ -371,5 +368,22 @@ defmodule Mix.Tasks.Hex.RepoTest do
371368
assert all_output =~ "repo2"
372369
end)
373370
end
371+
372+
test "add repo named 'hexpm' defaults to oauth_exchange enabled" do
373+
in_tmp(fn ->
374+
Hex.State.put(:config_home, File.cwd!())
375+
376+
Mix.Tasks.Hex.Repo.run([
377+
"add",
378+
"hexpm",
379+
"http://example.com"
380+
])
381+
382+
# Check the actual repo state (after merging with defaults)
383+
# not the raw config (which may have oauth_exchange removed if it matches default)
384+
repo = Hex.State.fetch!(:repos)["hexpm"]
385+
assert repo.oauth_exchange == true
386+
end)
387+
end
374388
end
375389
end

test/support/case.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ defmodule HexTest.Case do
258258
Hex.State.update!(:repos, &put_in(&1["hexpm"].url, "http://localhost:4043/repo"))
259259
Hex.State.update!(:repos, &put_in(&1["hexpm"].public_key, public_key))
260260
Hex.State.update!(:repos, &put_in(&1["hexpm"].auth_key, nil))
261+
Hex.State.update!(:repos, &put_in(&1["hexpm"].oauth_exchange, true))
261262
Hex.State.put(:repos_key, nil)
262263
Hex.State.put(:pbkdf2_iters, 10)
263264
Hex.State.put(:clean_pass, false)

0 commit comments

Comments
 (0)