tmariaz
My API which authenticates via auth0 and returns the access_token. During the callback, the access_token throws invalid_token error. Not sure what is going on.
Here are my codes.
router.ex
scope "/auth", PxrfWeb do
pipe_through [:browser]
get "/:provider", AuthController, :request
get "/:provider/callback", AuthController, :callback
end
config.exs
config :pxrf, Pxrf.Auth.Guardian,
issuer: "https://myissuer.xx.auth0.com/",
secret_key: "xxxxxxxx"
auth_controller.ex
...
def callback(%{assigns: %{ueberauth_auth: auth}} = conn, %{"state" => state} = _params) do
IO.inspect(auth)
token = auth.extra.raw_info.token.access_token
case Pxrf.Auth.Guardian.decode_and_verify(token) do
{:ok, claims} ->
user = Accounts.get_user_by_username(claims["sub"])
{:error, _reason} ->
nil
end
end
...
When I run the following
iex> Guardian.decode_and_verify("eyJasds-----access-token-")
{:error, :invalid_token}
Been breaking my head for the past couple of days and can’t figure it out what is wrong. Looks like all my configuration and setup in the auth0 application seems to be correct. But still no luck.
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
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tmariaz
Tried 2 methods
Method 1
Method 2
Not sure which method is correct?
codeanpeace
Are you using this Auth0 Ueberauth strategy?
https://github.com/achedeuzot/ueberauth_auth0
If so, take a look at the callback functions in
auth_controller.exfrom the ueberauth example repo:tmariaz
Yes I am using ueberauth_auth0 Strategy.
config.exs
I wanted to use the Method 1 I’ve mentioned above whereby a user login with their auth0 credentials and which in return sends back the
access_token. So I wanted to pass that token to my protected api. When I do that I getinvalid_tokenerror.I have the follow pipeline and I think the error is thrown from there.
Still no luck decoding and verifying the token with the following
Also tried Method 2 and with the access_token still can’t decode and verify. Am I missing something?
Btw “
secret_key” and “client_secret” of Guardian and auth0 strategy is same.codeanpeace
If you peek inside the plug
Guardian.Plug.VerifyHeader, you can see that it already handles decoding and verifying the header on line 92.https://github.com/ueberauth/guardian/blob/v2.3.1/lib/guardian/plug/verify_header.ex#L92
So the invalid token error is likely from attempting to decode and verify a token that’s already been decoded and verified. If you take another look at the example in my previous post, it shows how the two
callbackfunction heads check for authentication success or failure in theconn.tmariaz
Is that means when I pass the token it is already validated and the current_resource is set into the connection? But I’m sending a valid token which I retrieved from auth0 login. If it is validated inside VerifyHeader then it is still throwing
invalid_tokenerror. What am I doing wrong?Let’s say if I comment out the VerifyHeader and it throws unauthenticated error.
Error:
I checked with jwt.io for the validity of the token and it is valid though. Still breaking my head. Sorry for being noob.
codeanpeace
The token is already validated when it reaches the
callbackfunction inauth_controller.exso you shouldn’t be callingdecode_and_verifyagain in the controller.The presence of the
ueberauth_failurekey withinconn.assignsindicates that it unsuccessfully ran through the Guardian pipeline whereas the presence ofueberauth_authindicates that it successfully ran through the Guardian pipeline, including verifying the sesson and header, ensuring authentication, and loading resource.If you want to access
claims["sub"], I suggest youIO.inspect(conn)and it should already be there.https://github.com/achedeuzot/ueberauth_auth0/blob/de1964785a3d8dc47fba2eb850345f0028b46651/lib/ueberauth/strategy/auth0.ex#L1-L13
https://github.com/achedeuzot/ueberauth_auth0/blob/de1964785a3d8dc47fba2eb850345f0028b46651/lib/ueberauth/strategy/auth0.ex#L193-L198
So since you’re using this
ueberauth_auth0strategy, you should be able to do something likeconn.private.auth0_user["sub"].If you comment out the
VerifyHeaderplug, it would make sense that a subsequent plug in the pipeline e.g.EnsureAuthenticatedmight fail with an unauthenticated error.tmariaz
Hey,
First of all thanks for the help. I found the problem with the Guardian algo. The current version of the algo is HS512 by default. However my token isn’t. I had to add the following algo to the config file. It partially work with VerifyHeader in the pipeline.
config.exs
However now I get another new problem.
pipeline.ex
Doesn’t work with claims param
Works without the claims param
Is it safer to do this?
But why with claims params I still get
invalid_tokenerror?codeanpeace
If it doesn’t work when specifying the claims param but works without, that suggests the
"typ"key in the decoded:claimsmap is not actually set to"access". Have you checked viaIO.inspectto see if it’s something else? Could you share what the decoded JWT and/orconnlooks like?For example, if the decoded claims response looks like this example from the Use Access Token | Auth0 docs, then
"typ"might be something like"JWT".tmariaz
Yea… I get
"typ":"JWT"so can I change the claim toJWTinstead ofaccess?Decoded JWT
codeanpeace
Yup, what you pass into claims option gets checked against the decoded token.
source: guardian/lib/guardian/plug/verify_header.ex at master · ueberauth/guardian · GitHub
Try this:
plug Guardian.Plug.VerifyHeader, scheme: "Bearer", claims: %{"typ" => "JWT"}