Skip to content

Commit 63af182

Browse files
Set cookie to expire after logon
1 parent 1ad9c7d commit 63af182

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

deps/rabbitmq_management/src/rabbit_mgmt_login.erl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,31 @@
1010
-export([init/2]).
1111

1212
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
13+
-include("rabbit_mgmt.hrl").
14+
1315
%%--------------------------------------------------------------------
1416

1517
init(Req0, State) ->
1618
login(cowboy_req:method(Req0), Req0, State).
1719

18-
login(<<"POST">>, Req0, State) ->
20+
login(<<"POST">>, Req0=#{scheme := Scheme}, State) ->
1921
{ok, Body, _} = cowboy_req:read_urlencoded_body(Req0),
2022
AccessToken = proplists:get_value(<<"access_token">>, Body),
2123
case rabbit_mgmt_util:is_authorized_user(Req0, #context{}, <<"">>, AccessToken, false) of
2224
{true, Req1, _} ->
23-
SetCookie = cowboy_req:set_resp_cookie(?OAUTH2_ACCESS_TOKEN_COOKIE, AccessToken, Req1),
25+
CookieSettings = #{
26+
http_only => true,
27+
path => "/js/oidc-oauth/bootstrap.js",
28+
max_age => 30,
29+
expires => os:system_time(millisecond) + 30000,
30+
same_site => strict
31+
},
32+
rabbit_log:debug("Setting access_token in cookie: ~p", [AccessToken]),
33+
SetCookie = cowboy_req:set_resp_cookie(?OAUTH2_ACCESS_TOKEN_COOKIE, AccessToken, Req1,
34+
case Scheme of
35+
<<"https">> -> CookieSettings#{ secure => true};
36+
_ -> CookieSettings
37+
end),
2438
Home = cowboy_req:uri(SetCookie, #{
2539
path => rabbit_mgmt_util:get_path_prefix() ++ "/"
2640
}),

deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl

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

1010
-export([init/2]).
11+
-include("rabbit_mgmt.hrl").
1112

1213
%%--------------------------------------------------------------------
1314

@@ -34,17 +35,27 @@ set_oauth_settings(AuthSettings) ->
3435
set_token_auth(AuthSettings, Req0) ->
3536
case proplists:get_value(oauth_enabled, AuthSettings, false) of
3637
true ->
37-
case cowboy_req:parse_header(<<"Authorization">>, Req0) of
38-
{bearer, Token} -> {
38+
case cowboy_req:parse_header(<<"authorization">>, Req0) of
39+
{bearer, Token} ->
40+
rabbit_log:debug("Request contained token in authorization header"),
41+
{
3942
Req0,
4043
["set_token_auth('", Token, "');"]
4144
};
4245
_ ->
4346
Cookies = cowboy_req:parse_cookies(Req0),
4447
case lists:keyfind(?OAUTH2_ACCESS_TOKEN_COOKIE, 1, Cookies) of
45-
{_, Token} -> {
48+
{_, Token} ->
49+
rabbit_log:debug("Request contained token in cookie: ~p", [Token]),
50+
{
4651
cowboy_req:set_resp_cookie(
47-
?OAUTH2_ACCESS_TOKEN_COOKIE, <<>>, Req0, #{max_age => 0}),
52+
?OAUTH2_ACCESS_TOKEN_COOKIE, <<"">>, Req0, #{
53+
max_age => 0,
54+
expires => os:system_time(millisecond) - 30000,
55+
http_only => true,
56+
path => "/js/oidc-oauth/bootstrap.js",
57+
same_site => strict
58+
}),
4859
["set_token_auth('", Token, "');"]
4960
};
5061
false -> {

0 commit comments

Comments
 (0)