make provider config simpler
All checks were successful
Build and Publish / build-release (push) Successful in 1m30s
All checks were successful
Build and Publish / build-release (push) Successful in 1m30s
This commit is contained in:
@@ -14,33 +14,20 @@ defmodule PolicyServiceWeb.Plugs.AuthenticationPlug do
|
||||
|
||||
## Options
|
||||
- :provider - The OIDCC provider configuration worker name (required)
|
||||
- :client_id - OAuth2 client ID (required) - can be a string or {module, function, args} tuple
|
||||
- :client_secret - OAuth2 client secret (required) - can be a string or {module, function, args} tuple
|
||||
- :required_scopes - List of required scopes (optional)
|
||||
"""
|
||||
def init(opts) do
|
||||
provider = Keyword.fetch!(opts, :provider)
|
||||
client_id = Keyword.fetch!(opts, :client_id)
|
||||
client_secret = Keyword.fetch!(opts, :client_secret)
|
||||
required_scopes = Keyword.get(opts, :required_scopes, [])
|
||||
|
||||
zitadel_config = Application.get_env(:policy_service, :zitadel, [])
|
||||
|
||||
%{
|
||||
provider: provider,
|
||||
client_id: resolve_config(client_id),
|
||||
client_secret: resolve_config(client_secret),
|
||||
required_scopes: required_scopes
|
||||
client_id: Keyword.get(zitadel_config, :client_id),
|
||||
client_secret: Keyword.get(zitadel_config, :client_secret),
|
||||
required_scopes: Keyword.get(zitadel_config, :required_scopes, [])
|
||||
}
|
||||
end
|
||||
|
||||
defp resolve_config({module, function, args})
|
||||
when is_atom(module) and is_atom(function) and is_list(args) do
|
||||
apply(module, function, args)
|
||||
end
|
||||
|
||||
defp resolve_config(value) when is_binary(value), do: value
|
||||
defp resolve_config(value) when is_function(value, 0), do: value.()
|
||||
defp resolve_config(value), do: value
|
||||
|
||||
@doc """
|
||||
Authenticates the request by validating the JWT token.
|
||||
|
||||
|
||||
@@ -10,10 +10,7 @@ defmodule PolicyServiceWeb.Router do
|
||||
|
||||
pipeline :authenticated do
|
||||
plug PolicyServiceWeb.Plugs.AuthenticationPlug,
|
||||
provider: PolicyService.ZitadelProvider,
|
||||
client_id: {__MODULE__, :get_zitadel_config, [:client_id]},
|
||||
client_secret: {__MODULE__, :get_zitadel_config, [:client_secret]},
|
||||
required_scopes: {__MODULE__, :get_zitadel_config, [:required_scopes]}
|
||||
provider: PolicyService.ZitadelProvider
|
||||
end
|
||||
|
||||
pipeline :authorized do
|
||||
@@ -41,8 +38,4 @@ defmodule PolicyServiceWeb.Router do
|
||||
scope "/swaggerui" do
|
||||
get "/", OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi"
|
||||
end
|
||||
|
||||
def get_zitadel_config(key) do
|
||||
Application.get_env(:policy_service, :zitadel)[key]
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user