Skip to content

Commit 1ad9c7d

Browse files
Use POST+Redirect_with_cookie
For idp-initiated logon
1 parent ad0c209 commit 1ad9c7d

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

deps/rabbitmq_management/include/rabbit_mgmt.hrl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
-define(MANAGEMENT_PG_GROUP, management_db).
1414

1515
-define(MANAGEMENT_DEFAULT_HTTP_MAX_BODY_SIZE, 20000000).
16+
17+
-define(OAUTH2_ACCESS_TOKEN_COOKIE, <<"access_token">>).

deps/rabbitmq_management/src/rabbit_mgmt_login.erl

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,34 @@ init(Req0, State) ->
1616
login(cowboy_req:method(Req0), Req0, State).
1717

1818
login(<<"POST">>, Req0, State) ->
19-
{ok, Body, _} = cowboy_req:read_urlencoded_body(Req0),
20-
AccessToken = proplists:get_value(<<"access_token">>, Body),
21-
case rabbit_mgmt_util:is_authorized_user(Req0, #context{}, <<"">>, AccessToken, false) of
22-
{true, Req1, _} ->
23-
NewBody = ["<html><head></head><body><script src='js/prefs.js'></script><script type='text/javascript'>",
24-
"set_token_auth('", AccessToken, "'); window.location = '", rabbit_mgmt_util:get_path_prefix(),
25-
"/'</script></body></html>"],
26-
Req2 = cowboy_req:reply(200, #{<<"content-type">> => <<"text/html; charset=utf-8">>}, NewBody, Req1),
27-
{ok, Req2, State};
28-
{false, ReqData1, Reason} ->
29-
Home = cowboy_req:uri(ReqData1, #{path => rabbit_mgmt_util:get_path_prefix() ++ "/", qs => "error=" ++ Reason}),
30-
ReqData2 = cowboy_req:reply(302,
31-
#{<<"Location">> => iolist_to_binary(Home) },
32-
<<>>, ReqData1),
33-
{ok, ReqData2, State}
34-
end;
19+
{ok, Body, _} = cowboy_req:read_urlencoded_body(Req0),
20+
AccessToken = proplists:get_value(<<"access_token">>, Body),
21+
case rabbit_mgmt_util:is_authorized_user(Req0, #context{}, <<"">>, AccessToken, false) of
22+
{true, Req1, _} ->
23+
SetCookie = cowboy_req:set_resp_cookie(?OAUTH2_ACCESS_TOKEN_COOKIE, AccessToken, Req1),
24+
Home = cowboy_req:uri(SetCookie, #{
25+
path => rabbit_mgmt_util:get_path_prefix() ++ "/"
26+
}),
27+
Redirect = cowboy_req:reply(302, #{
28+
<<"Location">> => iolist_to_binary(Home)
29+
}, <<>>, SetCookie),
30+
{ok, Redirect, State};
31+
{false, ReqData1, Reason} ->
32+
replyWithError(Reason, ReqData1, State)
33+
end;
3534

3635
login(_, Req0, State) ->
3736
%% Method not allowed.
3837
{ok, cowboy_req:reply(405, Req0), State}.
38+
39+
replyWithError(Reason, Req, State) ->
40+
Home = cowboy_req:uri(Req, #{
41+
path => rabbit_mgmt_util:get_path_prefix() ++ "/",
42+
qs => "error=" ++ Reason
43+
}),
44+
Req2 = cowboy_req:reply(302, #{
45+
<<"Location">> => iolist_to_binary(Home)
46+
}, <<>>, Req),
47+
{ok, Req2, State}.
48+
49+

deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ init(Req0, State) ->
1818
bootstrap_oauth(Req0, State) ->
1919
AuthSettings = rabbit_mgmt_wm_auth:authSettings(),
2020
Dependencies = oauth_dependencies(),
21+
{Req1, SetTokenAuth} = set_token_auth(AuthSettings, Req0),
2122
JSContent = import_dependencies(Dependencies) ++
2223
set_oauth_settings(AuthSettings) ++
23-
set_token_auth(AuthSettings, Req0) ++
24+
SetTokenAuth ++
2425
export_dependencies(Dependencies),
26+
2527
{ok, cowboy_req:reply(200, #{<<"content-type">> => <<"text/javascript; charset=utf-8">>},
26-
JSContent, Req0), State}.
28+
JSContent, Req1), State}.
2729

2830
set_oauth_settings(AuthSettings) ->
2931
JsonAuthSettings = rabbit_json:encode(rabbit_mgmt_format:format_nulls(AuthSettings)),
@@ -32,12 +34,29 @@ set_oauth_settings(AuthSettings) ->
3234
set_token_auth(AuthSettings, Req0) ->
3335
case proplists:get_value(oauth_enabled, AuthSettings, false) of
3436
true ->
35-
case cowboy_req:parse_header(<<"authorization">>, Req0) of
36-
{bearer, Token} -> ["set_token_auth('", Token, "');"];
37-
_ -> []
37+
case cowboy_req:parse_header(<<"Authorization">>, Req0) of
38+
{bearer, Token} -> {
39+
Req0,
40+
["set_token_auth('", Token, "');"]
41+
};
42+
_ ->
43+
Cookies = cowboy_req:parse_cookies(Req0),
44+
case lists:keyfind(?OAUTH2_ACCESS_TOKEN_COOKIE, 1, Cookies) of
45+
{_, Token} -> {
46+
cowboy_req:set_resp_cookie(
47+
?OAUTH2_ACCESS_TOKEN_COOKIE, <<>>, Req0, #{max_age => 0}),
48+
["set_token_auth('", Token, "');"]
49+
};
50+
false -> {
51+
Req0,
52+
[]
53+
}
54+
end
3855
end;
39-
false ->
40-
[]
56+
false -> {
57+
Req0,
58+
[]
59+
}
4160
end.
4261

4362
import_dependencies(Dependencies) ->

selenium/bin/components/fakeportal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ start_fakeportal() {
5252
--env CLIENT_ID="${CLIENT_ID}" \
5353
--env CLIENT_SECRET="${CLIENT_SECRET}" \
5454
--env NODE_EXTRA_CA_CERTS=/etc/uaa/ca_uaa_certificate.pem \
55-
-v ${TEST_CONFIG_PATH}/uaa:/etc/uaa \
55+
-v ${TEST_CONFIG_DIR}/uaa:/etc/uaa \
5656
-v ${FAKEPORTAL_DIR}:/code/fakeportal \
5757
mocha-test:${mocha_test_tag} run fakeportal
5858

0 commit comments

Comments
 (0)