PhoenixKit.Integrations.OAuth (phoenix_kit v1.7.102)

Copy Markdown View Source

Generic OAuth 2.0 flow for service integrations.

Handles authorization URL generation, code-to-token exchange, token refresh, and userinfo fetching. Provider-specific details (URLs, scopes, extra params) come from the provider definition in PhoenixKit.Integrations.Providers.

Summary

Functions

Exchange an authorization code for access and refresh tokens.

Fetch user info from the provider's userinfo endpoint.

Generate a random state token for CSRF protection in OAuth flows.

Refresh an expired access token using the refresh token.

Functions

authorization_url(oauth_config, integration_data, redirect_uri, extra_scopes \\ nil, state \\ nil)

@spec authorization_url(map(), map(), String.t(), String.t() | nil, String.t() | nil) ::
  {:ok, String.t()} | {:error, atom()}

Build the OAuth authorization URL for a provider.

Requires client_id to be present in the integration data and the provider to have oauth_config with an auth_url.

exchange_code(oauth_config, integration_data, code, redirect_uri)

@spec exchange_code(map(), map(), String.t(), String.t()) ::
  {:ok, map()} | {:error, term()}

Exchange an authorization code for access and refresh tokens.

fetch_userinfo(oauth_config, access_token)

@spec fetch_userinfo(map(), String.t()) :: {:ok, map()} | {:error, term()}

Fetch user info from the provider's userinfo endpoint.

Returns a map with at least "email" if available.

generate_state()

@spec generate_state() :: String.t()

Generate a random state token for CSRF protection in OAuth flows.

refresh_access_token(oauth_config, integration_data)

@spec refresh_access_token(map(), map()) ::
  {:ok, String.t(), map()} | {:error, term()}

Refresh an expired access token using the refresh token.