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

JWT adapter helper module.

You can configure which JWT adapter Assent uses by setting the configuration:

    jwt_adapter: Assent.JWTAdapter.AssentJWT

Default options can be set by passing a list of options:

    jwt_adapter: {Assent.JWTAdapter.AssentJWT, [...]}

You can also set global application config:

    config :assent, :jwt_adapter, Assent.JWTAdapter.AssentJWT

## Usage

    defmodule MyApp.MyJWTAdapter do
      @behaviour Assent.JWTAdapter

      @impl true
      def sign(claims, alg, secret, opts) do
        # Implement your JWT signing logic here. Return {:ok, token}.
      end

      @impl true
      def verify(token, secret, opts) do
        # Implement your JWT verification logic here. Return {:ok, claims}.
      end
    end

# `sign`

```elixir
@callback sign(map(), binary(), binary(), Keyword.t()) ::
  {:ok, binary()} | {:error, term()}
```

# `verify`

```elixir
@callback verify(binary(), binary() | map() | nil, Keyword.t()) ::
  {:ok, map()} | {:error, term()}
```

# `decode_pem`

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

Decodes PEM encoded keys.

# `load_private_key`

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

Loads a private key from the provided configuration.

## Options

  * `:private_key_path` - the path to the private key file, optional

  * `:private_key` - the private key content, required if `:private_key_path`
    is not set

# `sign`

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

Generates a signed JSON Web Token signature.

## Options

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

  * `:jwt_adapter` - the JWT adapter module to use, optional, defaults to
    `Assent.JWTAdapter.AssentJWT`

## Examples

    iex> Assent.JWTAdapter.sign(%{"sub" => "123"}, "HS256", "secret")
    {:ok, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjMifQ.jROrizwXV3Yj14Ofaz1zAq07YgNrxjQNyL-0slLq5Tw"}

# `verify`

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

Verifies the JSON Web Token signature.

## Options

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

  * `:jwt_adapter` - the JWT adapter module to use, optional, defaults to
    `Assent.JWTAdapter.AssentJWT`

## Examples

    iex> {:ok, jwt} = Assent.JWTAdapter.verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjMifQ.jROrizwXV3Yj14Ofaz1zAq07YgNrxjQNyL-0slLq5Tw", "secret")
    iex> jwt.verified?
    true
    iex> jwt.claims
    %{"sub" => "123"}

---

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