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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
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:
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:
danschultzer
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
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/3does exactly what I want. Thank you!Catharz
I’ve been having similar issues, trying to sign something just using
JOSE.JWT.sign/3.It seemed that using
File.read/1and thenJOSE.JWK.from_pem/1was giving different results toJOSE.JWK.from_pem_file/1. Unfortunately, the documentation for JOSE is really sparse, so I never worked out exactly why.enkr1
Is anybody still on here?
am I not able to verify a JWT by
public.pemwith the limited resources online…I really need help
Thanks in advance.
Best wishes,
Jing Hui P.
benwilson512
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
Thank you @benwilson512 for taking the time to reply to my question!!
Here is how i did it:
output:
Best,
Jing Hui P.
enkr1
I also tried this guy’s solution Using Joken to validate Google JWTs - #12 by vinagrito1
but what i am getting is
falseinstead oftrue: