# `Assent.Strategy`
[🔗](https://github.com/pow-auth/assent/blob/v0.4.0/lib/assent/strategy.ex#L1)

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

# `claim_type`

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

# `claim_types`

```elixir
@type claim_types() :: %{optional(binary()) =&gt; claim_type()}
```

# `authorize_url`

```elixir
@callback authorize_url(Keyword.t()) ::
  {:ok, %{:url =&gt; binary(), optional(atom()) =&gt; any()}} | {:error, term()}
```

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

# `callback`

```elixir
@callback callback(Keyword.t(), map()) ::
  {:ok, %{:user =&gt; map(), optional(atom()) =&gt; any()}} | {:error, term()}
```

Completes the authorization callback and returns the authenticated user.

# `cast_claims`

```elixir
@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 `t: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. `t: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`

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

Decodes a JSON string.

## Options

  * `:json_library` - the JSON library to use, see `Assent.json_library/1`

# `http_request`

```elixir
@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.

# `normalize_userinfo`

```elixir
@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 `t: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](https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.1).

## Examples

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

# `sign_jwt`

```elixir
@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`

```elixir
@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`

```elixir
@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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
