Skip to content

Commit b6b90f5

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

File tree

1 file changed

+24
-18
lines changed
  • oauth2-framework-impl/oauth2-authorization-server/src/main/java/com/baeldung/oauth2/authorization/server/api

1 file changed

+24
-18
lines changed

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Arrays;
1919
import java.util.Base64;
2020
import java.util.List;
21-
import java.util.Objects;
2221

2322
@Path("token")
2423
public class TokenEndpoint {
@@ -39,36 +38,34 @@ public Response token(MultivaluedMap<String, String> params,
3938

4039
//Check grant_type params
4140
String grantType = params.getFirst("grant_type");
42-
Objects.requireNonNull(grantType, "grant_type params is required");
43-
if (!supportedGrantTypes.contains(grantType)) {
44-
JsonObject error = Json.createObjectBuilder()
45-
.add("error", "unsupported_grant_type")
46-
.add("error_description", "grant type should be one of :" + supportedGrantTypes)
47-
.build();
48-
return Response.status(Response.Status.BAD_REQUEST)
49-
.entity(error).build();
41+
if (grantType == null || grantType.isEmpty())
42+
return responseError("Invalid_request", "grant_type is required", Response.Status.BAD_REQUEST);
5043

44+
if (!supportedGrantTypes.contains(grantType)) {
45+
return responseError("unsupported_grant_type", "grant_type should be one of :" + supportedGrantTypes, Response.Status.BAD_REQUEST);
5146
}
5247

5348
//Client Authentication
5449
String[] clientCredentials = extract(authHeader);
50+
if (clientCredentials.length != 2) {
51+
return responseError("Invalid_request", "Bad Credentials client_id/client_secret", Response.Status.BAD_REQUEST);
52+
}
5553
String clientId = clientCredentials[0];
56-
String clientSecret = clientCredentials[1];
5754
Client client = appDataRepository.getClient(clientId);
58-
if (client == null || clientSecret == null || !clientSecret.equals(client.getClientSecret())) {
59-
JsonObject error = Json.createObjectBuilder()
60-
.add("error", "invalid_client")
61-
.build();
62-
return Response.status(Response.Status.UNAUTHORIZED)
63-
.entity(error).build();
55+
if (client == null) {
56+
return responseError("Invalid_request", "Invalid client_id", Response.Status.BAD_REQUEST);
57+
}
58+
String clientSecret = clientCredentials[1];
59+
if (!clientSecret.equals(client.getClientSecret())) {
60+
return responseError("Invalid_request", "Invalid client_secret", Response.Status.UNAUTHORIZED);
6461
}
6562

6663
AuthorizationGrantTypeHandler authorizationGrantTypeHandler = authorizationGrantTypeHandlers.select(NamedLiteral.of(grantType)).get();
6764
JsonObject tokenResponse = null;
6865
try {
6966
tokenResponse = authorizationGrantTypeHandler.createAccessToken(clientId, params);
7067
} catch (Exception e) {
71-
e.printStackTrace();
68+
return responseError("Invalid_request", "Can't get token", Response.Status.INTERNAL_SERVER_ERROR);
7269
}
7370

7471
return Response.ok(tokenResponse)
@@ -81,6 +78,15 @@ private String[] extract(String authHeader) {
8178
if (authHeader != null && authHeader.startsWith("Basic ")) {
8279
return new String(Base64.getDecoder().decode(authHeader.substring(6))).split(":");
8380
}
84-
return null;
81+
return new String[]{};
82+
}
83+
84+
private Response responseError(String error, String errorDescription, Response.Status status) {
85+
JsonObject errorResponse = Json.createObjectBuilder()
86+
.add("error", error)
87+
.add("error_description", errorDescription)
88+
.build();
89+
return Response.status(status)
90+
.entity(errorResponse).build();
8591
}
8692
}

0 commit comments

Comments
 (0)