Skip to content

Commit

Permalink
Transform none_alg_used error to Elixir struct (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen authored Sep 14, 2023
1 parent 0175e55 commit 92bad54
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 59 deletions.
80 changes: 36 additions & 44 deletions lib/oidcc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,16 @@ defmodule Oidcc do
opts :: :oidcc_token.retrieve_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) do
with {:ok, token} <-
:oidcc.retrieve_token(
auth_code,
provider_configuration_name,
client_id,
client_secret,
opts
) do
{:ok, Oidcc.Token.record_to_struct(token)}
end
end
def retrieve_token(auth_code, provider_configuration_name, client_id, client_secret, opts),
do:
auth_code
|> :oidcc.retrieve_token(
provider_configuration_name,
client_id,
client_secret,
opts
)
|> Oidcc.Token.normalize_token_response()

@doc """
Refresh Token
Expand Down Expand Up @@ -145,16 +143,14 @@ defmodule Oidcc do
token when is_binary(token) -> token
end

with {:ok, token} <-
:oidcc.refresh_token(
token,
provider_configuration_name,
client_id,
client_secret,
opts
) do
{:ok, Oidcc.Token.record_to_struct(token)}
end
token
|> :oidcc.refresh_token(
provider_configuration_name,
client_id,
client_secret,
opts
)
|> Oidcc.Token.normalize_token_response()
end

@doc """
Expand Down Expand Up @@ -300,17 +296,15 @@ defmodule Oidcc do
def jwt_profile_token(subject, provider_configuration_name, client_id, client_secret, jwk, opts) do
jwk = JOSE.JWK.to_record(jwk)

with {:ok, token} <-
:oidcc.jwt_profile_token(
subject,
provider_configuration_name,
client_id,
client_secret,
jwk,
opts
) do
{:ok, Oidcc.Token.record_to_struct(token)}
end
subject
|> :oidcc.jwt_profile_token(
provider_configuration_name,
client_id,
client_secret,
jwk,
opts
)
|> Oidcc.Token.normalize_token_response()
end

@doc """
Expand Down Expand Up @@ -341,15 +335,13 @@ defmodule Oidcc do
opts :: :oidcc_token.client_credentials_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) do
with {:ok, token} <-
:oidcc.client_credentials_token(
provider_configuration_name,
client_id,
client_secret,
opts
) do
{:ok, Oidcc.Token.record_to_struct(token)}
end
end
def client_credentials_token(provider_configuration_name, client_id, client_secret, opts),
do:
provider_configuration_name
|> :oidcc.client_credentials_token(
client_id,
client_secret,
opts
)
|> Oidcc.Token.normalize_token_response()
end
42 changes: 27 additions & 15 deletions lib/oidcc/token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ defmodule Oidcc.Token do
def retrieve(auth_code, client_context, opts) do
client_context = ClientContext.struct_to_record(client_context)

with {:ok, token} <- :oidcc_token.retrieve(auth_code, client_context, opts) do
{:ok, record_to_struct(token)}
end
auth_code
|> :oidcc_token.retrieve(client_context, opts)
|> normalize_token_response()
end

@doc """
Expand Down Expand Up @@ -209,9 +209,9 @@ defmodule Oidcc.Token do

client_context = ClientContext.struct_to_record(client_context)

with {:ok, token} <- :oidcc_token.refresh(token, client_context, opts) do
{:ok, record_to_struct(token)}
end
token
|> :oidcc_token.refresh(client_context, opts)
|> normalize_token_response()
end

@doc """
Expand Down Expand Up @@ -301,9 +301,9 @@ defmodule Oidcc.Token do
jwk = JOSE.JWK.to_record(jwk)
client_context = ClientContext.struct_to_record(client_context)

with {:ok, token} <- :oidcc_token.jwt_profile(subject, client_context, jwk, opts) do
{:ok, record_to_struct(token)}
end
subject
|> :oidcc_token.jwt_profile(client_context, jwk, opts)
|> normalize_token_response()
end

@doc """
Expand Down Expand Up @@ -339,13 +339,25 @@ defmodule Oidcc.Token do
client_context :: ClientContext.t(),
opts :: :oidcc_token.client_credentials_opts()
) :: {:ok, t()} | {:error, :oidcc_token.error()}
def client_credentials(client_context, opts) do
client_context = ClientContext.struct_to_record(client_context)
def client_credentials(client_context, opts),
do:
client_context
|> ClientContext.struct_to_record()
|> :oidcc_token.client_credentials(opts)
|> normalize_token_response()

@doc false
@spec normalize_token_response(
response :: {:ok, :oidcc_token.t()} | {:error, :oidcc_token.error()}
) ::
{:ok, t()} | {:error, :oidcc_token.error()}
def normalize_token_response(response)
def normalize_token_response({:ok, token}), do: {:ok, record_to_struct(token)}

with {:ok, token} <- :oidcc_token.client_credentials(client_context, opts) do
{:ok, record_to_struct(token)}
end
end
def normalize_token_response({:error, {:none_alg_used, token}}),
do: {:error, {:none_alg_used, record_to_struct(token)}}

def normalize_token_response({:error, reason}), do: {:error, reason}

@impl Oidcc.RecordStruct
def record_to_struct(record) do
Expand Down

0 comments on commit 92bad54

Please sign in to comment.