oauth2

Types

pub type AccessTokenResponse {
  TokenErrorResponse(
    status: Int,
    error: String,
    error_description: option.Option(String),
    error_uri: option.Option(String),
  )
  AccessTokenResponse(
    access_token: String,
    token_type: String,
    expires_in: option.Option(Int),
    refresh_token: option.Option(String),
    scope: List(String),
  )
}

Constructors

  • TokenErrorResponse(
      status: Int,
      error: String,
      error_description: option.Option(String),
      error_uri: option.Option(String),
    )
  • AccessTokenResponse(
      access_token: String,
      token_type: String,
      expires_in: option.Option(Int),
      refresh_token: option.Option(String),
      scope: List(String),
    )
pub type AuthorizationCodeGrantRedirectUri {
  AuthorizationCodeGrantRedirectUri(
    oauth_server: uri.Uri,
    response_type: ResponseType,
    redirect_uri: option.Option(uri.Uri),
    client_id: ClientId,
    scope: List(String),
    state: option.Option(State),
  )
  AuthorizationCodeGrantRedirectUriWithPKCE(
    oauth_server: uri.Uri,
    response_type: ResponseType,
    redirect_uri: option.Option(uri.Uri),
    client_id: ClientId,
    scope: List(String),
    state: option.Option(State),
    code_challange: String,
  )
}

Constructors

pub type ClientAuthentication {
  ClientSecretBasic(client_id: ClientId, client_secret: Secret)
  ClientSecretPost(client_id: ClientId, client_secret: Secret)
  PublicAuthentication(client_id: ClientId)
}

Constructors

pub type ClientId {
  ClientId(value: String)
}

Constructors

  • ClientId(value: String)
pub type Error {
  SecretExpired
  InvalidUri
}

Constructors

  • SecretExpired
  • InvalidUri
pub type ResponseType {
  Code
  Token
}

Constructors

  • Code
  • Token
pub type Scope =
  List(String)
pub type Secret {
  Secret(value: String)
  SecretWithExpiration(
    value: String,
    expires_at: timestamp.Timestamp,
  )
}

Constructors

  • Secret(value: String)
  • SecretWithExpiration(
      value: String,
      expires_at: timestamp.Timestamp,
    )
pub type State {
  State(value: String)
}

Constructors

  • State(value: String)
pub type TokenRequest {
  AuthorizationCodeGrantTokenRequest(
    token_endpoint: uri.Uri,
    authentication: ClientAuthentication,
    redirect_uri: option.Option(uri.Uri),
    code: String,
  )
  AuthorizationCodeGrantTokenRequestWithPKCE(
    token_endpoint: uri.Uri,
    authentication: ClientAuthentication,
    redirect_uri: option.Option(uri.Uri),
    code: String,
    code_verifier: String,
  )
  ResourceOwnerCredentialsGrantTokenRequest(
    token_endpoint: uri.Uri,
    authentication: ClientAuthentication,
    username: String,
    password: String,
    scope: List(String),
  )
  RefreshTokenGrantRequest(
    token_endpoint: uri.Uri,
    authentication: ClientAuthentication,
    refresh_token: String,
    scope: List(String),
  )
  ClientCredentialsGrantTokenRequest(
    token_endpoint: uri.Uri,
    authentication: ClientAuthentication,
    scope: List(String),
  )
}

Constructors

Values

pub fn make_redirect_uri(
  redirect_config: AuthorizationCodeGrantRedirectUri,
) -> uri.Uri

Creates the uri that the resource owner should be redirected too.

pub fn parse_scope(scope: String) -> List(String)

Parses a string containing the space separated scopes.

Example

parse_scope("scope1 scope2")
pub fn parse_token_response(
  response: response.Response(String),
) -> Result(AccessTokenResponse, json.DecodeError)

Parses a token response and returns the access and refresh token if valid response, otherwise the error response.

pub fn random_state(length: Int) -> State

Generates a random State with the specified length including only uppercase and lowercase letters If length <= 0 returns an empty string

pub fn random_state32() -> State

Generates a random 32 character long State

pub fn secret_is_valid(secret: Secret) -> Bool

Checks if a given secret is not expired. Returns always true for secrets that cannot expire.

pub fn to_http_request(
  request: TokenRequest,
) -> Result(request.Request(String), Error)

Creates a http request from the given TokenRequest, but does not send. Sending the request is done by the user of the function.

Search Document