Auth.OAuth

Auth

Authorization Code + PKCE OAuth client.

Pass provider URLs explicitly (Google, GitHub, …). The app stores state (and usually the PKCE verifier) between redirect and callback — for example in Auth.Session or Cache.

Types

Provider

type alias Provider =
{ clientId : String
, clientSecret : String
, authorizeUrl : String
, tokenUrl : String
, redirectUri : String
, scopes : List String
}

OAuth client settings for one provider.

Examples

provider =
    { clientId = "…"
    , clientSecret = "…"
    , authorizeUrl = "https://accounts.google.com/o/oauth2/v2/auth"
    , tokenUrl = "https://oauth2.googleapis.com/token"
    , redirectUri = "https://app.example/oauth/callback"
    , scopes = [ "openid", "email" ]
    }

Tokens

type alias Tokens =
{ accessToken : String
, refreshToken : Maybe String
, idToken : Maybe String
, tokenType : String
, expiresIn : Maybe Int
, raw : Value
}

Token endpoint response (plus raw JSON).

Examples

tokens.accessToken

Pkce

type alias Pkce =
{ verifier : String
, challenge : String
}

PKCE verifier and S256 challenge.

Examples

{ verifier, challenge } = Auth.OAuth.pkce

Error

type Error
= Http Req.Error
| BadStatus Int String
| BadBody String

Why authorize URL building or token exchange failed.

Examples

Err (Auth.OAuth.BadBody "missing access_token")

Values

pkce

pkce : Pkce

Mint a PKCE verifier and S256 code_challenge.

Examples

Auth.OAuth.pkce

authorizeRedirect

authorizeRedirect : Provider -> { state : String , challenge : String } -> String

Build the provider authorize redirect URL (response_type=code, S256).

Examples

Auth.OAuth.authorizeRedirect provider
    { state = state, challenge = pkce.challenge }

exchange

exchange : Provider -> { code : String , verifier : String } -> Result Error Tokens

Exchange an authorization code + PKCE verifier for tokens.

Examples

Auth.OAuth.exchange provider { code = code, verifier = pkce.verifier }

refresh

refresh : Provider -> String -> Result Error Tokens

Refresh an access token.

Examples

Auth.OAuth.refresh provider refreshToken

errorToString

errorToString : Error -> String

Human-readable form of an Error for logs.

Examples

Auth.OAuth.errorToString err