OAuth2Authentication narrowed = authentication;
if (scope != null && !scope.isEmpty()) {
OAuth2Request clientAuth = authentication.getOAuth2Request();
Set<String> originalScope = clientAuth.getScope();
if (originalScope == null || !originalScope.containsAll(scope)) {
throw new InvalidScopeException("Unable to narrow the scope of the client authentication to " + scope
+ ".", originalScope);
}
else {
narrowed = new OAuth2Authentication(clientAuth.narrowScope(scope),
authentication.getUserAuthentication());