Skip to content

Commit 4c6182d

Browse files
EZZEDDINE.ELHAZATIEZZEDDINE.ELHAZATI
authored andcommitted
refactoring.
1 parent b6b90f5 commit 4c6182d

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

oauth2-framework-impl/oauth2-authorization-server/src/main/java/com/baeldung/oauth2/authorization/server/api/TokenEndpoint.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public Response token(MultivaluedMap<String, String> params,
6464
JsonObject tokenResponse = null;
6565
try {
6666
tokenResponse = authorizationGrantTypeHandler.createAccessToken(clientId, params);
67+
} catch (WebApplicationException e) {
68+
return e.getResponse();
6769
} catch (Exception e) {
6870
return responseError("Invalid_request", "Can't get token", Response.Status.INTERNAL_SERVER_ERROR);
6971
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.json.JsonObject;
99
import javax.ws.rs.WebApplicationException;
1010
import javax.ws.rs.core.MultivaluedMap;
11+
import javax.ws.rs.core.Response;
1112
import java.util.Arrays;
1213
import java.util.Date;
1314
import java.util.HashSet;
@@ -48,7 +49,12 @@ public JsonObject createAccessToken(String clientId, MultivaluedMap<String, Stri
4849
Set<String> rScopes = new HashSet(Arrays.asList(requestedScopes.split(" ")));
4950
Set<String> aScopes = new HashSet(Arrays.asList(approvedScopes.split(" ")));
5051
if (!aScopes.containsAll(rScopes)) {
51-
throw new WebApplicationException("Requested scopes should be a subset of those authorized by the resource owner.");
52+
JsonObject error = Json.createObjectBuilder()
53+
.add("error", "Invalid_request")
54+
.add("error_description", "Requested scopes should be a subset of the original scopes.")
55+
.build();
56+
Response response = Response.status(Response.Status.BAD_REQUEST).entity(error).build();
57+
throw new WebApplicationException(response);
5258
}
5359
} else {
5460
requestedScopes = approvedScopes;

oauth2-framework-impl/oauth2-client/src/main/java/com/baeldung/oauth2/client/RefreshTokenServlet.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import javax.ws.rs.core.Form;
1616
import javax.ws.rs.core.HttpHeaders;
1717
import javax.ws.rs.core.MediaType;
18+
import javax.ws.rs.core.Response;
1819
import java.io.IOException;
1920

2021
@WebServlet(urlPatterns = "/refreshtoken")
@@ -42,16 +43,13 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
4243
form.param("scope", scope);
4344
}
4445

45-
JsonObject tokenResponse = target.request(MediaType.APPLICATION_JSON_TYPE)
46+
Response jaxrsResponse = target.request(MediaType.APPLICATION_JSON_TYPE)
4647
.header(HttpHeaders.AUTHORIZATION, getAuthorizationHeaderValue(clientId, clientSecret))
47-
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), JsonObject.class);
48+
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), Response.class);
49+
JsonObject tokenResponse = jaxrsResponse.readEntity(JsonObject.class);
50+
System.out.println(tokenResponse);
4851

49-
String error = tokenResponse.getString("error");
50-
if (error != null) {
51-
request.setAttribute("error", error);
52-
} else {
53-
request.getSession().setAttribute("tokenResponse", tokenResponse);
54-
}
52+
request.getSession().setAttribute("tokenResponse", tokenResponse);
5553
dispatch("/", request, response);
5654
}
5755
}

0 commit comments

Comments
 (0)