PhoenixApiToolkit.TestHelpers.gen_jwt
You're seeing just the function
gen_jwt
, go back to PhoenixApiToolkit.TestHelpers module for more information.
Specs
gen_jwt(gen_jwt_defaults(), gen_jwt_opts()) :: binary()
Generate a JSON Web Token for testing purposes, with an "exp" claim 5 minutes in the future.
It is possible to override parts of the signing key, payload and signature to test with different
scopes, expiration times, issuers, key ID's etc, override the entire signing key, payload or signature.
The defaults should generate a valid JWT. For use with endpoints secured with PhoenixApiToolkit.Security.Oauth2Plug
.
Examples
@jwt_defaults %{
jwk: gen_jwk(),
jws: gen_jws(),
payload: gen_payload(iss: "http://my-oauth2-provider")
}
# the defaults as created above generate a valid JWT, provided that the claims match those verified
# the header, payload and signature can be inspected using JOSE.JWS.peek* functions
iex> jwt = gen_jwt(@jwt_defaults)
iex> jwt |> JOSE.JWS.peek_protected() |> Jason.decode!()
%{"alg" => "RS256", "kid" => "my_test_key", "typ" => "JWT"}
iex> jwt |> JOSE.JWS.peek_payload() |> Jason.decode!() |> Map.drop(["exp"])
%{"iss" => "http://my-oauth2-provider"}
# parts of the jwk, payload and jws can be overridden for testing purposes
iex> gen_jwt(@jwt_defaults, payload: [iss: "boom"]) |> JOSE.JWS.peek_payload() |> Jason.decode!() |> Map.drop(["exp"])
%{"iss" => "boom"}
iex> gen_jwt(@jwt_defaults, jws: [kid: "other key"]) |> JOSE.JWS.peek_protected() |> Jason.decode!()
%{"alg" => "RS256", "kid" => "other key", "typ" => "JWT"}
iex> gen_jwt(@jwt_defaults, payload: [exp: 12345]) |> JOSE.JWS.peek_payload() |> Jason.decode!() |> Map.get("exp")
12345