Skip to content

Commit cdc8f22

Browse files
Use POST+Redirect_with_cookie
For idp-initiated logon
1 parent 04a8067 commit cdc8f22

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

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(<<"token">>, 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: 16 additions & 5 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)),
@@ -33,11 +35,20 @@ set_token_auth(AuthSettings, Req0) ->
3335
case proplists:get_value(oauth_enabled, AuthSettings, false) of
3436
true ->
3537
case cowboy_req:parse_header(<<"authorization">>, Req0) of
36-
{bearer, Token} -> ["set_token_auth('", Token, "');"];
37-
_ -> []
38+
{bearer, Token} ->
39+
{Req0, ["set_token_auth('", Token, "');"]};
40+
_ ->
41+
Cookies = cowboy_req:parse_cookies(Req0),
42+
case lists:keyfind(<<"token">>, 1, Cookies) of
43+
{_, Token} ->
44+
{cowboy_req:set_resp_cookie(
45+
<<"token">>, <<>>, Req0, #{max_age => 0}),
46+
["set_token_auth('", Token, "');"]};
47+
false -> {Req0, []}
48+
end
3849
end;
3950
false ->
40-
[]
51+
{Req0, []}
4152
end.
4253

4354
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)