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.
Makes an HTTP request.
Normalizes API user request response into standard claims.
Signs a JSON Web Token.
Generates a URL.
Verifies a JSON Web Token.
Types
@type claim_type() :: :binary | :integer | :boolean | claim_types()
@type claim_types() :: %{optional(binary()) => claim_type()}
Callbacks
@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 callback(Keyword.t(), map()) :: {:ok, %{:user => map(), optional(atom()) => any()}} | {:error, term()}
Completes the authorization callback and returns the authenticated user.
Functions
@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}}
Decodes a JSON string.
Options
:json_library- the JSON library to use, seeAssent.json_library/1
@spec http_request( Assent.HTTPAdapter.method(), binary(), Assent.HTTPAdapter.body(), Assent.HTTPAdapter.headers(), Keyword.t() ) :: {:ok, Assent.HTTPAdapter.HTTPResponse.t()} | {:error, Assent.HTTPAdapter.HTTPResponse.t()} | {:error, term()}
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.
@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"}}
Signs a JSON Web Token.
See Assent.JWTAdapter.sign/3 for options.
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"
Verifies a JSON Web Token.
See Assent.JWTAdapter.verify/3 for options.