Java Code Examples for com.nimbusds.jose.jwk.RSAKey#Builder

The following examples show how to use com.nimbusds.jose.jwk.RSAKey#Builder . 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: JWKSResponseBuilder.java    From cellery-security with Apache License 2.0 6 votes vote down vote up
/**
 * Builds the JSON response of JWKS.
 *
 * @param publicKey   Public Key which should be included in the jwks response.
 * @param certificate Certificate which should be in the jwks response.
 * @return JSON JWKS response.
 * @throws CertificateException
 * @throws NoSuchAlgorithmException
 * @throws ParseException
 */
public static String buildResponse(PublicKey publicKey, Certificate certificate) throws CertificateException,
        NoSuchAlgorithmException, ParseException {

    JSONArray jwksArray = new JSONArray();
    JSONObject jwksJson = new JSONObject();

    if (publicKey instanceof RSAPublicKey) {
        RSAKey.Builder jwk = new RSAKey.Builder((RSAPublicKey) publicKey);
        jwk.keyID(CertificateUtils.getThumbPrint(certificate));
        jwk.algorithm(JWSAlgorithm.RS256);
        jwk.keyUse(KeyUse.parse("sig"));
        jwksArray.put(jwk.build().toJSONObject());
        jwksJson.put("keys", jwksArray);
        log.debug(jwksJson.toString());
    }
    return jwksJson.toString();
}
 
Example 2
Source File: JWTTokenGenerator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Builds RSAKey with generated key pair
 *
 * @param jwtTokenDTO      JWT Token info object
 * @param keyPairGenerator keyPairGenerator
 * @return RSAKey built RSA Key which can be used to sign
 */
private RSAKey generateRSAKey(JWTTokenInfoDTO jwtTokenDTO, KeyPairGenerator keyPairGenerator) {

    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    jwtTokenDTO.setGeneratedKeyPair(keyPair);
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAKey.Builder builder = new RSAKey.Builder(publicKey)
            .privateKey(privateKey);
    RSAKey rsaKey = builder.keyID(jwtTokenDTO.getToken()).build();
    jwtTokenDTO.setRsaKey(rsaKey);
    return rsaKey;
}
 
Example 3
Source File: JWTTokenGenerator.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Builds RSAKey using key store.
 *
 * @param jwtTokenDTO      token info object
 * @param keyPairGenerator key pair generator
 * @return RSAKey built RSA Key which can be used to sign
 * @throws Exception
 */
private RSAKey generateRSAKeyWithKeyStore(JWTTokenInfoDTO jwtTokenDTO, KeyPairGenerator keyPairGenerator) throws Exception {

    KeyStore keystore = KeyStoreManager.getInstance(AppDeployerUtils.getTenantId()).getPrimaryKeyStore();
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAKey.Builder builder = new RSAKey.Builder(publicKey)
            .privateKey(privateKey).keyStore(keystore);
    return builder.keyID(jwtTokenDTO.getToken()).build();
}
 
Example 4
Source File: JWKSourceDataProviderTest.java    From cellery-security with Apache License 2.0 5 votes vote down vote up
private RSAKey getRSAKey(RSAPublicKey publicKey, Certificate certificate) throws
        CertificateEncodingException, NoSuchAlgorithmException, ParseException {

    if (publicKey instanceof RSAPublicKey) {
        RSAKey.Builder jwk = new RSAKey.Builder((RSAPublicKey) publicKey);
        jwk.keyID(CertificateUtils.getThumbPrint(certificate));
        jwk.algorithm(JWSAlgorithm.RS256);
        jwk.keyUse(KeyUse.parse("sig"));
        return jwk.build();

    }
    return null;

}
 
Example 5
Source File: CustomAuthorizationServerConfigurer.java    From spring-microservice-exam with MIT License 5 votes vote down vote up
@Bean
public JWKSet jwkSet() {
	RSAKey.Builder builder = new RSAKey.Builder((RSAPublicKey) keyPair().getPublic())
			.keyUse(KeyUse.SIGNATURE)
			.algorithm(JWSAlgorithm.RS256)
			.keyID("bael-key-id");
	return new JWKSet(builder.build());
}
 
Example 6
Source File: JwkKeyPairManager.java    From OAuth-2.0-Cookbook with MIT License 5 votes vote down vote up
public JwkKeyPairManager() {
    KeyPair keyPair = createRSA256KeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RandomValueStringGenerator random = new RandomValueStringGenerator();
    RSAKey.Builder builder = new RSAKey.Builder(publicKey);
    builder.keyID(random.generate());
    builder.privateKey(privateKey);
    this.clientJwk = builder.build();
}
 
Example 7
Source File: JwkAuthorizationServerConfiguration.java    From spring-security-oauth with MIT License 5 votes vote down vote up
@Bean
public JWKSet jwkSet() {
    RSAKey.Builder builder = new RSAKey.Builder((RSAPublicKey) keyPair().getPublic()).keyUse(KeyUse.SIGNATURE)
        .algorithm(JWSAlgorithm.RS256)
        .keyID(JWK_KID);
    return new JWKSet(builder.build());
}