NoWayJose v0.3.0 NoWayJose View Source

Provides functions for signing a map of "claims" into a JWT using a signing key.

Link to this section Summary

Types

Algorithm used in JWT signing.

A map containing the claims to be encoded. Map keys must be strings.

RSA private key.

The format of the provided key.

Key Identifier – Acts as an alias for the key

JSON Web Token

Functions

Generates an RSA private key based on the given bit size and format.

Generates a signed JWT from the given claims and signing options.

Generates a signed JWT from the given claims and key.

Link to this section Types

Algorithm used in JWT signing.

Link to this type

claims()

View Source
claims() :: %{required(binary()) => term()}

A map containing the claims to be encoded. Map keys must be strings.

RSA private key.

The key can be either DER or PEM encoded.

Generating a key

der = NoWayJose.generate_rsa(4096, :der)
pem = NoWayJose.generate_rsa(4096, :pem)

Optionally, you can extract the DER data from a PEM encoded private key in code using the following:

{:ok, key} = File.read("private.pem")
[{:RSAPrivateKey, der, _}] = :public_key.pem_decode(key)
Link to this type

key_format()

View Source
key_format() :: :der | :pem

The format of the provided key.

Key Identifier – Acts as an alias for the key

Link to this type

signing_option()

View Source
signing_option() ::
  {:alg, alg()} | {:format, key_format()} | {:key, key()} | {:kid, kid()}
Link to this type

signing_options()

View Source
signing_options() :: [signing_option()]

JSON Web Token

Link to this section Functions

Link to this function

generate_rsa(bits, format)

View Source
generate_rsa(integer(), key_format()) :: binary()

Generates an RSA private key based on the given bit size and format.

Link to this function

sign(claims, key)

View Source
sign(claims(), signing_options()) :: {:ok, token()} | {:error, term()}
sign(claims(), signing_options()) :: {:ok, token()} | {:error, term()}

Generates a signed JWT from the given claims and signing options.

Example

# Get the private signing key
{:ok, key} = File.read("private.pem")

# Build your claims
claims = %{
  "exp" => 1571065163,
  "iat" => 1571061563,
  "iss" => "example.com",
  "jti" => "a3a31258-2450-490b-86ed-2b8e67f91e20",
  "nbf" => 1571061563,
  "scopes" => [
    "posts.r+w",
    "comments.r+w"
  ],
  "sub" => "4d3796ca-19e0-40e6-97fe-060c0b7e3ce3"
}

# Sign the claims into a JWT
{:ok, token} = NoWayJose.sign(claims, alg: :rs512, key: key, format: :pem, kid: "1")

Generates a signed JWT from the given claims and key.

Returns a JWT on success and raises an error on error.