Skip to content

Commit b2771fa

Browse files
committed
Add hostname as oauth token name
1 parent bb3f52e commit b2771fa

32 files changed

+133
-39
lines changed

lib/hex/api/oauth.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ defmodule Hex.API.OAuth do
99
Initiates the OAuth device authorization flow.
1010
1111
Returns device code, user code, and verification URIs for user authentication.
12+
Optionally accepts a name parameter to identify the token.
1213
1314
## Examples
1415
15-
iex> Hex.API.OAuth.device_authorization()
16+
iex> Hex.API.OAuth.device_authorization("api")
1617
{:ok, {200, _headers, %{
1718
"device_code" => "...",
1819
"user_code" => "ABCD-1234",
@@ -22,9 +23,10 @@ defmodule Hex.API.OAuth do
2223
"interval" => 5
2324
}}}
2425
"""
25-
def device_authorization(scopes \\ "api repositories") do
26+
def device_authorization(scopes, name \\ nil) do
2627
config = Client.config()
27-
:mix_hex_api_oauth.device_authorization(config, @client_id, scopes)
28+
opts = if name, do: [name: name], else: []
29+
:mix_hex_api_oauth.device_authorization(config, @client_id, scopes, opts)
2830
end
2931

3032
@doc """

lib/mix/tasks/hex.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ defmodule Mix.Tasks.Hex do
125125
auth_device(opts)
126126
end
127127

128+
defp get_hostname() do
129+
case :inet.gethostname() do
130+
{:ok, hostname} -> to_string(hostname)
131+
{:error, _} -> nil
132+
end
133+
end
134+
128135
@doc false
129136
def auth_device(_opts \\ []) do
130137
# Clean up any existing authentication
@@ -133,7 +140,9 @@ defmodule Mix.Tasks.Hex do
133140

134141
Hex.Shell.info("Starting OAuth device flow authentication...")
135142

136-
case Hex.API.OAuth.device_authorization() do
143+
name = get_hostname()
144+
145+
case Hex.API.OAuth.device_authorization("api repositories", name) do
137146
{:ok, {200, _, device_response}} ->
138147
perform_device_flow(device_response)
139148

src/mix_hex_api.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% Vendored from hex_core v0.11.0 (75e889f), do not edit manually
1+
%% Vendored from hex_core v0.11.0 (94a912d), do not edit manually
22

33
%% @doc
44
%% Hex HTTP API

src/mix_hex_api_auth.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% Vendored from hex_core v0.11.0 (75e889f), do not edit manually
1+
%% Vendored from hex_core v0.11.0 (94a912d), do not edit manually
22

33
%% @doc
44
%% Hex HTTP API - Authentication.

src/mix_hex_api_key.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% Vendored from hex_core v0.11.0 (75e889f), do not edit manually
1+
%% Vendored from hex_core v0.11.0 (94a912d), do not edit manually
22

33
%% @doc
44
%% Hex HTTP API - Keys.

src/mix_hex_api_oauth.erl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
%% Vendored from hex_core v0.11.0 (75e889f), do not edit manually
1+
%% Vendored from hex_core v0.11.0 (94a912d), do not edit manually
22

33
%% @doc
44
%% Hex HTTP API - OAuth.
55
-module(mix_hex_api_oauth).
66
-export([
77
device_authorization/3,
8+
device_authorization/4,
89
poll_device_token/3,
910
exchange_token/4,
1011
refresh_token/3,
@@ -33,11 +34,32 @@
3334
%% @end
3435
-spec device_authorization(mix_hex_core:config(), binary(), binary()) -> mix_hex_api:response().
3536
device_authorization(Config, ClientId, Scope) ->
37+
device_authorization(Config, ClientId, Scope, []).
38+
39+
%% @doc
40+
%% Initiates the OAuth device authorization flow with optional parameters.
41+
%%
42+
%% Options:
43+
%% * name - A name to identify the token (e.g., hostname of the device)
44+
%%
45+
%% Examples:
46+
%%
47+
%% ```
48+
%% 1> Config = mix_hex_core:default_config().
49+
%% 2> mix_hex_api_oauth:device_authorization(Config, <<"cli">>, <<"api:write">>, [{name, <<"MyMachine">>}]).
50+
%% '''
51+
%% @end
52+
-spec device_authorization(mix_hex_core:config(), binary(), binary(), proplists:proplist()) -> mix_hex_api:response().
53+
device_authorization(Config, ClientId, Scope, Opts) ->
3654
Path = <<"oauth/device_authorization">>,
37-
Params = #{
55+
Params0 = #{
3856
<<"client_id">> => ClientId,
3957
<<"scope">> => Scope
4058
},
59+
Params = case proplists:get_value(name, Opts) of
60+
undefined -> Params0;
61+
Name -> Params0#{<<"name">> => Name}
62+
end,
4163
mix_hex_api:post(Config, Path, Params).
4264

4365
%% @doc

src/mix_hex_api_organization.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% Vendored from hex_core v0.11.0 (75e889f), do not edit manually
1+
%% Vendored from hex_core v0.11.0 (94a912d), do not edit manually
22

33
%% @doc
44
%% Hex HTTP API - Organizations.

src/mix_hex_api_organization_member.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% Vendored from hex_core v0.11.0 (75e889f), do not edit manually
1+
%% Vendored from hex_core v0.11.0 (94a912d), do not edit manually
22

33
%% @doc
44
%% Hex HTTP API - Organization Members.

src/mix_hex_api_package.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% Vendored from hex_core v0.11.0 (75e889f), do not edit manually
1+
%% Vendored from hex_core v0.11.0 (94a912d), do not edit manually
22

33
%% @doc
44
%% Hex HTTP API - Packages.

src/mix_hex_api_package_owner.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% Vendored from hex_core v0.11.0 (75e889f), do not edit manually
1+
%% Vendored from hex_core v0.11.0 (94a912d), do not edit manually
22

33
%% @doc
44
%% Hex HTTP API - Package Owners.

0 commit comments

Comments
 (0)