@@ -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 ].
5355groups () ->
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
123192normalize_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