Search in sources :

Example 1 with JwtRuntimeException

use of org.forgerock.json.jose.exceptions.JwtRuntimeException in project OpenAM by OpenRock.

the class AuthIdHelper method verifyAuthId.

/**
     * Verifies the signature of the JWT, to ensure the JWT is valid.
     *
     * @param realmDN The DN for the realm being authenticated against.
     * @param authId The authentication id JWT.
     */
public void verifyAuthId(String realmDN, String authId) throws RestAuthException {
    SecretKey key = getSigningKey(realmDN);
    try {
        final SigningHandler signingHandler = signingManager.newHmacSigningHandler(key.getEncoded());
        boolean verified = jwtBuilderFactory.reconstruct(authId, SignedJwt.class).verify(signingHandler);
        if (!verified) {
            throw new RestAuthException(ResourceException.BAD_REQUEST, "AuthId JWT Signature not valid");
        }
    } catch (JwtRuntimeException e) {
        throw new RestAuthException(ResourceException.BAD_REQUEST, "Failed to parse JWT, " + e.getLocalizedMessage(), e);
    }
}
Also used : RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) SecretKey(javax.crypto.SecretKey) JwtRuntimeException(org.forgerock.json.jose.exceptions.JwtRuntimeException) SignedJwt(org.forgerock.json.jose.jws.SignedJwt) SigningHandler(org.forgerock.json.jose.jws.handlers.SigningHandler)

Example 2 with JwtRuntimeException

use of org.forgerock.json.jose.exceptions.JwtRuntimeException in project OpenAM by OpenRock.

the class StatelessSessionFactory method getSessionInfo.

/**
     * Will create the SessionInfo from the JWT contained within the
     * SessionID.
     *
     * Side Effect: Will cache the generated JWT and SessionInfo combination.
     *
     * @param sessionID Maybe null SessionID.
     *
     * @return SessionInfo Non null SessionInfo which corresponds to the SessionID.
     *
     * @throws SessionException If there was any problem with getting the SessionInfo
     * from the JWT within with SessionID
     */
public SessionInfo getSessionInfo(SessionID sessionID) throws SessionException {
    String jwt = getJWTFromSessionID(sessionID, true);
    if (cache.contains(jwt)) {
        return cache.getSessionInfo(jwt);
    }
    SessionInfo sessionInfo;
    try {
        sessionInfo = getJwtSessionMapper().fromJwt(jwt);
    } catch (JwtRuntimeException e) {
        throw new SessionException(e);
    }
    cache.cache(sessionInfo, jwt);
    return sessionInfo;
}
Also used : JwtRuntimeException(org.forgerock.json.jose.exceptions.JwtRuntimeException) SessionInfo(com.iplanet.dpro.session.share.SessionInfo) SessionException(com.iplanet.dpro.session.SessionException)

Example 3 with JwtRuntimeException

use of org.forgerock.json.jose.exceptions.JwtRuntimeException in project OpenAM by OpenRock.

the class JwtSessionMapper method fromJwt.

/**
     * Extract the SessionInfo stored in the provided JWT's serialized_session claim.
     *
     * @param jwtString Non-null, String which represents a JWT with SessionInfo state assigned to a serialized_session claim.
     *
     * @return SessionInfo A correctly parsed SessionInfo for the given JWT String.
     *
     * @throws JwtRuntimeException If there was a problem reconstructing the JWT
     */
public SessionInfo fromJwt(@Nonnull String jwtString) throws JwtRuntimeException {
    Reject.ifNull(jwtString, "jwtString must not be null.");
    SignedJwt signedJwt;
    if (encryptionKeyPair != null) {
        // could throw JwtRuntimeException
        SignedEncryptedJwt signedEncryptedJwt = jwtBuilderFactory.reconstruct(jwtString, SignedEncryptedJwt.class);
        signedEncryptedJwt.decrypt(encryptionKeyPair.getPrivate());
        signedJwt = signedEncryptedJwt;
    } else {
        // could throw JwtRuntimeException
        signedJwt = jwtBuilderFactory.reconstruct(jwtString, SignedJwt.class);
    }
    if (!doesJwtAlgorithmMatch(signedJwt) || !signedJwt.verify(verificationHandler)) {
        throw new JwtRuntimeException("Invalid JWT!");
    }
    JwtClaimsSet claimsSet = signedJwt.getClaimsSet();
    String serializedSession = claimsSet.getClaim(SERIALIZED_SESSION_CLAIM, String.class);
    return fromJson(serializedSession);
}
Also used : SignedEncryptedJwt(org.forgerock.json.jose.jws.SignedEncryptedJwt) JwtClaimsSet(org.forgerock.json.jose.jwt.JwtClaimsSet) JwtRuntimeException(org.forgerock.json.jose.exceptions.JwtRuntimeException) SignedJwt(org.forgerock.json.jose.jws.SignedJwt)

Aggregations

JwtRuntimeException (org.forgerock.json.jose.exceptions.JwtRuntimeException)3 SignedJwt (org.forgerock.json.jose.jws.SignedJwt)2 SessionException (com.iplanet.dpro.session.SessionException)1 SessionInfo (com.iplanet.dpro.session.share.SessionInfo)1 SecretKey (javax.crypto.SecretKey)1 SignedEncryptedJwt (org.forgerock.json.jose.jws.SignedEncryptedJwt)1 SigningHandler (org.forgerock.json.jose.jws.handlers.SigningHandler)1 JwtClaimsSet (org.forgerock.json.jose.jwt.JwtClaimsSet)1 RestAuthException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthException)1