Main OAuth2 flow orchestrator.
Handles complete OAuth2 authorization code flow with PKCE support, session management, and token exchange with automatic audit logging.
Summary
Functions
Generates OAuth authorization URL with PKCE support.
Generate authorization URL with optional scopes.
Build OAuth callback URL from connection, handling proxy headers.
Build connection response for OAuth callbacks.
Build HTTPS callback URL from connection.
Classify OAuth error types.
Cleans up expired OAuth sessions.
Creates a new OAuth session for provider and tenant.
Safely decode OAuth state parameter.
Exchanges authorization code for access token.
Generate OAuth callback HTML page for popup flows.
Gets a valid (non-expired) session by token.
Log OAuth callback errors with audit trail.
Map OAuth error codes to atoms.
Parse OAuth scopes from various formats.
Perform OAuth callback exchange for popup flows.
Types
@type cleanup_result() :: {:ok, non_neg_integer()} | {:error, atom()}
@type connection_result() :: {:ok, %Tango.Schemas.Connection{ __meta__: term(), access_token: term(), auth_failures: term(), auto_refresh_enabled: term(), connection_config: term(), expires_at: term(), granted_scopes: term(), id: term(), inserted_at: term(), last_auth_failure: term(), last_refresh_failure: term(), last_used_at: term(), lock_version: term(), metadata: term(), next_refresh_at: term(), provider: term(), provider_id: term(), raw_payload: term(), refresh_attempts: term(), refresh_exhausted: term(), refresh_token: term(), status: term(), tenant_id: term(), token_type: term(), updated_at: term() }} | {:error, atom()}
@type session_result() :: {:ok, %Tango.Schemas.OAuthSession{ __meta__: term(), code_verifier: term(), expires_at: term(), id: term(), inserted_at: term(), metadata: term(), provider: term(), provider_id: term(), redirect_uri: term(), scopes: term(), session_token: term(), state: term(), tenant_id: term(), updated_at: term() }} | {:error, atom()}
Functions
@spec authorize_url( String.t(), keyword() ) :: auth_url_result()
Generates OAuth authorization URL with PKCE support.
Examples
iex> authorize_url("session_token_123", redirect_uri: "https://app.com/callback")
{:ok, "https://github.com/login/oauth/authorize?client_id=..."}
iex> authorize_url("invalid_token", redirect_uri: "https://app.com/callback")
{:error, :session_not_found}
Generate authorization URL with optional scopes.
Uses session/provider defaults when scopes list is empty, otherwise uses explicit scopes provided.
Build OAuth callback URL from connection, handling proxy headers.
Build connection response for OAuth callbacks.
Build HTTPS callback URL from connection.
Classify OAuth error types.
@spec cleanup_expired_sessions() :: cleanup_result()
Cleans up expired OAuth sessions.
Should be called periodically to prevent table bloat.
Examples
iex> cleanup_expired_sessions()
{:ok, 5} # 5 sessions cleaned up
@spec create_session(String.t(), String.t(), keyword()) :: session_result()
Creates a new OAuth session for provider and tenant.
Generates secure session tokens, PKCE parameters, and CSRF state.
Examples
iex> create_session("github", "user-123")
{:ok, %OAuthSession{}}
iex> create_session("nonexistent", "user-123")
{:error, :provider_not_found}
Safely decode OAuth state parameter.
@spec exchange_code(String.t(), String.t(), String.t(), keyword()) :: connection_result()
Exchanges authorization code for access token.
Validates session state, exchanges code with provider, and creates connection. SECURITY: Requires tenant_id for multi-tenant isolation.
Examples
iex> exchange_code("state_123", "auth_code_456", "tenant-123", redirect_uri: "https://app.com/callback")
{:ok, %Connection{}}
iex> exchange_code("invalid_state", "code", "tenant-123", redirect_uri: "https://app.com/callback")
{:error, :invalid_state}
iex> exchange_code("state_123", "code", "wrong-tenant", redirect_uri: "https://app.com/callback")
{:error, :tenant_mismatch}
Generate OAuth callback HTML page for popup flows.
@spec get_session(String.t()) :: session_result()
Gets a valid (non-expired) session by token.
Examples
iex> get_session("valid_token")
{:ok, %OAuthSession{}}
iex> get_session("expired_token")
{:error, :session_expired}
Log OAuth callback errors with audit trail.
Map OAuth error codes to atoms.
Parse OAuth scopes from various formats.
Perform OAuth callback exchange for popup flows.