|
10 | 10 | -export([init/2]).
|
11 | 11 |
|
12 | 12 | -include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
|
| 13 | +-include("rabbit_mgmt.hrl"). |
| 14 | + |
13 | 15 | %%--------------------------------------------------------------------
|
14 | 16 |
|
15 | 17 | init(Req0, State) ->
|
16 | 18 | login(cowboy_req:method(Req0), Req0, State).
|
17 | 19 |
|
18 |
| -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; |
| 20 | +login(<<"POST">>, Req0=#{scheme := Scheme}, State) -> |
| 21 | + {ok, Body, _} = cowboy_req:read_urlencoded_body(Req0), |
| 22 | + AccessToken = proplists:get_value(<<"access_token">>, Body), |
| 23 | + case rabbit_mgmt_util:is_authorized_user(Req0, #context{}, <<"">>, AccessToken, false) of |
| 24 | + {true, Req1, _} -> |
| 25 | + CookieSettings = #{ |
| 26 | + http_only => true, |
| 27 | + path => ?OAUTH2_ACCESS_TOKEN_COOKIE_PATH, |
| 28 | + max_age => 30, |
| 29 | + same_site => strict |
| 30 | + }, |
| 31 | + SetCookie = cowboy_req:set_resp_cookie(?OAUTH2_ACCESS_TOKEN_COOKIE_NAME, AccessToken, Req1, |
| 32 | + case Scheme of |
| 33 | + <<"https">> -> CookieSettings#{ secure => true}; |
| 34 | + _ -> CookieSettings |
| 35 | + end), |
| 36 | + Home = cowboy_req:uri(SetCookie, #{ |
| 37 | + path => rabbit_mgmt_util:get_path_prefix() ++ "/" |
| 38 | + }), |
| 39 | + Redirect = cowboy_req:reply(302, #{ |
| 40 | + <<"Location">> => iolist_to_binary(Home) |
| 41 | + }, <<>>, SetCookie), |
| 42 | + {ok, Redirect, State}; |
| 43 | + {false, ReqData1, Reason} -> |
| 44 | + replyWithError(Reason, ReqData1, State) |
| 45 | + end; |
35 | 46 |
|
36 | 47 | login(_, Req0, State) ->
|
37 | 48 | %% Method not allowed.
|
38 | 49 | {ok, cowboy_req:reply(405, Req0), State}.
|
| 50 | + |
| 51 | +replyWithError(Reason, Req, State) -> |
| 52 | + Home = cowboy_req:uri(Req, #{ |
| 53 | + path => rabbit_mgmt_util:get_path_prefix() ++ "/", |
| 54 | + qs => "error=" ++ Reason |
| 55 | + }), |
| 56 | + Req2 = cowboy_req:reply(302, #{ |
| 57 | + <<"Location">> => iolist_to_binary(Home) |
| 58 | + }, <<>>, Req), |
| 59 | + {ok, Req2, State}. |
| 60 | + |
| 61 | + |
0 commit comments