truqu/elm-oauth2/8.0.0

NOTE: the latest version is 8.0.1

OAuth.AuthorizationCode.PKCE

OAuth 2.0 public clients utilizing the Authorization Code Grant are susceptible to the authorization code interception attack. A possible mitigation against the threat is to use a technique called Proof Key for Code Exchange (PKCE, pronounced "pixy") when supported by the target authorization server. See also RFC 7636.

                                     +-----------------+
                                     |  Auth   Server  |
    +-------+                        | +-------------+ |
    |       |--(1)- Auth Request --->| |             | |
    |       |    + code_challenge    | |    Auth     | |
    |       |                        | |   Endpoint  | |
    |       |<-(2)-- Auth Code ------| |             | |
    |  Elm  |                        | +-------------+ |
    |  App  |                        |                 |
    |       |                        | +-------------+ |
    |       |--(3)- Token Request -->| |             | |
    |       |      + code_verifier   | |   Token     | |
    |       |                        | |  Endpoint   | |
    |       |<-(4)- Access Token --->| |             | |
    +-------+                        | +-------------+ |
                                     +-----------------+

See also the Authorization Code flow for details about the basic version of this flow.

Code Verifier / Challenge

= CodeVerifier Encoder

An opaque type representing a code verifier. Typically constructed from a high quality entropy.

case codeVerifierFromBytes entropy of
  Nothing -> {- ...-}
  Just codeVerifier -> {- ... -}
= CodeChallenge Encoder

An opaque type representing a code challenge. Typically constructed from a CodeVerifier.

let codeChallenge = mkCodeChallenge codeVerifier

Construct a code verifier from a byte sequence generated from a high quality randomness source (i.e. cryptographic).

Ideally, the byte sequence should be 32 or 64 bytes, and it must be at least 32 bytes and at most 90 bytes.

Convert a code verifier to its string representation.

Construct a CodeChallenge to send to the authorization server. Upon receiving the authorization code, the client can then the associated CodeVerifier to prove it is the rightful owner of the authorization code.

Convert a code challenge to its string representation.

Authorize

makeAuthorizationUrl : Authorization -> Url

Redirects the resource owner (user) to the resource provider server using the specified authorization flow.

type alias Authorization =
{ clientId : String
, url : Url
, redirectUri : Url
, scope : List String
, state : Maybe String
, codeChallenge : CodeChallenge
}

Request configuration for an authorization (Authorization Code & Implicit flows)

  • clientId (REQUIRED): The client identifier issues by the authorization server via an off-band mechanism.

  • url (REQUIRED): The authorization endpoint to contact the authorization server.

  • redirectUri (OPTIONAL): After completing its interaction with the resource owner, the authorization server directs the resource owner's user-agent back to the client via this URL. May be already defined on the authorization server itself.

  • scope (OPTIONAL): The scope of the access request.

  • state (RECOMMENDED): An opaque value used by the client to maintain state between the request and callback. The authorization server includes this value when redirecting the user-agent back to the client. The parameter SHOULD be used for preventing cross-site request forgery.

  • codeChallenge (REQUIRED): A challenge derived from the code verifier that is sent in the authorization request, to be verified against later.

parseCode : Url -> AuthorizationResult

Parse the location looking for a parameters set by the resource provider server after redirecting the resource owner (user).

Returns AuthorizationResult Empty when there's nothing.

type alias AuthorizationResult =
AuthorizationResultWith AuthorizationError AuthorizationSuccess

Describes errors coming from attempting to parse a url after an OAuth redirection

  • Empty: means there were nothing (related to OAuth 2.0) to parse
  • Error: a successfully parsed OAuth 2.0 error
  • Success: a successfully parsed token and response
type alias AuthorizationError =
{ error : ErrorCode
, errorDescription : Maybe String
, errorUri : Maybe String
, state : Maybe String
}

Describes an OAuth error as a result of an authorization request failure

  • error (REQUIRED): A single ASCII error code.

  • errorDescription (OPTIONAL) Human-readable ASCII text providing additional information, used to assist the client developer in understanding the error that occurred. Values for the errorDescription parameter MUST NOT include characters outside the set %x20-21 / %x23-5B / %x5D-7E.

  • errorUri (OPTIONAL): A URI identifying a human-readable web page with information about the error, used to provide the client developer with additional information about the error. Values for the errorUri parameter MUST conform to the URI-reference syntax and thus MUST NOT include characters outside the set %x21 / %x23-5B / %x5D-7E.

  • state (REQUIRED if state was present in the authorization request): The exact value received from the client

{ code : String
, state : Maybe String
}

The response obtained as a result of an authorization

  • code (REQUIRED): The authorization code generated by the authorization server. The authorization code MUST expire shortly after it is issued to mitigate the risk of leaks. A maximum authorization code lifetime of 10 minutes is RECOMMENDED. The client MUST NOT use the authorization code more than once. If an authorization code is used more than once, the authorization server MUST deny the request and SHOULD revoke (when possible) all tokens previously issued based on that authorization code. The authorization code is bound to the client identifier and redirection URI.

  • state (REQUIRED if state was present in the authorization request): The exact value received from the client

type alias AuthorizationCode =
String

A simple type alias to ease readability of type signatures

Authenticate

(Result Error AuthenticationSuccess -> msg)
-> Authentication
-> RequestParts msg

Builds a the request components required to get a token from an authorization code

let req : Http.Request AuthenticationSuccess
    req = makeTokenRequest toMsg authentication |> Http.request
type alias Authentication =
{ credentials : Credentials
, code : String
, codeVerifier : CodeVerifier
, redirectUri : Url
, url : Url
}

Request configuration for an AuthorizationCode authentication

  • credentials (REQUIRED): Only the clientId is required. Specify a secret if a Basic OAuth is required by the resource provider.

  • code (REQUIRED): Authorization code from the authorization result

  • codeVerifier (REQUIRED): The code verifier proving you are the rightful recipient of the access token.

  • url (REQUIRED): Token endpoint of the resource provider

  • redirectUri (REQUIRED): Redirect Uri to your webserver used in the authorization step, provided here for verification.

type alias Credentials =
{ clientId : String
, secret : Maybe String
}

Describes at least a clientId and if define, a complete set of credentials with the secret. The secret is so-to-speak optional and depends on whether the authorization server you interact with requires a 'Basic' authentication on top of the authentication request. Provides it if you need to do so.

  { clientId = "<my-client-id>"
  , secret = Just "<my-client-secret>"
  }
{ token : Token
, refreshToken : Maybe Token
, expiresIn : Maybe Int
, scope : List String
}

The response obtained as a result of an authentication (implicit or not)

  • token (REQUIRED): The access token issued by the authorization server.

  • refreshToken (OPTIONAL): The refresh token, which can be used to obtain new access tokens using the same authorization grant as described in Section 6.

  • expiresIn (RECOMMENDED): The lifetime in seconds of the access token. For example, the value "3600" denotes that the access token will expire in one hour from the time the response was generated. If omitted, the authorization server SHOULD provide the expiration time via other means or document the default value.

  • scope (OPTIONAL, if identical to the scope requested; otherwise, REQUIRED): The scope of the access token as described by Section 3.3.

type alias AuthenticationError =
{ error : ErrorCode
, errorDescription : Maybe String
, errorUri : Maybe String
}

Describes an OAuth error as a result of a request failure

  • error (REQUIRED): A single ASCII error code.

  • errorDescription (OPTIONAL) Human-readable ASCII text providing additional information, used to assist the client developer in understanding the error that occurred. Values for the errorDescription parameter MUST NOT include characters outside the set %x20-21 / %x23-5B / %x5D-7E.

  • errorUri (OPTIONAL): A URI identifying a human-readable web page with information about the error, used to provide the client developer with additional information about the error. Values for the errorUri parameter MUST conform to the URI-reference syntax and thus MUST NOT include characters outside the set %x21 / %x23-5B / %x5D-7E.

type alias RequestParts a =
{ method : String
, headers : List Header
, url : String
, body : Body
, expect : Expect a
, timeout : Maybe Float
, tracker : Maybe String
}

Parts required to build a request. This record is given to Http.request in order to create a new request and may be adjusted at will.

JSON Decoders

defaultAuthenticationSuccessDecoder : Decoder AuthenticationSuccess

Json decoder for a positive response. You may provide a custom response decoder using other decoders from this module, or some of your own craft.

defaultAuthenticationSuccessDecoder : Decoder AuthenticationSuccess
defaultAuthenticationSuccessDecoder =
    D.map4 AuthenticationSuccess
        tokenDecoder
        refreshTokenDecoder
        expiresInDecoder
        scopeDecoder
defaultAuthenticationErrorDecoder : Decoder AuthenticationError

Json decoder for an errored response.

case res of
    Err (Http.BadStatus { body }) ->
        case Json.decodeString OAuth.AuthorizationCode.defaultAuthenticationErrorDecoder body of
            Ok { error, errorDescription } ->
                doSomething


            _ ->
                parserFailed


    _ ->
        someOtherError

Custom Decoders & Parsers (advanced)

Authorize

makeAuthorizationUrlWith : ResponseType -> Dict String String -> Authorization -> Url

Like 'makeAuthorizationUrl', but gives you the ability to specify a custom response type and extra fields to be set on the query.

makeAuthorizationUrl : Authorization -> Url
makeAuthorizationUrl =
    makeAuthorizationUrlWith Code Dict.empty

For example, to interact with a service implementing OpenID+Connect you may require a different token type and an extra query parameter as such:

makeAuthorizationUrlWith
    (CustomResponse "code+id_token")
    (Dict.fromList [ ( "resource", "001" ) ])
    authorization
type AuthorizationResultWith error success
= Empty
| Error error
| Success success

A parameterized 'AuthorizationResult'. See 'parseCodeWith'.

Authenticate

-> Decoder success
-> Dict String String
-> (Result Error success -> msg)
-> Authentication
-> RequestParts msg

Like 'makeTokenRequest', but gives you the ability to specify custom grant type and extra fields to be set on the query.

makeTokenRequest : (Result Http.Error AuthenticationSuccess -> msg) -> Authentication -> RequestParts msg
makeTokenRequest =
    makeTokenRequestWith
        AuthorizationCode
        defaultAuthenticationSuccessDecoder
        Dict.empty

Json Decoders

defaultExpiresInDecoder : Decoder (Maybe Int)

Json decoder for an 'expire' timestamp

defaultScopeDecoder : Decoder (List String)

Json decoder for a 'scope'

lenientScopeDecoder : Decoder (List String)

Json decoder for a 'scope', allowing comma- or space-separated scopes

Json decoder for an 'access_token'

Json decoder for a 'refresh_token'

Json decoder for 'error' field

defaultErrorDescriptionDecoder : Decoder (Maybe String)

Json decoder for 'error_description' field

defaultErrorUriDecoder : Decoder (Maybe String)

Json decoder for 'error_uri' field

Query Parsers

Parsers error success
-> Url
-> AuthorizationResultWith error success

See parseCode, but gives you the ability to provide your own custom parsers.

type alias Parsers error success =
{ codeParser : Parser (Maybe String)
, errorParser : Parser (Maybe ErrorCode)
, authorizationSuccessParser : String -> Parser success
, authorizationErrorParser : ErrorCode -> Parser error
}

Parsers used in the 'parseCode' function.

  • codeParser: looks for a 'code' string
  • errorParser: looks for an 'error' to build a corresponding ErrorCode
  • authorizationSuccessParser: selected when the tokenParser succeeded to parse the remaining parts
  • authorizationErrorParser: selected when the errorParser succeeded to parse the remaining parts
defaultParsers : Parsers AuthorizationError AuthorizationSuccess

Default parsers according to RFC-6749

defaultCodeParser : Parser (Maybe String)

Default 'code' parser according to RFC-6749

defaultErrorParser : Parser (Maybe ErrorCode)

Default 'error' parser according to RFC-6749

defaultAuthorizationSuccessParser : String -> Parser AuthorizationSuccess

Default response success parser according to RFC-6749

defaultAuthorizationErrorParser : ErrorCode -> Parser AuthorizationError

Default response error parser according to RFC-6749