fireproofsocks
I’ve been trying out the Joken package to work with JWTs. Right now I’m unable to verify the JWTs that Google generates during Oauth. Has anyone implemented this? I think Joken has all the pieces, I just can’t figure out the winning combination. I’ve been reading this: Authenticate with a backend server | Web guides | Google for Developers
Google’s public keys are available in JWK format https://www.googleapis.com/oauth2/v3/certs
and in PEM format: https://www.googleapis.com/oauth2/v1/certs
I can get Google JWT, and I’ve tried to set up a module for these particular Google JWTs:
defmodule GoogleJwt do
use Joken.Config, default_signer: nil # no signer
def token_config do
default_claims()
end
end
But the following always generates an error of invalid signature:
MyGoogleJwt.verify_and_validate(idtoken)
Anyone have any tips?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hi, I’m evaluating Ash as a foundation for our future business applications.
So far I love it, and there’s just one usecase I haven’t be...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #security
- #elixirconf-eu
- #api
- #forms
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
easco
I think your first mistake is using the mechanism where you
use Joken.Configto create a custom module for handling tokens. In this case you want to handle someone else’s tokens so I’m not sure what you’re doing is the right strategy. But more on that later.You’re going to have to do something to get a Joken
Signerbased on the Google keys.In my application I have the signing keys specified as JWK maps. I create the Joken
Signerusing:I then verify the token as:
This lets me know that the claims in the token were signed by the signer which is all I want to know.
If you want to validate that the verified claims are good, then you woud create a
token_config()with the validations added. Riffing on an example in the docs it might look something like:Then you could pass the
token_configtoJoken.verify_and_validateIf you were going to make the mechanism you have above work (with the
use Joken.Config), you would have to provide adefault_signer. Joken expects you to specify thedefault_signerin your application’sconfig.exs. It wants you to provide the signer that your app would use to sign its own tokens. You might be able to do that grab Google’s the signing keys then, query the web when config.exs is compiled, but if the keys changed you’d be in trouble.(you would also implement a
token_configfunction in your module to return something like thetoken_configconstructed in the sample above)You might be able to create a function that grabs the signature at runtime and jams the default signer into the application config using
Application.put_env- something along the lines ofApplication.put_env(:your_app_key, GoogleJWT, default_signer: signer). You’d need a strategy to update that signer (either periodically, or when a verification fails… something like that).dom
YMMV, but I found it much easier to validate tokens using the JOSE library directly than with Jokens. The errors contain more information on what failed, and the code is much easier to follow (no macros, overrides, callbacks).
https://github.com/potatosalad/erlang-jose/blob/master/README.md#usage
fireproofsocks
Thanks easco… yeah, I have no desire to tie into the whole config stuff just to verify that Google’s token is valid, but I can’t seem to make much work in the Joken package – I’ve tried variants of your solution, but no dice. I’m not sure where
Joken.verify(iam_token, signer, [])comes from, but it generates an error for me:function Joken.Signer.verify/3 is undefined or private.I am assuming that Google’s certs listed at https://www.googleapis.com/oauth2/v1/certs are PEMs, but I’m pretty much just guessing at the exact algorithm. The docs don’t appear to list the supported algorithms, but one of the error messages states that the possible values are
[“HS256”, “HS384”, “HS512”, “RS256”, “RS384”, “RS512”, “ES256”, “ES384”, “ES512”, “PS256”, “PS384”, “PS512”, “Ed25519”, “Ed25519ph”, “Ed448”, “Ed448ph”]
However, trying to use some of these comes up with errors that the algorithm isn’t recognized. So that leaves me pretty confused. The best I’ve been able to do is to get a
{:error, :signature_error}Converting the Google RSA cert into an Elixir data structure looks like this:
But this too throws an error:
fireproofsocks
Thanks dom, I looked at Jose, but unfortunately its documentation is so sparse that I think only an expert could make use of it. I think I have a decent handle on SSH and certs, and I couldn’t follow it.
benwilson512
It’s really not too bad. This is what I use to validate a JWT token against AWS Cognito keyset:
I retrieve the keyset on application boot, shove them into an ets table, and then use the Enum.find bit with a given token to see if the token is valid with any of the keys. I looked at Joken and it doesn’t appear to support this use case.
easco
You’ve got two keys in the keys array:
You will either need to verify the JWT against both keys, or find additional information about which key you should use. In my case, the key is identified using a “kid” key in the header of the JWT. I use
Joken.peek_header(my_token)to peek at the header and find out which key was used to sign the token, then I pull that key out of the list of keys and verify the token against it.fireproofsocks
We were joking in the office about handing out a button that says “IT WORKS FOR ME” any time code failed for one dev but worked for another. If you have gotten the JOSE to work, then I suppose it’s “not too bad”, but for me it was a genuine struggle. Eventually I got it to work. Thank you for sharing your example.
Ultimately I could never work out how to use the RSA certs at https://www.googleapis.com/oauth2/v3/certs I would only get errors like this:
I could never figure out what exactly needed to be done to any single key or a list of keys to make them work with JOSE. As far as I could tell, those are all in standard JWK format…
However, the PEM format is much simpler to work with, and per @easco I used
Joken.peek_header(my_token)to get the exact key in PEM format from https://www.googleapis.com/oauth2/v1/certs that was used to sign the JWT.I can run something like:
Note: Confusingly, on JOSE.JWK — JOSE v1.11.12, the args are ordered
signed,jwkBut on GitHub - potatosalad/erlang-jose: JSON Object Signing and Encryption (JOSE) for Erlang and Elixir · GitHub the args are ordered
jwk,compact_signedThe other very important note here is that the JWT that you get once you complete Google’s OAuth is fairly short-lived – maybe only valid for a couple minutes. Also tricky is the fact the public keys used to sign those keys change intermittently, so one must check Google’s cert page pretty often.
Once that is done, however, I was finally able to verify that my Google JWT was valid, and I think I have gained enough clarity here to submit a PR for the relevant JOSE docs/examples.
thezjy
I had the same problem as you. After some struggle I did manage to validate Google idToken with Joken. But as you said the public keys are regularly rotated, which means we need to fetch the key every time when validating? (I checked the official NodeJS library, which does exactly this.)
Meanwhile, Google provides an endpoint to validate the token for you, which the doc says is only for debugging because it involves network request. This is confusing to me since both methods involve network request.
Do you have any ideas?
easco
According to this:
The
Cache-Controlheader returned with the key indicates when you need to revalidate the key. You can create a process that continuously retrieves the key, and waits for the provided invalidation interval before retrieving the next key.That process can always return the current valid key and the system only makes net requests when necessary.
fireproofsocks
Yeah, the tokens rotate, so you will have to query that endpoint periodically to fetch the latest keys. I haven’t written code for that yet, but it should be a simple curl operation or something.