Skip to content
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
6 changes: 6 additions & 0 deletions lib/oidcc/client_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ defmodule Oidcc.ClientContext do
@doc """
Create Client Context from a `Oidcc.ProviderConfiguration.Worker`

An empty `client_secret` (`""`) is rejected with `{:error, :empty_client_secret}`.
Use `:unauthenticated` for public clients.

## Examples

iex> {:ok, pid} =
Expand Down Expand Up @@ -96,6 +99,9 @@ defmodule Oidcc.ClientContext do
@doc """
Create Client Context manually

An empty `client_secret` (`""`) raises an `ErlangError` with
`:empty_client_secret`. Use `:unauthenticated` for public clients.

## Examples

iex> {:ok, {configuration, _expiry}} =
Expand Down
18 changes: 17 additions & 1 deletion src/oidcc_client_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ To use the record, import the definition:
-type opts() :: authenticated_opts() | unauthenticated_opts().

-doc #{since => <<"3.0.0">>}.
-type error() :: provider_not_ready.
-type error() :: provider_not_ready | empty_client_secret.

-doc """
Create Client Context from a `m:oidcc_provider_configuration_worker`.
Expand All @@ -93,6 +93,9 @@ from_configuration_worker(ProviderName, ClientId, ClientSecret) ->
-doc """
Create Client Context from a `m:oidcc_provider_configuration_worker`.

An empty `ClientSecret` (`<<>>`) is rejected with `{error, empty_client_secret}`.
Use `unauthenticated` for public clients.

## Examples

```erlang
Expand Down Expand Up @@ -139,6 +142,8 @@ Create Client Context from a `m:oidcc_provider_configuration_worker`.
ClientId :: binary(),
ClientSecret :: unauthenticated,
Opts :: unauthenticated_opts().
from_configuration_worker(_ProviderName, _ClientId, <<>>, _Opts) ->
{error, empty_client_secret};
from_configuration_worker(ProviderName, ClientId, ClientSecret, Opts) ->
case resolve_pid(ProviderName) of
undefined ->
Expand Down Expand Up @@ -217,6 +222,9 @@ from_manual(Configuration, Jwks, ClientId, ClientSecret) ->
-doc """
Create Client Context manually.

An empty `ClientSecret` (`<<>>`) raises an `empty_client_secret` error.
Use `unauthenticated` for public clients.

## Examples

```erlang
Expand Down Expand Up @@ -264,6 +272,14 @@ from_manual(
client_id = ClientId,
client_secret = unauthenticated
};
from_manual(
#oidcc_provider_configuration{},
#jose_jwk{},
ClientId,
<<>>,
_Opts
) when is_binary(ClientId) ->
erlang:error(empty_client_secret);
from_manual(
#oidcc_provider_configuration{} = Configuration,
#jose_jwk{} = Jwks,
Expand Down
3 changes: 3 additions & 0 deletions src/oidcc_jwt_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ client_secret_oct_keys(undefined, _ClientSecret) ->
none;
client_secret_oct_keys(_AllowedAlgorithms, unauthenticated) ->
none;
%% An empty secret would yield an HMAC key anyone can reproduce; never use it.
client_secret_oct_keys(_AllowedAlgorithms, <<>>) ->
none;
client_secret_oct_keys(AllowedAlgorithms, ClientSecret) ->
case
lists:member(<<"HS256">>, AllowedAlgorithms) or
Expand Down
11 changes: 11 additions & 0 deletions test/oidcc/client_context_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,15 @@ defmodule Oidcc.ClientContextTest do
)
end
end

describe "empty client secret" do
test "from_configuration_worker rejects it" do
assert {:error, :empty_client_secret} =
Oidcc.ClientContext.from_configuration_worker(
__MODULE__.AnyProvider,
"client_id",
""
)
end
end
end
3 changes: 2 additions & 1 deletion test/oidcc_authorization_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ create_redirect_url_with_request_object_no_hmac_test() ->
},

ClientId = <<"client_id">>,
ClientSecret = <<"">>,
%% Dummy secret as in guides/private-key-jwt.md; an empty one is rejected.
ClientSecret = <<"dummy_client_secret">>,

Jwks0 = jose_jwk:to_public(jose_jwk:from_pem_file(PrivDir ++ "/test/fixtures/jwk.pem")),
Jwks = Jwks0#jose_jwk{fields = #{<<"use">> => <<"sig">>}},
Expand Down
32 changes: 32 additions & 0 deletions test/oidcc_client_context_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,35 @@ client_context_fixture() ->
ClientSecret = <<"client_secret">>,

oidcc_client_context:from_manual(Configuration, Jwks, ClientId, ClientSecret).

empty_client_secret_test() ->
PrivDir = code:priv_dir(oidcc),
{ok, ConfigurationBinary} = file:read_file(PrivDir ++ "/test/fixtures/example-metadata.json"),
{ok, Configuration} =
oidcc_provider_configuration:decode_configuration(json:decode(ConfigurationBinary)),
Jwks = jose_jwk:from_pem_file(PrivDir ++ "/test/fixtures/jwk.pem"),

?assertError(
empty_client_secret,
oidcc_client_context:from_manual(Configuration, Jwks, <<"client_id">>, <<>>)
),
?assertError(
empty_client_secret,
oidcc_client_context:from_manual(Configuration, Jwks, <<"client_id">>, <<>>, #{})
),
?assertMatch(
#oidcc_client_context{client_secret = unauthenticated},
oidcc_client_context:from_manual(Configuration, Jwks, <<"client_id">>, unauthenticated)
),

%% Rejected before the provider is even looked up
?assertEqual(
{error, empty_client_secret},
oidcc_client_context:from_configuration_worker(invalid, <<"client_id">>, <<>>)
),
?assertEqual(
{error, empty_client_secret},
oidcc_client_context:from_configuration_worker(invalid, <<"client_id">>, <<>>, #{})
),

ok.
14 changes: 14 additions & 0 deletions test/oidcc_jwt_util_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,17 @@ encrypt_with_kid(Key, Kid) ->
Jwe = #{<<"alg">> => <<"RSA-OAEP">>, <<"enc">> => <<"A256GCM">>, <<"kid">> => Kid},
{_Jwe, Token} = jose_jwe:compact(jose_jwk:block_encrypt(<<"{}">>, Jwe, Key)),
Token.

client_secret_oct_keys_empty_secret_test() ->
HmacAlgs = [<<"HS256">>, <<"HS384">>, <<"HS512">>],
?assertEqual(none, oidcc_jwt_util:client_secret_oct_keys(HmacAlgs, <<>>)),
?assertEqual(none, oidcc_jwt_util:client_secret_oct_keys(HmacAlgs, unauthenticated)),
?assertMatch(
#jose_jwk{},
oidcc_jwt_util:client_secret_oct_keys(HmacAlgs, <<"at_least_32_character_client_secret">>)
),

Jwks = jose_jwk:generate_key({rsa, 2048}),
?assertEqual(Jwks, oidcc_jwt_util:merge_client_secret_oct_keys(Jwks, HmacAlgs, <<>>)),

ok.
68 changes: 68 additions & 0 deletions test/oidcc_token_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2891,3 +2891,71 @@ retrieve_with_refresh_reports_refresh_info_test() ->
),

ok.

validate_id_token_empty_client_secret_test() ->
PrivDir = code:priv_dir(oidcc),

{ok, ConfigurationBinary} = file:read_file(PrivDir ++ "/test/fixtures/example-metadata.json"),
{ok, #oidcc_provider_configuration{issuer = Issuer} = Configuration} =
oidcc_provider_configuration:decode_configuration(json:decode(ConfigurationBinary)),

ClientId = <<"client_id">>,
Claims =
#{
<<"iss">> => Issuer,
<<"sub">> => <<"sub">>,
<<"aud">> => ClientId,
<<"iat">> => erlang:system_time(second),
<<"exp">> => erlang:system_time(second) + 10
},

%% Anyone can produce an HMAC with an empty key.
{_Jws, Token} = jose_jws:compact(
jose_jwt:sign(jose_jwk:from_oct(<<>>), #{<<"alg">> => <<"HS256">>}, jose_jwt:from(Claims))
),

OtherJwk = jose_jwk:from_file(PrivDir ++ "/test/fixtures/openid-certification-jwks.json"),
%% from_manual/4 rejects an empty secret, so set it on the record to check
%% that key derivation refuses it on its own as well.
ClientContext0 = oidcc_client_context:from_manual(
Configuration, OtherJwk, ClientId, <<"at_least_32_character_client_secret">>
),
ClientContext = ClientContext0#oidcc_client_context{client_secret = <<>>},

?assertMatch(
{error, no_matching_key},
oidcc_token:validate_id_token(Token, ClientContext, any)
),

ok.

validate_jarm_empty_client_secret_test() ->
#oidcc_client_context{
client_id = ClientId,
provider_configuration =
#oidcc_provider_configuration{issuer = Issuer} = Configuration0
} = ClientContext0 = client_context_fapi2_fixture(),
ClientContext = ClientContext0#oidcc_client_context{
client_secret = <<>>,
provider_configuration = Configuration0#oidcc_provider_configuration{
authorization_signing_alg_values_supported = [<<"HS256">>]
}
},
JarmClaims = #{
<<"iss">> => Issuer,
<<"aud">> => ClientId,
<<"code">> => <<"123456">>,
<<"exp">> => erlang:system_time(second) + 10
},
{_, JarmToken} = jose_jws:compact(
jose_jwt:sign(
jose_jwk:from_oct(<<>>), #{<<"alg">> => <<"HS256">>}, jose_jwt:from(JarmClaims)
)
),

?assertMatch(
{error, no_matching_key},
oidcc_token:validate_jarm(JarmToken, ClientContext, #{})
),

ok.
Loading