|
| 1 | +%% @doc |
| 2 | +%% Hex HTTP API - OAuth. |
| 3 | +-module(hex_api_oauth). |
| 4 | +-export([ |
| 5 | + device_authorization/3, |
| 6 | + device_authorization/4, |
| 7 | + poll_device_token/3, |
| 8 | + exchange_token/4, |
| 9 | + refresh_token/3, |
| 10 | + revoke_token/3 |
| 11 | +]). |
| 12 | + |
| 13 | +%% @doc |
| 14 | +%% Initiates the OAuth device authorization flow. |
| 15 | +%% |
| 16 | +%% @see device_authorization/4 |
| 17 | +%% @end |
| 18 | +-spec device_authorization(hex_core:config(), binary(), binary()) -> hex_api:response(). |
| 19 | +device_authorization(Config, ClientId, Scope) -> |
| 20 | + device_authorization(Config, ClientId, Scope, []). |
| 21 | + |
| 22 | +%% @doc |
| 23 | +%% Initiates the OAuth device authorization flow with optional parameters. |
| 24 | +%% |
| 25 | +%% Returns device code, user code, and verification URIs for user authentication. |
| 26 | +%% |
| 27 | +%% Options: |
| 28 | +%% * `name' - A name to identify the token (e.g., hostname of the device) |
| 29 | +%% |
| 30 | +%% Examples: |
| 31 | +%% |
| 32 | +%% ``` |
| 33 | +%% 1> Config = hex_core:default_config(). |
| 34 | +%% 2> hex_api_oauth:device_authorization(Config, <<"cli">>, <<"api:write">>). |
| 35 | +%% {ok,{200, ..., #{ |
| 36 | +%% <<"device_code">> => <<"...">>, |
| 37 | +%% <<"user_code">> => <<"ABCD-1234">>, |
| 38 | +%% <<"verification_uri">> => <<"https://hex.pm/oauth/device">>, |
| 39 | +%% <<"verification_uri_complete">> => <<"https://hex.pm/oauth/device?user_code=ABCD-1234">>, |
| 40 | +%% <<"expires_in">> => 600, |
| 41 | +%% <<"interval">> => 5 |
| 42 | +%% }}} |
| 43 | +%% |
| 44 | +%% 3> hex_api_oauth:device_authorization(Config, <<"cli">>, <<"api:write">>, [{name, <<"MyMachine">>}]). |
| 45 | +%% ''' |
| 46 | +%% @end |
| 47 | +-spec device_authorization(hex_core:config(), binary(), binary(), proplists:proplist()) -> hex_api:response(). |
| 48 | +device_authorization(Config, ClientId, Scope, Opts) -> |
| 49 | + Path = <<"oauth/device_authorization">>, |
| 50 | + Params0 = #{ |
| 51 | + <<"client_id">> => ClientId, |
| 52 | + <<"scope">> => Scope |
| 53 | + }, |
| 54 | + Params = case proplists:get_value(name, Opts) of |
| 55 | + undefined -> Params0; |
| 56 | + Name -> Params0#{<<"name">> => Name} |
| 57 | + end, |
| 58 | + hex_api:post(Config, Path, Params). |
| 59 | + |
| 60 | +%% @doc |
| 61 | +%% Polls the OAuth token endpoint for device authorization completion. |
| 62 | +%% |
| 63 | +%% Returns: |
| 64 | +%% - `{ok, {200, _, Token}}` - Authorization complete |
| 65 | +%% - `{ok, {400, _, #{<<"error">> => <<"authorization_pending">>}}}` - Still waiting |
| 66 | +%% - `{ok, {400, _, #{<<"error">> => <<"slow_down">>}}}` - Polling too fast |
| 67 | +%% - `{ok, {400, _, #{<<"error">> => <<"expired_token">>}}}` - Code expired |
| 68 | +%% - `{ok, {403, _, #{<<"error">> => <<"access_denied">>}}}` - User denied |
| 69 | +%% |
| 70 | +%% Examples: |
| 71 | +%% |
| 72 | +%% ``` |
| 73 | +%% 1> Config = hex_core:default_config(). |
| 74 | +%% 2> hex_api_oauth:poll_device_token(Config, <<"cli">>, DeviceCode). |
| 75 | +%% {ok, {200, _, #{ |
| 76 | +%% <<"access_token">> => <<"...">>, |
| 77 | +%% <<"refresh_token">> => <<"...">>, |
| 78 | +%% <<"token_type">> => <<"Bearer">>, |
| 79 | +%% <<"expires_in">> => 3600 |
| 80 | +%% }}} |
| 81 | +%% ''' |
| 82 | +%% @end |
| 83 | +-spec poll_device_token(hex_core:config(), binary(), binary()) -> hex_api:response(). |
| 84 | +poll_device_token(Config, ClientId, DeviceCode) -> |
| 85 | + Path = <<"oauth/token">>, |
| 86 | + Params = #{ |
| 87 | + <<"grant_type">> => <<"urn:ietf:params:oauth:grant-type:device_code">>, |
| 88 | + <<"device_code">> => DeviceCode, |
| 89 | + <<"client_id">> => ClientId |
| 90 | + }, |
| 91 | + hex_api:post(Config, Path, Params). |
| 92 | + |
| 93 | +%% @doc |
| 94 | +%% Exchanges a token for a new token with different scopes using RFC 8693 token exchange. |
| 95 | +%% |
| 96 | +%% Examples: |
| 97 | +%% |
| 98 | +%% ``` |
| 99 | +%% 1> Config = hex_core:default_config(). |
| 100 | +%% 2> hex_api_oauth:exchange_token(Config, <<"cli">>, SubjectToken, <<"api:write">>). |
| 101 | +%% {ok, {200, _, #{ |
| 102 | +%% <<"access_token">> => <<"...">>, |
| 103 | +%% <<"refresh_token">> => <<"...">>, |
| 104 | +%% <<"token_type">> => <<"Bearer">>, |
| 105 | +%% <<"expires_in">> => 3600 |
| 106 | +%% }}} |
| 107 | +%% ''' |
| 108 | +%% @end |
| 109 | +-spec exchange_token(hex_core:config(), binary(), binary(), binary()) -> hex_api:response(). |
| 110 | +exchange_token(Config, ClientId, SubjectToken, Scope) -> |
| 111 | + Path = <<"oauth/token">>, |
| 112 | + Params = #{ |
| 113 | + <<"grant_type">> => <<"urn:ietf:params:oauth:grant-type:token-exchange">>, |
| 114 | + <<"subject_token">> => SubjectToken, |
| 115 | + <<"subject_token_type">> => <<"urn:ietf:params:oauth:token-type:access_token">>, |
| 116 | + <<"client_id">> => ClientId, |
| 117 | + <<"scope">> => Scope |
| 118 | + }, |
| 119 | + hex_api:post(Config, Path, Params). |
| 120 | + |
| 121 | +%% @doc |
| 122 | +%% Refreshes an access token using a refresh token. |
| 123 | +%% |
| 124 | +%% Examples: |
| 125 | +%% |
| 126 | +%% ``` |
| 127 | +%% 1> Config = hex_core:default_config(). |
| 128 | +%% 2> hex_api_oauth:refresh_token(Config, <<"cli">>, RefreshToken). |
| 129 | +%% {ok, {200, _, #{ |
| 130 | +%% <<"access_token">> => <<"...">>, |
| 131 | +%% <<"refresh_token">> => <<"...">>, |
| 132 | +%% <<"token_type">> => <<"Bearer">>, |
| 133 | +%% <<"expires_in">> => 3600 |
| 134 | +%% }}} |
| 135 | +%% ''' |
| 136 | +%% @end |
| 137 | +-spec refresh_token(hex_core:config(), binary(), binary()) -> hex_api:response(). |
| 138 | +refresh_token(Config, ClientId, RefreshToken) -> |
| 139 | + Path = <<"oauth/token">>, |
| 140 | + Params = #{ |
| 141 | + <<"grant_type">> => <<"refresh_token">>, |
| 142 | + <<"refresh_token">> => RefreshToken, |
| 143 | + <<"client_id">> => ClientId |
| 144 | + }, |
| 145 | + hex_api:post(Config, Path, Params). |
| 146 | + |
| 147 | +%% @doc |
| 148 | +%% Revokes an OAuth token (RFC 7009). |
| 149 | +%% |
| 150 | +%% Can revoke either access tokens or refresh tokens. |
| 151 | +%% Returns 200 OK regardless of whether the token was found, |
| 152 | +%% following RFC 7009 security recommendations. |
| 153 | +%% |
| 154 | +%% Examples: |
| 155 | +%% |
| 156 | +%% ``` |
| 157 | +%% 1> Config = hex_core:default_config(). |
| 158 | +%% 2> hex_api_oauth:revoke_token(Config, <<"cli">>, Token). |
| 159 | +%% {ok, {200, ..., nil}} |
| 160 | +%% ''' |
| 161 | +%% @end |
| 162 | +-spec revoke_token(hex_core:config(), binary(), binary()) -> hex_api:response(). |
| 163 | +revoke_token(Config, ClientId, Token) -> |
| 164 | + Path = <<"oauth/revoke">>, |
| 165 | + Params = #{ |
| 166 | + <<"token">> => Token, |
| 167 | + <<"client_id">> => ClientId |
| 168 | + }, |
| 169 | + hex_api:post(Config, Path, Params). |
0 commit comments