Search in sources :

Example 1 with NotRenewableException

use of io.hops.hopsworks.jwt.exception.NotRenewableException in project hopsworks by logicalclocks.

the class JWTController method autoRenewToken.

/**
 * Renews a jwt if it is renewable, not invalidated, and expired but within the renewal period.
 *
 * @param token
 * @return
 * @throws SigningKeyNotFoundException
 * @throws NotRenewableException
 * @throws InvalidationException
 */
public String autoRenewToken(String token) throws SigningKeyNotFoundException, NotRenewableException, InvalidationException {
    DecodedJWT jwt = verifyTokenForRenewal(token);
    boolean isRenewable = getRenewableClaim(jwt);
    if (!isRenewable) {
        throw new NotRenewableException("Token not renewable.");
    }
    Date currentTime = new Date();
    if (currentTime.before(jwt.getExpiresAt())) {
        throw new NotRenewableException("Token not expired.");
    }
    // Keep the same lifetime of the current token
    long lifetimeMs = jwt.getExpiresAt().getTime() - jwt.getIssuedAt().getTime();
    JsonWebToken _jwt = new JsonWebToken(jwt);
    _jwt.setExpiresAt(new Date(System.currentTimeMillis() + lifetimeMs));
    _jwt.setNotBefore(new Date());
    Map<String, Object> claims = new HashMap<>(3);
    addDefaultClaimsIfMissing(claims, _jwt.isRenewable(), getExpLeewayOrDefault(_jwt.getExpLeeway()), _jwt.getRole().toArray(new String[1]));
    String renewedToken = createToken(_jwt, claims);
    invalidateJWT(jwt.getId(), jwt.getExpiresAt(), _jwt.getExpLeeway());
    return renewedToken;
}
Also used : NotRenewableException(io.hops.hopsworks.jwt.exception.NotRenewableException) HashMap(java.util.HashMap) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Date(java.util.Date)

Example 2 with NotRenewableException

use of io.hops.hopsworks.jwt.exception.NotRenewableException in project hopsworks by logicalclocks.

the class JWTController method renewToken.

/**
 * Creates a new token with the same values as the given token but with newExp and notBefore.
 * @param token Token to renew
 * @param newExp New expiration date
 * @param notBefore New not-valid-before date
 * @param invalidate Flag whether to invalidate the old token or not
 * @param claims Set of claims added to the new token
 * @param force Flag whether to check if it is time to renew or not
 * @return
 * @throws SigningKeyNotFoundException
 * @throws NotRenewableException
 * @throws InvalidationException
 */
public String renewToken(String token, Date newExp, Date notBefore, boolean invalidate, Map<String, Object> claims, boolean force) throws SigningKeyNotFoundException, NotRenewableException, InvalidationException {
    DecodedJWT jwt = verifyTokenForRenewal(token);
    if (!force) {
        Date currentTime = new Date();
        if (currentTime.before(jwt.getExpiresAt())) {
            throw new NotRenewableException("Token not expired.");
        }
    }
    JsonWebToken _jwt = new JsonWebToken(jwt);
    _jwt.setExpiresAt(newExp);
    _jwt.setNotBefore(notBefore);
    claims = addDefaultClaimsIfMissing(claims, _jwt.isRenewable(), getExpLeewayOrDefault(_jwt.getExpLeeway()), _jwt.getRole().toArray(new String[1]));
    String renewedToken = createToken(_jwt, claims);
    if (invalidate) {
        invalidateJWT(jwt.getId(), jwt.getExpiresAt(), _jwt.getExpLeeway());
    }
    return renewedToken;
}
Also used : NotRenewableException(io.hops.hopsworks.jwt.exception.NotRenewableException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Date(java.util.Date)

Aggregations

DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)2 NotRenewableException (io.hops.hopsworks.jwt.exception.NotRenewableException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)1