Assent.Strategy behaviour (Assent v0.4.0)

Copy Markdown View Source

Used for creating strategies.

Usage

Set up my_strategy.ex the following way:

defmodule MyStrategy do
  @behaviour Assent.Strategy

  alias Assent.Strategy, as: Helpers

  @impl Assent.Strategy
  def authorize_url(config) do
    # Generate redirect URL

    {:ok, %{url: url, ...}}
  end

  @impl Assent.Strategy
  def callback(config, params) do
    # Fetch user data

    case Helpers.normalize_userinfo(userinfo) do
      {:ok, user} -> {:ok, %{user: user}}
      {:error, error} -> {:error, error}
    end
  end
end

Summary

Callbacks

Generates the URL that redirects the user to the authorization provider.

Completes the authorization callback and returns the authenticated user.

Functions

Casts the given claims according to the provided claim types.

Decodes a JSON string.

Normalizes API user request response into standard claims.

Signs a JSON Web Token.

Verifies a JSON Web Token.

Types

claim_type()

@type claim_type() :: :binary | :integer | :boolean | claim_types()

claim_types()

@type claim_types() :: %{optional(binary()) => claim_type()}

Callbacks

authorize_url(t)

@callback authorize_url(Keyword.t()) ::
  {:ok, %{:url => binary(), optional(atom()) => any()}} | {:error, term()}

Generates the URL that redirects the user to the authorization provider.

callback(t, map)

@callback callback(Keyword.t(), map()) ::
  {:ok, %{:user => map(), optional(atom()) => any()}} | {:error, term()}

Completes the authorization callback and returns the authenticated user.

Functions

cast_claims(claims, claim_types)

@spec cast_claims(map(), claim_types()) ::
  {:ok, map()} | {:error, Assent.Strategy.CastError.t()}

Casts the given claims according to the provided claim types.

The supported types are defined by claim_type/0. Integers can be cast to binaries, integer strings can be cast to integers, and "true" and "false" can be cast to booleans.

Claims that are not present in the claim types or have nil values are discarded. Assent.Strategy.CastError.t/0 is returned if a claim cannot be cast.

Examples

iex> claims = %{"sub" => "123", "email_verified" => "invalid"}
iex> claim_types = %{"sub" => :binary, "email_verified" => :boolean}
iex> {:error, error} = Assent.Strategy.cast_claims(claims, claim_types)
iex> error.invalid_types
%{"email_verified" => :boolean}

iex> claims = %{"sub" => "123", "email_verified" => "true"}
iex> claim_types = %{"sub" => :binary, "email_verified" => :boolean}
iex> Assent.Strategy.cast_claims(claims, claim_types)
{:ok, %{"sub" => "123", "email_verified" => true}}

decode_json(response, config)

@spec decode_json(binary(), Keyword.t()) :: {:ok, term()} | {:error, term()}

Decodes a JSON string.

Options

http_request(method, url, body, headers, config)

Makes an HTTP request.

Options

  • :prepare_http_request - an MFA tuple {module, function, arguments}. It is invoked before an HTTP request is sent. The request is prepended to the argument list.

See Assent.HTTPAdapter.request/2 for adapter options.

normalize_userinfo(claims, extra \\ %{})

@spec normalize_userinfo(map(), map()) ::
  {:ok, map()} | {:error, Assent.Strategy.CastError.t()}

Normalizes API user request response into standard claims.

The function will cast values to adhere to the following types:

%{
  "address" => %{
    "country" => :binary,
    "formatted" => :binary,
    "locality" => :binary,
    "postal_code" => :binary,
    "region" => :binary,
    "street_address" => :binary
  },
  "birthdate" => :binary,
  "email" => :binary,
  "email_verified" => :boolean,
  "family_name" => :binary,
  "gender" => :binary,
  "given_name" => :binary,
  "locale" => :binary,
  "middle_name" => :binary,
  "name" => :binary,
  "nickname" => :binary,
  "phone_number" => :binary,
  "phone_number_verified" => :boolean,
  "picture" => :binary,
  "preferred_username" => :binary,
  "profile" => :binary,
  "sub" => :binary,
  "updated_at" => :integer,
  "website" => :binary,
  "zoneinfo" => :binary
}

Returns Assent.Strategy.CastError.t/0 error if any of the above types can't be cast.

Based on the OpenID Connect Core 1.0 standard claims.

Examples

iex> Assent.Strategy.normalize_userinfo(%{"sub" => "123"}, %{"extra_claim" => "value"})
{:ok, %{"extra_claim" => "value", "sub" => "123"}}

sign_jwt(claims, alg, secret, config)

@spec sign_jwt(map(), binary(), binary(), Keyword.t()) ::
  {:ok, binary()} | {:error, term()}

Signs a JSON Web Token.

See Assent.JWTAdapter.sign/3 for options.

to_url(base_url, uri, params \\ [])

@spec to_url(binary(), binary(), Keyword.t()) :: binary()

Generates a URL.

Examples

iex> Assent.Strategy.to_url("https://example.com", "/authorize")
"https://example.com/authorize"

iex> Assent.Strategy.to_url("https://example.com", "/authorize", response_type: "code")
"https://example.com/authorize?response_type=code"

verify_jwt(token, secret, config)

@spec verify_jwt(binary(), binary() | map() | nil, Keyword.t()) ::
  {:ok, map()} | {:error, any()}

Verifies a JSON Web Token.

See Assent.JWTAdapter.verify/3 for options.