Skip to content

Commit bef581a

Browse files
Successful unit test introspect token
1 parent 4772572 commit bef581a

File tree

4 files changed

+91
-78
lines changed

4 files changed

+91
-78
lines changed

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ introspect_token(Token) ->
6464
undefined -> [];
6565
SSL -> [{ssl, SSL}]
6666
end ++ get_default_timeout(),
67-
Options = [],
67+
Options = [],
68+
rabbit_log:debug("Sending introspect_request URL:~p Header: ~p Body: ~p",
69+
[URL, Header, Body]),
6870
Response = httpc:request(post, {URL, Header, Type, Body}, HTTPOptions, Options),
6971
parse_introspect_token_response(Response);
7072
{error, _} = Error -> Error
@@ -754,10 +756,14 @@ assert_token_is_active({ok, Response} = Value) ->
754756
true -> Value
755757
end.
756758

757-
map_to_unsuccessful_introspect_token_response(Map) ->
759+
map_to_unsuccessful_introspect_token_response(Map) when is_map(Map) ->
758760
#unsuccessful_introspect_token_response{
759-
error = maps:get(?RESPONSE_ERROR, Map),
761+
error = maps:get(?RESPONSE_ERROR, Map, "unknown"),
760762
error_description = maps:get(?RESPONSE_ERROR_DESCRIPTION, Map, undefined)
763+
};
764+
map_to_unsuccessful_introspect_token_response(_) ->
765+
#unsuccessful_introspect_token_response{
766+
error = "unknown"
761767
}.
762768
parse_access_token_response({error, Reason}) ->
763769
{error, Reason};

deps/rabbitmq_management/src/rabbit_mgmt_wm_oauth_introspect.erl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
-module(rabbit_mgmt_wm_oauth_introspect).
99

1010
-export([init/2,
11-
is_authorized/2, allowed_methods/2]).
11+
content_types_accepted/2, allowed_methods/2, accept_content/2, content_types_provided/2]).
1212
-export([variances/2]).
1313
-include("rabbit_mgmt.hrl").
1414

@@ -29,16 +29,26 @@ allowed_methods(ReqData, Context) ->
2929
variances(Req, Context) ->
3030
{[<<"accept-encoding">>, <<"origin">>], Req, Context}.
3131

32-
is_authorized(ReqData, Context) ->
32+
content_types_accepted(ReqData, Context) ->
33+
{[{'*', accept_content}], ReqData, Context}.
34+
35+
accept_content(ReqData, Context) ->
36+
rabbit_mgmt_util:post_respond(do_it(ReqData, Context)).
37+
38+
content_types_provided(ReqData, Context) ->
39+
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.
40+
41+
do_it(ReqData, Context) ->
3342
rabbit_log:debug("to_json rabbit_mgmt_wm_oauth_introspect"),
3443
case cowboy_req:parse_header(<<"authorization">>, ReqData) of
3544
{bearer, Token} ->
3645
case oauth2_client:introspect_token(Token) of
3746
{error, Reason} ->
3847
rabbit_log:error("Failed to introspect token due to ~p", [Reason]),
3948
rabbit_mgmt_util:bad_request(<<"Cannot introspect tokenr">>, ReqData, Context);
40-
JwtToken ->
41-
rabbit_mgmt_util:reply(JwtToken,ReqData, Context)
49+
{ok, JwtToken} ->
50+
rabbit_log:debug("Got jwt token : ~p", [JwtToken]),
51+
rabbit_mgmt_util:reply(JwtToken, ReqData, Context)
4252
end;
4353
_ ->
4454
rabbit_mgmt_util:bad_request(<<"Opaque token not found in authorization header">>, ReqData, Context)

deps/rabbitmq_management/test/introspect_http_handler.erl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@
44
-export([init/2, terminate/3]).
55

66
init(Req, State) ->
7-
case cowboy_req:parse_header(<<"authorization">>, Req) of
8-
{bearer, <<"active">>} ->
9-
Body = rabbit_json:encode([{"active", true}, {"scope", "rabbitmq.tag:administrator"}]),
10-
{ok, cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>},
11-
Body, Req), State};
12-
{bearer, <<"inactive">>} ->
13-
Body = rabbit_json:encode([{"active", false}, {"scope", "rabbitmq.tag:administrator"}]),
14-
{ok, cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>},
15-
Body, Req), State};
16-
_ ->
7+
ct:log("introspect_http_handler init : ~p", [Req]),
8+
case cowboy_req:read_urlencoded_body(Req) of
9+
{ok, KeyValues, _Req} ->
10+
ct:log("introspect_http_handler responding with active token: ~p", [KeyValues]),
11+
case proplists:get_value(<<"token">>, KeyValues) of
12+
undefined ->
13+
{ok, cowboy_req:reply(401, #{}, [], Req), State};
14+
<<"active">> ->
15+
Body = rabbit_json:encode([{"active", true}, {"scope", "rabbitmq.tag:administrator"}]),
16+
{ok, cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>},
17+
Body, Req), State};
18+
<<"inactive">> -> Body = rabbit_json:encode([{"active", false}, {"scope", "rabbitmq.tag:administrator"}]),
19+
{ok, cowboy_req:reply(200, #{<<"content-type">> => <<"application/json">>},
20+
Body, Req), State}
21+
end;
22+
Other ->
23+
ct:log("introspect_http_handler responding with 401 : ~p", [Other]),
1724
{ok, cowboy_req:reply(401, #{}, [], Req), State}
1825
end.
1926

deps/rabbitmq_management/test/rabbit_mgmt_wm_auth_SUITE.erl

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ groups() ->
5252
[
5353
{run_with_broker, [], [
5454
{verify_introspection_endpoint, [], [
55-
test_login,
5655
introspect_opaque_token_returns_active_jwt_token
5756
]}
5857
]},
@@ -543,16 +542,20 @@ init_per_group(verify_introspection_endpoint, Config) ->
543542

544543
PortBase = rabbit_ct_broker_helpers:get_node_config(Config, 0, tcp_ports_base),
545544
Port = PortBase + 100,
546-
545+
AuthorizationServerURL = uri_string:normalize(#{
546+
scheme => "https",port => Port,path => "/introspect",host => "localhost"}),
547+
547548
CertsDir = ?config(rmq_certsdir, Config),
548-
Endpoints = [ {"/introspect", introspect_endpoint, []}],
549+
Endpoints = [ {"/introspect", introspect_http_handler, []}],
549550
Dispatch = cowboy_router:compile([{'_', Endpoints}]),
550551
{ok, _} = cowboy:start_tls(introspection_http_listener,
551552
[{port, Port},
552553
{certfile, filename:join([CertsDir, "server", "cert.pem"])},
553554
{keyfile, filename:join([CertsDir, "server", "key.pem"])}],
554555
#{env => #{dispatch => Dispatch}}),
555-
Config;
556+
557+
[ {authorization_server_url, AuthorizationServerURL},
558+
{authorization_server_ca_cert, filename:join([CertsDir, "testca", "cacert.pem"])} | Config];
556559

557560
init_per_group(_, Config) ->
558561
Config.
@@ -690,22 +693,45 @@ end_per_group(verify_introspection_endpoint, Config) ->
690693
end_per_group(_, Config) ->
691694
Config.
692695

693-
696+
init_per_testcase(introspect_opaque_token_returns_active_jwt_token, Config) ->
697+
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
698+
[rabbitmq_auth_backend_oauth2, introspection_endpoint,
699+
?config(authorization_server_url, Config)]),
700+
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
701+
[rabbitmq_auth_backend_oauth2, introspection_client_id, "some-id"]),
702+
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
703+
[rabbitmq_auth_backend_oauth2, introspection_client_secret, "some-secret"]),
704+
CaCertFile = ?config(authorization_server_ca_cert, Config),
705+
706+
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
707+
[rabbitmq_auth_backend_oauth2, key_config, [{cacertfile, CaCertFile}]]),
708+
709+
rabbit_ct_helpers:testcase_started(Config, introspect_opaque_token_returns_active_jwt_token).
710+
711+
end_per_testcase(introspect_opaque_token_returns_active_jwt_token, Config) ->
712+
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, unset_env,
713+
[rabbitmq_auth_backend_oauth2, introspection_endpoint]),
714+
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, unset_env,
715+
[rabbitmq_auth_backend_oauth2, introspection_client_id]),
716+
ok = rabbit_ct_broker_helpers:rpc(Config, 0, application, unset_env,
717+
[rabbitmq_auth_backend_oauth2, introspection_client_secret]),
718+
Config.
719+
694720
start_broker(Config) ->
695-
Setup0 = rabbit_ct_broker_helpers:setup_steps(),
696-
Setup1 = rabbit_ct_client_helpers:setup_steps(),
697-
Steps = Setup0 ++ Setup1,
698-
case rabbit_ct_helpers:run_setup_steps(Config, Steps) of
699-
{skip, _} = Skip ->
700-
Skip;
701-
Config1 ->
702-
Ret = rabbit_ct_broker_helpers:enable_feature_flag(
703-
Config1, 'rabbitmq_4.0.0'),
704-
case Ret of
705-
ok -> Config1;
706-
_ -> Ret
707-
end
708-
end.
721+
Setup0 = rabbit_ct_broker_helpers:setup_steps(),
722+
Setup1 = rabbit_ct_client_helpers:setup_steps(),
723+
Steps = Setup0 ++ Setup1,
724+
case rabbit_ct_helpers:run_setup_steps(Config, Steps) of
725+
{skip, _} = Skip ->
726+
Skip;
727+
Config1 ->
728+
Ret = rabbit_ct_broker_helpers:enable_feature_flag(
729+
Config1, 'rabbitmq_4.0.0'),
730+
case Ret of
731+
ok -> Config1;
732+
_ -> Ret
733+
end
734+
end.
709735
finish_init(Group, Config) ->
710736
rabbit_ct_helpers:log_environment(),
711737
inets:start(),
@@ -914,50 +940,14 @@ should_return_mgt_oauth_resource_a_with_token_endpoint_params_1(Config) ->
914940
assertEqual_on_attribute_for_oauth_resource_server(authSettings(),
915941
Config, a, oauth_token_endpoint_params, token_params_1).
916942

917-
test_login(Config) ->
918-
http_put(Config, "/users/myuser", [{password, <<"myuser">>},
919-
{tags, <<"management">>}], {group, '2xx'}),
920-
%% Let's do a post without any other form of authorization
921-
{ok, {{_, CodeAct, _}, Headers, _}} =
922-
req(Config, 0, post, "/login",
923-
[{"content-type", "application/x-www-form-urlencoded"}],
924-
<<"username=myuser&password=myuser">>),
925-
?assertEqual(200, CodeAct),
926-
927-
%% Extract the authorization header
928-
Cookie = list_to_binary(proplists:get_value("set-cookie", Headers)),
929-
[_, Auth] = binary:split(Cookie, <<"=">>, []),
930-
931-
%% Request the overview with the auth obtained
932-
{ok, {{_, CodeAct1, _}, _, _}} =
933-
req(Config, get, "/overview", [{"Authorization", "Basic " ++ binary_to_list(Auth)}]),
934-
?assertEqual(200, CodeAct1),
935-
936-
%% Let's request a login with an unknown user
937-
{ok, {{_, CodeAct2, _}, Headers2, _}} =
938-
req(Config, 0, post, "/login",
939-
[{"content-type", "application/x-www-form-urlencoded"}],
940-
<<"username=misteryusernumber1&password=myuser">>),
941-
?assertEqual(401, CodeAct2),
942-
?assert(not proplists:is_defined("set-cookie", Headers2)),
943-
944-
http_delete(Config, "/users/myuser", {group, '2xx'}),
945-
passed.
943+
introspect_opaque_token_returns_active_jwt_token(Config) ->
944+
{ok, {{_HTTP, _, _}, _Headers, ResBody}} = req(Config, 0, post, "/auth/introspect", [
945+
{"authorization", "bearer active"}], []),
946+
JSON = rabbit_json:decode(rabbit_data_coercion:to_binary(ResBody)),
947+
?assertEqual(true, maps:get(<<"active">>, JSON)),
948+
?assertEqual("rabbitmq.tag:administrator", maps:get(<<"scope">>, JSON)).
946949

947950

948-
introspect_opaque_token_returns_active_jwt_token(Config) ->
949-
Result2 = req(Config, 0, post, "/auth/introspect", [
950-
{"Authorization", "Bearer active"}, {"Accept", "application/json"}], []),
951-
952-
ct:log("Result: ~p", [Result2]).
953-
% _Result2 = httpc:request(post, {uri_base_from(Config, 0, "auth/introspect"),
954-
% [{"Authorization", "Bearer active"}]}, [], []).
955-
956-
uri_base_from(Config, Node, Base) ->
957-
Port = rabbit_ct_broker_helpers:get_node_config(Config, Node, tcp_port_mgmt),
958-
Prefix = "/api",
959-
Uri = list_to_binary(lists:flatten(io_lib:format("http://localhost:~w~ts/~ts", [Port, Prefix, Base]))),
960-
binary_to_list(Uri).
961951

962952
%% -------------------------------------------------------------------
963953
%% Utility/helper functions

0 commit comments

Comments
 (0)