markupguy
Joken errors when trying to create a JWT
Hello good people ![]()
I’m trying to create JWT’s with dynamic config using Joken (i.e I have zero config in my config.exs).
One similar issue I saw was that the person wasn’t sending the token_config to the Joken.generate_and_sign function, but based on various examples you can send an empty map. Or I’m missing something.
Here is my code:
claims = %{}
|> Joken.Config.add_claim("iss", fn -> iss end, &(&1 == iss))
|> Joken.Config.add_claim("sub", fn -> iss end, &(&1 == sub))
|> Joken.Config.add_claim("aud", fn -> iss end, &(&1 == aud))
|> Joken.Config.add_claim("iat", fn -> iss end, &(&1 == iat))
|> Joken.Config.add_claim("exp", fn -> iss end, &(&1 == exp))
signer = Joken.Signer.create("RS256", %{"pem" => key}, %{"kid" => kid})
{:ok, token, _claims} = Joken.generate_and_sign(%{}, token_config, claims, signer)
IO.inspect token
FWIW I’m trying to create a token from a Google service_account blob (not file), so…maybe I’m doing it all wrong?
I’d appreciate any help ![]()
Marked As Solved
markupguy
Thanks I figured it out eventually, heh. I turned the claims into a simple map, and then used this:
signer = Joken.Signer.create("RS256", %{"pem" => key})
{:ok, token, _claims} = Joken.encode_and_sign(claims, signer)
Worked fine, got my token, could finally go sleep ![]()
Also Liked
stevensonmt
Yeah it looks like Joken.generate_and_sign expects the signer arg to be the third argument and you have fourth:
generate_and_sign(token_config, extra_claims \\ %{}, signer_arg \\ :default_signer, hooks \\ [])
Specs
generate_and_sign(token_config(), claims(), signer_arg(), [module()]) ::
{:ok, bearer_token(), claims()} | {:error, error_reason()}
The map you’re putting in the first argument doesn’t seem to be necessary, maybe?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








