From 92bad54fc08505af2f4a5894557437ede5580c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Thu, 14 Sep 2023 22:24:55 +0200 Subject: [PATCH] Transform none_alg_used error to Elixir struct (#228) --- lib/oidcc.ex | 80 +++++++++++++++++++++------------------------- lib/oidcc/token.ex | 42 +++++++++++++++--------- 2 files changed, 63 insertions(+), 59 deletions(-) diff --git a/lib/oidcc.ex b/lib/oidcc.ex index 47bfc79..e05c1f8 100644 --- a/lib/oidcc.ex +++ b/lib/oidcc.ex @@ -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 @@ -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 """ @@ -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 """ @@ -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 diff --git a/lib/oidcc/token.ex b/lib/oidcc/token.ex index 37a4c29..173dbdb 100644 --- a/lib/oidcc/token.ex +++ b/lib/oidcc/token.ex @@ -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 """ @@ -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 """ @@ -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 """ @@ -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