Java Code Examples for org.apache.cxf.rs.security.jose.jwk.JsonWebKey#setAlgorithm()

The following examples show how to use org.apache.cxf.rs.security.jose.jwk.JsonWebKey#setAlgorithm() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestJwk.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
private static JsonWebKey createRsa(String keyId, String algorithm, String e, String n, String d) {
	JsonWebKey result = new JsonWebKey();

	result.setKeyId(keyId);
	result.setKeyType(KeyType.RSA);
	result.setAlgorithm(algorithm);
	result.setPublicKeyUse(PublicKeyUse.SIGN);

	if (d != null) {
		result.setProperty("d", d);
	}

	result.setProperty("e", e);
	result.setProperty("n", n);

	return result;
}
 
Example 2
Source File: TestJwk.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
private static JsonWebKey createOct(String keyId, String algorithm, String k) {
	JsonWebKey result = new JsonWebKey();

	result.setKeyId(keyId);
	result.setKeyType(KeyType.OCTET);
	result.setAlgorithm(algorithm);
	result.setPublicKeyUse(PublicKeyUse.SIGN);
	result.setProperty("k", k);

	return result;
}
 
Example 3
Source File: JwtVerifier.java    From deprecated-security-advanced-modules with Apache License 2.0 4 votes vote down vote up
public JwtToken getVerifiedJwtToken(String encodedJwt) throws BadCredentialsException {
	try {
		JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(encodedJwt);
		JwtToken jwt = jwtConsumer.getJwtToken();

		String escapedKid = jwt.getJwsHeaders().getKeyId();
		String kid = escapedKid;
		if (!Strings.isNullOrEmpty(kid) && !kid.isEmpty()) {
			kid = StringEscapeUtils.unescapeJava(escapedKid);
			if (escapedKid != kid) {
				log.info("Escaped Key ID from JWT Token");
			}
		}
		JsonWebKey key = keyProvider.getKey(kid);
		
		// Algorithm is not mandatory for the key material, so we set it to the same as the JWT
		if (key.getAlgorithm() == null && key.getPublicKeyUse() == PublicKeyUse.SIGN && key.getKeyType() == KeyType.RSA)
		{
			key.setAlgorithm(jwt.getJwsHeaders().getAlgorithm());
		}
		
		JwsSignatureVerifier signatureVerifier = getInitializedSignatureVerifier(key, jwt);


		boolean signatureValid = jwtConsumer.verifySignatureWith(signatureVerifier);

		if (!signatureValid && Strings.isNullOrEmpty(kid)) {
			key = keyProvider.getKeyAfterRefresh(null);
			signatureVerifier = getInitializedSignatureVerifier(key, jwt);
			signatureValid = jwtConsumer.verifySignatureWith(signatureVerifier);
		}

		if (!signatureValid) {
			throw new BadCredentialsException("Invalid JWT signature");
		}

		validateClaims(jwt);

		return jwt;
	} catch (JwtException e) {
		throw new BadCredentialsException(e.getMessage(), e);
	}
}