1212-include_lib (" kernel/include/logger.hrl" ).
1313
1414% %--------------------------------------------------------------------
15- % % js/oidc-oauth/bootstrap.js
16- % % It produces a javascript file with all the oauth2 configuration needed
15+ % % js/oidc-oauth/bootstrap.js
16+ % % It produces a javascript file with all the oauth2 configuration needed
1717% % in the client-side of the management ui.
1818% % This endpoint only accepts GET method.
1919% %
20- % % It can work in conjunction with the /api/login endpoint. If the users are
20+ % % It can work in conjunction with the /api/login endpoint. If the users are
2121% % redirected to the home page of the management ui, and eventually to this endpoint,
22- % % via the /api/login endpoint is very likely that the request carries a cookie.
22+ % % via the /api/login endpoint is very likely that the request carries a cookie.
2323% % It can be the <<"access_token">> cookie or the cookies <<"strict_auth_mechanism">>
2424% % or <<"preferred_auth_mechanism">>.
2525% % These cookies are consumed by this endpoint and removed afterwards.
2626% %
2727% % Additionally, this endpoint may accept users' authentication mechanism preferences
28- % % via its corresponding header, in addition to the two cookies mentioned above.
29- % % But not via request parameters. If this endpoint would have accepted request parameters,
30- % % it would have to use the "Referer" header to extract the original request parameters.
28+ % % via its corresponding header, in addition to the two cookies mentioned above.
29+ % % But not via request parameters. If this endpoint would have accepted request parameters,
30+ % % it would have to use the "Referer" header to extract the original request parameters.
3131% % It is possible that in some environments, these headers may be dropped before they reach this endpoint.
32- % % Therefore, users who can only use request parameters, they have to use the /api/login
32+ % % Therefore, users who can only use request parameters, they have to use the /api/login
3333% % endpoint instead.
3434
3535init (Req0 , State ) ->
@@ -45,7 +45,7 @@ bootstrap_oauth(Req0, State) ->
4545 set_oauth_settings (AuthSettings ) ++
4646 SetTokenAuth ++
4747 export_dependencies (Dependencies ),
48-
48+
4949 {ok , cowboy_req :reply (200 , #{<<" content-type" >> => <<" text/javascript; charset=utf-8" >>},
5050 JSContent , Req2 ), State }.
5151
@@ -56,11 +56,11 @@ enrich_oauth_settings(Req0, AuthSettings) ->
5656 {preferred_auth_mechanism , Args } -> {Req1 , [{preferred_auth_mechanism , Args } | AuthSettings ]};
5757 {strict_auth_mechanism , Args } -> {Req1 , [{strict_auth_mechanism , Args } | AuthSettings ]};
5858 {error , Reason } -> ? LOG_DEBUG (" ~p " , [Reason ]),
59- {Req1 , AuthSettings }
59+ {Req1 , AuthSettings }
6060 end .
6161get_auth_mechanism (Req ) ->
62- case get_auth_mechanism_from_cookies (Req ) of
63- undefined ->
62+ case get_auth_mechanism_from_cookies (Req ) of
63+ undefined ->
6464 case cowboy_req :header (<<" x-" , ? MANAGEMENT_LOGIN_STRICT_AUTH_MECHANISM /binary >>, Req ) of
6565 undefined ->
6666 case cowboy_req :header (<<" x-" , ? MANAGEMENT_LOGIN_PREFERRED_AUTH_MECHANISM /binary >>, Req ) of
@@ -69,37 +69,37 @@ get_auth_mechanism(Req) ->
6969 end ;
7070 Val -> {Req , {strict_auth_mechanism , Val }}
7171 end ;
72- {Type , _ } = Auth -> { cowboy_req :set_resp_cookie (term_to_binary (Type ),
72+ {Type , _ } = Auth -> { cowboy_req :set_resp_cookie (term_to_binary (Type ),
7373 <<" " >>, Req , #{
7474 max_age => 0 ,
7575 http_only => true ,
7676 path => ? OAUTH2_BOOTSTRAP_PATH ,
7777 same_site => strict
78- }),
78+ }),
7979 Auth
8080 }
8181 end .
8282
8383get_auth_mechanism_from_cookies (Req ) ->
8484 Cookies = cowboy_req :parse_cookies (Req ),
85- case proplists :get_value (? MANAGEMENT_LOGIN_STRICT_AUTH_MECHANISM , Cookies ) of
86- undefined ->
87- case proplists :get_value (? MANAGEMENT_LOGIN_PREFERRED_AUTH_MECHANISM , Cookies ) of
85+ case proplists :get_value (? MANAGEMENT_LOGIN_STRICT_AUTH_MECHANISM , Cookies ) of
86+ undefined ->
87+ case proplists :get_value (? MANAGEMENT_LOGIN_PREFERRED_AUTH_MECHANISM , Cookies ) of
8888 undefined -> undefined ;
8989 Val -> {preferred_auth_mechanism , Val }
9090 end ;
9191 Val -> {strict_auth_mechanism , Val }
9292 end .
93- validate_auth_mechanism ({Type , <<" oauth2:" , Id /binary >>}, AuthSettings ) ->
94- case maps :is_key (Id , proplists :get_value (oauth_resource_servers , AuthSettings )) of
93+ validate_auth_mechanism ({Type , <<" oauth2:" , Id /binary >>}, AuthSettings ) ->
94+ case maps :is_key (Id , proplists :get_value (oauth_resource_servers , AuthSettings )) of
9595 true -> {Type , [{type , <<" oauth2" >>}, {resource_id , Id }]};
9696 _ -> {error , {unknown_resource_id , Id }}
9797 end ;
98- validate_auth_mechanism ({Type , <<" basic" >>}, _AuthSettings ) ->
98+ validate_auth_mechanism ({Type , <<" basic" >>}, _AuthSettings ) ->
9999 {Type , [{type , <<" basic" >>}]};
100100validate_auth_mechanism ({_ , _ }, _AuthSettings ) -> {error , unknown_auth_mechanism };
101101validate_auth_mechanism (_ , _ ) -> {error , unknown_auth_mechanism }.
102-
102+
103103set_oauth_settings (AuthSettings ) ->
104104 JsonAuthSettings = rabbit_json :encode (rabbit_mgmt_format :format_nulls (AuthSettings )),
105105 [" set_oauth_settings(" , JsonAuthSettings , " );" ].
@@ -108,33 +108,33 @@ set_token_auth(AuthSettings, Req0) ->
108108 case proplists :get_value (oauth_enabled , AuthSettings , false ) of
109109 true ->
110110 case cowboy_req :parse_header (<<" authorization" >>, Req0 ) of
111- {bearer , Token } ->
111+ {bearer , Token } ->
112112 {
113- Req0 ,
113+ Req0 ,
114114 [" set_token_auth('" , Token , " ');" ]
115115 };
116- _ ->
117- Cookies = cowboy_req :parse_cookies (Req0 ),
118- case lists :keyfind (? OAUTH2_ACCESS_TOKEN , 1 , Cookies ) of
119- {_ , Token } ->
116+ _ ->
117+ Cookies = cowboy_req :parse_cookies (Req0 ),
118+ case proplists :get_value (? OAUTH2_ACCESS_TOKEN , Cookies ) of
119+ undefined -> {
120+ Req0 ,
121+ []
122+ };
123+ Token ->
120124 {
121125 cowboy_req :set_resp_cookie (
122126 ? OAUTH2_ACCESS_TOKEN , <<" " >>, Req0 , #{
123127 max_age => 0 ,
124128 http_only => true ,
125129 path => ? OAUTH2_BOOTSTRAP_PATH ,
126130 same_site => strict
127- }),
131+ }),
128132 [" set_token_auth('" , Token , " ');" ]
129- };
130- false -> {
131- Req0 ,
132- []
133133 }
134134 end
135135 end ;
136136 false -> {
137- Req0 ,
137+ Req0 ,
138138 []
139139 }
140140 end .
0 commit comments