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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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

We're in Beta

About us Mission Statement