zoedsoupe

zoedsoupe

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?

Showing Posts 1 to 5

zoedsoupe

zoedsoupe OP

There is a public repo here: https://github.com/zoedsoupe/sntx, on branch feature/blog-posts

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

adamu

adamu

Not directly answering your question, but just in case

zoedsoupe

zoedsoupe OP

Oh thank you! I generated the token with mix guardian.gen.secret but it seem was generated with a + char!

al2o3cr

al2o3cr

Yeah, that’s a tricky one - the sets of characters used by encode64 and url_encode64 are nearly the same, so you got a bad roll of the dice.

I created an issue on Guardian to address / document this:

https://github.com/ueberauth/guardian/issues/718

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews