Skip to content

Commit d54c136

Browse files
REmove from code keycloak format
Update existing test case with the appropriate configuration Add more tests
1 parent eebe67c commit d54c136

File tree

5 files changed

+100
-65
lines changed

5 files changed

+100
-65
lines changed

deps/rabbitmq_auth_backend_oauth2/app.bzl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def all_beam_files(name = "all_beam_files"):
1313
"src/Elixir.RabbitMQ.CLI.Ctl.Commands.AddUaaKeyCommand.erl",
1414
"src/rabbit_auth_backend_oauth2.erl",
1515
"src/rabbit_auth_backend_oauth2_app.erl",
16-
"src/rabbit_oauth2_keycloak.erl",
1716
"src/rabbit_oauth2_provider.erl",
1817
"src/rabbit_oauth2_rar.erl",
1918
"src/rabbit_oauth2_resource_server.erl",
@@ -51,7 +50,6 @@ def all_test_beam_files(name = "all_test_beam_files"):
5150
"src/Elixir.RabbitMQ.CLI.Ctl.Commands.AddUaaKeyCommand.erl",
5251
"src/rabbit_auth_backend_oauth2.erl",
5352
"src/rabbit_auth_backend_oauth2_app.erl",
54-
"src/rabbit_oauth2_keycloak.erl",
5553
"src/rabbit_oauth2_provider.erl",
5654
"src/rabbit_oauth2_rar.erl",
5755
"src/rabbit_oauth2_resource_server.erl",
@@ -101,7 +99,6 @@ def all_srcs(name = "all_srcs"):
10199
"src/Elixir.RabbitMQ.CLI.Ctl.Commands.AddUaaKeyCommand.erl",
102100
"src/rabbit_auth_backend_oauth2.erl",
103101
"src/rabbit_auth_backend_oauth2_app.erl",
104-
"src/rabbit_oauth2_keycloak.erl",
105102
"src/rabbit_oauth2_provider.erl",
106103
"src/rabbit_oauth2_rar.erl",
107104
"src/rabbit_oauth2_resource_server.erl",

deps/rabbitmq_auth_backend_oauth2/src/rabbit_auth_backend_oauth2.erl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
get_scope/1, set_scope/2,
2929
resolve_resource_server/1]).
3030

31-
-import(rabbit_oauth2_keycloak, [has_keycloak_scopes/1, extract_scopes_from_keycloak_format/1]).
3231
-import(rabbit_oauth2_rar, [extract_scopes_from_rich_auth_request/2, has_rich_auth_request_scopes/1]).
3332

3433
-import(rabbit_oauth2_scope, [filter_matching_scope_prefix_and_drop_it/2]).
@@ -245,24 +244,19 @@ normalize_token_scope(ResourceServer, Payload) ->
245244
false -> Payload0
246245
end,
247246

248-
Payload2 = case has_keycloak_scopes(Payload1) of
249-
true -> extract_scopes_from_keycloak_format(Payload1);
250-
false -> Payload1
247+
Payload2 = case ResourceServer#resource_server.scope_aliases of
248+
undefined -> Payload1;
249+
ScopeAliases -> extract_scopes_using_scope_aliases(ScopeAliases, Payload1)
251250
end,
252251

253-
Payload3 = case ResourceServer#resource_server.scope_aliases of
254-
undefined -> Payload2;
255-
ScopeAliases -> extract_scopes_using_scope_aliases(ScopeAliases, Payload2)
256-
end,
257-
258-
Payload4 = case has_rich_auth_request_scopes(Payload3) of
259-
true -> extract_scopes_from_rich_auth_request(ResourceServer, Payload3);
260-
false -> Payload3
252+
Payload3 = case has_rich_auth_request_scopes(Payload2) of
253+
true -> extract_scopes_from_rich_auth_request(ResourceServer, Payload2);
254+
false -> Payload2
261255
end,
262256

263257
FilteredScopes = filter_matching_scope_prefix_and_drop_it(
264-
get_scope(Payload4), ResourceServer#resource_server.scope_prefix),
265-
set_scope(FilteredScopes, Payload4).
258+
get_scope(Payload3), ResourceServer#resource_server.scope_prefix),
259+
set_scope(FilteredScopes, Payload3).
266260

267261

268262
-spec extract_scopes_using_scope_aliases(
@@ -293,7 +287,7 @@ extract_scopes_using_scope_aliases(ScopeAliasMapping, Payload) ->
293287
has_additional_scopes_key(ResourceServer, Payload) when is_map(Payload) ->
294288
case ResourceServer#resource_server.additional_scopes_key of
295289
undefined -> false;
296-
ScopeKey -> maps:is_key(ScopeKey, Payload)
290+
_ -> true
297291
end.
298292

299293
%% Path is a binary expression which is a plain word like <<"roles">>
@@ -373,14 +367,16 @@ split_path(Path) when is_binary(Path) ->
373367
binary:split(Path, <<".">>, [global, trim_all]).
374368

375369

376-
377-
378370
-spec extract_scopes_from_additional_scopes_key(
379371
ResourceServer :: resource_server(), Payload :: map()) -> map().
380372
extract_scopes_from_additional_scopes_key(ResourceServer, Payload) ->
381-
Claim = maps:get(ResourceServer#resource_server.additional_scopes_key, Payload),
382-
AdditionalScopes = extract_additional_scopes(ResourceServer, Claim),
383-
set_scope(AdditionalScopes ++ get_scope(Payload), Payload).
373+
Paths = case ResourceServer#resource_server.additional_scopes_key of
374+
B when is_binary(B) -> binary:split(B, <<" ">>, [global, trim_all]);
375+
L when is_list(L) -> L
376+
end,
377+
AdditionalScopes = [ extract_token_value(ResourceServer,
378+
Payload, Path, fun extract_scope_list_from_token_value/2) || Path <- Paths],
379+
set_scope(lists:flatten(AdditionalScopes) ++ get_scope(Payload), Payload).
384380

385381
extract_additional_scopes(ResourceServer, ComplexClaim) ->
386382
ResourceServerId = ResourceServer#resource_server.id,

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_keycloak.erl

Lines changed: 0 additions & 41 deletions
This file was deleted.

deps/rabbitmq_auth_backend_oauth2/test/config_schema_SUITE_data/rabbitmq_auth_backend_oauth2.snippets

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,15 @@
316316
}
317317
]}
318318
], []
319+
},
320+
{additional_scopes_key,
321+
"auth_oauth2.resource_server_id = new_resource_server_id
322+
auth_oauth2.additional_scopes_key = roles realm.roles",
323+
[
324+
{rabbitmq_auth_backend_oauth2, [
325+
{resource_server_id,<<"new_resource_server_id">>},
326+
{extra_scopes_source, <<"roles realm.roles">> }
327+
]}
328+
], []
319329
}
320330
].

deps/rabbitmq_auth_backend_oauth2/test/unit_SUITE.erl

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ all() ->
4242
test_token_expiration,
4343
test_invalid_signature,
4444
test_incorrect_kid,
45+
normalize_token_scope_using_multiple_scopes_key,
4546
normalize_token_scope_with_keycloak_scopes,
4647
normalize_token_scope_with_rich_auth_request,
4748
normalize_token_scope_with_rich_auth_request_using_regular_expression_with_cluster,
4849
test_unsuccessful_access_with_a_token_that_uses_missing_scope_alias_in_scope_field,
4950
test_unsuccessful_access_with_a_token_that_uses_missing_scope_alias_in_extra_scope_source_field,
5051
test_username_from,
5152
{group, with_rabbitmq_node}
53+
5254
].
5355
groups() ->
5456
[
@@ -119,6 +121,73 @@ end_per_group(_, Config) ->
119121
-define(RESOURCE_SERVER_TYPE, <<"rabbitmq-type">>).
120122
-define(DEFAULT_SCOPE_PREFIX, <<"rabbitmq.">>).
121123

124+
normalize_token_scope_using_multiple_scopes_key(_) ->
125+
Pairs = [
126+
%% common case
127+
{
128+
"keycloak format 1",
129+
#{<<"authorization">> =>
130+
#{<<"permissions">> =>
131+
[#{<<"rsid">> => <<"2c390fe4-02ad-41c7-98a2-cebb8c60ccf1">>,
132+
<<"rsname">> => <<"allvhost">>,
133+
<<"scopes">> => [<<"rabbitmq-resource.read:*/*">>]},
134+
#{<<"rsid">> => <<"e7f12e94-4c34-43d8-b2b1-c516af644cee">>,
135+
<<"rsname">> => <<"vhost1">>,
136+
<<"scopes">> => [<<"rabbitmq-resource.write:vhost1/*">>]},
137+
#{<<"rsid">> => <<"12ac3d1c-28c2-4521-8e33-0952eff10bd9">>,
138+
<<"rsname">> => <<"Default Resource">>,
139+
<<"scopes">> => [<<"unknown-resource.write:vhost1/*">>]}
140+
]
141+
}
142+
},
143+
[<<"read:*/*">>, <<"write:vhost1/*">>]
144+
},
145+
{
146+
"keycloak format 2 using realm_access",
147+
#{<<"realm_access">> =>
148+
#{<<"roles">> => [<<"rabbitmq-resource.read:format2/*">>]}
149+
},
150+
[<<"read:format2/*">>]
151+
},
152+
{
153+
"keycloak format 2 using resource_access",
154+
#{<<"resource_access">> =>
155+
#{<<"account">> => #{<<"roles">> => [<<"rabbitmq-resource.read:format2bis/*">>]} }
156+
},
157+
[<<"read:format2bis/*">>]
158+
},
159+
{
160+
"both formats",
161+
#{<<"authorization">> =>
162+
#{<<"permissions">> =>
163+
[#{<<"rsid">> => <<"2c390fe4-02ad-41c7-98a2-cebb8c60ccf1">>,
164+
<<"rsname">> => <<"allvhost">>,
165+
<<"scopes">> => [<<"rabbitmq-resource.read:*/*">>]},
166+
#{<<"rsid">> => <<"e7f12e94-4c34-43d8-b2b1-c516af644cee">>,
167+
<<"rsname">> => <<"vhost1">>,
168+
<<"scopes">> => [<<"rabbitmq-resource.write:vhost1/*">>]},
169+
#{<<"rsid">> => <<"12ac3d1c-28c2-4521-8e33-0952eff10bd9">>,
170+
<<"rsname">> => <<"Default Resource">>,
171+
<<"scopes">> => [<<"unknown-resource.write:vhost1/*">>]}
172+
]
173+
},
174+
<<"realm_access">> =>
175+
#{<<"roles">> => [<<"rabbitmq-resource.read:format2/*">>]},
176+
<<"resource_access">> =>
177+
#{<<"account">> => #{<<"roles">> => [<<"rabbitmq-resource.read:format2bis/*">>]} }
178+
},
179+
[<<"read:*/*">>, <<"write:vhost1/*">>, <<"read:format2/*">>, <<"read:format2bis/*">>]
180+
}
181+
],
182+
183+
lists:foreach(fun({Case, Token0, ExpectedScope}) ->
184+
ResourceServer0 = new_resource_server(<<"rabbitmq-resource">>),
185+
ResourceServer = ResourceServer0#resource_server{
186+
additional_scopes_key = <<"authorization.permissions.scopes realm_access.roles resource_access.account.roles">>
187+
},
188+
Token = normalize_token_scope(ResourceServer, Token0),
189+
?assertEqual(ExpectedScope, uaa_jwt:get_scope(Token), Case)
190+
end, Pairs).
122191

123192
normalize_token_scope_with_keycloak_scopes(_) ->
124193
Pairs = [
@@ -172,7 +241,10 @@ normalize_token_scope_with_keycloak_scopes(_) ->
172241
],
173242

174243
lists:foreach(fun({Case, Authorization, ExpectedScope}) ->
175-
ResourceServer = new_resource_server(<<"rabbitmq-resource">>),
244+
ResourceServer0 = new_resource_server(<<"rabbitmq-resource">>),
245+
ResourceServer = ResourceServer0#resource_server{
246+
additional_scopes_key = <<"authorization.permissions.scopes">>
247+
},
176248
Token0 = #{<<"authorization">> => Authorization},
177249
Token = normalize_token_scope(ResourceServer, Token0),
178250
?assertEqual(ExpectedScope, uaa_jwt:get_scope(Token), Case)
@@ -1313,6 +1385,7 @@ test_extract_scope_from_path_expression(_) ->
13131385
<<"rabbitmq">> => <<"role1 role2">>
13141386
}},
13151387
<<"auth">>, M),
1388+
%% this is the old keycloak format
13161389
[<<"role1">>,<<"role2">>] = extract_token_value(R,
13171390
#{ <<"auth">> => #{
13181391
<<"permission">> => [

0 commit comments

Comments
 (0)