Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOTOR-1278]: Make log level configurable #74

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ config :prima_auth0_ex, :server,
# This is useful for local development but should NEVER be enabled on production-like systems.
# Defaults to false.
ignore_signature: false
# Level used to log requests where the authorization header is missing.
missing_auth_header_log_level: :warn
```

## Usage
Expand Down
21 changes: 18 additions & 3 deletions lib/prima_auth0_ex/plug/verify_and_validate_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,25 @@ defmodule PrimaAuth0Ex.Plug.VerifyAndValidateToken do
ignore_signature = Keyword.get(opts, :ignore_signature, global_ignore_signature())
required_permissions = Keyword.get(opts, :required_permissions, [])

if authorized?(conn, audience, required_permissions, ignore_signature), do: conn, else: forbidden(conn, dry_run?)
missing_auth_header_log_level =
Keyword.get(opts, :missing_auth_header_log_level, global_missing_auth_header_log_level())

if authorized?(conn, audience, required_permissions, ignore_signature, missing_auth_header_log_level),
do: conn,
else: forbidden(conn, dry_run?)
end

defp authorized?(conn, audience, required_permissions, ignore_signature) do
defp authorized?(conn, audience, required_permissions, ignore_signature, missing_auth_header_log_level) do
case get_req_header(conn, "authorization") do
[] ->
Logger.log(missing_auth_header_log_level, "Authorization header not found")
false

["Bearer " <> token] ->
valid_token?(token, audience, required_permissions, ignore_signature)

_other ->
Logger.warn("Authorization header malformed or not found")
Logger.warn("Authorization header malformed")
false
end
end
Expand Down Expand Up @@ -82,4 +91,10 @@ defmodule PrimaAuth0Ex.Plug.VerifyAndValidateToken do
:prima_auth0_ex
|> Application.get_env(:server, [])
|> Keyword.get(:ignore_signature, false)

defp global_missing_auth_header_log_level,
do:
:prima_auth0_ex
|> Application.get_env(:server, [])
|> Keyword.get(:missing_auth_header_log_level, :warn)
end