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;
}
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);
}
}
Aggregations