@@ -36,10 +36,9 @@ all() ->
3636 test_token_expiration ,
3737 test_invalid_signature ,
3838 test_incorrect_kid ,
39- test_post_process_token_payload ,
40- test_post_process_token_payload_keycloak ,
41- test_post_process_payload_rich_auth_request ,
42- test_post_process_payload_rich_auth_request_using_regular_expression_with_cluster ,
39+ normalize_token_scope_with_keycloak_scopes ,
40+ normalize_token_scope_with_rich_auth_request ,
41+ normalize_token_scope_with_rich_auth_request_using_regular_expression_with_cluster ,
4342 test_unsuccessful_access_with_a_token_that_uses_missing_scope_alias_in_scope_field ,
4443 test_unsuccessful_access_with_a_token_that_uses_missing_scope_alias_in_extra_scope_source_field ,
4544 test_username_from ,
@@ -62,7 +61,7 @@ groups() ->
6261 test_successful_authentication_without_scopes ,
6362 test_successful_access_with_a_token_that_uses_single_scope_alias_in_extra_scope_source_field ,
6463 test_successful_access_with_a_token_that_uses_multiple_scope_aliases_in_extra_scope_source_field ,
65- test_post_process_token_payload_complex_claims ,
64+ normalize_token_scope_with_additional_scopes_complex_claims ,
6665 test_successful_access_with_a_token_that_uses_single_scope_alias_in_scope_field_and_custom_scope_prefix
6766
6867 ]}
@@ -121,70 +120,67 @@ end_per_group(_, Config) ->
121120-define (DEFAULT_SCOPE_PREFIX , <<" rabbitmq." >>).
122121
123122
124- test_post_process_token_payload_keycloak (_ ) ->
123+ normalize_token_scope_with_keycloak_scopes (_ ) ->
125124 Pairs = [
126125 % % common case
127- {
128- #{<<" permissions" >> =>
129- [#{<<" rsid" >> => <<" 2c390fe4-02ad-41c7-98a2-cebb8c60ccf1" >>,
130- <<" rsname" >> => <<" allvhost" >>,
131- <<" scopes" >> => [<<" rabbitmq-resource.read:*/*" >>]},
132- #{<<" rsid" >> => <<" e7f12e94-4c34-43d8-b2b1-c516af644cee" >>,
133- <<" rsname" >> => <<" vhost1" >>,
134- <<" scopes" >> => [<<" rabbitmq-resource.write:vhost1/*" >>]},
135- #{<<" rsid" >> => <<" 12ac3d1c-28c2-4521-8e33-0952eff10bd9" >>,
136- <<" rsname" >> => <<" Default Resource" >>,
137- <<" scopes" >> => [<<" unknown-resource.write:vhost1/*" >>]}
138- ]
139- },
140- [<<" read:*/*" >>, <<" write:vhost1/*" >>]
141- },
142-
143- % % one scopes field with a string instead of an array
144- {
145- #{<<" permissions" >> =>
146- [#{<<" rsid" >> => <<" 2c390fe4-02ad-41c7-98a2-cebb8c60ccf1" >>,
147- <<" rsname" >> => <<" allvhost" >>,
148- <<" scopes" >> => <<" rabbitmq-resource.read:*/*" >>},
149- #{<<" rsid" >> => <<" e7f12e94-4c34-43d8-b2b1-c516af644cee" >>,
150- <<" rsname" >> => <<" vhost1" >>,
151- <<" scopes" >> => [<<" unknown-resource-read" >>]},
152- #{<<" rsid" >> => <<" 12ac3d1c-28c2-4521-8e33-0952eff10bd9" >>,
153- <<" rsname" >> => <<" Default Resource" >>}]},
154- [<<" rabbitmq-resource.read:*/*" >>]
126+ {
127+ " common case" ,
128+ #{<<" permissions" >> =>
129+ [#{<<" rsid" >> => <<" 2c390fe4-02ad-41c7-98a2-cebb8c60ccf1" >>,
130+ <<" rsname" >> => <<" allvhost" >>,
131+ <<" scopes" >> => [<<" rabbitmq-resource.read:*/*" >>]},
132+ #{<<" rsid" >> => <<" e7f12e94-4c34-43d8-b2b1-c516af644cee" >>,
133+ <<" rsname" >> => <<" vhost1" >>,
134+ <<" scopes" >> => [<<" rabbitmq-resource.write:vhost1/*" >>]},
135+ #{<<" rsid" >> => <<" 12ac3d1c-28c2-4521-8e33-0952eff10bd9" >>,
136+ <<" rsname" >> => <<" Default Resource" >>,
137+ <<" scopes" >> => [<<" unknown-resource.write:vhost1/*" >>]}
138+ ]
155139 },
156-
157- % % no scopes field in permissions
158- {
159- #{<<" permissions" >> =>
160- [#{<<" rsid" >> => <<" 2c390fe4-02ad-41c7-98a2-cebb8c60ccf1" >>,
161- <<" rsname" >> => <<" allvhost" >>},
162- #{<<" rsid" >> => <<" e7f12e94-4c34-43d8-b2b1-c516af644cee" >>,
163- <<" rsname" >> => <<" vhost1" >>},
164- #{<<" rsid" >> => <<" 12ac3d1c-28c2-4521-8e33-0952eff10bd9" >>,
165- <<" rsname" >> => <<" Default Resource" >>}]},
166- []
167- },
168-
169- % % no permissions
170- {
171- #{<<" permissions" >> => []},
140+ [<<" read:*/*" >>, <<" write:vhost1/*" >>]
141+ },
142+ {
143+ " one scopes field with a string instead of an array" ,
144+ #{<<" permissions" >> =>
145+ [#{<<" rsid" >> => <<" 2c390fe4-02ad-41c7-98a2-cebb8c60ccf1" >>,
146+ <<" rsname" >> => <<" allvhost" >>,
147+ <<" scopes" >> => <<" rabbitmq-resource.read:*/*" >>},
148+ #{<<" rsid" >> => <<" e7f12e94-4c34-43d8-b2b1-c516af644cee" >>,
149+ <<" rsname" >> => <<" vhost1" >>,
150+ <<" scopes" >> => [<<" unknown-resource-read" >>]},
151+ #{<<" rsid" >> => <<" 12ac3d1c-28c2-4521-8e33-0952eff10bd9" >>,
152+ <<" rsname" >> => <<" Default Resource" >>}]},
153+ [<<" read:*/*" >>]
154+ },
155+ {
156+ " no scopes field in permissions" ,
157+ #{<<" permissions" >> =>
158+ [#{<<" rsid" >> => <<" 2c390fe4-02ad-41c7-98a2-cebb8c60ccf1" >>,
159+ <<" rsname" >> => <<" allvhost" >>},
160+ #{<<" rsid" >> => <<" e7f12e94-4c34-43d8-b2b1-c516af644cee" >>,
161+ <<" rsname" >> => <<" vhost1" >>},
162+ #{<<" rsid" >> => <<" 12ac3d1c-28c2-4521-8e33-0952eff10bd9" >>,
163+ <<" rsname" >> => <<" Default Resource" >>}]},
164+ []
165+ },
166+ {
167+ " no permissions" ,
168+ #{<<" permissions" >> => []},
172169 []
173- },
174- % % missing permissions key
175- {#{}, []}
170+ },
171+ {" missing permissions key" , #{}, []}
176172 ],
177- lists :foreach (fun ({Authorization , ExpectedScope }) ->
173+
174+ lists :foreach (fun ({Case , Authorization , ExpectedScope }) ->
178175 ResourceServer = resource_server :new_resource_server (<<" rabbitmq-resource" >>),
179176 Token0 = #{<<" authorization" >> => Authorization },
180177 Token = normalize_token_scope (ResourceServer , Token0 ),
181- ? assertEqual (ExpectedScope , uaa_jwt :get_scope (Token ))
178+ ? assertEqual (ExpectedScope , uaa_jwt :get_scope (Token ), Case )
182179 end , Pairs ).
183180
184- test_post_process_payload_rich_auth_request_using_regular_expression_with_cluster (_ ) ->
181+ normalize_token_scope_with_rich_auth_request_using_regular_expression_with_cluster (_ ) ->
185182
186183 Pairs = [
187-
188184 { " should filter out those permisions whose locations do not refer to cluster : {resource_server_id}" ,
189185 [ #{<<" type" >> => ? RESOURCE_SERVER_TYPE ,
190186 <<" locations" >> => [<<" cluster:rabbitmq-test" >>],
@@ -230,7 +226,7 @@ test_post_process_payload_rich_auth_request_using_regular_expression_with_cluste
230226 lists :sort (uaa_jwt :get_scope (Token )), Case )
231227 end , Pairs ).
232228
233- test_post_process_payload_rich_auth_request (_ ) ->
229+ normalize_token_scope_with_rich_auth_request (_ ) ->
234230
235231 Pairs = [
236232 { " should merge all permissions for the current cluster" ,
@@ -546,75 +542,75 @@ test_post_process_payload_rich_auth_request(_) ->
546542 ? assertEqual (ExpectedScopes , ActualScopes , Case )
547543 end , Pairs ).
548544
549- test_post_process_token_payload_complex_claims (_ ) ->
545+ normalize_token_scope_with_additional_scopes_complex_claims (_ ) ->
550546 Pairs = [
551- % % claims in form of binary
552- {
553- <<" rabbitmq.rabbitmq-resource.read:*/* rabbitmq.rabbitmq-resource-read" >>,
554- [<<" rabbitmq.rabbitmq-resource.read:*/*" >>, <<" rabbitmq.rabbitmq-resource-read" >>]
555- },
556- % % claims in form of binary - empty result
557- {<<>>, []},
558- % % claims in form of list
559- {
560- [<<" rabbitmq.rabbitmq-resource.read:*/*" >>,
547+ {
548+ " claims in form of binary" ,
549+ <<" rabbitmq.rabbitmq-resource.read:*/* rabbitmq.rabbitmq-resource-read" >>,
550+ [<<" read:*/*" >>]
551+ },
552+ {" claims in form of binary - empty result" , <<>>, []},
553+ {
554+ " claims in form of list" ,
555+ [<<" rabbitmq.rabbitmq-resource.read:*/*" >>,
561556 <<" rabbitmq2.rabbitmq-resource-read" >>],
562- [<<" rabbitmq.rabbitmq-resource.read:*/*" >>, <<" rabbitmq2.rabbitmq-resource-read" >>]
563- },
564- % % claims in form of list - empty result
565- {[], []},
566- % % claims are map with list content
567- {
568- #{<<" rabbitmq" >> =>
569- [<<" rabbitmq-resource.read:*/*" >>,
570- <<" rabbitmq-resource-read" >>],
571- <<" rabbitmq3" >> =>
572- [<<" rabbitmq-resource.write:*/*" >>,
573- <<" rabbitmq-resource-write" >>]},
574- [<<" rabbitmq.rabbitmq-resource.read:*/*" >>, <<" rabbitmq.rabbitmq-resource-read" >>]
575- },
576- % % claims are map with list content - empty result
577- {
578- #{<<" rabbitmq2" >> =>
579- [<<" rabbitmq-resource.read:*/*" >>,
580- <<" rabbitmq-resource-read" >>]},
581- []
582- },
583- % % claims are map with binary content
584- {
585- #{<<" rabbitmq" >> => <<" rabbitmq-resource.read:*/* rabbitmq-resource-read" >>,
586- <<" rabbitmq3" >> => <<" rabbitmq-resource.write:*/* rabbitmq-resource-write" >>},
587- [<<" rabbitmq.rabbitmq-resource.read:*/*" >>, <<" rabbitmq.rabbitmq-resource-read" >>]
588- },
589- % % claims are map with binary content - empty result
590- {
591- #{<<" rabbitmq2" >> => <<" rabbitmq-resource.read:*/* rabbitmq-resource-read" >>}, []
592- },
593- % % claims are map with empty binary content - empty result
594- {
595- #{<<" rabbitmq" >> => <<>>}, []
596- },
597- % % claims are map with empty list content - empty result
598- {
599- #{<<" rabbitmq" >> => []}, []
557+ [<<" read:*/*" >>]
558+ },
559+ {" claims in form of list - empty result" , [], []},
560+ {
561+ " claims are map with list content" ,
562+ #{<<" rabbitmq" >> =>
563+ [<<" rabbitmq-resource.read:*/*" >>,
564+ <<" rabbitmq-resource-read" >>],
565+ <<" rabbitmq3" >> =>
566+ [<<" rabbitmq-resource.write:*/*" >>,
567+ <<" rabbitmq-resource-write" >>]},
568+ [<<" read:*/*" >>, <<" rabbitmq.rabbitmq-resource-read" >>]
569+ },
570+ {
571+ " claims are map with list content - empty result" ,
572+ #{<<" rabbitmq2" >> =>
573+ [<<" rabbitmq-resource.read:*/*" >>,
574+ <<" rabbitmq-resource-read" >>]},
575+ []
576+ },
577+ {
578+ " claims are map with binary content" ,
579+ #{ <<" rabbitmq" >> => <<" rabbitmq-resource.read:*/* rabbitmq-resource-read" >>,
580+ <<" rabbitmq3" >> => <<" rabbitmq-resource.write:*/* rabbitmq-resource-write" >>},
581+ [<<" rabbitmq.rabbitmq-resource.read:*/*" >>, <<" rabbitmq.rabbitmq-resource-read" >>]
582+ },
583+ {
584+ " claims are map with binary content - empty result" ,
585+ #{<<" rabbitmq2" >> => <<" rabbitmq-resource.read:*/* rabbitmq-resource-read" >>}, []
586+ },
587+ {
588+ " claims are map with empty binary content - empty result" ,
589+ #{<<" rabbitmq" >> => <<>>}, []
590+ },
591+ {
592+ " claims are map with empty list content - empty result" ,
593+ #{<<" rabbitmq" >> => []}, []
594+ },
595+ {
596+ " no extra claims provided" ,
597+ [], []
598+ },
599+ {
600+ " no extra claims provided" , #{}, []
601+ }],
602+ lists :foreach (fun ({Case , Authorization , ExpectedScope0 }) ->
603+ ResourceServer0 = resource_server :new_resource_server (? RESOURCE_SERVER_ID ),
604+ ResourceServer = ResourceServer0 # resource_server {
605+ scope_prefix = <<" rabbitmq.rabbitmq-resource." >>,
606+ additional_scopes_key = <<" custom-key" >>
600607 },
601- % % no extra claims provided
602- {[], []},
603- % % no extra claims provided
604- {#{}, []}
605- ],
606- lists :foreach (
607- fun ({Authorization , ExpectedScope }) ->
608- Payload = post_process_payload_with_complex_claim_authorization (<<" rabbitmq-resource" >>, Authorization ),
609- ? assertEqual (ExpectedScope , maps :get (<<" scope" >>, Payload ))
610- end , Pairs ).
611-
612- post_process_payload_with_complex_claim_authorization (ResourceServerId , Authorization ) ->
613- Jwk = ? UTIL_MOD :fixture_jwk (),
614- Token = maps :put (<<" additional_rabbitmq_scopes" >>, Authorization , ? UTIL_MOD :fixture_token_with_scopes ([])),
615- {_ , EncodedToken } = ? UTIL_MOD :sign_token_hs (Token , Jwk ),
616- {true , Payload } = uaa_jwt_jwt :decode_and_verify (Jwk , EncodedToken ),
617- rabbit_auth_backend_oauth2 :post_process_payload (ResourceServerId , Payload ).
608+ Token0 = #{<<" custom-key" >> => Authorization },
609+ Token = normalize_token_scope (ResourceServer , Token0 ),
610+ ExpectedScopes = lists :sort (ExpectedScope0 ),
611+ ActualScopes = lists :sort (uaa_jwt :get_scope (Token )),
612+ ? assertEqual (ExpectedScopes , ActualScopes , Case )
613+ end , Pairs ).
618614
619615test_successful_authentication_without_scopes (_ ) ->
620616 Jwk = ? UTIL_MOD :fixture_jwk (),
0 commit comments