View Source JWT.Jws (yajwt v1.4.0)

Represent content to be secured with digital signatures or Message Authentication Codes (MACs)

see http://tools.ietf.org/html/rfc7515

Link to this section Summary

Functions

Return a JSON Web Signature (JWS), a string representing a digitally signed payload

Return a JWS that provides no integrity protection (i.e. lacks a signature)

Return jws parts if the signature is verified, raises error otherwise

Return a tuple {:ok, jws_parts} if the signature is verified, or {:error, exception} otherwise

Link to this section Functions

Link to this function

sign(header, payload, key)

View Source
@spec sign(map(), binary(), binary()) :: binary()

Return a JSON Web Signature (JWS), a string representing a digitally signed payload

example

Example

iex> key = "gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C"
...> JWT.Jws.sign(%{alg: "HS256"}, "payload", key)
"eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
Link to this function

unsecured_message(header, payload)

View Source

Return a JWS that provides no integrity protection (i.e. lacks a signature)

example

Example

iex> JWT.Jws.unsecured_message(%{alg: "none"}, "payload")
"eyJhbGciOiJub25lIn0.cGF5bG9hZA."

see http://tools.ietf.org/html/rfc7515#page-47

Link to this function

verify!(jws, algorithm, key)

View Source
@spec verify!(binary(), binary(), binary()) :: [binary()] | no_return()

Return jws parts if the signature is verified, raises error otherwise

example

Example

iex> jws = "eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C"
...> JWT.Jws.verify!(jws, "HS256", key)
["eyJhbGciOiJIUzI1NiJ9", "cGF5bG9hZA", "uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"]

iex> jws = "eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "invalid-key-invalid-key-invalid-key"
...> JWT.Jws.verify!(jws, "HS256", key)
** (JWT.InvalidSignatureError) Invalid Signature

iex> jws = "eyJhbGciOiJIUzI1NiJ9.modified-content.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C"
...> JWT.Jws.verify!(jws, "HS256", key)
** (JWT.InvalidSignatureError) Invalid Signature

iex> jws = "eyJhb%%%%%%%%%%%%%%%.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C"
...> JWT.Jws.verify!(jws, "HS256", key)
** (JWT.DecodeError) Failed to decode base64 string

iex> jws = "eyJhbGciOiJIUzI1NiJ9.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = nil
...> JWT.Jws.verify!(jws, "HS256", key)
** (JWT.MissingKeyError) Key is required for all algorithms but 'none'

iex> jws = "eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C"
...> JWT.Jws.verify!(jws, "%%%%%", key)
** (JWT.UnmatchedAlgorithmError) Algorithm not matching 'alg' header parameter
Link to this function

verify(jws, algorithm, key)

View Source
@spec verify(binary(), binary(), binary()) :: {:ok, [binary()]} | {:error, atom()}

Return a tuple {:ok, jws_parts} if the signature is verified, or {:error, exception} otherwise

example

Example

iex> jws = "eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C"
...> JWT.Jws.verify(jws, "HS256", key)
{:ok, ["eyJhbGciOiJIUzI1NiJ9", "cGF5bG9hZA", "uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"]}

iex> jws = "eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "invalid-key-invalid-key-invalid-key"
...> JWT.Jws.verify(jws, "HS256", key)
{:error, JWT.InvalidSignatureError}

iex> jws = "eyJhbGciOiJIUzI1NiJ9.modified-content.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C"
...> JWT.Jws.verify(jws, "HS256", key)
{:error, JWT.InvalidSignatureError}

iex> jws = "eyJhb%%%%%%%%%%%%%%%.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C"
...> JWT.Jws.verify(jws, "HS256", key)
{:error, JWT.DecodeError}

iex> jws = "eyJhbGciOiJIUzI1NiJ9.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = nil
...> JWT.Jws.verify(jws, "HS256", key)
{:error, JWT.MissingKeyError}

iex> jws = "eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4"
...> key = "gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C"
...> JWT.Jws.verify(jws, "%%%%%", key)
{:error, JWT.UnmatchedAlgorithmError}