Guardian & Guardian.DB 1.0 - No secret key configured for JWT

After upgrading from 0.14 to 1.0, I can’t figured out how to setup Guardian with Guardian.DB.
A token is corrected stored in the database, but verification fails.

CONFIG

config :MyApp, MyApp.Guardian,
  allowed_algos: ["HS512"],
  #token_verify_module: MyApp.Guardian.Common,
  issuer: "MyApp",
  default_token_type: :access,
  token_ttl: %{
                :access => {2, :hours}
              },
  allowed_drift: 2000,
  verify_issuer: true,
  secret_key: System.get_env("MyApp_GUARDIAN_KEY"),
  permissions: %{
    account: [:read, :write, :delete, :admin, :export, :dashboard],
    group: [:read, :write, :delete, :admin, :export, :dashboard],
    mgmt: [:read, :write, :delete, :admin, :export, :dashboard],
  }

config :MyApp, MyApp.Guardian.AuthPipeline,
  module: MyApp.Guardian.Common,
  error_handler: MyAppWeb.Auth.UeberauthController

config :guardian, Guardian.DB,
  repo: MyApp.Repo,
  schema_name: "guardian_tokens",
  sweep_interval: 60

GUARDIAN

...
  # GUARDIAN DB
  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
...

PIPELINE

defmodule MyApp.Guardian.AuthPipeline do
  @moduledoc """
  Guardian Auth Pipeline Settings.
  """

  use Guardian.Plug.Pipeline, otp_app: :phishx,
                              module: MyApp.Guardian.Common,
                              error_error_handler: MyAppWeb.Auth.UeberauthController,
                              key: System.get_env("MyApp_GUARDIAN_KEY")

  plug Guardian.Plug.VerifySession, key: System.get_env("PHISHX_GUARDIAN_KEY")
  #plug Guardian.Plug.EnsureAuthenticated
  #plug Guardian.Plug.LoadResource, key: @key

end

ERROR

{:invalid_token, %RuntimeError{message: “No secret key configured for JWT”}}

1 Like

I had the same issue but cannot remember what was causing it, give this code a try and see if it helps:

defmodule MyWeb.Guardian.AuthPipeline do
  use Guardian.Plug.Pipeline, otp_app: :my,
    module: MyWeb.Guardian,
    error_handler: MyWeb.Guardian.AuthErrorHandler

  plug Guardian.Plug.VerifyHeader, realm: "Bearer"
  #plug Guardian.Plug.VerifySession
  plug Guardian.Plug.EnsureAuthenticated, claims: %{"typ" => "access"} // pay attention to this line, try it

end

The above suits API authentication, modify it based on your needs.