Skip to content

Commit 84f2d67

Browse files
authored
Fix OAuth exchange attempted for repos configured before v2.4.0 (#1120)
Repos configured before v2.4.0 don't have the oauth_exchange key in their saved config. The default in build_hex_core_config was true, causing hex to attempt OAuth token exchange for repos that don't support it (e.g. getoban.pro), resulting in :exchange_failed errors. Change the default to false. This is safe because hexpm repos always have oauth_exchange: true explicitly set, and repos added with v2.4.0 have it explicitly set by the add command. Also fix a test isolation issue where oauth_token from the user's actual ~/.hex/hex.config leaked into test state via init_reset_state. Closes #1119
1 parent 11a8395 commit 84f2d67

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

lib/hex/repo.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ defmodule Hex.Repo do
293293
cond do
294294
# First priority: explicit repo auth key with OAuth exchange disabled - use API key directly
295295
repo_config.auth_key && Map.get(repo_config, :trusted, true) &&
296-
Map.get(repo_config, :oauth_exchange, true) == false ->
296+
Map.get(repo_config, :oauth_exchange, false) == false ->
297297
%{config | repo_key: repo_config.auth_key}
298298

299-
# Second priority: Exchange API key for OAuth token if enabled (default)
299+
# Second priority: Exchange API key for OAuth token if enabled
300300
repo_config.auth_key && Map.get(repo_config, :trusted, true) ->
301301
case exchange_api_key_for_token(repo_config, repo_name) do
302302
{:ok, access_token} ->

test/hex/repo_test.exs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,43 @@ defmodule Hex.RepoTest do
5656
assert {:ok, {404, _, "not found"}} = Hex.Repo.get_public_key(config)
5757
end
5858

59+
test "add repo persists oauth_exchange through config round-trip" do
60+
in_tmp(fn ->
61+
Hex.State.put(:config_home, File.cwd!())
62+
63+
Mix.Tasks.Hex.Repo.run(["add", "myrepo", "http://example.com", "--auth-key", "mykey"])
64+
65+
# Reload config from disk to simulate a fresh session
66+
config = Hex.Config.read()
67+
repos = Hex.Config.read_repos(config)
68+
69+
assert repos["myrepo"].auth_key == "mykey"
70+
assert repos["myrepo"].oauth_exchange == false
71+
end)
72+
end
73+
74+
test "does not attempt oauth exchange when oauth_exchange is not set" do
75+
# Simulates a repo configured before v2.4.0 (no oauth_exchange key).
76+
# If oauth exchange were attempted with an invalid key it would raise.
77+
auth =
78+
HexTest.Hexpm.new_user(
79+
"no_oauth_key_user",
80+
"no_oauth_key@example.com",
81+
"password",
82+
"no_oauth_key_key"
83+
)
84+
85+
repos = Hex.State.fetch!(:repos)
86+
hexpm = Map.delete(repos["hexpm"], :oauth_exchange)
87+
hexpm = %{hexpm | auth_key: auth[:key]}
88+
Hex.State.put(:repos, %{repos | "hexpm" => hexpm})
89+
90+
assert {:ok, {200, _, _}} = Hex.Repo.get_package("hexpm", "postgrex", "")
91+
92+
repos_after = Hex.State.fetch!(:repos)
93+
assert Map.get(repos_after["hexpm"], :oauth_token) == nil
94+
end
95+
5996
test "fetch_repo/1" do
6097
assert Hex.Repo.fetch_repo("foo") == :error
6198

test/support/case.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ defmodule HexTest.Case do
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))
261261
Hex.State.update!(:repos, &put_in(&1["hexpm"].oauth_exchange, true))
262+
263+
Hex.State.update!(
264+
:repos,
265+
&update_in(&1, ["hexpm"], fn repo -> Map.delete(repo, :oauth_token) end)
266+
)
267+
262268
Hex.State.put(:repos_key, nil)
263269
Hex.State.put(:pbkdf2_iters, 10)
264270
Hex.State.put(:clean_pass, false)

0 commit comments

Comments
 (0)