Search in sources :

Example 6 with NumericDate

use of org.jose4j.jwt.NumericDate in project box-java-sdk by box.

the class BoxDeveloperEditionAPIConnection method getDateForJWTConstruction.

private NumericDate getDateForJWTConstruction(BoxAPIException apiException, long secondsSinceResponseDateReceived) {
    NumericDate currentTime;
    List<String> responseDates = apiException.getHeaders().get("Date");
    if (responseDates != null) {
        String responseDate = responseDates.get(0);
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");
        try {
            Date date = dateFormat.parse(responseDate);
            currentTime = NumericDate.fromMilliseconds(date.getTime());
            currentTime.addSeconds(secondsSinceResponseDateReceived);
        } catch (ParseException e) {
            currentTime = NumericDate.now();
        }
    } else {
        currentTime = NumericDate.now();
    }
    return currentTime;
}
Also used : NumericDate(org.jose4j.jwt.NumericDate) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) NumericDate(org.jose4j.jwt.NumericDate)

Example 7 with NumericDate

use of org.jose4j.jwt.NumericDate in project kylo by Teradata.

the class JwtRememberMeServices method encodeCookie.

/**
 * Encodes the specified tokens into a JWT cookie.
 *
 * <p>The first element of {@code tokens} should be the user's principal. The remaining elements are the groups assigned to the user.</p>
 *
 * @param tokens an array with the username and group names
 * @return a JWT cookie
 * @throws IllegalStateException if the secret key is invalid
 */
@Nonnull
@Override
protected String encodeCookie(@Nonnull final String[] tokens) {
    // Determine expiration time
    final NumericDate expireTime = NumericDate.fromMilliseconds(DateTimeUtils.currentTimeMillis());
    expireTime.addSeconds(getExpirationTimeSeconds());
    // Build the JSON Web Token
    final JwtClaims claims = new JwtClaims();
    claims.setExpirationTime(expireTime);
    claims.setSubject(tokens[0]);
    claims.setStringListClaim(PRINCIPALS, Arrays.asList(tokens).subList(1, tokens.length));
    // Generate a signature
    final JsonWebSignature jws = new JsonWebSignature();
    jws.setAlgorithmHeaderValue(algorithmIdentifier);
    jws.setKey(getSecretKey());
    jws.setKeyIdHeaderValue(getSecretKey().getAlgorithm());
    jws.setPayload(claims.toJson());
    // Serialize the cookie
    try {
        return jws.getCompactSerialization();
    } catch (final JoseException e) {
        log.error("Unable to encode cookie: ", e);
        throw new IllegalStateException("Unable to encode cookie: ", e);
    }
}
Also used : NumericDate(org.jose4j.jwt.NumericDate) JsonWebSignature(org.jose4j.jws.JsonWebSignature) JwtClaims(org.jose4j.jwt.JwtClaims) JoseException(org.jose4j.lang.JoseException) Nonnull(javax.annotation.Nonnull)

Aggregations

NumericDate (org.jose4j.jwt.NumericDate)7 JwtClaims (org.jose4j.jwt.JwtClaims)5 JoseException (org.jose4j.lang.JoseException)4 InvalidJwtException (org.jose4j.jwt.consumer.InvalidJwtException)3 JwtContext (org.jose4j.jwt.consumer.JwtContext)3 ServiceException (io.jenkins.blueocean.commons.ServiceException)2 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 Collection (java.util.Collection)2 MalformedClaimException (org.jose4j.jwt.MalformedClaimException)2 JwtConsumer (org.jose4j.jwt.consumer.JwtConsumer)2 JwtConsumerBuilder (org.jose4j.jwt.consumer.JwtConsumerBuilder)2 JsonWebStructure (org.jose4j.jwx.JsonWebStructure)2 JsonObject (com.eclipsesource.json.JsonObject)1 Preconditions (com.google.common.base.Preconditions)1 JwtAuthenticationStore (io.jenkins.blueocean.auth.jwt.JwtAuthenticationStore)1 JwtToken (io.jenkins.blueocean.auth.jwt.JwtToken)1 SigningPublicKey (io.jenkins.blueocean.auth.jwt.SigningPublicKey)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1