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

johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement