Search in sources :

Example 6 with RSAKey

use of com.nimbusds.jose.jwk.RSAKey in project spring-security by spring-projects.

the class NimbusJwtEncoderTests method encodeWhenHeadersNotProvidedThenDefaulted.

@Test
public void encodeWhenHeadersNotProvidedThenDefaulted() {
    // @formatter:off
    RSAKey rsaJwk = TestJwks.jwk(TestKeys.DEFAULT_PUBLIC_KEY, TestKeys.DEFAULT_PRIVATE_KEY).keyID("rsa-jwk-1").build();
    this.jwkList.add(rsaJwk);
    // @formatter:on
    JwtClaimsSet jwtClaimsSet = TestJwtClaimsSets.jwtClaimsSet().build();
    Jwt encodedJws = this.jwtEncoder.encode(JwtEncoderParameters.from(jwtClaimsSet));
    assertThat(encodedJws.getHeaders().get(JoseHeaderNames.ALG)).isEqualTo(SignatureAlgorithm.RS256);
}
Also used : RSAKey(com.nimbusds.jose.jwk.RSAKey) Test(org.junit.jupiter.api.Test)

Example 7 with RSAKey

use of com.nimbusds.jose.jwk.RSAKey in project spring-security by spring-projects.

the class NimbusJwtEncoderTests method encodeWhenJwkMultipleSelectedThenThrowJwtEncodingException.

@Test
public void encodeWhenJwkMultipleSelectedThenThrowJwtEncodingException() throws Exception {
    RSAKey rsaJwk = TestJwks.DEFAULT_RSA_JWK;
    this.jwkList.add(rsaJwk);
    this.jwkList.add(rsaJwk);
    JwsHeader jwsHeader = JwsHeader.with(SignatureAlgorithm.RS256).build();
    JwtClaimsSet jwtClaimsSet = TestJwtClaimsSets.jwtClaimsSet().build();
    assertThatExceptionOfType(JwtEncodingException.class).isThrownBy(() -> this.jwtEncoder.encode(JwtEncoderParameters.from(jwsHeader, jwtClaimsSet))).withMessageContaining("Found multiple JWK signing keys for algorithm 'RS256'");
}
Also used : RSAKey(com.nimbusds.jose.jwk.RSAKey) Test(org.junit.jupiter.api.Test)

Example 8 with RSAKey

use of com.nimbusds.jose.jwk.RSAKey in project spring-security by spring-projects.

the class JwtDecoderProviderConfigurationUtilsTests method getSignatureAlgorithmsWhenAlgorithmThenParses.

// gh-9651
@Test
public void getSignatureAlgorithmsWhenAlgorithmThenParses() throws Exception {
    JWKSource<SecurityContext> jwkSource = mock(JWKSource.class);
    RSAKey key = new RSAKey.Builder(TestKeys.DEFAULT_PUBLIC_KEY).keyUse(KeyUse.SIGNATURE).algorithm(new Algorithm(JwsAlgorithms.RS256)).build();
    given(jwkSource.get(any(JWKSelector.class), isNull())).willReturn(Collections.singletonList(key));
    Set<SignatureAlgorithm> algorithms = JwtDecoderProviderConfigurationUtils.getSignatureAlgorithms(jwkSource);
    assertThat(algorithms).containsOnly(SignatureAlgorithm.RS256);
}
Also used : JWKSelector(com.nimbusds.jose.jwk.JWKSelector) RSAKey(com.nimbusds.jose.jwk.RSAKey) SecurityContext(com.nimbusds.jose.proc.SecurityContext) SignatureAlgorithm(org.springframework.security.oauth2.jose.jws.SignatureAlgorithm) JWSAlgorithm(com.nimbusds.jose.JWSAlgorithm) SignatureAlgorithm(org.springframework.security.oauth2.jose.jws.SignatureAlgorithm) Algorithm(com.nimbusds.jose.Algorithm) Test(org.junit.jupiter.api.Test)

Example 9 with RSAKey

use of com.nimbusds.jose.jwk.RSAKey in project spring-security by spring-projects.

the class JwtDecoderProviderConfigurationUtilsTests method getSignatureAlgorithmsWhenJwkSetSpecifiesAlgorithmThenUses.

@Test
public void getSignatureAlgorithmsWhenJwkSetSpecifiesAlgorithmThenUses() throws Exception {
    JWKSource<SecurityContext> jwkSource = mock(JWKSource.class);
    RSAKey key = new RSAKey.Builder(TestKeys.DEFAULT_PUBLIC_KEY).keyUse(KeyUse.SIGNATURE).algorithm(JWSAlgorithm.RS384).build();
    given(jwkSource.get(any(JWKSelector.class), isNull())).willReturn(Collections.singletonList(key));
    Set<SignatureAlgorithm> algorithms = JwtDecoderProviderConfigurationUtils.getSignatureAlgorithms(jwkSource);
    assertThat(algorithms).containsOnly(SignatureAlgorithm.RS384);
}
Also used : JWKSelector(com.nimbusds.jose.jwk.JWKSelector) RSAKey(com.nimbusds.jose.jwk.RSAKey) SecurityContext(com.nimbusds.jose.proc.SecurityContext) SignatureAlgorithm(org.springframework.security.oauth2.jose.jws.SignatureAlgorithm) Test(org.junit.jupiter.api.Test)

Example 10 with RSAKey

use of com.nimbusds.jose.jwk.RSAKey in project oxAuth by GluuFederation.

the class CrossEncryptionTest method encryptWithNimbusJoseJwt.

private String encryptWithNimbusJoseJwt() {
    try {
        RSAKey senderJWK = (RSAKey) JWK.parse(senderJwkJson);
        RSAKey recipientPublicJWK = (RSAKey) (JWK.parse(recipientJwkJson));
        // Create JWT
        // SignedJWT signedJWT = new SignedJWT(
        // new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(senderJWK.getKeyID()).build(),
        // new JWTClaimsSet.Builder()
        // .subject("testi")
        // .issuer("https:devgluu.saminet.local")
        // .build());
        // Sign the JWT
        // signedJWT.sign(new RSASSASigner(senderJWK));
        // Create JWE object with signed JWT as payload
        // JWEObject jweObject = new JWEObject(
        // new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM)
        // .contentType("JWT") // required to indicate nested JWT
        // .build(),
        // new Payload(signedJWT));
        @SuppressWarnings("deprecation") JWEObject jweObject = new JWEObject(new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM).type(JOSEObjectType.JWT).keyID(senderJWK.getKeyID()).build(), new Payload(Base64Util.base64urlencode(PAYLOAD.getBytes(Charsets.UTF_8))));
        // Encrypt with the recipient's public key
        RSAEncrypter encrypter = new RSAEncrypter(recipientPublicJWK);
        jweObject.encrypt(encrypter);
        // Serialise to JWE compact form
        return jweObject.serialize();
    } catch (Exception e) {
        System.out.println("Error encryption with Nimbus: " + e.getMessage());
        return null;
    }
}
Also used : RSAKey(com.nimbusds.jose.jwk.RSAKey) RSAEncrypter(com.nimbusds.jose.crypto.RSAEncrypter) JSONException(org.json.JSONException) ParseException(java.text.ParseException) InvalidJwtException(org.gluu.oxauth.model.exception.InvalidJwtException) IOException(java.io.IOException) InvalidJweException(org.gluu.oxauth.model.exception.InvalidJweException)

Aggregations

RSAKey (com.nimbusds.jose.jwk.RSAKey)36 Test (org.junit.jupiter.api.Test)14 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)10 SignedJWT (com.nimbusds.jwt.SignedJWT)9 ParseException (java.text.ParseException)9 RSASSAVerifier (com.nimbusds.jose.crypto.RSASSAVerifier)8 JWK (com.nimbusds.jose.jwk.JWK)8 IOException (java.io.IOException)6 JOSEException (com.nimbusds.jose.JOSEException)5 JWKSelector (com.nimbusds.jose.jwk.JWKSelector)5 SecurityContext (com.nimbusds.jose.proc.SecurityContext)5 InvalidJweException (org.gluu.oxauth.model.exception.InvalidJweException)5 InvalidJwtException (org.gluu.oxauth.model.exception.InvalidJwtException)5 JSONException (org.json.JSONException)5 RSADecrypter (com.nimbusds.jose.crypto.RSADecrypter)4 Base64URL (com.nimbusds.jose.util.Base64URL)4 RSAPublicKey (java.security.interfaces.RSAPublicKey)4 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)3 JWSVerifier (com.nimbusds.jose.JWSVerifier)3 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)3