Skip to content

Commit cf4ecd0

Browse files
committed
Propagate oauth_exchange to organization repos
Organization repos inherit config from their parent via default_organization but oauth_exchange and oauth_exchange_url were not being propagated. This meant org repos like hexpm:rtpacnroll never had the oauth_exchange key set, falling back to the default of false in build_hex_core_config instead of inheriting true from hexpm.
1 parent 2520243 commit cf4ecd0

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lib/hex/repo.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ defmodule Hex.Repo do
4848
public_key = merge_values(Map.get(repo, :public_key), source.public_key)
4949
auth_key = merge_values(Map.get(repo, :auth_key), source.auth_key)
5050

51+
oauth_exchange =
52+
merge_values(Map.get(repo, :oauth_exchange), Map.get(source, :oauth_exchange))
53+
54+
oauth_exchange_url =
55+
merge_values(Map.get(repo, :oauth_exchange_url), Map.get(source, :oauth_exchange_url))
56+
5157
repo
5258
|> Map.put(:url, url)
5359
|> Map.put(:public_key, public_key)
5460
|> Map.put(:auth_key, auth_key)
61+
|> Map.put(:oauth_exchange, oauth_exchange)
62+
|> Map.put(:oauth_exchange_url, oauth_exchange_url)
5563
|> Map.put(:trusted, Map.has_key?(repo, :auth_key) or source.trusted)
5664
end
5765

test/hex/repo_test.exs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ defmodule Hex.RepoTest do
107107
assert {:ok,
108108
%{
109109
auth_key: nil,
110+
oauth_exchange: true,
110111
public_key: _,
111112
trusted: true,
112113
url: "http://localhost:4043/repo/repos/acme"
@@ -126,6 +127,7 @@ defmodule Hex.RepoTest do
126127
assert {:ok,
127128
%{
128129
auth_key: "key",
130+
oauth_exchange: true,
129131
public_key: _,
130132
trusted: true,
131133
url: "http://example.com/repos/acme"
@@ -145,6 +147,7 @@ defmodule Hex.RepoTest do
145147
assert {:ok,
146148
%{
147149
auth_key: "key",
150+
oauth_exchange: true,
148151
public_key: _,
149152
trusted: false,
150153
url: "http://example.com/repos/acme"
@@ -160,6 +163,7 @@ defmodule Hex.RepoTest do
160163
url: "http://example.com",
161164
public_key: "public",
162165
auth_key: "auth",
166+
oauth_exchange: true,
163167
trusted: true
164168
},
165169
"hexpm:acme" => %{}
@@ -168,6 +172,7 @@ defmodule Hex.RepoTest do
168172
assert %{
169173
"hexpm:acme" => %{
170174
auth_key: "auth",
175+
oauth_exchange: true,
171176
public_key: "public",
172177
trusted: true,
173178
url: "http://example.com/repos/acme"
@@ -188,6 +193,47 @@ defmodule Hex.RepoTest do
188193
end
189194

190195
describe "automatic API key to OAuth token exchange" do
196+
test "organization repo inherits oauth_exchange from parent" do
197+
auth =
198+
HexTest.Hexpm.new_user(
199+
"org_oauth_user",
200+
"org_oauth@example.com",
201+
"password",
202+
"org_oauth_key"
203+
)
204+
205+
repos = Hex.State.fetch!(:repos)
206+
repos = put_in(repos["hexpm"].auth_key, auth[:key])
207+
Hex.State.put(:repos, repos)
208+
209+
assert {:ok, {200, _, _}} = Hex.Repo.get_package("hexpm:testorg", "foo", "")
210+
211+
repos_after = Hex.State.fetch!(:repos)
212+
token_data = repos_after["hexpm:testorg"].oauth_token
213+
assert is_binary(token_data["access_token"])
214+
end
215+
216+
test "organization repo skips oauth_exchange when disabled on parent" do
217+
auth =
218+
HexTest.Hexpm.new_user(
219+
"org_no_oauth_user",
220+
"org_no_oauth@example.com",
221+
"password",
222+
"org_no_oauth_key"
223+
)
224+
225+
repos = Hex.State.fetch!(:repos)
226+
repos = put_in(repos["hexpm"].auth_key, auth[:key])
227+
repos = put_in(repos["hexpm"], Map.put(repos["hexpm"], :oauth_exchange, false))
228+
Hex.State.put(:repos, repos)
229+
230+
assert {:ok, {200, _, _}} = Hex.Repo.get_package("hexpm:testorg", "foo", "")
231+
232+
repos_after = Hex.State.fetch!(:repos)
233+
org_repo = Map.get(repos_after, "hexpm:testorg")
234+
assert org_repo == nil or Map.get(org_repo, :oauth_token) == nil
235+
end
236+
191237
test "automatically exchanges API key for OAuth token when making request" do
192238
auth =
193239
HexTest.Hexpm.new_user(

0 commit comments

Comments
 (0)