Search in sources :

Example 1 with JwsAlgorithm

use of org.forgerock.json.jose.jws.JwsAlgorithm in project OpenAM by OpenRock.

the class OpenIdConnectTokenGenerationImpl method generate.

@Override
public String generate(SSOToken subjectToken, STSInstanceState stsInstanceState, TokenGenerationServiceInvocationState invocationState) throws TokenCreationException {
    final OpenIdConnectTokenConfig tokenConfig = stsInstanceState.getConfig().getOpenIdConnectTokenConfig();
    final long issueInstant = System.currentTimeMillis();
    final String subject = ssoTokenIdentity.validateAndGetTokenPrincipal(subjectToken);
    STSOpenIdConnectToken openIdConnectToken = buildToken(subjectToken, tokenConfig, invocationState.getOpenIdConnectTokenGenerationState(), issueInstant / 1000, subject);
    final JwsAlgorithm jwsAlgorithm = tokenConfig.getSignatureAlgorithm();
    final JwsAlgorithmType jwsAlgorithmType = jwsAlgorithm.getAlgorithmType();
    String tokenString;
    if (JwsAlgorithmType.HMAC.equals(jwsAlgorithmType)) {
        final SignedJwt signedJwt = symmetricSign(openIdConnectToken, jwsAlgorithm, tokenConfig.getClientSecret());
        tokenString = signedJwt.build();
    } else if (JwsAlgorithmType.RSA.equals(jwsAlgorithmType)) {
        final SignedJwt signedJwt = asymmetricSign(openIdConnectToken, jwsAlgorithm, getKeyPair(stsInstanceState.getOpenIdConnectTokenPKIProvider(), tokenConfig.getSignatureKeyAlias(), tokenConfig.getSignatureKeyPassword()), determinePublicKeyReferenceType(tokenConfig));
        tokenString = signedJwt.build();
    } else {
        throw new TokenCreationException(ResourceException.BAD_REQUEST, "Unknown JwsAlgorithmType: " + jwsAlgorithmType);
    }
    if (stsInstanceState.getConfig().persistIssuedTokensInCTS()) {
        try {
            ctsTokenPersistence.persistToken(invocationState.getStsInstanceId(), TokenType.OPENIDCONNECT, tokenString, subject, issueInstant, tokenConfig.getTokenLifetimeInSeconds());
        } catch (CTSTokenPersistenceException e) {
            throw new TokenCreationException(e.getCode(), e.getMessage(), e);
        }
    }
    return tokenString;
}
Also used : JwsAlgorithm(org.forgerock.json.jose.jws.JwsAlgorithm) JwsAlgorithmType(org.forgerock.json.jose.jws.JwsAlgorithmType) SignedJwt(org.forgerock.json.jose.jws.SignedJwt) TokenCreationException(org.forgerock.openam.sts.TokenCreationException) CTSTokenPersistenceException(org.forgerock.openam.sts.CTSTokenPersistenceException) OpenIdConnectTokenConfig(org.forgerock.openam.sts.config.user.OpenIdConnectTokenConfig)

Example 2 with JwsAlgorithm

use of org.forgerock.json.jose.jws.JwsAlgorithm in project OpenAM by OpenRock.

the class IdTokenClaimGatherer method verify.

private boolean verify(byte[] clientSecret, KeyPair keyPair, SignedJwt signedJwt) {
    JwsAlgorithm jwsAlgorithm = signedJwt.getHeader().getAlgorithm();
    SigningHandler signingHandler;
    if (JwsAlgorithmType.RSA.equals(jwsAlgorithm.getAlgorithmType())) {
        signingHandler = signingManager.newRsaSigningHandler(keyPair.getPublic());
    } else {
        signingHandler = signingManager.newHmacSigningHandler(clientSecret);
    }
    return signedJwt.verify(signingHandler);
}
Also used : JwsAlgorithm(org.forgerock.json.jose.jws.JwsAlgorithm) SigningHandler(org.forgerock.json.jose.jws.handlers.SigningHandler)

Example 3 with JwsAlgorithm

use of org.forgerock.json.jose.jws.JwsAlgorithm in project OpenAM by OpenRock.

the class OpenIdConnectToken method sign.

/**
     * Signs the OpenId Connect token.
     *
     * @return A SignedJwt
     * @throws SignatureException If an error occurs with the signing of the OpenId Connect token.
     */
public SignedJwt sign() throws SignatureException {
    final JwsAlgorithm jwsAlgorithm = JwsAlgorithm.valueOf(algorithm);
    if (jwsAlgorithm == null) {
        logger.error("Unable to find jws algorithm for: " + algorithm);
        throw new SignatureException();
    }
    final SigningHandler signingHandler;
    if (JwsAlgorithmType.RSA.equals(jwsAlgorithm.getAlgorithmType())) {
        signingHandler = new SigningManager().newRsaSigningHandler(keyPair.getPrivate());
    } else {
        signingHandler = new SigningManager().newHmacSigningHandler(clientSecret);
    }
    JwsHeaderBuilder builder = jwtBuilderFactory.jws(signingHandler).headers().alg(jwsAlgorithm);
    JwtClaimsSet claimsSet = jwtBuilderFactory.claims().claims(asMap()).build();
    if (kid != null) {
        builder.kid(kid);
    }
    return builder.done().claims(claimsSet).asJwt();
}
Also used : JwsAlgorithm(org.forgerock.json.jose.jws.JwsAlgorithm) JwtClaimsSet(org.forgerock.json.jose.jwt.JwtClaimsSet) SignatureException(java.security.SignatureException) JwsHeaderBuilder(org.forgerock.json.jose.builders.JwsHeaderBuilder) SigningHandler(org.forgerock.json.jose.jws.handlers.SigningHandler) SigningManager(org.forgerock.json.jose.jws.SigningManager)

Example 4 with JwsAlgorithm

use of org.forgerock.json.jose.jws.JwsAlgorithm in project OpenAM by OpenRock.

the class OpenAMTokenStore method generateHash.

/**
     * Generates hash values, by hashing the valueToEncode using the requests's "alg"
     * parameter, then returning the base64url encoding of the
     * leftmost half of the returned bytes. Used for both at_hash and c_hash claims.
     */
private String generateHash(String algorithm, String valueToEncode, OAuth2ProviderSettings providerSettings) throws ServerException {
    if (!providerSettings.getSupportedIDTokenSigningAlgorithms().contains(algorithm)) {
        logger.message("Unsupported signing algorithm requested for hash value.");
        return null;
    }
    final JwsAlgorithm alg = JwsAlgorithm.valueOf(algorithm);
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance(alg.getMdAlgorithm());
    } catch (NoSuchAlgorithmException e) {
        logger.message("Unsupported signing algorithm chosen for hashing.");
        throw new ServerException("Algorithm not supported.");
    }
    final byte[] result = digest.digest(valueToEncode.getBytes(Utils.CHARSET));
    final byte[] toEncode = Arrays.copyOfRange(result, 0, result.length / 2);
    return Base64url.encode(toEncode);
}
Also used : JwsAlgorithm(org.forgerock.json.jose.jws.JwsAlgorithm) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

JwsAlgorithm (org.forgerock.json.jose.jws.JwsAlgorithm)4 SigningHandler (org.forgerock.json.jose.jws.handlers.SigningHandler)2 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SignatureException (java.security.SignatureException)1 JwsHeaderBuilder (org.forgerock.json.jose.builders.JwsHeaderBuilder)1 JwsAlgorithmType (org.forgerock.json.jose.jws.JwsAlgorithmType)1 SignedJwt (org.forgerock.json.jose.jws.SignedJwt)1 SigningManager (org.forgerock.json.jose.jws.SigningManager)1 JwtClaimsSet (org.forgerock.json.jose.jwt.JwtClaimsSet)1 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)1 CTSTokenPersistenceException (org.forgerock.openam.sts.CTSTokenPersistenceException)1 TokenCreationException (org.forgerock.openam.sts.TokenCreationException)1 OpenIdConnectTokenConfig (org.forgerock.openam.sts.config.user.OpenIdConnectTokenConfig)1