markupguy

markupguy

Joken errors when trying to create a JWT

Hello good people :slight_smile:

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 :slight_smile:

Marked As Solved

markupguy

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 :wink:

Also Liked

stevensonmt

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?

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement