Skip to content

Commit f7b8c58

Browse files
First commit to support opaque tokens for idp scenarios
1 parent 26cccf8 commit f7b8c58

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
-export([init/2]).
1111
-include("rabbit_mgmt.hrl").
12+
-include_lib("kernel/include/logger.hrl").
1213

1314
%%--------------------------------------------------------------------
1415

@@ -37,10 +38,28 @@ set_token_auth(AuthSettings, Req0) ->
3738
true ->
3839
case cowboy_req:parse_header(<<"authorization">>, Req0) of
3940
{bearer, Token} ->
40-
{
41-
Req0,
42-
["set_token_auth('", Token, "');"]
43-
};
41+
case oauth2_client:is_jwt_token(Token) of
42+
true ->
43+
{
44+
Req0,
45+
["set_token_auth('", Token, "');"]
46+
};
47+
false ->
48+
case oauth2_client:introspect_token(Token) of
49+
{ok, Tk1} ->
50+
?LOG_DEBUG("Successfully introspected token : ~p", [Tk1]),
51+
{
52+
Req0,
53+
["set_token_auth('", Tk1, "');"]
54+
};
55+
{error, Err1} ->
56+
?LOG_ERROR("Failed to introspected token due to ~p", [Err1]),
57+
{
58+
Req0,
59+
[]
60+
}
61+
end
62+
end;
4463
_ ->
4564
Cookies = cowboy_req:parse_cookies(Req0),
4665
case lists:keyfind(?OAUTH2_ACCESS_TOKEN_COOKIE_NAME, 1, Cookies) of

deps/rabbitmq_management/test/rabbit_mgmt_wm_auth_SUITE.erl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ groups() ->
5454
{verify_introspection_endpoint, [], [
5555
introspect_opaque_token_returns_active_jwt_token,
5656
introspect_opaque_token_returns_inactive_jwt_token,
57-
introspect_opaque_token_returns_401_from_auth_server
57+
introspect_opaque_token_returns_401_from_auth_server,
58+
idp_introspect_opaque_token
5859
]}
5960
]},
6061
{verify_multi_resource_and_provider, [], [
@@ -697,7 +698,9 @@ end_per_group(_, Config) ->
697698

698699
init_per_testcase(Testcase, Config) when Testcase =:= introspect_opaque_token_returns_active_jwt_token orelse
699700
Testcase =:= introspect_opaque_token_returns_inactive_jwt_token orelse
700-
Testcase =:= introspect_opaque_token_returns_401_from_auth_server ->
701+
Testcase =:= introspect_opaque_token_returns_401_from_auth_server orelse
702+
Testcase =:= idp_introspect_opaque_token ->
703+
701704
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
702705
[rabbitmq_auth_backend_oauth2, introspection_endpoint,
703706
?config(authorization_server_url, Config)]),
@@ -721,7 +724,8 @@ init_per_testcase(Testcase, Config) ->
721724

722725
end_per_testcase(Testcase, Config) when Testcase =:= introspect_opaque_token_returns_active_jwt_token orelse
723726
Testcase =:= introspect_opaque_token_returns_inactive_jwt_token orelse
724-
Testcase =:= introspect_opaque_token_returns_401_from_auth_server ->
727+
Testcase =:= introspect_opaque_token_returns_401_from_auth_server orelse
728+
Testcase =:= idp_introspect_opaque_token ->
725729
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, unset_env,
726730
[rabbitmq_auth_backend_oauth2, introspection_endpoint]),
727731
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, unset_env,
@@ -958,22 +962,24 @@ should_return_mgt_oauth_resource_a_with_token_endpoint_params_1(Config) ->
958962

959963
introspect_opaque_token_returns_active_jwt_token(Config) ->
960964
{ok, {{_HTTP, 200, _}, _Headers, ResBody}} = req(Config, 0, post, "/auth/introspect", [
961-
{"authorization", "bearer active"}], []),
962-
963-
Split = binary:split(rabbit_data_coercion:to_binary(ResBody), <<".">>),
964-
ct:log("split: ~p", [Split]).
965+
{"authorization", "bearer active"}], []).
965966

966967
introspect_opaque_token_returns_inactive_jwt_token(Config) ->
967968
{ok, {{_HTTP, 401, _}, _Headers, ResBody}} = req(Config, 0, post, "/auth/introspect", [
968969
{"authorization", "bearer inactive"}], []),
969-
JSON = rabbit_json:decode(rabbit_data_coercion:to_binary(ResBody)),
970+
JSON = rabbit_json:decode(rabbit_data_coercion:to_binary(ResBody)),
970971
?assertEqual(<<"not_authorised">>, maps:get(<<"error">>, JSON)),
971972
?assertEqual(<<"Introspected token is not active">>, maps:get(<<"reason">>, JSON)).
972973

973974
introspect_opaque_token_returns_401_from_auth_server(Config) ->
974975
{ok, {{_HTTP, 401, _}, _Headers, _ResBody}} = req(Config, 0, post, "/auth/introspect", [
975976
{"authorization", "bearer 401"}], []).
976977

978+
idp_introspect_opaque_token(Config) ->
979+
URI = rabbit_mgmt_test_util:uri_base_from(Config, 0, "") ++ "js/oidc-oauth/bootstrap.js",
980+
Result = httpc:request(get, {URI, [{"Authorization", "bearer active"}]}, [], []),
981+
ct:log("response idp: ~p ~p", [URI, Result]).
982+
977983

978984
%% -------------------------------------------------------------------
979985
%% Utility/helper functions

0 commit comments

Comments
 (0)