sjoconnor

sjoconnor

Trouble with Guardian JWT Config

I’m trying to setup Guardian to work with Phoenix and Ldap. I seem to be stuck with some configuration . My Guardian config looks roughly like this:

config :guardian, Guardian,
  allowed_algos: ["HS512"],
  verify_module: Guardian.JWT,
  issuer: "Issuer",
  ttl: { 30, :days},
  verify_issuer: true,
  secret_key: <MY KEY>,
  serializer: MyApp.Auth.GuardianSerializer

I have a SessionController where I’m hitting Ldap and then doing something like this:

    case Ldap.authenticate(username, password) do
      :ok -> handle_sign_in(conn, username)
      _   -> handle_error(conn)
    end

    {:ok, user} = lookup_user(username)
    conn
    |> put_flash(:info, "Logged in.")
    |> MyApp.Auth.Guardian.Plug.sign_in(user)
    |> redirect(to: "/")

The Ldap portion is working fine, but I keep getting this error from Guardian:

Request: POST /sign_in
** (exit) an exception was raised:
    ** (RuntimeError) No secret key configured for JWT
        (guardian) lib/guardian/token/jwt.ex:319: Guardian.Token.Jwt.fetch_secret/2
        (guardian) lib/guardian/token/jwt.ex:163: Guardian.Token.Jwt.create_token/3
        (guardian) lib/guardian.ex:754: Guardian.returning_tuple/1
        (guardian) lib/guardian.ex:581: Guardian.encode_and_sign/4
        (guardian) lib/guardian/plug.ex:172: Guardian.Plug.sign_in/5
        (infrared) lib/myapp_web/controllers/session_controller.ex:23: MyAppWeb.SessionController.handle_sign_in/2
        (infrared) lib/myapp_web/controllers/session_controller.ex:1: MyAppWeb.SessionController.action/2
        (infrared) lib/myapp_web/controllers/session_controller.ex:1: MyAppWeb.SessionController.phoenix_controller_pipeline/2
        (infrared) lib/myapp_web/endpoint.ex:1: MyAppWeb.Endpoint.instrument/4
        (phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
        (infrared) lib/myapp_web/endpoint.ex:1: MyAppWeb.Endpoint.plug_builder_call/2
        (infrared) lib/plug/debugger.ex:99: MyAppWeb.Endpoint."call (overridable 3)"/2
        (infrared) lib/myapp_web/endpoint.ex:1: MyAppWeb.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4

I can’t tell if this is a config issue on my side or a bug. Any help would be much appreciated.

Most Liked

subetei

subetei

Can’t tell for sure since you obviously had to blank out your key but maybe you haven’t generated one in the format they need and added it to config. Did you already follow the instructions here first? https://hexdocs.pm/jose/key-generation.html

I used a file at first and then went to keep it in config so after generating a valid key the config option simply looked like so

secret_key: %{
    "k" => "supersecretimportantkey",
    "kty" => "oct"
  },

Let me know if I can help you test anything if that turns out to be useless. happy to help and have a working setup atm

sjoconnor

sjoconnor

Looks like my problem was two-fold.

  1. I wasn’t generating my key right. Thanks for pointing me in the right direction there.
  2. I didn’t set otp_app: :someapp correctly in my module. What I named it didn’t match my config, silly mistake.

Thanks for the help! That got me moving forward for now.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
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
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement