tmariaz

tmariaz

API token is invalid_token when decoding with guardian

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.

First Post!

tmariaz

tmariaz

Tried 2 methods

Method 1

  1. Login via frontend to auth0
  2. Read the token and pass it to api
  3. Backend API is to validate the token

Method 2

  1. Send the request from api to auth0
  2. Callback should validate the return struct
  3. There the token shows invalid_token when decoding it.

Not sure which method is correct?

Last Post!

tmariaz

tmariaz

Hi… sorry for late reply…
I just happen to try with the following and it still throws invalid_token

 plug Guardian.Plug.VerifyHeader, scheme: "Bearer", claims: %{"typ" => "JWT"}

Should I just remove the claims altogether? Without claims it works fine. Or is this security issue?

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New

We're in Beta

About us Mission Statement