Skip to content

Commit 9605c96

Browse files
Fix issues in endpoint
related to accepted content type
1 parent fbbcc20 commit 9605c96

File tree

2 files changed

+81
-17
lines changed

2 files changed

+81
-17
lines changed

deps/rabbitmq_management/src/rabbit_mgmt_wm_oauth_introspect.erl

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,39 @@
77

88
-module(rabbit_mgmt_wm_oauth_introspect).
99

10-
-export([init/2, to_json/2, content_types_provided/2, is_authorized/2]).
10+
-export([init/2,
11+
is_authorized/2, allowed_methods/2]).
12+
-export([variances/2]).
1113
-include("rabbit_mgmt.hrl").
1214

15+
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
16+
1317
%%--------------------------------------------------------------------
1418

15-
init(Req, State) ->
16-
{cowboy_rest, rabbit_mgmt_headers:set_no_cache_headers(
17-
rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), ?MODULE), State}.
19+
init(Req, _) ->
20+
Ret = {cowboy_rest, rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), #context{}},
21+
rabbit_log:debug("init rabbit_mgmt_wm_oauth_introspect"),
22+
Ret.
23+
%{cowboy_rest, rabbit_mgmt_headers:set_no_cache_headers(
24+
% rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), ?MODULE), State}.
1825

1926
allowed_methods(ReqData, Context) ->
2027
{[<<"POST">>, <<"OPTIONS">>], ReqData, Context}.
2128

2229
variances(Req, Context) ->
2330
{[<<"accept-encoding">>, <<"origin">>], Req, Context}.
2431

25-
content_types_provided(ReqData, Context) ->
26-
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.
27-
28-
to_json(ReqData, Context) ->
32+
is_authorized(ReqData, Context) ->
33+
rabbit_log:debug("to_json rabbit_mgmt_wm_oauth_introspect"),
2934
case cowboy_req:parse_header(<<"authorization">>, ReqData) of
3035
{bearer, Token} ->
31-
rabbit_mgmt_util:reply(
32-
maps:from_list(oauth2_client:introspect_token(Token)),
33-
ReqData, Context);
36+
case oauth2_client:introspect_token(Token) of
37+
{error, Reason} ->
38+
rabbit_log:error("Failed to introspect token due to ~p", [Reason]),
39+
rabbit_mgmt_util:bad_request(<<"Cannot introspect tokenr">>, ReqData, Context);
40+
JwtToken ->
41+
rabbit_mgmt_util:reply(JwtToken,ReqData, Context)
42+
end;
3443
_ ->
3544
rabbit_mgmt_util:bad_request(<<"Opaque token not found in authorization header">>, ReqData, Context)
3645
end.
37-
38-
is_authorized(ReqData, Context) ->
39-
{true, ReqData, Context}.

deps/rabbitmq_management/test/rabbit_mgmt_wm_auth_SUITE.erl

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414
-import(rabbit_mgmt_test_util, [req/5]).
1515
-compile(export_all).
1616

17+
-import(rabbit_mgmt_test_util, [assert_list/2, assert_item/2, test_item/2,
18+
assert_keys/2, assert_no_keys/2,
19+
decode_body/1,
20+
http_get/2, http_get/3, http_get/5,
21+
http_get_no_auth/3,
22+
http_get_no_decode/5,
23+
http_put/4, http_put/6,
24+
http_post/4, http_post/6,
25+
http_post_json/4,
26+
http_upload_raw/8,
27+
http_delete/3, http_delete/4, http_delete/5,
28+
http_put_raw/4, http_post_accept_json/4,
29+
req/4, auth_header/2,
30+
assert_permanent_redirect/3,
31+
uri_base_from/2, format_for_upload/1,
32+
amqp_port/1, req/6]).
1733
all() ->
1834
[
1935
{group, without_any_settings},
@@ -36,6 +52,7 @@ groups() ->
3652
[
3753
{run_with_broker, [], [
3854
{verify_introspection_endpoint, [], [
55+
test_login,
3956
introspect_opaque_token_returns_active_jwt_token
4057
]}
4158
]},
@@ -897,9 +914,50 @@ should_return_mgt_oauth_resource_a_with_token_endpoint_params_1(Config) ->
897914
assertEqual_on_attribute_for_oauth_resource_server(authSettings(),
898915
Config, a, oauth_token_endpoint_params, token_params_1).
899916

900-
introspect_opaque_token_returns_active_jwt_token(Config) ->
901-
_Result = req(Config, 0, post, "/introspect", [{"Authorization", "Bearer active"}]).
902-
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.
946+
947+
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).
903961

904962
%% -------------------------------------------------------------------
905963
%% Utility/helper functions

0 commit comments

Comments
 (0)