Aurinko.Auth (Aurinko v0.2.1)

Copy Markdown View Source

OAuth authentication helpers for Aurinko.

Aurinko supports three OAuth flows:

  1. Account Flow — User-delegated authorization producing an account + access token.
  2. Service Account Flow — Admin/Org-level authorization.
  3. User Session Flow — Produces an Aurinko User, primary account, and session token.

Account Flow Example

# Step 1: Redirect user to authorization URL
url = Aurinko.Auth.authorize_url(
  service_type: "Google",
  scopes: ["Mail.Read", "Mail.Send", "Calendars.ReadWrite"],
  return_url: "https://yourapp.com/auth/callback",
  response_type: "code"
)

# Step 2: Exchange the code for a token
{:ok, token_info} = Aurinko.Auth.exchange_code(code)

# Use token_info.token for subsequent API calls

Summary

Functions

Builds the Aurinko OAuth authorization URL.

Exchanges an authorization code for an Aurinko account access token.

Refreshes an expired Aurinko access token.

Types

token_info()

@type token_info() :: %{
  token: String.t(),
  account_id: integer(),
  service_type: String.t(),
  email: String.t() | nil
}

Functions

authorize_url(opts \\ [])

@spec authorize_url(keyword()) :: String.t()

Builds the Aurinko OAuth authorization URL.

Options

  • :service_type — Provider to authorize (e.g. "Google", "Office365"). Required.
  • :scopes — List of permission scopes. Defaults to ["Mail.Read"].
  • :return_url — Callback URL after authorization. Required.
  • :response_type"code" (default) or "token".
  • :login_hint — Pre-fill the user's email address.
  • :state — Optional opaque state value for CSRF protection.

Examples

iex> Aurinko.Auth.authorize_url(
...>   service_type: "Google",
...>   scopes: ["Mail.Read", "Calendars.ReadWrite"],
...>   return_url: "https://myapp.com/callback"
...> )
"https://api.aurinko.io/v1/auth/authorize?..."

exchange_code(code, opts \\ [])

@spec exchange_code(
  String.t(),
  keyword()
) :: {:ok, token_info()} | {:error, Aurinko.Error.t()}

Exchanges an authorization code for an Aurinko account access token.

Examples

{:ok, %{token: token, account_id: id}} = Aurinko.Auth.exchange_code("auth_code_here")

refresh_token(refresh_token, opts \\ [])

@spec refresh_token(
  String.t(),
  keyword()
) :: {:ok, token_info()} | {:error, Aurinko.Error.t()}

Refreshes an expired Aurinko access token.