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,38 +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- ? LOG_DEBUG (" get_auth_mechanism_from_cookies: ~p " , [Cookies ]),
86- case proplists :get_value (? MANAGEMENT_LOGIN_STRICT_AUTH_MECHANISM , Cookies ) of
87- undefined ->
88- 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
8988 undefined -> undefined ;
9089 Val -> {preferred_auth_mechanism , Val }
9190 end ;
9291 Val -> {strict_auth_mechanism , Val }
9392 end .
94- validate_auth_mechanism ({Type , <<" oauth2:" , Id /binary >>}, AuthSettings ) ->
95- 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
9695 true -> {Type , [{type , <<" oauth2" >>}, {resource_id , Id }]};
9796 _ -> {error , {unknown_resource_id , Id }}
9897 end ;
99- validate_auth_mechanism ({Type , <<" basic" >>}, _AuthSettings ) ->
98+ validate_auth_mechanism ({Type , <<" basic" >>}, _AuthSettings ) ->
10099 {Type , [{type , <<" basic" >>}]};
101100validate_auth_mechanism ({_ , _ }, _AuthSettings ) -> {error , unknown_auth_mechanism };
102101validate_auth_mechanism (_ , _ ) -> {error , unknown_auth_mechanism }.
103-
102+
104103set_oauth_settings (AuthSettings ) ->
105104 JsonAuthSettings = rabbit_json :encode (rabbit_mgmt_format :format_nulls (AuthSettings )),
106105 [" set_oauth_settings(" , JsonAuthSettings , " );" ].
@@ -109,34 +108,33 @@ set_token_auth(AuthSettings, Req0) ->
109108 case proplists :get_value (oauth_enabled , AuthSettings , false ) of
110109 true ->
111110 case cowboy_req :parse_header (<<" authorization" >>, Req0 ) of
112- {bearer , Token } ->
111+ {bearer , Token } ->
113112 {
114- Req0 ,
113+ Req0 ,
115114 [" set_token_auth('" , Token , " ');" ]
116115 };
117- _ ->
116+ _ ->
118117 Cookies = cowboy_req :parse_cookies (Req0 ),
119- ? LOG_DEBUG (" set_token_auth: ~p " , [Cookies ]),
120- case proplists :get_value (? OAUTH2_ACCESS_TOKEN , Cookies ) of
118+ case proplists :get_value (? OAUTH2_ACCESS_TOKEN , Cookies ) of
121119 undefined -> {
122- Req0 ,
120+ Req0 ,
123121 []
124122 };
125- Token ->
123+ Token ->
126124 {
127125 cowboy_req :set_resp_cookie (
128126 ? OAUTH2_ACCESS_TOKEN , <<" " >>, Req0 , #{
129127 max_age => 0 ,
130128 http_only => true ,
131129 path => ? OAUTH2_BOOTSTRAP_PATH ,
132130 same_site => strict
133- }),
131+ }),
134132 [" set_token_auth('" , Token , " ');" ]
135- }
133+ }
136134 end
137135 end ;
138136 false -> {
139- Req0 ,
137+ Req0 ,
140138 []
141139 }
142140 end .
0 commit comments