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);
}
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'");
}
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);
}
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);
}
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;
}
}
Aggregations