fireproofsocks

fireproofsocks

Does anyone have a full working example of signing JWTs? I guess this is partly a question about the process too… this is related to earlier work I did (and this other post).

When you complete a sign-in with Google, you are given a JWT. You can can look up the PEM that was used to sign the key at https://www.googleapis.com/oauth2/v1/certs

And that can be used to verify that the JWT has not been tampered with.

From
https://github.com/danharper/hmac-examples#elixir

There’s this Elixir example:

key = 'the shared secret key here'
message = 'the message to hash here'

signature = :crypto.hmac(:sha256, key, message)

# to lowercase hexits
Base.encode16(signature, case: :lower)

When you’re dealing with a JWT, the message is a Base64 encoding of the JSON header + the JSON claims. But what’s the key in this scenario? When Google lets us query its PEM, what is in that PEM? Is it a private key? A public key? Or a combination? And what gets used to sign the JWT?

I’m writing tests around this stuff, so I need to be able to generate a public + private key, convert them (or one of them?) to PEM format, and then properly sign the key so that the JWT can be properly checked.

Showing Posts 1 to 8

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @fireproofsocks it’s a private key. Here is code I wrote to validate JWT keys against google firebase pem. You can probably refactor it to grab pems from somewhere else.

First, I had a genserver which, on boot, would fetch the keys from Google and put them in ets:

def request_keys(_) do
    url = keys_url()

    {:ok, 200, headers, body} = :hackney.request(:get, url, [], "", [:with_body])

    {_, cache_control} =
      Enum.find(headers, fn {k, _} ->
        String.downcase(k) == "cache-control"
      end)

    [_, expire_in] = Regex.run(~r/max\-age\=([0-9]*),/, cache_control)
    expire_in = String.to_integer(expire_in)

    body
    |> Jason.decode!()
    |> Map.values()
    |> Enum.map(&JOSE.JWK.from_pem/1)
    |> Enum.map(fn key ->
      %{type: :firebase, key: key, expire_in: expire_in * 1000}
    end)
  end

  defp keys_url() do
    "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com"
  end

That’s the fetching and parsing code, the ets and refresh based on expires in is left as an exercise to the reader.

In any case once that’s in place, when a request comes in with a JWT key you can verify it via:

def verify(token) do
    [{:keys, keys}] = :ets.lookup(Maven.Accounts.Auth, :keys)

    Enum.find_value(keys, &do_verify(&1, token))
  end

  defp do_verify(key_data, token) do
    case JOSE.JWT.verify(key_data.key, token) do
      {true, %{fields: fields}, _} ->
        {:ok, key_data.type, fields}

      _ ->
        nil
    end
  end
danschultzer

danschultzer

Pow Core Team

I wrote a JWT adapter that might be interesting to you: assent/lib/assent/jwt_adapter/assent_jwt.ex at 78e75769296fbe4f4383795e1ec6327d63fda60e · pow-auth/assent · GitHub

Here’s the test with public key example (I just hard code the keys): assent/test/assent/jwt_adapter/assent_jwt_test.exs at 78e75769296fbe4f4383795e1ec6327d63fda60e · pow-auth/assent · GitHub

Though I would recommend that you just rely on JOSE to handle it similar to the above example by @benwilson512.

fireproofsocks

fireproofsocks OP

Thanks @benwilson512 – this is a clean example of how to verify keys, cleaner than what I had worked out. However, I was looking for how to sign the key. @danschultzer – I think AssentJWT.sign/3 does exactly what I want. Thank you!

Catharz

Catharz

I’ve been having similar issues, trying to sign something just using JOSE.JWT.sign/3.

It seemed that using File.read/1 and then JOSE.JWK.from_pem/1 was giving different results to JOSE.JWK.from_pem_file/1. Unfortunately, the documentation for JOSE is really sparse, so I never worked out exactly why.

enkr1

enkr1

Is anybody still on here?

am I not able to verify a JWT by public.pem with the limited resources online…

I really need help

Thanks in advance.

Best wishes,
Jing Hui P.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @enkr1, please provide samples of the code you are running, and the resources you’ve tried to follow. We can’t help if you don’t show us what you’ve tried so far.

enkr1

enkr1

Thank you @benwilson512 for taking the time to reply to my question!!

Here is how i did it:

return_token = "eyJhb...."

case File.read("./public.pem") do
  {:ok, public_key} ->
    {_, %{"n" => n} = key_map} =
      JOSE.JWK.from_pem(public_key)
      # |> IO.inspect(label: "orignal")
      |> JOSE.JWK.to_map()
      # |> IO.inspect(label: "mapified")

    signer =
      Joken.Signer.create("RS256", key_map)
      |> IO.inspect(label: "signer")

    Joken.Signer.verify(return_token, signer)
    |> IO.inspect(label: "verify") # Here is the part where it gives error

  {:error, unsupported_case} ->
    unsupported_case |> IO.inspect(label: "i didnt expect this.")
    {:erorr, "Something went wrong while trying to read the public.pem ..."}
end

output:

# ...

signer: %Joken.Signer{
  jwk: %JOSE.JWK{
    keys: :undefined,
    kty: {:jose_jwk_kty_rsa,
     {:RSAPublicKey,
 2374353...43,
      65537}},
    fields: %{}
  },
  jws: %JOSE.JWS{
    alg: {:jose_jws_alg_rsa_pkcs1_v1_5, :RS256},
    b64: :undefined,
    fields: %{"typ" => "JWT"}
  },
  alg: "RS256"
}

verify: {:error, :signature_error}
# ...

I was about to create a post and saw your reply.. thank you ma mannnnn

Best,
Jing Hui P.

enkr1

enkr1

I also tried this guy’s solution Using Joken to validate Google JWTs - #12 by vinagrito1

but what i am getting is false instead of true:

{false,
 %JOSE.JWT{
   fields: %{
      # ...
   }
 },
 %JOSE.JWS{
   alg: {:jose_jws_alg_rsa_pkcs1_v1_5, :RS256},
   b64: :undefined,
   fields: %{"typ" => "JWT"}
 }}

I have just created a new thread: How to verify RS256 JWT with Joken.Signer.verify/2 - 2022

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews