use of org.forgerock.oauth2.core.exceptions.InvalidGrantException in project OpenAM by OpenRock.
the class AuthorizationCodeGrantTypeHandler method checkCodeVerifier.
private void checkCodeVerifier(AuthorizationCode authorizationCode, String codeVerifier) throws InvalidGrantException, InvalidRequestException {
final String codeChallenge = authorizationCode.getCodeChallenge();
final String codeChallengeMethod = authorizationCode.getCodeChallengeMethod();
if (OAuth2Constants.Custom.CODE_CHALLENGE_METHOD_PLAIN.equals(codeChallengeMethod)) {
checkCodeChallenge(codeChallenge, codeVerifier);
} else if (OAuth2Constants.Custom.CODE_CHALLENGE_METHOD_S_256.equals(codeChallengeMethod)) {
String encodedCodeVerifier = null;
try {
encodedCodeVerifier = Base64url.encode(MessageDigest.getInstance("SHA-256").digest(codeVerifier.getBytes(StandardCharsets.US_ASCII)));
checkCodeChallenge(codeChallenge, encodedCodeVerifier);
} catch (NoSuchAlgorithmException e) {
logger.error("Error encoding code verifier.");
throw new InvalidGrantException();
}
} else {
throw new InvalidRequestException("Invalid code challenge method specified.");
}
}
use of org.forgerock.oauth2.core.exceptions.InvalidGrantException in project OpenAM by OpenRock.
the class AccessTokenProtectionFilter method beforeHandle.
@Override
protected int beforeHandle(Request request, Response response) {
ChallengeResponse challengeResponse = request.getChallengeResponse();
Status failure = null;
if (challengeResponse == null) {
failure = new Status(401, new InvalidTokenException());
} else {
String tokenId = challengeResponse.getRawValue();
try {
OAuth2Request oAuth2Request = requestFactory.create(request);
AccessToken accessToken = tokenStore.readAccessToken(oAuth2Request, tokenId);
if (accessToken == null || accessToken.isExpired()) {
failure = new Status(401, new InvalidTokenException());
} else if (requiredScope != null && !accessToken.getScope().contains(requiredScope)) {
failure = new Status(403, new InsufficientScopeException(requiredScope));
} else {
oAuth2Request.setToken(AccessToken.class, accessToken);
}
} catch (ServerException e) {
failure = new Status(500, e);
} catch (NotFoundException e) {
debug.message("Error loading token with id: " + tokenId, e);
failure = new Status(404, e);
} catch (InvalidGrantException e) {
debug.message("Error loading token with id: " + tokenId, e);
failure = new Status(401, new InvalidTokenException());
}
}
if (failure != null) {
response.setStatus(failure);
return STOP;
}
return super.beforeHandle(request, response);
}
use of org.forgerock.oauth2.core.exceptions.InvalidGrantException in project OpenAM by OpenRock.
the class OpenAMTokenStoreTest method shouldReadAccessTokenWhenNull.
@Test(expectedExceptions = InvalidGrantException.class)
public void shouldReadAccessTokenWhenNull() throws Exception {
//Given
given(tokenStore.read("TOKEN_ID")).willReturn(null);
OAuth2Request request = oAuth2RequestFactory.create(this.request);
//When
openAMtokenStore.readAccessToken(request, "TOKEN_ID");
//Then
//Expected InvalidGrantException
}
use of org.forgerock.oauth2.core.exceptions.InvalidGrantException in project OpenAM by OpenRock.
the class OAuth2AuditRefreshTokenContextProvider method retrieveRefreshTokenFromChallengeResponse.
private RefreshToken retrieveRefreshTokenFromChallengeResponse(Request request) {
RefreshToken refreshToken;
ChallengeResponse challengeResponse = request.getChallengeResponse();
if (challengeResponse == null) {
return null;
}
String bearerToken = challengeResponse.getRawValue();
if ("undefined".equals(bearerToken)) {
return null;
}
OAuth2Request oAuth2Request = requestFactory.create(request);
try {
refreshToken = tokenStore.readRefreshToken(oAuth2Request, bearerToken);
} catch (ServerException | InvalidGrantException | NotFoundException e) {
return null;
}
return refreshToken;
}
use of org.forgerock.oauth2.core.exceptions.InvalidGrantException in project OpenAM by OpenRock.
the class OAuth2AuditAccessTokenContextProvider method retrieveAccessTokenFromChallengeResponse.
private AccessToken retrieveAccessTokenFromChallengeResponse(Request request) {
AccessToken token;
ChallengeResponse challengeResponse = request.getChallengeResponse();
if (challengeResponse == null) {
return null;
}
String bearerToken = challengeResponse.getRawValue();
if ("undefined".equals(bearerToken)) {
return null;
}
OAuth2Request oAuth2Request = requestFactory.create(request);
try {
token = tokenStore.readAccessToken(oAuth2Request, bearerToken);
} catch (ServerException | InvalidGrantException | NotFoundException e) {
return null;
}
return token;
}
Aggregations