* @param scope The scope for the refreshed token.
* @return The refreshed authentication.
* @throws InvalidScopeException If the scope requested is invalid or wider than the original scope.
*/
private OAuth2Authentication createRefreshedAuthentication(OAuth2Authentication authentication, Set<String> scope) {
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());
}
}
return narrowed;
}