Skip to content

Commit cd195a6

Browse files
Use strict_auth_mechanism and preferred_auth_mechanism
Request parameters and corresponding header to control default auth mechanism or fixed auth mechanism
1 parent 6f76982 commit cd195a6

File tree

5 files changed

+74
-46
lines changed

5 files changed

+74
-46
lines changed

deps/rabbitmq_management/priv/www/js/main.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ function startWithOAuthLogin (oauth) {
4141
}
4242
}
4343
function render_login_oauth(oauth, messages) {
44-
let formatData = {}
45-
formatData.warnings = []
46-
formatData.notAuthorized = false
47-
formatData.resource_servers = oauth.resource_servers
48-
formatData.declared_resource_servers_count = oauth.declared_resource_servers_count
49-
formatData.oauth_disable_basic_auth = oauth.oauth_disable_basic_auth
44+
let formatData = {};
45+
formatData.warnings = [];
46+
formatData.notAuthorized = false;
47+
formatData.resource_servers = oauth.resource_servers;
48+
formatData.declared_resource_servers_count = oauth.declared_resource_servers_count;
49+
formatData.oauth_disable_basic_auth = oauth.oauth_disable_basic_auth;
50+
formatData.strict_auth_mechanism = oauth.strict_auth_mechanism;
51+
formatData.preferred_auth_mechanism = oauth.preferred_auth_mechanism;
5052

5153
if (Array.isArray(messages)) {
5254
formatData.warnings = messages

deps/rabbitmq_management/priv/www/js/oidc-oauth/helper.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,25 @@ function oauth_initialize_user_manager(resource_server) {
193193
});
194194

195195
}
196+
function parseAuthMechanism(oauth, name, auth_mechanism) {
197+
if (!auth_mechanism) {
198+
return oauth;
199+
}
200+
201+
if (auth_mechanism.includes(':')) {
202+
// OAuth2 case: "oauth2:prod"
203+
const [authMethod, resourceId] = auth_mechanism.split(':');
204+
if (authMethod === 'oauth2' && resourceId) {
205+
if (oauth.resource_servers.some(resource => resource.id === resourceId)) {
206+
oauth[name] = {type: "oauth2", resource_id: resourceId};
207+
}
208+
}
209+
} else if (auth_mechanism === 'basic' && oauth.oauth_disable_basic_auth === false) {
210+
oauth[name] = {type: "basic"};
211+
}
212+
return oauth;
213+
}
214+
196215
export function oauth_initialize(authSettings) {
197216
authSettings = auth_settings_apply_defaults(authSettings);
198217
let oauth = {
@@ -202,7 +221,14 @@ export function oauth_initialize(authSettings) {
202221
"oauth_disable_basic_auth" : authSettings.oauth_disable_basic_auth
203222
}
204223
if (!oauth.enabled) return oauth;
205-
224+
if (authSettings.resource_servers.length > 1 || !authSettings.oauth_disable_basic_auth) {
225+
if (authSettings.strict_auth_mechanism) {
226+
oauth = parseAuthMechanism(oauth, "strict_auth_mechanism", authSettings.strict_auth_mechanism);
227+
}else if (authSettings.preferred_auth_mechanism) {
228+
oauth = parseAuthMechanism(oauth, "preferred_auth_mechanism", authSettings.preferred_auth_mechanism);
229+
}
230+
}
231+
206232
let resource_server = null;
207233

208234
if (oauth.resource_servers.length == 1) {

deps/rabbitmq_management/priv/www/js/tmpl/login_oauth.ejs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
<% } %>
1313
</div>
1414
<% if (!notAuthorized) { %>
15-
<% if ((typeof resource_servers == 'object' && resource_servers.length == 1) && oauth_disable_basic_auth) { %>
15+
<% if (strict_auth_mechanism !== undefined && strict_auth_mechanism.type === "oauth2") { %>
16+
<button id="login" onclick="oauth_initiateLogin('<%=strict_auth_mechanism.resource_id%>')">Click here to log in</button>
17+
<% } else if ((typeof resource_servers == 'object' && resource_servers.length == 1) && oauth_disable_basic_auth) { %>
1618
<button id="login" onclick="oauth_initiateLogin('<%=resource_servers[0].id%>')">Click here to log in</button>
17-
<% } else if (typeof resource_servers == 'object' && resource_servers.length >= 1) { %>
19+
<% } else if (typeof resource_servers == 'object' && resource_servers.length >= 1 && strict_auth_mechanism == undefined) { %>
1820
1921
<b>Login with :</b>
2022
<p/>
23+
<% const preferredResourceId = preferred_auth_mechanism !== undefined && preferred_auth_mechanism.type === "oauth2" ? preferred_auth_mechanism.resource_id : null; %>
2124
<!-- begin login with oauth2 -->
2225
<div class="section" id="login-with-oauth2">
2326
<h2>OAuth 2.0</h2>
@@ -30,7 +33,7 @@
3033
<label for="oauth2-resource">Resource:</label>
3134
<select id="oauth2-resource">
3235
<% for (var i = 0; i < resource_servers.length; i++) { %>
33-
<option value="<%= fmt_string(resource_servers[i].id) %>">
36+
<option value="<%= fmt_string(resource_servers[i].id) %>" <%= (preferredResourceId === resource_servers[i].id) ? 'selected="selected"' : '' %>>
3437
<%= fmt_string(resource_servers[i].label != null ? resource_servers[i].label : resource_servers[i].id) %>
3538
</option>
3639
<% } %>
@@ -45,9 +48,9 @@
4548
<!-- end login with oauth2 -->
4649
<% } %>
4750
48-
<!-- begin login with basic auth -->
49-
<% if (!oauth_disable_basic_auth) { %>
50-
<div class="section-hidden" id="login-with-basic-auth">
51+
<!-- begin login with basic auth -->
52+
<% if (!oauth_disable_basic_auth && (strict_auth_mechanism === undefined || strict_auth_mechanism.type === "basic")) { %>
53+
<div class="section-hidden <%= (strict_auth_mechanism != undefined && strict_auth_mechanism.type === 'basic')? 'section-visible' : '' %> " id="login-with-basic-auth">
5154
<h2>Basic Authentication</h2>
5255
<div class="hider">
5356
<div class="updatable">

deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
-export([init/2]).
1111
-include("rabbit_mgmt.hrl").
12+
-include_lib("kernel/include/logger.hrl").
1213

1314
%%--------------------------------------------------------------------
1415

@@ -18,6 +19,7 @@ init(Req0, State) ->
1819

1920
bootstrap_oauth(Req0, State) ->
2021
AuthSettings = enrich_oauth_settings(Req0, rabbit_mgmt_wm_auth:authSettings()),
22+
?LOG_DEBUG("AuthSettings: ~p", [AuthSettings]),
2123
Dependencies = oauth_dependencies(),
2224
{Req1, SetTokenAuth} = set_token_auth(AuthSettings, Req0),
2325
JSContent = import_dependencies(Dependencies) ++
@@ -29,7 +31,10 @@ bootstrap_oauth(Req0, State) ->
2931
JSContent, Req1), State}.
3032

3133
enrich_oauth_settings(Req0, AuthSettings) ->
32-
case get_auth_mechanism(Req0) of
34+
Auth = get_auth_mechanism(Req0),
35+
ValidAuth = validate_auth_mechanism(Auth, AuthSettings),
36+
?LOG_DEBUG("validate_auth_mechanism ~p -> ~p", [Auth, ValidAuth]),
37+
case ValidAuth of
3338
undefined -> AuthSettings;
3439
{_, _} = Auth -> [Auth | AuthSettings]
3540
end.
@@ -42,9 +47,31 @@ get_auth_mechanism(Req) ->
4247
end;
4348
Val -> {strict_auth_mechanism, Val}
4449
end.
50+
validate_auth_mechanism({_, <<"oauth2:", Id/binary>>} = Auth, AuthSettings) ->
51+
case maps:is_key(Id, proplists:get_value(oauth_resource_servers, AuthSettings)) of
52+
true -> Auth;
53+
_ -> undefined
54+
end;
55+
validate_auth_mechanism({_, <<"basic">>} = Auth, _AuthSettings) -> Auth;
56+
validate_auth_mechanism({_, _}, _AuthSettings) -> undefined;
57+
validate_auth_mechanism(_, _) -> undefined.
58+
59+
extract_referer_params(Req) ->
60+
case cowboy_req:header(<<"referer">>, Req) of
61+
undefined -> [];
62+
Referer ->
63+
case uri_string:parse(Referer) of
64+
#{query := Query} when Query =/= undefined ->
65+
uri_string:dissect_query(Query);
66+
_ ->
67+
[]
68+
end
69+
end.
4570
get_param_or_header(ParamName, HeaderName, Req) ->
46-
case rabbit_mgmt_util:qs_val(ParamName, Req) of
47-
undefined -> cowboy_req:parse_header(HeaderName, Req);
71+
ReqParams = maps:from_list(extract_referer_params(Req)),
72+
?LOG_DEBUG("ReqParams: ~p", [ReqParams]),
73+
case maps:get(ParamName, ReqParams, undefined) of
74+
undefined -> cowboy_req:header(HeaderName, Req);
4875
Val -> Val
4976
end.
5077

deps/rabbitmq_management/test/rabbit_mgmt_http_SUITE.erl

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ all_tests() -> [
201201
rates_test,
202202
single_active_consumer_cq_test,
203203
single_active_consumer_qq_test,
204-
%% This test needs the OAuth 2 plugin to be enabled
205-
%% oauth_test,
206204
disable_basic_auth_test,
207205
login_test,
208206
csp_headers_test,
@@ -4003,34 +4001,6 @@ stats_redirect_test(Config) ->
40034001
assert_permanent_redirect(Config, "doc/stats.html", "/api/index.html"),
40044002
passed.
40054003

4006-
oauth_test(Config) ->
4007-
ok = rabbit_ct_broker_helpers:enable_plugin(Config, 0, "rabbitmq_auth_backend_oauth2"),
4008-
4009-
Map1 = http_get(Config, "/auth", ?OK),
4010-
%% Defaults
4011-
?assertEqual(false, maps:get(oauth_enabled, Map1)),
4012-
4013-
%% Misconfiguration
4014-
rpc(Config, application, set_env, [rabbitmq_management, oauth_enabled, true]),
4015-
Map2 = http_get(Config, "/auth", ?OK),
4016-
?assertEqual(false, maps:get(oauth_enabled, Map2)),
4017-
?assertEqual(<<>>, maps:get(oauth_client_id, Map2)),
4018-
?assertEqual(<<>>, maps:get(oauth_provider_url, Map2)),
4019-
%% Valid config requires non empty OAuthClientId, OAuthClientSecret, OAuthResourceId, OAuthProviderUrl
4020-
rpc(Config, application, set_env, [rabbitmq_management, oauth_client_id, "rabbit_user"]),
4021-
rpc(Config, application, set_env, [rabbitmq_management, oauth_client_secret, "rabbit_secret"]),
4022-
rpc(Config, application, set_env, [rabbitmq_management, oauth_provider_url, "http://localhost:8080/uaa"]),
4023-
rpc(Config, application, set_env, [rabbitmq_auth_backend_oauth2, resource_server_id, "rabbitmq"]),
4024-
Map3 = http_get(Config, "/auth", ?OK),
4025-
println(Map3),
4026-
?assertEqual(true, maps:get(oauth_enabled, Map3)),
4027-
?assertEqual(<<"rabbit_user">>, maps:get(oauth_client_id, Map3)),
4028-
?assertEqual(<<"rabbit_secret">>, maps:get(oauth_client_secret, Map3)),
4029-
?assertEqual(<<"rabbitmq">>, maps:get(resource_server_id, Map3)),
4030-
?assertEqual(<<"http://localhost:8080/uaa">>, maps:get(oauth_provider_url, Map3)),
4031-
%% cleanup
4032-
rpc(Config, application, unset_env, [rabbitmq_management, oauth_enabled]).
4033-
40344004
version_test(Config) ->
40354005
ActualVersion = http_get(Config, "/version"),
40364006
ct:log("ActualVersion : ~p", [ActualVersion]),

0 commit comments

Comments
 (0)