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

Support client_secret_jwt & private_key_jwt for token auth #251

Merged
merged 1 commit into from
Sep 21, 2023
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
3 changes: 2 additions & 1 deletion include/oidcc_client_context.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
provider_configuration :: oidcc_provider_configuration:t(),
jwks :: jose_jwk:key(),
client_id :: binary(),
client_secret :: binary()
client_secret :: binary(),
client_jwks = none :: jose_jwk:key() | none
}).

-define(OIDCC_CLIENT_CONTEXT_HRL, 1).
Expand Down
18 changes: 9 additions & 9 deletions lib/oidcc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Oidcc do
provider_configuration_name :: GenServer.name(),
client_id :: String.t(),
client_secret :: String.t(),
opts :: :oidcc_authorization.opts()
opts :: :oidcc_authorization.opts() | :oidcc_client_context.opts()
) ::
{:ok, :uri_string.uri_string()}
| {:error, :oidcc_client_context.error() | :oidcc_client_context.error()}
Expand Down Expand Up @@ -87,7 +87,7 @@ defmodule Oidcc do
provider_configuration_name :: GenServer.name(),
client_id :: String.t(),
client_secret :: String.t(),
opts :: :oidcc_token.retrieve_opts()
opts :: :oidcc_token.retrieve_opts() | :oidcc_client_context.opts()
) ::
{:ok, Oidcc.Token.t()} | {:error, :oidcc_client_context.error() | :oidcc_token.error()}
def retrieve_token(auth_code, provider_configuration_name, client_id, client_secret, opts),
Expand Down Expand Up @@ -130,14 +130,14 @@ defmodule Oidcc do
provider_configuration_name :: GenServer.name(),
client_id :: String.t(),
client_secret :: String.t(),
opts :: :oidcc_token.refresh_opts()
opts :: :oidcc_token.refresh_opts() | :oidcc_client_context.opts()
) :: {:ok, Oidcc.Token.t()} | {:error, :oidcc_token.error()}
@spec refresh_token(
token :: Oidcc.Token.t(),
provider_configuration_name :: GenServer.name(),
client_id :: String.t(),
client_secret :: String.t(),
opts :: :oidcc_token.refresh_opts_no_sub()
opts :: :oidcc_token.refresh_opts_no_sub() | :oidcc_client_context.opts()
) ::
{:ok, Oidcc.Token.t()} | {:error, :oidcc_client_context.error() | :oidcc_token.error()}
def refresh_token(token, provider_configuration_name, client_id, client_secret, opts \\ %{}) do
Expand Down Expand Up @@ -182,7 +182,7 @@ defmodule Oidcc do
provider_configuration_name :: GenServer.name(),
client_id :: String.t(),
client_secret :: String.t(),
opts :: :oidcc_token_introspection.opts()
opts :: :oidcc_token_introspection.opts() | :oidcc_client_context.opts()
) ::
{:ok, Oidcc.TokenIntrospection.t()}
| {:error, :oidcc_client_context.error() | :oidcc_token_introspection.error()}
Expand Down Expand Up @@ -240,14 +240,14 @@ defmodule Oidcc do
provider_configuration_name :: GenServer.name(),
client_id :: String.t(),
client_secret :: String.t(),
opts :: :oidcc_userinfo.retrieve_opts_no_sub()
opts :: :oidcc_userinfo.retrieve_opts_no_sub() | :oidcc_client_context.opts()
) :: {:ok, :oidcc_jwt_util.claims()} | {:error, :oidcc_userinfo.error()}
@spec retrieve_userinfo(
access_token :: String.t(),
provider_configuration_name :: GenServer.name(),
client_id :: String.t(),
client_secret :: String.t(),
opts :: :oidcc_userinfo.retrieve_opts()
opts :: :oidcc_userinfo.retrieve_opts() | :oidcc_client_context.opts()
) ::
{:ok, :oidcc_jwt_util.claims()}
| {:error, :oidcc_client_context.error() | :oidcc_userinfo.error()}
Expand Down Expand Up @@ -297,7 +297,7 @@ defmodule Oidcc do
client_id :: String.t(),
client_secret :: String.t(),
jwk :: JOSE.JWK.t(),
opts :: :oidcc_token.jwt_profile_opts()
opts :: :oidcc_token.jwt_profile_opts() | :oidcc_client_context.opts()
) ::
{:ok, Oidcc.Token.t()} | {:error, :oidcc_client_context.error() | :oidcc_token.error()}
def jwt_profile_token(subject, provider_configuration_name, client_id, client_secret, jwk, opts) do
Expand Down Expand Up @@ -340,7 +340,7 @@ defmodule Oidcc do
provider_configuration_name :: GenServer.name(),
client_id :: String.t(),
client_secret :: String.t(),
opts :: :oidcc_token.client_credentials_opts()
opts :: :oidcc_token.client_credentials_opts() | :oidcc_client_context.opts()
) ::
{:ok, Oidcc.Token.t()} | {:error, :oidcc_client_context.error() | :oidcc_token.error()}
def client_credentials_token(provider_configuration_name, client_id, client_secret, opts),
Expand Down
36 changes: 27 additions & 9 deletions lib/oidcc/client_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ defmodule Oidcc.ClientContext do
provider_configuration: ProviderConfiguration.t(),
jwks: JOSE.JWK.t(),
client_id: String.t(),
client_secret: String.t()
client_secret: String.t(),
client_jwks: JOSE.JWK.t() | none
}

@doc """
Expand All @@ -46,21 +47,26 @@ defmodule Oidcc.ClientContext do
...> Oidcc.ClientContext.from_configuration_worker(
...> pid,
...> "client_id",
...> "client_Secret"
...> "client_Secret",
...> %{client_jwks: JOSE.JWK.generate_key(16)}
...> )
"""
@doc since: "3.0.0"
@spec from_configuration_worker(
provider_name :: GenServer.name(),
client_id :: String.t(),
client_secret :: String.t()
client_secret :: String.t(),
opts :: :oidcc_client_context.opts()
) :: {:ok, t()} | {:error, :oidcc_client_context.t()}
def from_configuration_worker(provider_name, client_id, client_secret) do
def from_configuration_worker(provider_name, client_id, client_secret, opts \\ %{}) do
opts = Map.update(opts, :client_jwks, :none, &JOSE.JWK.to_record/1)

with {:ok, client_context} <-
:oidcc_client_context.from_configuration_worker(
provider_name,
client_id,
client_secret
client_secret,
opts
) do
{:ok, record_to_struct(client_context)}
end
Expand All @@ -86,22 +92,25 @@ defmodule Oidcc.ClientContext do
...> configuration,
...> jwks,
...> "client_id",
...> "client_Secret"
...> "client_Secret",
...> %{client_jwks: JOSE.JWK.generate_key(16)}
...> )
"""
@doc since: "3.0.0"
@spec from_manual(
configuration :: ProviderConfiguration.t(),
jwks :: JOSE.JWK.t(),
client_id :: String.t(),
client_secret :: String.t()
client_secret :: String.t(),
opts :: :oidcc_client_context.opts()
) :: t()
def from_manual(configuration, jwks, client_id, client_secret) do
def from_manual(configuration, jwks, client_id, client_secret, opts \\ %{}) do
configuration = ProviderConfiguration.struct_to_record(configuration)
jwks = JOSE.JWK.to_record(jwks)
opts = Map.update(opts, :client_jwks, :none, &JOSE.JWK.to_record/1)

configuration
|> :oidcc_client_context.from_manual(jwks, client_id, client_secret)
|> :oidcc_client_context.from_manual(jwks, client_id, client_secret, opts)
|> record_to_struct()
end

Expand All @@ -111,13 +120,22 @@ defmodule Oidcc.ClientContext do
|> super()
|> Map.update!(:provider_configuration, &ProviderConfiguration.record_to_struct/1)
|> Map.update!(:jwks, &JOSE.JWK.from_record/1)
|> update_if_not_none(:client_jwks, &JOSE.JWK.from_record/1)
end

@impl Oidcc.RecordStruct
def struct_to_record(struct) do
struct
|> Map.update!(:provider_configuration, &ProviderConfiguration.struct_to_record/1)
|> Map.update!(:jwks, &JOSE.JWK.to_record/1)
|> update_if_not_none(:client_jwks, &JOSE.JWK.to_record/1)
|> super()
end

defp update_if_not_none(map, key, callback) do
Map.update!(map, key, fn
:none -> :none
other -> callback.(other)
end)
end
end
Loading
Loading