Skip to content

Commit 4a6d6e9

Browse files
Fix issue and test invalid oapque tokens
1 parent c305165 commit 4a6d6e9

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,14 +738,22 @@ map_to_introspect_token_response(Code, Reason, Headers, Body) ->
738738
Error;
739739
Value ->
740740
case Code of
741-
200 -> {ok, Value};
742-
201 -> {ok, Value};
741+
200 -> assert_token_is_active({ok, Value});
742+
201 -> assert_token_is_active({ok, Value});
743743
204 -> {ok, []};
744744
400 -> {error, map_to_unsuccessful_introspect_token_response(Value)};
745745
401 -> {error, map_to_unsuccessful_introspect_token_response(Value)};
746746
_ -> {error, Reason}
747747
end
748748
end.
749+
assert_token_is_active({ok, Response} = Value) ->
750+
ct:log("Token : ~p", [Response]),
751+
case maps:get(<<"active">>, Response, undefined) of
752+
undefined -> {error, introspected_token_not_valid};
753+
false -> {error, introspected_token_not_valid};
754+
true -> Value
755+
end.
756+
749757
map_to_unsuccessful_introspect_token_response(Map) ->
750758
#unsuccessful_introspect_token_response{
751759
error = maps:get(?RESPONSE_ERROR, Map),

deps/oauth2_client/test/system_SUITE.erl

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@ groups() ->
4949
cannot_introspect_due_to_missing_configuration,
5050
{https, [], [
5151
{with_introspection_basic_client_credentials, [], [
52-
can_introspect_token
52+
can_introspect_token
5353
]},
5454
{with_introspection_request_param_client_credentials, [], [
5555
can_introspect_token
56+
]},
57+
{introspection_endpoint_returns_non_active_tokens, [], [
58+
introspected_token_is_not_active
5659
]}
5760
]}
5861
]},
@@ -215,6 +218,25 @@ init_per_group(with_introspection_basic_client_credentials, Config) ->
215218
with_introspection_basic_client_credentials, Config)}
216219

217220
]} | Config];
221+
init_per_group(introspection_endpoint_returns_non_active_tokens, Config) ->
222+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_client_id,
223+
"some-client-id"),
224+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_client_secret,
225+
"some-client-secret"),
226+
application:set_env(rabbitmq_auth_backend_oauth2, introspection_client_auth_method,
227+
basic),
228+
[{introspected_token_is_not_active, [
229+
{introspection_endpoint, build_http_mock_behaviour(
230+
build_introspection_token_request(?MOCK_OPAQUE_TOKEN, basic, <<"some-client-id">>,
231+
<<"some-client-secret">>),
232+
build_http_200_introspection_token_response([
233+
{active, false},
234+
{scope, <<"openid">>}
235+
]))},
236+
{get_openid_configuration, get_openid_configuration_http_expectation(
237+
with_introspection_basic_client_credentials, Config)}
238+
239+
]} | Config];
218240

219241
init_per_group(with_introspection_request_param_client_credentials, Config) ->
220242
application:set_env(rabbitmq_auth_backend_oauth2, introspection_client_id,
@@ -723,6 +745,9 @@ cannot_introspect_due_to_missing_configuration(_Config)->
723745
can_introspect_token(_Config) ->
724746
{ok, _} = oauth2_client:introspect_token(?MOCK_OPAQUE_TOKEN).
725747

748+
introspected_token_is_not_active(_Config) ->
749+
{error, introspected_token_not_valid} = oauth2_client:introspect_token(?MOCK_OPAQUE_TOKEN).
750+
726751
%%% HELPERS
727752

728753
build_issuer(Scheme) ->
@@ -959,13 +984,15 @@ build_introspection_token_request(Token, request_param, ClientId, ClientSecret)
959984
{?REQUEST_CLIENT_SECRET, ClientSecret}
960985
]).
961986
build_http_200_introspection_token_response() ->
987+
build_http_200_introspection_token_response([
988+
{active, true},
989+
{scope, <<"openid">>}
990+
]).
991+
build_http_200_introspection_token_response(PayloodList) ->
962992
[
963993
{code, 200},
964994
{content_type, ?CONTENT_JSON},
965-
{payload, [
966-
{active, true},
967-
{scope, <<"openid">>}
968-
]}
995+
{payload, PayloodList}
969996
].
970997
auth_server_error_when_access_token_request_expectation() ->
971998
build_http_mock_behaviour(build_http_request(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
auth_oauth2.access_token_format = opaque
2+
auth_oauth2.introspection_client_auth_method = basic
3+
auth_oauth2.introspection_client_id = rabbitmq_client_code_opaque
4+
auth_oauth2.introspection_client_secret = rabbitmq_client_code_opaque

0 commit comments

Comments
 (0)