|
| 1 | +defmodule Hex.API.OAuth do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + alias Hex.API.Client |
| 5 | + |
| 6 | + @client_id "78ea6566-89fd-481e-a1d6-7d9d78eacca8" |
| 7 | + |
| 8 | + @doc """ |
| 9 | + Initiates the OAuth device authorization flow. |
| 10 | +
|
| 11 | + Returns device code, user code, and verification URIs for user authentication. |
| 12 | + Optionally accepts a name parameter to identify the token. |
| 13 | +
|
| 14 | + ## Examples |
| 15 | +
|
| 16 | + iex> Hex.API.OAuth.device_authorization("api") |
| 17 | + {:ok, {200, _headers, %{ |
| 18 | + "device_code" => "...", |
| 19 | + "user_code" => "ABCD-1234", |
| 20 | + "verification_uri" => "https://hex.pm/oauth/device", |
| 21 | + "verification_uri_complete" => "https://hex.pm/oauth/device?user_code=ABCD-1234", |
| 22 | + "expires_in" => 600, |
| 23 | + "interval" => 5 |
| 24 | + }}} |
| 25 | + """ |
| 26 | + def device_authorization(scopes, name \\ nil) do |
| 27 | + config = Client.config() |
| 28 | + opts = if name, do: [name: name], else: [] |
| 29 | + :mix_hex_api_oauth.device_authorization(config, @client_id, scopes, opts) |
| 30 | + end |
| 31 | + |
| 32 | + @doc """ |
| 33 | + Polls the OAuth token endpoint for device authorization completion. |
| 34 | +
|
| 35 | + ## Examples |
| 36 | +
|
| 37 | + iex> Hex.API.OAuth.poll_device_token(device_code) |
| 38 | + {:ok, {200, _headers, %{ |
| 39 | + "access_token" => "...", |
| 40 | + "refresh_token" => "...", |
| 41 | + "token_type" => "Bearer", |
| 42 | + "expires_in" => 3600 |
| 43 | + }}} |
| 44 | + """ |
| 45 | + def poll_device_token(device_code) do |
| 46 | + config = Client.config() |
| 47 | + :mix_hex_api_oauth.poll_device_token(config, @client_id, device_code) |
| 48 | + end |
| 49 | + |
| 50 | + @doc """ |
| 51 | + Exchanges a token for a new token with different scopes using RFC 8693 token exchange. |
| 52 | +
|
| 53 | + ## Examples |
| 54 | +
|
| 55 | + iex> Hex.API.OAuth.exchange_token(subject_token, "api:write") |
| 56 | + {:ok, {200, _headers, %{ |
| 57 | + "access_token" => "...", |
| 58 | + "refresh_token" => "...", |
| 59 | + "token_type" => "Bearer", |
| 60 | + "expires_in" => 3600 |
| 61 | + }}} |
| 62 | + """ |
| 63 | + def exchange_token(subject_token, scope) do |
| 64 | + config = Client.config() |
| 65 | + :mix_hex_api_oauth.exchange_token(config, @client_id, subject_token, scope) |
| 66 | + end |
| 67 | + |
| 68 | + @doc """ |
| 69 | + Refreshes an access token using a refresh token. |
| 70 | +
|
| 71 | + ## Examples |
| 72 | +
|
| 73 | + iex> Hex.API.OAuth.refresh_token(refresh_token) |
| 74 | + {:ok, {200, _headers, %{ |
| 75 | + "access_token" => "...", |
| 76 | + "refresh_token" => "...", |
| 77 | + "token_type" => "Bearer", |
| 78 | + "expires_in" => 3600 |
| 79 | + }}} |
| 80 | + """ |
| 81 | + def refresh_token(refresh_token) do |
| 82 | + config = Client.config() |
| 83 | + :mix_hex_api_oauth.refresh_token(config, @client_id, refresh_token) |
| 84 | + end |
| 85 | + |
| 86 | + @doc """ |
| 87 | + Revokes an OAuth token (access or refresh token). |
| 88 | +
|
| 89 | + ## Examples |
| 90 | +
|
| 91 | + iex> Hex.API.OAuth.revoke_token(token) |
| 92 | + {:ok, {200, _headers, nil}} |
| 93 | + """ |
| 94 | + def revoke_token(token) do |
| 95 | + config = Client.config() |
| 96 | + :mix_hex_api_oauth.revoke_token(config, @client_id, token) |
| 97 | + end |
| 98 | +end |
0 commit comments