zoedsoupe

zoedsoupe

Guardian error when creating jwt token

I have this Guardian module:

defmodule Sntx.Guardian do
  use Guardian, otp_app: :sntx

  alias Sntx.Repo
  alias Sntx.User.Account

  def subject_for_token(user, _claims) do
    sub = to_string(user.id)
    {:ok, sub}
  end

  def resource_from_claims(claims) do
    user = Repo.get(Account, claims["sub"])
    {:ok, user}
  end

  def after_encode_and_sign(resource, claims, token, _options) do
    with {:ok, _} <- Guardian.DB.after_encode_and_sign(resource, claims["typ"], claims, token) do
      {:ok, token}
    end
  end

  def on_verify(claims, token, _options) do
    with {:ok, _} <- Guardian.DB.on_verify(claims, token) do
      {:ok, claims}
    end
  end

  def on_refresh({old_token, old_claims}, {new_token, new_claims}, _options) do
    with {:ok, _, _} <- Guardian.DB.on_refresh({old_token, old_claims}, {new_token, new_claims}) do
      {:ok, {old_token, old_claims}, {new_token, new_claims}}
    end
  end

  def on_revoke(claims, token, _options) do
    with {:ok, _} <- Guardian.DB.on_revoke(claims, token) do
      {:ok, claims}
    end
  end
end

Then, if I try to execute Sntx.Guardian.encode_and_sign(%{id: "123"}) I receive this error:

** (CaseClauseError) no case clause matching: 43
    (jose 1.11.5) src/base/jose_base64url.erl:136: :jose_base64url."-decode!/2-lbc$^0/2-0-"/2
    (jose 1.11.5) src/base/jose_base64url.erl:136: :jose_base64url.decode!/2
    (jose 1.11.5) src/jwk/jose_jwk_kty_oct.erl:50: :jose_jwk_kty_oct.from_map/1
    (jose 1.11.5) src/jwk/jose_jwk.erl:304: :jose_jwk.from_map/1
    (jose 1.11.5) lib/jose/jwk.ex:140: JOSE.JWK.from_map/1
    (guardian 2.3.1) lib/guardian/token/jwt.ex:272: Guardian.Token.Jwt.create_token/3
    (guardian 2.3.1) lib/guardian.ex:776: Guardian.returning_tuple/1
    (guardian 2.3.1) lib/guardian.ex:602: Guardian.encode_and_sign/4

I’m really confused about this error. Any idea that what this means?

Marked As Solved

al2o3cr

al2o3cr

The basic cause is that a + character (byte value 43) appears in a string that :jose_jwk.from_map/1 treats as encoded in URL-safe Base64, which doesn’t allow that character. That behavior is directly from the specification for kty = oct.

The root cause is that this incorrectly-encoded value is being set in config/config.exs from an environment variable: https://github.com/zoedsoupe/sntx/blob/feature/blog-posts/config/config.exs#L30

Also Liked

adamu

adamu

Not directly answering your question, but just in case

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
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
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement