make provider config simpler
All checks were successful
Build and Publish / build-release (push) Successful in 1m30s

This commit is contained in:
2026-05-04 16:06:19 -05:00
parent 44d89014fd
commit 2137cf4959
3 changed files with 9 additions and 31 deletions

View File

@@ -5,12 +5,10 @@ defmodule PolicyService.Application do
use Application use Application
defp get_zitadel_config(key) do
Application.get_env(:policy_service, :zitadel)[key]
end
@impl true @impl true
def start(_type, _args) do def start(_type, _args) do
zitadel_config = Application.get_env(:policy_service, :zitadel, [])
children = [ children = [
PolicyService.CommandedApp, PolicyService.CommandedApp,
PolicyService.Handlers.QuoteRequestHandler, PolicyService.Handlers.QuoteRequestHandler,
@@ -25,7 +23,7 @@ defmodule PolicyService.Application do
{Phoenix.PubSub, name: PolicyService.PubSub, pool_size: 1}, {Phoenix.PubSub, name: PolicyService.PubSub, pool_size: 1},
{Oidcc.ProviderConfiguration.Worker, {Oidcc.ProviderConfiguration.Worker,
%{ %{
issuer: get_zitadel_config(:issuer), issuer: Keyword.get(zitadel_config, :issuer),
name: PolicyService.ZitadelProvider name: PolicyService.ZitadelProvider
}}, }},
PolicyServiceWeb.Endpoint PolicyServiceWeb.Endpoint

View File

@@ -14,33 +14,20 @@ defmodule PolicyServiceWeb.Plugs.AuthenticationPlug do
## Options ## Options
- :provider - The OIDCC provider configuration worker name (required) - :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 def init(opts) do
provider = Keyword.fetch!(opts, :provider) provider = Keyword.fetch!(opts, :provider)
client_id = Keyword.fetch!(opts, :client_id)
client_secret = Keyword.fetch!(opts, :client_secret) zitadel_config = Application.get_env(:policy_service, :zitadel, [])
required_scopes = Keyword.get(opts, :required_scopes, [])
%{ %{
provider: provider, provider: provider,
client_id: resolve_config(client_id), client_id: Keyword.get(zitadel_config, :client_id),
client_secret: resolve_config(client_secret), client_secret: Keyword.get(zitadel_config, :client_secret),
required_scopes: required_scopes required_scopes: Keyword.get(zitadel_config, :required_scopes, [])
} }
end 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 """ @doc """
Authenticates the request by validating the JWT token. Authenticates the request by validating the JWT token.

View File

@@ -10,10 +10,7 @@ defmodule PolicyServiceWeb.Router do
pipeline :authenticated do pipeline :authenticated do
plug PolicyServiceWeb.Plugs.AuthenticationPlug, plug PolicyServiceWeb.Plugs.AuthenticationPlug,
provider: PolicyService.ZitadelProvider, 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]}
end end
pipeline :authorized do pipeline :authorized do
@@ -41,8 +38,4 @@ defmodule PolicyServiceWeb.Router do
scope "/swaggerui" do scope "/swaggerui" do
get "/", OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi" get "/", OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi"
end end
def get_zitadel_config(key) do
Application.get_env(:policy_service, :zitadel)[key]
end
end end