Skip to content

Commit 14ad6ef

Browse files
Test active and non active opaque tokens
1 parent 6871985 commit 14ad6ef

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

deps/rabbitmq_management/src/rabbit_mgmt_util.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
is_authorized_vhost_visible/2,
1818
is_authorized_vhost_visible_for_monitoring/2,
1919
is_authorized_global_parameters/2]).
20+
-export([not_authorised/3]).
2021
-export([user/1]).
2122
-export([bad_request/3, service_unavailable/3, bad_request_exception/4,
2223
internal_server_error/3, internal_server_error/4, precondition_failed/3,

deps/rabbitmq_management/src/rabbit_mgmt_wm_oauth_introspect.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ do_it(ReqData, Context) ->
4343
case cowboy_req:parse_header(<<"authorization">>, ReqData) of
4444
{bearer, Token} ->
4545
case oauth2_client:introspect_token(Token) of
46+
{error, introspected_token_not_valid} ->
47+
rabbit_log:error("Failed to introspect token due to ~p", [introspected_token_not_valid]),
48+
rabbit_mgmt_util:not_authorised("Introspected token is not active", ReqData, Context);
4649
{error, Reason} ->
4750
rabbit_log:error("Failed to introspect token due to ~p", [Reason]),
48-
rabbit_mgmt_util:bad_request(<<"Cannot introspect tokenr">>, ReqData, Context);
51+
rabbit_mgmt_util:not_authorised(Reason, ReqData, Context);
4952
{ok, JwtToken} ->
5053
rabbit_log:debug("Got jwt token : ~p", [JwtToken]),
5154
rabbit_mgmt_util:reply(JwtToken, ReqData, Context)

deps/rabbitmq_management/test/introspect_http_handler.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ init(Req, State) ->
1515
Body = rabbit_json:encode([{"active", true}, {"scope", "rabbitmq.tag:administrator"}]),
1616
{ok, cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>},
1717
Body, Req), State};
18-
<<"inactive">> -> Body = rabbit_json:encode([{"active", false}, {"scope", "rabbitmq.tag:administrator"}]),
18+
<<"inactive">> ->
19+
Body = rabbit_json:encode([{"active", false}, {"scope", "rabbitmq.tag:administrator"}]),
1920
{ok, cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>},
2021
Body, Req), State}
2122
end;

deps/rabbitmq_management/test/rabbit_mgmt_wm_auth_SUITE.erl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ groups() ->
5252
[
5353
{run_with_broker, [], [
5454
{verify_introspection_endpoint, [], [
55-
introspect_opaque_token_returns_active_jwt_token
55+
introspect_opaque_token_returns_active_jwt_token,
56+
introspect_opaque_token_returns_inactive_jwt_token
5657
]}
5758
]},
5859
{verify_multi_resource_and_provider, [], [
@@ -693,7 +694,8 @@ end_per_group(verify_introspection_endpoint, Config) ->
693694
end_per_group(_, Config) ->
694695
Config.
695696

696-
init_per_testcase(introspect_opaque_token_returns_active_jwt_token, Config) ->
697+
init_per_testcase(Testcase, Config) when Testcase =:= introspect_opaque_token_returns_active_jwt_token orelse
698+
Testcase =:= introspect_opaque_token_returns_inactive_jwt_token ->
697699
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
698700
[rabbitmq_auth_backend_oauth2, introspection_endpoint,
699701
?config(authorization_server_url, Config)]),
@@ -706,9 +708,10 @@ init_per_testcase(introspect_opaque_token_returns_active_jwt_token, Config) ->
706708
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
707709
[rabbitmq_auth_backend_oauth2, key_config, [{cacertfile, CaCertFile}]]),
708710

709-
rabbit_ct_helpers:testcase_started(Config, introspect_opaque_token_returns_active_jwt_token).
711+
rabbit_ct_helpers:testcase_started(Config, Testcase).
710712

711-
end_per_testcase(introspect_opaque_token_returns_active_jwt_token, Config) ->
713+
end_per_testcase(Testcase, Config) when Testcase =:= introspect_opaque_token_returns_active_jwt_token orelse
714+
Testcase =:= introspect_opaque_token_returns_inactive_jwt_token ->
712715
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, unset_env,
713716
[rabbitmq_auth_backend_oauth2, introspection_endpoint]),
714717
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, unset_env,
@@ -941,12 +944,19 @@ should_return_mgt_oauth_resource_a_with_token_endpoint_params_1(Config) ->
941944
Config, a, oauth_token_endpoint_params, token_params_1).
942945

943946
introspect_opaque_token_returns_active_jwt_token(Config) ->
944-
{ok, {{_HTTP, _, _}, _Headers, ResBody}} = req(Config, 0, post, "/auth/introspect", [
947+
{ok, {{_HTTP, 200, _}, _Headers, ResBody}} = req(Config, 0, post, "/auth/introspect", [
945948
{"authorization", "bearer active"}], []),
946949
JSON = rabbit_json:decode(rabbit_data_coercion:to_binary(ResBody)),
947950
?assertEqual(true, maps:get(<<"active">>, JSON)),
948951
?assertEqual("rabbitmq.tag:administrator", maps:get(<<"scope">>, JSON)).
949952

953+
introspect_opaque_token_returns_inactive_jwt_token(Config) ->
954+
{ok, {{_HTTP, 401, _}, _Headers, ResBody}} = req(Config, 0, post, "/auth/introspect", [
955+
{"authorization", "bearer inactive"}], []),
956+
JSON = rabbit_json:decode(rabbit_data_coercion:to_binary(ResBody)),
957+
?assertEqual(<<"not_authorised">>, maps:get(<<"error">>, JSON)),
958+
?assertEqual(<<"Introspected token is not active">>, maps:get(<<"reason">>, JSON)).
959+
950960

951961

952962
%% -------------------------------------------------------------------

0 commit comments

Comments
 (0)