Skip to content

Commit a930fd1

Browse files
Add more tsets
1 parent 4251a71 commit a930fd1

File tree

3 files changed

+111
-14
lines changed

3 files changed

+111
-14
lines changed

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ refresh_access_token(OAuthProvider, Request) ->
5050
parse_access_token_response(Response).
5151

5252
-spec introspect_token(binary()) ->
53-
{ok, successful_access_token_response()} |
53+
{ok, map()} |
5454
{error, unsuccessful_access_token_response() | any()}.
5555
introspect_token(Token) ->
5656
case build_introspection_request() of
@@ -108,14 +108,16 @@ build_introspection_request() ->
108108
Providers = case Result of
109109
{ok, _} -> Result;
110110
{error, _} ->
111-
maps:filter(fun(K,V) ->
112-
case {V#oauth_provider.introspection_client_id,
113-
V#oauth_provider.introspection_client_secret} of
114-
{undefined, _} -> false;
115-
{_Id, _Secret} ->
116-
case get_oauth_provider(K, [introspection_endpoint]) of
117-
{ok, _} -> true;
118-
_ -> false
111+
maps:filter(fun(K,_V) ->
112+
case get_oauth_provider(K, [introspection_endpoint]) of
113+
{error, _} -> false;
114+
{ok, P} ->
115+
case {P#oauth_provider.introspection_client_id,
116+
P#oauth_provider.introspection_client_secret,
117+
P#oauth_provider.introspection_endpoint} of
118+
{undefined, _, _} -> false;
119+
{_Id, _Secret, undefined} -> false;
120+
{_Id, _Secret, _Endpoint} -> true
119121
end
120122
end
121123
end, get_env(oauth_providers, #{}))

deps/oauth2_client/test/oauth_http_mock.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ match_request(Req, #{method := Method} = ExpectedRequest) ->
3232
case maps:is_key(parameters, ExpectedRequest) of
3333
true -> match_request_parameters_in_body(Req, ExpectedRequest);
3434
false -> ok
35+
end,
36+
case maps:is_key(headers, ExpectedRequest) of
37+
true -> maps:foreach(fun(K,V) ->
38+
?assertEqual(V, cowbow_req:header(K, Req)) end,
39+
maps:get(headers, ExpectedRequest));
40+
false -> ok
3541
end.
36-
3742
produce_expected_response(ExpectedResponse) ->
3843
case proplists:is_defined(content_type, ExpectedResponse) of
3944
true ->

deps/oauth2_client/test/system_SUITE.erl

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
%% This Source Code Form is subject to the terms of the Mozilla Public
21
%% License, v. 2.0. If a copy of the MPL was not distributed with this
32
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
43
%%
@@ -17,6 +16,8 @@
1716

1817
-compile(export_all).
1918

19+
-define(MOCK_OPAQUE_TOKEN, <<"some opaque token">>).
20+
-define(MOCK_INTROSPECTION_ENDPOINT, <<"/introspection">>).
2021
-define(MOCK_TOKEN_ENDPOINT, <<"/token">>).
2122
-define(AUTH_PORT, 8000).
2223
-define(ISSUER_PATH, "/somepath").
@@ -28,7 +29,8 @@ all() ->
2829
[
2930
{group, https_down},
3031
{group, https},
31-
{group, with_all_oauth_provider_settings}
32+
{group, with_all_oauth_provider_settings},
33+
{group, verify_introspect_token}
3234

3335
].
3436

@@ -40,6 +42,20 @@ groups() ->
4042
jwks_uri_takes_precedence_over_jwks_url,
4143
jwks_url_is_used_in_absense_of_jwks_uri
4244
]},
45+
{verify_introspect_token, [], [
46+
{with_all_oauth_provider_settings, [], [
47+
cannot_introspect_due_to_missing_configuration,
48+
{with_introspection_endpoint, [], [
49+
cannot_introspect_due_to_missing_configuration,
50+
{with_introspection_basic_client_credentials, [], [
51+
can_introspect_token
52+
]},
53+
{with_introspection_request_param_client_credentials, [], [
54+
can_introspect_token
55+
]}
56+
]}
57+
]}
58+
]},
4359
{without_all_oauth_providers_settings, [], [
4460
{group, verify_get_oauth_provider}
4561
]},
@@ -152,6 +168,40 @@ init_per_group(with_default_oauth_provider, Config) ->
152168
OAuthProvider#oauth_provider.id),
153169
Config;
154170

171+
init_per_group(with_introspection_endpoint, Config) ->
172+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_endpoint,
173+
"https://introspection"),
174+
Config;
175+
176+
init_per_group(with_introspection_basic_client_credentials, Config) ->
177+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_endpoint_client_id,
178+
"some-client-id"),
179+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_endpoint_client_secret,
180+
"some-client-secret"),
181+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_endpoint_client_auth_method,
182+
basic),
183+
[{with_introspection_basic_client_credentials, [
184+
{introspection_endpoint, build_http_mock_behaviour(
185+
build_introspection_token_request(?MOCK_OPAQUE_TOKEN, basic, <<"some-client-id">>,
186+
<<"some-client-secret">>),
187+
build_http_200_introspection_token_response())}
188+
]} | Config];
189+
190+
init_per_group(with_introspection_request_param_client_credentials, Config) ->
191+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_endpoint_client_id,
192+
"some-client-id"),
193+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_endpoint_client_secret,
194+
"some-client-secret"),
195+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_endpoint_client_auth_method,
196+
request_param),
197+
[{with_introspection_request_param_client_credentials, [
198+
{introspection_endpoint, build_http_mock_behaviour(
199+
build_introspection_token_request(?MOCK_OPAQUE_TOKEN, request_param, <<"some-client-id">>,
200+
<<"some-client-secret">>),
201+
build_http_200_introspection_token_response())}
202+
]} | Config];
203+
204+
155205
init_per_group(_, Config) ->
156206
Config.
157207

@@ -311,6 +361,10 @@ end_per_group(with_default_oauth_provider, Config) ->
311361
application:unset_env(rabbitmq_auth_backend_oauth2, default_oauth_provider),
312362
Config;
313363

364+
end_per_group(with_introspection_endpoint, Config) ->
365+
application:unset_env(rabbitmq_auth_backend_oauth2, introspection_endpoint),
366+
Config;
367+
314368
end_per_group(_, Config) ->
315369
Config.
316370

@@ -598,19 +652,25 @@ get_oauth_provider_given_oauth_provider_id(Config) ->
598652
Jwks_uri)
599653
end.
600654

601-
jwks_url_is_used_in_absense_of_jwks_uri(Config) ->
655+
jwks_url_is_used_in_absense_of_jwks_uri(_Config) ->
602656
{ok, #oauth_provider{
603657
jwks_uri = Jwks_uri}} = oauth2_client:get_oauth_provider([jwks_uri]),
604658
?assertEqual(
605659
proplists:get_value(jwks_url, get_env(key_config, []), undefined),
606660
Jwks_uri).
607661

608-
jwks_uri_takes_precedence_over_jwks_url(Config) ->
662+
jwks_uri_takes_precedence_over_jwks_url(_Config) ->
609663
{ok, #oauth_provider{
610664
jwks_uri = Jwks_uri}} = oauth2_client:get_oauth_provider([jwks_uri]),
611665
?assertEqual(get_env(jwks_uri), Jwks_uri).
612666

613667

668+
cannot_introspect_due_to_missing_configuration(_Config)->
669+
{error, not_found_introspection_endpoint} = oauth2_client:introspect_token(<<"some token">>).
670+
671+
can_introspect_token(_Config) ->
672+
{ok, _} = oauth2_client:introspect_token(<<"some token">>).
673+
614674
%%% HELPERS
615675

616676
build_issuer(Scheme) ->
@@ -816,6 +876,36 @@ denies_access_token_expectation() ->
816876
{?REQUEST_CLIENT_SECRET, <<"password">>}
817877
]), build_http_400_access_token_response()
818878
).
879+
build_introspection_token_request(Token, basic, ClientId, ClientSecret) ->
880+
Map = build_http_request(
881+
<<"POST">>,
882+
?MOCK_TOKEN_ENDPOINT,
883+
[
884+
{?REQUEST_TOKEN, Token}
885+
]),
886+
Credentials = binary_to_list(<<ClientId/binary,":",ClientSecret/binary>>),
887+
AuthStr = base64:encode_to_string(Credentials),
888+
maps:put(headers, #{
889+
<<"authorization">> => "Basic " ++ AuthStr
890+
}, Map);
891+
build_introspection_token_request(Token, request_param, ClientId, ClientSecret) ->
892+
build_http_request(
893+
<<"POST">>,
894+
?MOCK_INTROSPECTION_ENDPOINT,
895+
[
896+
{?REQUEST_TOKEN, Token},
897+
{?REQUEST_CLIENT_ID, ClientId},
898+
{?REQUEST_CLIENT_SECRET, ClientSecret}
899+
]).
900+
build_http_200_introspection_token_response() ->
901+
[
902+
{code, 200},
903+
{content_type, ?CONTENT_JSON},
904+
{payload, [
905+
{active, true},
906+
{scope, <<"openid">>}
907+
]}
908+
].
819909
auth_server_error_when_access_token_request_expectation() ->
820910
build_http_mock_behaviour(build_http_request(
821911
<<"POST">>,

0 commit comments

Comments
 (0)