@@ -166,10 +166,36 @@ protected OAuth2AccessToken getAccessToken(ClientDetails client, TokenRequest to
166166
167167 Set <String > ticketScopes = ticket .getPermission ().getScopes ();
168168 Set <String > policyScopes = result .getMatched ().getScopes ();
169+ Set <String > requestScopes = tokenRequest .getScope ();
170+ Set <String > clientScopes = clientEntity .getScope ();
169171
172+ Set <String > permissionScopes = new HashSet <>();
173+
174+ // start with the scopes the client requested
175+ permissionScopes .addAll (requestScopes );
176+
177+ if (permissionScopes .isEmpty ()) {
178+ // if none were requested by the client, see if the ticket has any
179+ permissionScopes .addAll (ticketScopes );
180+ }
181+
182+ if (permissionScopes .isEmpty ()) {
183+ // if still none are requested, go with what the client is registered for by default
184+ permissionScopes .addAll (clientScopes );
185+ }
186+
187+ if (permissionScopes .isEmpty ()) {
188+ // if still none are requested, just go with the matched policy set
189+ permissionScopes .addAll (policyScopes );
190+ } else {
191+ // if there were some requested scopes, make sure the final result contains only the subset given by the fulfilled policy
192+ permissionScopes .retainAll (policyScopes );
193+ }
194+
195+
170196 Permission perm = new Permission ();
171197 perm .setResourceSet (ticket .getPermission ().getResourceSet ());
172- perm .setScopes (new HashSet <>( Sets . intersection ( ticketScopes , policyScopes )) );
198+ perm .setScopes (permissionScopes );
173199
174200 token .setPermissions (Sets .newHashSet (perm ));
175201
0 commit comments