|
6 | 6 | device_authorization/4, |
7 | 7 | poll_device_token/3, |
8 | 8 | refresh_token/3, |
9 | | - revoke_token/3 |
| 9 | + revoke_token/3, |
| 10 | + client_credentials_token/4, |
| 11 | + client_credentials_token/5 |
10 | 12 | ]). |
11 | 13 |
|
12 | 14 | %% @doc |
@@ -115,6 +117,59 @@ refresh_token(Config, ClientId, RefreshToken) -> |
115 | 117 | }, |
116 | 118 | hex_api:post(Config, Path, Params). |
117 | 119 |
|
| 120 | +%% @doc |
| 121 | +%% Exchanges an API key for an OAuth access token using the client credentials grant. |
| 122 | +%% |
| 123 | +%% @see client_credentials_token/5 |
| 124 | +%% @end |
| 125 | +-spec client_credentials_token(hex_core:config(), binary(), binary(), binary()) -> hex_api:response(). |
| 126 | +client_credentials_token(Config, ClientId, ApiKey, Scope) -> |
| 127 | + client_credentials_token(Config, ClientId, ApiKey, Scope, []). |
| 128 | + |
| 129 | +%% @doc |
| 130 | +%% Exchanges an API key for an OAuth access token using the client credentials grant with optional parameters. |
| 131 | +%% |
| 132 | +%% This grant type allows exchanging a long-lived API key for a short-lived OAuth access token. |
| 133 | +%% The API key is sent as the client_secret parameter. |
| 134 | +%% |
| 135 | +%% Options: |
| 136 | +%% * `name' - A name to identify the token (e.g., hostname of the client) |
| 137 | +%% |
| 138 | +%% Returns: |
| 139 | +%% - `{ok, {200, _, Token}}` - Token exchange successful |
| 140 | +%% - `{ok, {400, _, #{<<"error">> => ...}}}` - Invalid request or scope |
| 141 | +%% - `{ok, {401, _, #{<<"error">> => ...}}}` - Invalid API key |
| 142 | +%% |
| 143 | +%% Examples: |
| 144 | +%% |
| 145 | +%% ``` |
| 146 | +%% 1> Config = hex_core:default_config(). |
| 147 | +%% 2> hex_api_oauth:client_credentials_token(Config, <<"cli">>, ApiKey, <<"api">>). |
| 148 | +%% {ok, {200, _, #{ |
| 149 | +%% <<"access_token">> => <<"...">>, |
| 150 | +%% <<"token_type">> => <<"bearer">>, |
| 151 | +%% <<"expires_in">> => 1800, |
| 152 | +%% <<"scope">> => <<"api">> |
| 153 | +%% }}} |
| 154 | +%% |
| 155 | +%% 3> hex_api_oauth:client_credentials_token(Config, <<"cli">>, ApiKey, <<"api">>, [{name, <<"MyMachine">>}]). |
| 156 | +%% ''' |
| 157 | +%% @end |
| 158 | +-spec client_credentials_token(hex_core:config(), binary(), binary(), binary(), proplists:proplist()) -> hex_api:response(). |
| 159 | +client_credentials_token(Config, ClientId, ApiKey, Scope, Opts) -> |
| 160 | + Path = <<"oauth/token">>, |
| 161 | + Params0 = #{ |
| 162 | + <<"grant_type">> => <<"client_credentials">>, |
| 163 | + <<"client_id">> => ClientId, |
| 164 | + <<"client_secret">> => ApiKey, |
| 165 | + <<"scope">> => Scope |
| 166 | + }, |
| 167 | + Params = case proplists:get_value(name, Opts) of |
| 168 | + undefined -> Params0; |
| 169 | + Name -> Params0#{<<"name">> => Name} |
| 170 | + end, |
| 171 | + hex_api:post(Config, Path, Params). |
| 172 | + |
118 | 173 | %% @doc |
119 | 174 | %% Revokes an OAuth token (RFC 7009). |
120 | 175 | %% |
|
0 commit comments