use of com.nimbusds.oauth2.sdk.token.AccessToken in project ddf by codice.
the class CustomOAuthCredentialsExtractorTest method setupClass.
@BeforeClass
public static void setupClass() throws Exception {
authorizationCode = CharStreams.toString(new InputStreamReader(CustomOAuthCredentialsExtractorTest.class.getClassLoader().getResourceAsStream("authorizationCode.txt")));
String accessTokenString = CharStreams.toString(new InputStreamReader(CustomOAuthCredentialsExtractorTest.class.getClassLoader().getResourceAsStream("accessToken.jwt")));
accessToken = new BearerAccessToken(accessTokenString);
authorizationHeader = "Bearer " + accessToken;
}
use of com.nimbusds.oauth2.sdk.token.AccessToken in project ddf by codice.
the class OidcTokenValidator method validateAccessTokenAtHash.
/**
* Validates the at_hash parameter in the ID token against the access token. If implicit flow is
* used with a id_token token response type is used. The at_hash value is required.
*
* @param accessToken - the token to validate
* @param idToken - the corresponding ID token
*/
private static void validateAccessTokenAtHash(AccessToken accessToken, JWT idToken, OidcConfiguration configuration) throws OidcValidationException {
try {
Object atHash = idToken.getJWTClaimsSet().getClaim("at_hash");
if (atHash == null && !IMPLICIT_FLOWS.contains(new ResponseType(configuration.getResponseType()))) {
return;
}
if (atHash == null) {
String errorMessage = "at_hash value not found in response. If the ID Token is issued from the Authorization Endpoint with " + "an access_token value, which is the case for the response_type value id_token token, this is REQUIRED";
LOGGER.error(errorMessage);
throw new OidcValidationException(errorMessage);
}
JWSAlgorithm jwsAlgorithm = new JWSAlgorithm(idToken.getHeader().getAlgorithm().getName());
AccessTokenHash accessTokenHash = new AccessTokenHash((String) atHash);
AccessTokenValidator.validate(accessToken, jwsAlgorithm, accessTokenHash);
} catch (Exception e) {
LOGGER.error(ACCESS_VALIDATION_ERR_MSG, e);
throw new OidcValidationException(ACCESS_VALIDATION_ERR_MSG, e);
}
}
use of com.nimbusds.oauth2.sdk.token.AccessToken in project ddf by codice.
the class OidcCredentialsResolver method trySendingGrantAndPopulatingCredentials.
private void trySendingGrantAndPopulatingCredentials(AuthorizationGrant grant, OidcCredentials credentials, WebContext webContext) throws IOException, ParseException {
final OIDCTokens oidcTokens = getOidcTokens(grant);
try {
JWT idToken = oidcTokens.getIDToken();
if (idToken != null) {
OidcTokenValidator.validateIdTokens(idToken, webContext, configuration, client);
}
AccessToken accessToken = oidcTokens.getAccessToken();
if (accessToken != null) {
OidcTokenValidator.validateAccessToken(accessToken, idToken, resourceRetriever, metadata, configuration);
}
credentials.setAccessToken(accessToken);
credentials.setIdToken(idToken);
credentials.setRefreshToken(oidcTokens.getRefreshToken());
} catch (OidcValidationException e) {
throw new TechnicalException(e);
}
}
use of com.nimbusds.oauth2.sdk.token.AccessToken in project ddf by codice.
the class OidcTokenValidatorTest method testValidateAccessTokenInvalidSignature.
@Test(expected = OidcValidationException.class)
public void testValidateAccessTokenInvalidSignature() throws Exception {
String accessTokenString = getAccessTokenBuilder().sign(invalidAlgorithm);
AccessToken accessToken = new BearerAccessToken(accessTokenString);
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(accessTokenString.getBytes(Charset.forName("US-ASCII")));
byte[] hash = messageDigest.digest();
byte[] firstHalf = Arrays.copyOf(hash, hash.length / 2);
String idToken = getIdTokenBuilder().withClaim("nonce", "myNonce").withClaim("at_hash", Base64URL.encode(firstHalf).toString()).sign(validAlgorithm);
JWT jwt = SignedJWT.parse(idToken);
OidcTokenValidator.validateAccessToken(accessToken, jwt, resourceRetriever, oidcProviderMetadata, configuration);
}
use of com.nimbusds.oauth2.sdk.token.AccessToken in project iaf by ibissource.
the class OAuthAccessTokenManager method parseResponse.
private void parseResponse(HTTPResponse httpResponse, String responseBody) throws HttpAuthenticationException {
try {
TokenResponse response = TokenResponse.parse(httpResponse);
if (!response.indicatesSuccess()) {
// We got an error response...
TokenErrorResponse errorResponse = response.toErrorResponse();
throw new HttpAuthenticationException(errorResponse.toJSONObject().toString());
}
AccessTokenResponse successResponse = response.toSuccessResponse();
// Get the access token
accessToken = successResponse.getTokens().getAccessToken();
// accessToken will be refreshed when it is half way expiration
accessTokenRefreshTime = System.currentTimeMillis() + expiryMs < 0 ? 500 * accessToken.getLifetime() : expiryMs;
} catch (ParseException e) {
throw new HttpAuthenticationException("Could not parse TokenResponse: " + responseBody, e);
}
}
Aggregations