Skip to content

Commit 7c59322

Browse files
Add more test coverage
1 parent 796ea95 commit 7c59322

File tree

4 files changed

+58
-25
lines changed

4 files changed

+58
-25
lines changed

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ build_introspection_request() ->
101101
case {Provider#oauth_provider.introspection_client_id,
102102
Provider#oauth_provider.introspection_client_secret} of
103103
{undefined, _} -> {error, not_found_introspection_endpoint};
104+
{_, undefined} -> {error, not_found_introspection_endpoint};
104105
{_, _} -> {ok, build_introspection_request(Provider)}
105106
end;
106107
{error, _} = Error -> Error

deps/oauth2_client/test/system_SUITE.erl

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,26 @@ groups() ->
5454
{with_introspection_request_param_client_credentials, [], [
5555
can_introspect_token
5656
]}
57+
]}
58+
]},
59+
{https, [], [
60+
{with_introspection_basic_client_credentials, [], [
61+
cannot_introspect_due_to_missing_configuration
62+
]},
63+
{with_introspection_request_param_client_credentials, [], [
64+
cannot_introspect_due_to_missing_configuration
5765
]}
58-
66+
]},
67+
{with_discovered_introspection_endpoint, [], [
68+
cannot_introspect_due_to_missing_configuration,
69+
{https, [], [
70+
{with_introspection_basic_client_credentials, [], [
71+
can_introspect_token
72+
]},
73+
{with_introspection_request_param_client_credentials, [], [
74+
can_introspect_token
75+
]}
76+
]}
5977
]}
6078
]}
6179
]},
@@ -176,18 +194,26 @@ init_per_group(with_introspection_endpoint, Config) ->
176194
build_token_introspection_endpoint("https")),
177195
Config;
178196

197+
init_per_group(with_discovered_introspection_endpoint, Config) ->
198+
Payload1 = [ {?RESPONSE_INTROSPECTION_ENDPOINT, build_token_introspection_endpoint("https")} |
199+
build_http_get_openid_configuration_payload() ],
200+
[{expected_openid_configuration_payload, Payload1} | Config];
201+
179202
init_per_group(with_introspection_basic_client_credentials, Config) ->
180203
application:set_env(rabbitmq_auth_backend_oauth2, introspection_client_id,
181204
"some-client-id"),
182205
application:set_env(rabbitmq_auth_backend_oauth2, introspection_client_secret,
183206
"some-client-secret"),
184207
application:set_env(rabbitmq_auth_backend_oauth2, introspection_client_auth_method,
185-
basic),
208+
basic),
186209
[{can_introspect_token, [
187210
{introspection_endpoint, build_http_mock_behaviour(
188211
build_introspection_token_request(?MOCK_OPAQUE_TOKEN, basic, <<"some-client-id">>,
189212
<<"some-client-secret">>),
190-
build_http_200_introspection_token_response())}
213+
build_http_200_introspection_token_response())},
214+
{get_openid_configuration, get_openid_configuration_http_expectation(
215+
with_introspection_basic_client_credentials, Config)}
216+
191217
]} | Config];
192218

193219
init_per_group(with_introspection_request_param_client_credentials, Config) ->
@@ -202,7 +228,7 @@ init_per_group(with_introspection_request_param_client_credentials, Config) ->
202228
build_introspection_token_request(?MOCK_OPAQUE_TOKEN, request_param, <<"some-client-id">>,
203229
<<"some-client-secret">>),
204230
build_http_200_introspection_token_response())}
205-
]} | Config];
231+
]} | Config];
206232

207233

208234
init_per_group(_, Config) ->
@@ -214,20 +240,24 @@ get_http_oauth_server_expectations(TestCase, Config) ->
214240
undefined ->
215241
[ {token_endpoint, build_http_mock_behaviour(build_http_access_token_request(),
216242
build_http_200_access_token_response())},
217-
{get_openid_configuration, get_openid_configuration_http_expectation(TestCase)}
243+
{get_openid_configuration, get_openid_configuration_http_expectation(TestCase, Config)}
218244
];
219245
Expectations ->
220246
Expectations
221247
end.
222-
get_openid_configuration_http_expectation(TestCaseAtom) ->
248+
get_openid_configuration_http_expectation(TestCaseAtom, Config) ->
223249
TestCase = binary_to_list(atom_to_binary(TestCaseAtom)),
224-
Payload = case string:find(TestCase, "returns_partial_payload") of
225-
nomatch ->
226-
build_http_get_openid_configuration_payload();
227-
_ ->
228-
List0 = proplists:delete(authorization_endpoint,
229-
build_http_get_openid_configuration_payload()),
230-
proplists:delete(end_session_endpoint, List0)
250+
Payload = case ?config(expected_openid_configuration_payload, Config) of
251+
undefined ->
252+
case string:find(TestCase, "returns_partial_payload") of
253+
nomatch ->
254+
build_http_get_openid_configuration_payload();
255+
_ ->
256+
List0 = proplists:delete(authorization_endpoint,
257+
build_http_get_openid_configuration_payload()),
258+
proplists:delete(end_session_endpoint, List0)
259+
end;
260+
P -> P
231261
end,
232262
Path = case string:find(TestCase, "path") of
233263
nomatch -> "";
@@ -244,7 +274,6 @@ lookup_expectation(Endpoint, Config) ->
244274
proplists:get_value(Endpoint, ?config(oauth_server_expectations, Config)).
245275

246276

247-
248277
configure_all_oauth_provider_settings(Config) ->
249278
OAuthProvider = ?config(oauth_provider, Config),
250279
OAuthProviders = #{ ?config(oauth_provider_id, Config) =>
@@ -681,7 +710,15 @@ jwks_uri_takes_precedence_over_jwks_url(_Config) ->
681710

682711

683712
cannot_introspect_due_to_missing_configuration(_Config)->
684-
{error, not_found_introspection_endpoint} = oauth2_client:introspect_token(?MOCK_OPAQUE_TOKEN).
713+
{error, not_found_introspection_endpoint} = oauth2_client:introspect_token(?MOCK_OPAQUE_TOKEN),
714+
715+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_client_id, "some-client-id"),
716+
{error, not_found_introspection_endpoint} = oauth2_client:introspect_token(?MOCK_OPAQUE_TOKEN),
717+
application:unset_env(rabbitmq_auth_backend_oauth2, introspection_client_id),
718+
719+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_client_secret, "some-client-secret"),
720+
{error, not_found_introspection_endpoint} = oauth2_client:introspect_token(?MOCK_OPAQUE_TOKEN),
721+
application:unset_env(rabbitmq_auth_backend_oauth2, introspection_client_secret).
685722

686723
can_introspect_token(_Config) ->
687724
{ok, _} = oauth2_client:introspect_token(?MOCK_OPAQUE_TOKEN).

deps/rabbitmq_auth_backend_oauth2/src/rabbit_auth_backend_oauth2.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ authenticate(_, AuthProps0) ->
179179
{error, Error} -> {refused, "Unable to introspect token: ~p", [Error]}
180180
end.
181181

182+
182183
-spec with_decoded_token(Token, Fun) -> Result
183184
when Token :: decoded_jwt_token(),
184185
Fun :: auth_user_extraction_fun(),

deps/rabbitmq_auth_backend_oauth2/src/uaa_jwt.erl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,6 @@ verify_signing_key(Type, Value) ->
165165
Err -> Err
166166
end.
167167

168-
% introspect_token(OpaqueToken) ->
169-
% case rabbit_oauth2_resource_server:resolve_single_resource_server_with_opaque_access_token_format() of
170-
% ResourceServer ->
171-
% case oauth2_client:get_oauth_provider(ResourceServer#resource_server.oauth_provider_id,
172-
% [introspection_endpoint]) of
173-
% Provider ->
174-
% Provider#oauth_provider.
175-
% {error,_} = Error -> Error
176-
% end.
177-
178168
-spec get_scope(map()) -> binary() | list().
179169
get_scope(#{?SCOPE_JWT_FIELD := Scope}) -> Scope;
180170
get_scope(#{}) -> [].
@@ -198,3 +188,7 @@ sub(DecodedToken) ->
198188
-spec sub(map(), any()) -> binary() | undefined.
199189
sub(DecodedToken, Default) ->
200190
maps:get(<<"sub">>, DecodedToken, Default).
191+
192+
-spec validate_introspected_token(Token) ->
193+
{ok, map()} | {error, term()}
194+
validate_introspected_token(Token) ->

0 commit comments

Comments
 (0)