Skip to content

Commit 7486f1a

Browse files
committed
scope in refresh token request should be a subset of those authorized by the resource owner.
1 parent b6de1db commit 7486f1a

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

oauth2-framework-impl/oauth2-authorization-server/src/main/java/com/baeldung/oauth2/authorization/server/handler/RefreshTokenGrantTypeHandler.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import javax.json.JsonObject;
99
import javax.ws.rs.WebApplicationException;
1010
import javax.ws.rs.core.MultivaluedMap;
11-
import java.util.*;
11+
import java.util.Arrays;
12+
import java.util.Date;
13+
import java.util.HashSet;
14+
import java.util.Set;
1215

1316
@Named("refresh_token")
1417
public class RefreshTokenGrantTypeHandler extends AbstractGrantTypeHandler {
@@ -40,24 +43,23 @@ public JsonObject createAccessToken(String clientId, MultivaluedMap<String, Stri
4043
String subject = signedRefreshToken.getJWTClaimsSet().getSubject();
4144
String approvedScopes = signedRefreshToken.getJWTClaimsSet().getStringClaim("scope");
4245

43-
String finalScope = approvedScopes;
4446
String requestedScopes = params.getFirst("scope");
4547
if (requestedScopes != null && !requestedScopes.isEmpty()) {
46-
Set<String> allowedScopes = new LinkedHashSet<>();
4748
Set<String> rScopes = new HashSet(Arrays.asList(requestedScopes.split(" ")));
4849
Set<String> aScopes = new HashSet(Arrays.asList(approvedScopes.split(" ")));
49-
for (String scope : rScopes) {
50-
if (aScopes.contains(scope)) allowedScopes.add(scope);
50+
if (!aScopes.containsAll(rScopes)) {
51+
throw new WebApplicationException("Requested scopes should be a subset of those authorized by the resource owner.");
5152
}
52-
finalScope = String.join(" ", allowedScopes);
53+
} else {
54+
requestedScopes = approvedScopes;
5355
}
5456

55-
String accessToken = getAccessToken(clientId, subject, finalScope);
57+
String accessToken = getAccessToken(clientId, subject, requestedScopes);
5658
return Json.createObjectBuilder()
5759
.add("token_type", "Bearer")
5860
.add("access_token", accessToken)
5961
.add("expires_in", expiresInMin * 60)
60-
.add("scope", finalScope)
62+
.add("scope", requestedScopes)
6163
.add("refresh_token", refreshToken)
6264
.build();
6365
}

0 commit comments

Comments
 (0)