use of com.nimbusds.jose.JWSVerifier in project scoold by Erudika.
the class ScooldUtils method isValidJWToken.
boolean isValidJWToken(String secret, String jwt) {
try {
if (secret != null && jwt != null) {
JWSVerifier verifier = new MACVerifier(secret);
SignedJWT sjwt = SignedJWT.parse(jwt);
if (sjwt.verify(verifier)) {
Date referenceTime = new Date();
JWTClaimsSet claims = sjwt.getJWTClaimsSet();
Date expirationTime = claims.getExpirationTime();
Date notBeforeTime = claims.getNotBeforeTime();
String jti = claims.getJWTID();
boolean expired = expirationTime != null && expirationTime.before(referenceTime);
boolean notYetValid = notBeforeTime != null && notBeforeTime.after(referenceTime);
boolean jtiRevoked = isApiKeyRevoked(jti, expired);
return !(expired || notYetValid || jtiRevoked);
}
}
} catch (JOSEException e) {
logger.warn(null, e);
} catch (ParseException ex) {
logger.warn(null, ex);
}
return false;
}
use of com.nimbusds.jose.JWSVerifier in project ORCID-Source by ORCID.
the class OpenIDConnectTest method checkJWT.
private SignedJWT checkJWT(String id) throws ParseException, JOSEException, InvalidHashException {
SignedJWT signedJWT = SignedJWT.parse(id);
Assert.assertEquals("https://orcid.org", signedJWT.getJWTClaimsSet().getIssuer());
Assert.assertEquals("https://orcid.org/9999-0000-0000-0004", signedJWT.getJWTClaimsSet().getSubject());
Assert.assertEquals("9999-0000-0000-0004", signedJWT.getJWTClaimsSet().getClaim("id_path"));
Assert.assertEquals("APP-9999999999999901", signedJWT.getJWTClaimsSet().getAudience().get(0));
Assert.assertEquals("yesMate", signedJWT.getJWTClaimsSet().getClaim("nonce"));
Assert.assertEquals("User One Credit name", signedJWT.getJWTClaimsSet().getClaim("name"));
Assert.assertEquals("One", signedJWT.getJWTClaimsSet().getClaim("family_name"));
Assert.assertEquals("User", signedJWT.getJWTClaimsSet().getClaim("given_name"));
// get JWKS
Client client = Client.create();
WebResource webResource = client.resource(baseUri + "/oauth/jwks");
ClientResponse jwksResponse = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
String jwkString = jwksResponse.getEntity(String.class);
RSAKey jwk = (RSAKey) JWKSet.parse(jwkString).getKeyByKeyId(signedJWT.getHeader().getKeyID());
// check sig
JWSVerifier verifier = new RSASSAVerifier(jwk);
Assert.assertTrue(signedJWT.verify(verifier));
return signedJWT;
}
use of com.nimbusds.jose.JWSVerifier in project ORCID-Source by ORCID.
the class OpenIDConnectKeyServiceTest method testKeyGenAndSigning.
@Test
public void testKeyGenAndSigning() throws JOSEException, NoSuchAlgorithmException, IOException, ParseException, URISyntaxException {
OpenIDConnectKeyService.OpenIDConnectKeyServiceConfig config = new OpenIDConnectKeyServiceConfig();
config.keyName = "IntTestKey1";
config.jsonKey = testKey;
OpenIDConnectKeyService service = new OpenIDConnectKeyService(config);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("test", "abcd1234");
JWTClaimsSet claims = new JWTClaimsSet.Builder().issuer("me").build();
SignedJWT signed = service.sign(claims);
JWSVerifier verifier = new RSASSAVerifier(((RSAKey) service.getPublicJWK().getKeyByKeyId(signed.getHeader().getKeyID())));
Assert.assertTrue(signed.verify(verifier));
}
use of com.nimbusds.jose.JWSVerifier in project knox by apache.
the class JWTTokenTest method testTokenSignature.
@Test
public void testTokenSignature() throws Exception {
String[] claims = new String[6];
claims[0] = "KNOXSSO";
claims[1] = "john.doe@example.com";
claims[2] = "https://login.example.com";
claims[3] = Long.toString((System.currentTimeMillis() / 1000) + 300);
claims[4] = "E0LDZulQ0XE_otJ5aoQtQu-RnXv8hU-M9U4dD7vDioA";
claims[5] = null;
JWT token = new JWTToken("RS256", claims);
assertEquals("KNOXSSO", token.getIssuer());
assertEquals("john.doe@example.com", token.getSubject());
assertEquals("https://login.example.com", token.getAudience());
// Sign the token
JWSSigner signer = new RSASSASigner(privateKey);
token.sign(signer);
assertTrue(token.getSignaturePayload().length > 0);
// Verify the signature
JWSVerifier verifier = new RSASSAVerifier(publicKey);
assertTrue(token.verify(verifier));
}
use of com.nimbusds.jose.JWSVerifier in project knox by apache.
the class DefaultTokenAuthorityService method verifyTokenUsingRSA.
private boolean verifyTokenUsingRSA(JWT token, RSAPublicKey publicKey) throws TokenServiceException {
try {
PublicKey key = publicKey;
if (key == null) {
key = keystoreService.getSigningKeystore().getCertificate(getSigningKeyAlias()).getPublicKey();
}
final JWSVerifier verifier = new RSASSAVerifier((RSAPublicKey) key);
// consider jwk for specifying the key too
return token.verify(verifier);
} catch (KeyStoreException | KeystoreServiceException e) {
throw new TokenServiceException("Cannot verify token.", e);
}
}
Aggregations