Skip to content

Commit

Permalink
Users should be able to change most config values at runtime (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
geofflane committed Nov 24, 2022
1 parent f6459d0 commit d8c87e3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog

## v2.3.1

* Change compile time loading of configuration to only load permissions
allowing the app to change things like ttl or secret key at runtime

## v2.3.0

* Fix warning about the usage of `Application.get_env` in the module scope
Expand Down
24 changes: 14 additions & 10 deletions lib/guardian.ex
Expand Up @@ -340,16 +340,20 @@ defmodule Guardian do
the_otp_app = unquote(otp_app)
the_opts = unquote(opts)

# Provide a way to get at the configuration during compile time
# for other macros that may want to use them
@config fn ->
the_otp_app |> Application.compile_env(__MODULE__, []) |> Keyword.merge(the_opts)
end
@config_with_key fn key ->
@config.() |> Keyword.get(key) |> Guardian.Config.resolve_value()
end
@config_with_key_and_default fn key, default ->
@config.() |> Keyword.get(key, default) |> Guardian.Config.resolve_value()
# Provide a way to get at the permissions during compile time. Uses
# permissions from config if they are available and falls back to the
# permissins defined on the `use Guardian` implementation
#
# NOTE: Generally you can't use compile_env for most keys because that
# would prevent people from changing them at runtime for differen
# environements.And hardcoding secret keys wouldn't be considered a good
# practice.
@config_permissions fn ->
perms =
Application.compile_env(the_otp_app, [__MODULE__, :permissions]) ||
Keyword.get(the_opts, :permissions, [])

Guardian.Config.resolve_value(perms)
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/guardian/permissions.ex
Expand Up @@ -137,7 +137,7 @@ defmodule Guardian.Permissions do

defdelegate max(), to: Guardian.Permissions

raw_perms = @config_with_key.(:permissions)
raw_perms = @config_permissions.()

unless raw_perms do
raise "Permissions are not defined for #{to_string(__MODULE__)}"
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -2,7 +2,7 @@ defmodule Guardian.Mixfile do
@moduledoc false
use Mix.Project

@version "2.3.0"
@version "2.3.1"
@url "https://github.com/ueberauth/guardian"
@maintainers [
"Daniel Neighman",
Expand Down

0 comments on commit d8c87e3

Please sign in to comment.