bponghneng

bponghneng

I’m trying to verify an AWS Cognito JWT. The closest help I have found is this thread:

https://forum.elixirforum.com/t/joken-using-joken-verify/10081/5

I’m attempting a similar technique:

authorization = conn |> get_req_header("authorization")
[authorizationHeaderValue | rest] = authorization
jwt = String.slice(authorizationHeaderValue, 7..1500)
awsJwks = Poison.decode!(~s({"keys":[{"alg":"RS256","e":"AQAB","kid":"hcju2zKBpJjOTDd214wkKnrRY9ApwTiPJq+Oe+q6Jz0=","kty":"RSA","n":"rDfAEMwYGBrol5mIA1aEcwtZBUgwYKcs8uVaLW64tZwcn9Nc4G6XMAw9igucWz-yt-4aFyKzw9mpBMqSUeNs7mDUczrMO4UbPn-5fN-cSgCKyLdAUjFKS8-L9O0tqXmBdEbtmferUrz2Nqt1pnCKU0gl2ZHb64hD98feBptqTYiCqh6VZnookBrjhvn9b0CHJnwEgPF__DeeSwlRYPt-pYpSCmI851zspLsnMwtoQQdfe1bL7H28OEa0-O4rRxcI_BJz79c7bHWllDMMhD51xmsn1rngCn8Y2tHIsGr_81Hx7k0Awma-ibJ8Zll8MNrJgmCG7ykz3bAfuDW20vKtpw","use":"sig"},{"alg":"RS256","e":"AQAB","kid":"ttY6v6AGiOqL+ZaDvyg953iG7RWLTwDVq9+cOa1hrZE=","kty":"RSA","n":"q70S6cdm28a0LRj_o8QP7Getgh-QygMbFFp_z-Yxjs1FkKrJva_yNYOdjiO5YC8Dqazcr6OEUYV6iSe7XyOvG6MeAh9Y9gvw_0HmFg-vYb_-pifht5C9E7iKRp5QIcyEO2rie-Gas7QyW8k1uTHJT68gxdoV8hverMwWw2H7L1Y7ec6-jN1tsq1ExYQbi8ELDxzCpeDF_e4FKp2COZEdV_CVN8qxdC6aBTIN7dSNPjXCOQSUv_gw_XIk_G_S2JDJCty_ffdiiXZItviIfi9WZP_2uW8xAo2KX2ssno_wVbTJ_XxxnWy6aFNeaW5yFzHNenSvjJGnKMgVRbkMClXLRQ","use":"sig"}]}))
token = Joken.token(jwt)
with_signer = token |> Joken.with_signer(Joken.rs256(JOSE.JWK.from_map(awsJwks)))
{:ok, claims} = with_signer |> Joken.verify!

BTW, in case it’s not obvious, the String.slice() is to trim off "Bearer " from the header value.

I only get an invalid signature error:

(MatchError) no match of right hand side value: {:error, "Invalid signature"}

FWIW, here is the token I’m trying to verify:

eyJraWQiOiJ0dFk2djZBR2lPcUwrWmFEdnlnOTUzaUc3UldMVHdEVnE5K2NPYTFoclpFPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiI5MGY4MTY1Mi0yYTQ3LTQ2MzgtYTU2ZC01NGU1Y2YwNTAwMWMiLCJldmVudF9pZCI6ImQ0OWYwNjYxLTBjYWMtMTFlOC1hZjE3LTZiODc1OTYxNTIzOCIsInRva2VuX3VzZSI6ImFjY2VzcyIsInNjb3BlIjoiYXdzLmNvZ25pdG8uc2lnbmluLnVzZXIuYWRtaW4iLCJpc3MiOiJodHRwczpcL1wvY29nbml0by1pZHAudXMtZWFzdC0xLmFtYXpvbmF3cy5jb21cL3VzLWVhc3QtMV9XaHRwQ1hlb1UiLCJleHAiOjE1MTgwODMzMDcsImlhdCI6MTUxODA3OTcwNywianRpIjoiZGJlNGQ5N2MtZWE4ZC00NmYyLTliODYtMDUyYjA0N2VmNjM5IiwiY2xpZW50X2lkIjoidW80bDNkdDNsYWwxamEwNGNiZDVlN3F0NCIsInVzZXJuYW1lIjoiOTBmODE2NTItMmE0Ny00NjM4LWE1NmQtNTRlNWNmMDUwMDFjIn0.J_9u8Wkx4-KY6ac5d57Ff_gEMCowpCD-ymyA2pzKMfeKGJ0Uz5ukufcsoEpPgaGcRQYTKKmYEXVhOz-gCwS1LninalrgTsEpyv8ULGmZaKDBsYvQIM5LaBXtW4ZVNGPzRKctjyXbV2tWGA-bSnu34CnEET-HQwgKUH4KkT9IOOGUHPUCDKKwLw1l-lZN2Q1lrCXNtUBJVyY7ZxuzFaiywARxSGBFZom797mEdEwb49_uugXoMRgTE-kS4pvVLgMYMXNqAROXyCkzRoF9Zs2Qkv9IWD27IjPShRBFkZbGdKsIoJu2c-vBDudh9dXD4YGZfsU5I5N7-PYLgEENhWDabA

Am I missing something in creating the Joken.Signer?

Cheers,
Robert

Showing Posts 1 to 10

cs-victor-nascimento

cs-victor-nascimento

It is difficult to know how you’ve signed your token. Though this token has a different “kid” claim on its header than the one your passing for JOSE.JWK.from_map/1.

Does this help? The kid on the token is: ttY6v6AGiOqL+ZaDvyg953iG7RWLTwDVq9+cOa1hrZE=\

reneerojas

reneerojas

i’ve tryed with this kid and the same token

awsJwks = Poison.decode!(~s({“alg”:“RS256”,“e”:“AQAB”,“kid”:“ttGiOqL+ZaDvyg953iG7R=”,“kty”:“RSA”,“n”:“q70S6cdm28a0LRj_o8QP7Getgh-QygM7iKRp5QIcyEO2rie-Gas7QyW8k1uTHJT68gxdoV8hverMwWw2H7L1Y7ec6-jN1tsq1ExYQbi8ELDxzCpeDF_e4FKp2COZEdV_CVN8qxdC6aBTIN7dSNP_gw_XIk_G_S2JDJCty_fviIfi9WZP_2uW8xAo2KX2ssno_wVbTJ_XxxnWy6aFNeaW5yFzHKMgVRbkMClXLRQ”,“use”:“sig”}))

no match of right hand side value: {:error, “Invalid signature”}

BTW, i’m trying to decode the same token…

thanks for help

regards,
Renee

bponghneng

bponghneng OP

@cs-victor-nascimento Thanks for your reply! @reneerojas and I are working on the same problem :slight_smile:

To clarify, we are trying to verify JWTs signed by AWS, which provides the following public key set:

https://cognito-idp.us-east-1.amazonaws.com/us-east-1_WhtpCXeoU/.well-known/jwks.json

Is the technique above the correct way to create a Joken.Signer?

  1. Decode the AWS key set JSON into an Elixir map
  2. Create a JOSE.JWK key set
  3. Create a Joken.Signer from the JOSE.JWK

I cannot see any other technique to create the Joken.Signer except to read the provided AWS key set JSON and observe that both keys in the set list RS256 as the encryption. Then hard code that by using Joken.rs256().

Is it best if we extract the AWS key with the correct kid from the set before creating the JOSE.JWK? (I think that is what @reneerojas has tried.)

Cheers,
Robert

Azolo

Azolo

I had to do something similar with Auth0, I ended up just using JOSE directly instead of Joken though.

But anyway, it looks like what you’re missing to me is the fact that the jwks is actually a key set. Meaning you need to get your token from the list before you try to verify. Assuming you only have one key then this should probably do it:

[jwk | _] = JOSE.JWK.from_map(awsJwks)

If not, this is the crazy function that I ended up getting to:

defp jwks_from_binary(jwks_binary) do
    with {:ok, map} <- Poison.decode(jwks_binary),
         {:ok, keys} when is_list(keys) <- Map.fetch(map, "keys"),
         jwks_list when is_list(jwks_list) <- Enum.map(keys, &JOSE.JWK.from_map/1),
         jwks when is_list(jwks) <- JOSE.JWK.from(jwks_list) do
      {:ok, jwks}
    else
      _ ->
        {:error, :invalid_jwks}
    end
  end
bponghneng

bponghneng OP

@Azolo Yeah, the Auth0 JWK set is exactly the same format as the AWS set, so I think we need your function, thanks! How are you verifying after extracting the key? Using Joken for that?

Cheers,
Robert

Azolo

Azolo

Well you’re supposed to use the data in the JWT to get the name of the key in the jwks, but I was pretty frustrated at that point, so I just did:

defp check_against_jwks(token, jwks) do
    Enum.any?(jwks, fn(jwk) ->
      case JOSE.JWT.verify_strict(jwk, ["RS256"], token) do
        {true, _, _} -> true
        _ -> false
      end
    end)
  end
bponghneng

bponghneng OP

Cool! We will give a try just directly with JOSE.JWT :slight_smile:

Cheers,
Robert

bponghneng

bponghneng OP

@Azolo Worked! Wow, great: Clears a big block for us. We’ll use your technique :slight_smile:

Cheers,
Robert

Azolo

Azolo

Glad I could help

zycavi

zycavi

I’ve stumbled upon the same issue - thanks for the useful answers, which pointed me the right way :smiley:

I actually took it a little further and got it integrated with Joken:

  1. ’ “forked” Joken.Plug and modified line 174 to pass the connection: verified_token = payload_fun.(conn) (see https://github.com/bryanjos/joken/blob/master/lib/joken/plug.ex#L174) Let’s call it Youproject.modifiedJokenPlug
    Adjust your router’s verify_function to take on argument: def verify_function(conn) do
    and your plug: plug Youproject.modifiedJokenPlug, verify: &Youproject.Router.verify_function/1 (don’t forget, you cb now takes one argument, so it’s /1)
  2. With the connection in the verify function, it is possible to get the claims on the JWT as follows:
["Bearer " <> bearer] = get_req_header(conn, "authorization")
    {:ok, claims} = bearer
    |> String.split(".")
    |> hd
    |> Base.decode64!
    |> Poison.decode
  # claims["kid"] and claims["alg"] are now populated
  1. Load the list of keys (with HTTPoison / auth0 in my case)
{:ok, keylist} = HTTPoison.get!("https://something.locale.auth0.com/.well-known/jwks.json").body # CHANGE URL
  1. Get the matching key and it’s certificate:
matchingKey = Enum.find(keylist["keys"], fn(x) -> x["kid"] == claims["kid"] && x["alg"] == claims["alg"] end)
key = hd(matchingKey["x5c"]) # I assume, the first key in the certificate chain is also the signing one
  1. Format the signing key so Joken understands it (as PEM, using JOSE, going by Joken’s docs here):
a = "-----BEGIN CERTIFICATE-----"
z = "-----END CERTIFICATE-----"
parts = key
    |> Stream.unfold(&String.split_at(&1, 64)) # 64 Characters per line
    |> Enum.take_while(&(&1 != "")) # Get all remaining chars as well
    fkey = a <>  "\n" <> Enum.join(parts, "\n") <>  "\n"  <> z <> "\n" # Join it all to form the PEM
    q = JOSE.JWK.from_pem(fkey) # And finally get the signer
  1. Return the verifier
   %Joken.Token{}
    |> with_json_module(Poison)
    |> with_signer(rs256(q))
    |> with_validation("aud", &(&1 == "<expected audience>" || Enum.member?(&1, "<expected audience>"))) # Audience claim can be an array, e.g. if you query it for your application while quering profile as well # CHANGE AUDIENCE
    |> with_validation("exp", &(&1 > current_time))
    |> with_validation("iat", &(&1 <= current_time))
    |> with_validation("iss", &(&1 == "https://someone.locale.auth0.com/")) # CHANGE URL

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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
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
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; 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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews