use of com.nimbusds.jwt.JWTClaimsSet in project pac4j by pac4j.
the class AzureAdProfile method isExpired.
@Override
public boolean isExpired() {
try {
JWT jwt = this.getIdToken();
JWTClaimsSet claims = jwt.getJWTClaimsSet();
Date expiresOn = claims.getExpirationTime();
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND, idTokenExpireAdvance);
if (expiresOn.before(now.getTime())) {
return true;
}
} catch (ParseException e) {
throw new TechnicalException(e);
}
return false;
}
use of com.nimbusds.jwt.JWTClaimsSet in project java-docs-samples by GoogleCloudPlatform.
the class BuildIapRequest method getSignedJwt.
private static String getSignedJwt(ServiceAccountCredentials credentials, String iapClientId) throws Exception {
Instant now = Instant.now(clock);
long expirationTime = now.getEpochSecond() + EXPIRATION_TIME_IN_SECONDS;
// generate jwt signed by service account
// header must contain algorithm ("alg") and key ID ("kid")
JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(credentials.getPrivateKeyId()).build();
// set required claims
JWTClaimsSet claims = new JWTClaimsSet.Builder().audience(OAUTH_TOKEN_URI).issuer(credentials.getClientEmail()).subject(credentials.getClientEmail()).issueTime(Date.from(now)).expirationTime(Date.from(Instant.ofEpochSecond(expirationTime))).claim("target_audience", iapClientId).build();
// sign using service account private key
JWSSigner signer = new RSASSASigner(credentials.getPrivateKey());
SignedJWT signedJwt = new SignedJWT(jwsHeader, claims);
signedJwt.sign(signer);
return signedJwt.serialize();
}
use of com.nimbusds.jwt.JWTClaimsSet in project java-docs-samples by GoogleCloudPlatform.
the class VerifyIapRequestHeader method verifyJwt.
private boolean verifyJwt(String jwtToken, String expectedAudience) throws Exception {
// parse signed token into header / claims
SignedJWT signedJwt = SignedJWT.parse(jwtToken);
JWSHeader jwsHeader = signedJwt.getHeader();
// header must have algorithm("alg") and "kid"
Preconditions.checkNotNull(jwsHeader.getAlgorithm());
Preconditions.checkNotNull(jwsHeader.getKeyID());
JWTClaimsSet claims = signedJwt.getJWTClaimsSet();
// claims must have audience, issuer
Preconditions.checkArgument(claims.getAudience().contains(expectedAudience));
Preconditions.checkArgument(claims.getIssuer().equals(IAP_ISSUER_URL));
// claim must have issued at time in the past
Date currentTime = Date.from(Instant.now(clock));
Preconditions.checkArgument(claims.getIssueTime().before(currentTime));
// claim must have expiration time in the future
Preconditions.checkArgument(claims.getExpirationTime().after(currentTime));
// must have subject, email
Preconditions.checkNotNull(claims.getSubject());
Preconditions.checkNotNull(claims.getClaim("email"));
// verify using public key : lookup with key id, algorithm name provided
ECPublicKey publicKey = getKey(jwsHeader.getKeyID(), jwsHeader.getAlgorithm().getName());
Preconditions.checkNotNull(publicKey);
JWSVerifier jwsVerifier = new ECDSAVerifier(publicKey);
return signedJwt.verify(jwsVerifier);
}
use of com.nimbusds.jwt.JWTClaimsSet in project registry by hortonworks.
the class TestJWTAuthenticationHandler method getJWT.
protected SignedJWT getJWT(String sub, Date expires, RSAPrivateKey privateKey) throws Exception {
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().subject(sub).issueTime(new Date(new Date().getTime())).issuer("https://c2id.com").claim("scope", "openid").audience("bar").expirationTime(expires).build();
List<String> aud = new ArrayList<String>();
aud.add("bar");
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).build();
SignedJWT signedJWT = new SignedJWT(header, claimsSet);
JWSSigner signer = new RSASSASigner(privateKey);
signedJWT.sign(signer);
return signedJWT;
}
use of com.nimbusds.jwt.JWTClaimsSet in project knox by apache.
the class JWTToken method getClaims.
/* (non-Javadoc)
* @see org.apache.knox.gateway.services.security.token.impl.JWT#getPayloadToSign()
*/
@Override
public String getClaims() {
String c = null;
JWTClaimsSet claims = null;
try {
claims = (JWTClaimsSet) jwt.getJWTClaimsSet();
c = claims.toJSONObject().toJSONString();
} catch (ParseException e) {
log.unableToParseToken(e);
}
return c;
}
Aggregations