Search in sources :

Example 1 with RSAKey

use of io.gravitee.am.model.jose.RSAKey in project gravitee-access-management by gravitee-io.

the class DynamicClientRegistrationResponseTest method convert.

@Test
public void convert() {
    RSAKey rsaKey = new RSAKey();
    rsaKey.setKty("RSA");
    rsaKey.setKid("kidRSA");
    rsaKey.setUse("enc");
    rsaKey.setE("exponent");
    rsaKey.setN("modulus");
    ECKey ecKey = new ECKey();
    ecKey.setKty("EC");
    ecKey.setKid("kidEC");
    ecKey.setUse("enc");
    ecKey.setCrv("P-256");
    ecKey.setX("vBT2JhFHd62Jcf4yyBzSV9NuDBNBssR1zlmnHelgZcs");
    ecKey.setY("up8E8b3TjeKS2v2GCH23UJP0bak0La77lkQ7_n4djqE");
    OKPKey okpKey = new OKPKey();
    okpKey.setKty("OKP");
    okpKey.setKid("kidOKP");
    okpKey.setCrv("Ed25519");
    okpKey.setX("vBNW8f19leF79U4U6NrDDQaK_i5kL0iMKghB39AUT2I");
    OCTKey octKey = new OCTKey();
    octKey.setKty("oct");
    octKey.setKid("kidOCT");
    octKey.setK("FdFYFzERwC2uCBB46pZQi4GG85LujR8obt-KWRBICVQ");
    JWKSet jwkSet = new JWKSet();
    jwkSet.setKeys(Arrays.asList(rsaKey, ecKey, okpKey, octKey));
    Client client = new Client();
    client.setClientId("clientId");
    client.setClientName("clientName");
    client.setJwks(jwkSet);
    DynamicClientRegistrationResponse response = DynamicClientRegistrationResponse.fromClient(client);
    assertNotNull("expecting response", response);
    assertEquals(response.getClientId(), "clientId");
    assertEquals(response.getClientName(), "clientName");
    assertTrue(response.getJwks().getKeys().size() == 4);
}
Also used : RSAKey(io.gravitee.am.model.jose.RSAKey) JWKSet(io.gravitee.am.model.oidc.JWKSet) OCTKey(io.gravitee.am.model.jose.OCTKey) ECKey(io.gravitee.am.model.jose.ECKey) Client(io.gravitee.am.model.oidc.Client) OKPKey(io.gravitee.am.model.jose.OKPKey) Test(org.junit.Test)

Example 2 with RSAKey

use of io.gravitee.am.model.jose.RSAKey in project gravitee-access-management by gravitee-io.

the class JWKServiceTest method testFilter_RSA_weakKey.

@Test
public void testFilter_RSA_weakKey() {
    // key size is <2048 bits
    RSAKey rsaEnc = new RSAKey();
    rsaEnc.setKty("RSA");
    rsaEnc.setKid("rsaEnc");
    rsaEnc.setUse("enc");
    rsaEnc.setN("nRuv8E_c8aLRlyMz4h2SKWKHkzmDO49TVXppes1IqRdFACg_7cEhKfV5-jiNVxH3nKFGcHw6IG3qCJe_-pEJhnTbIdYS98UJmVZuudD_7lH5JgVhaV3ZwY6aQIMsoE5YhMyi55jbHPS-GqSIGonlVlgpHX_VjxKtj-u_-824xZU");
    RSAKey rsaSig = new RSAKey();
    rsaSig.setKty("RSA");
    rsaSig.setKid("rsaSig");
    rsaSig.setUse("sig");
    JWKSet rsaSet = new JWKSet();
    rsaSet.setKeys(Arrays.asList(rsaEnc, rsaSig));
    TestObserver testObserver = jwkService.filter(rsaSet, JWKFilter.RSA_KEY_ENCRYPTION()).test();
    testObserver.assertNoErrors();
    testObserver.assertComplete();
    testObserver.assertResult();
}
Also used : RSAKey(io.gravitee.am.model.jose.RSAKey) JWKSet(io.gravitee.am.model.oidc.JWKSet) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 3 with RSAKey

use of io.gravitee.am.model.jose.RSAKey in project gravitee-access-management by gravitee-io.

the class JWSServiceTest method testVerifier_RSA_invalid.

@Test(expected = IllegalArgumentException.class)
public void testVerifier_RSA_invalid() {
    RSAKey key = new RSAKey();
    key.setKty("RSA");
    key.setKid(KID);
    key.setE(Base64.getUrlEncoder().encodeToString("exponent".getBytes()));
    key.setN(Base64.getUrlEncoder().encodeToString("modulus".getBytes()));
    jwsService.verifier(key);
}
Also used : RSAKey(io.gravitee.am.model.jose.RSAKey) Test(org.junit.Test)

Example 4 with RSAKey

use of io.gravitee.am.model.jose.RSAKey in project gravitee-access-management by gravitee-io.

the class JWSServiceTest method testValidSignature_RSA.

@Test
public void testValidSignature_RSA() throws NoSuchAlgorithmException, JOSEException {
    // Generate RSA key
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(2048);
    KeyPair rsaKey = kpg.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) rsaKey.getPublic();
    RSAKey key = new RSAKey();
    key.setKty("RSA");
    key.setKid(KID);
    key.setE(Base64.getUrlEncoder().encodeToString(publicKey.getPublicExponent().toByteArray()));
    key.setN(Base64.getUrlEncoder().encodeToString(publicKey.getModulus().toByteArray()));
    // Sign JWT with RSA algorithm
    SignedJWT signedJWT = new SignedJWT(new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(KID).build(), new JWTClaimsSet.Builder().expirationTime(Date.from(Instant.now().plus(1, ChronoUnit.DAYS))).build());
    signedJWT.sign(new RSASSASigner((RSAPrivateKey) rsaKey.getPrivate()));
    assertTrue("Should be ok", jwsService.isValidSignature(signedJWT, key));
}
Also used : KeyPair(java.security.KeyPair) OctetKeyPair(com.nimbusds.jose.jwk.OctetKeyPair) RSAKey(io.gravitee.am.model.jose.RSAKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) OctetKeyPairGenerator(com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator) KeyPairGenerator(java.security.KeyPairGenerator) SignedJWT(com.nimbusds.jwt.SignedJWT) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) JWSHeader(com.nimbusds.jose.JWSHeader) Test(org.junit.Test)

Example 5 with RSAKey

use of io.gravitee.am.model.jose.RSAKey in project gravitee-access-management by gravitee-io.

the class ProviderJWKSetEndpointHandlerTest method shouldInvokeJWKSetEndpoint.

@Test
public void shouldInvokeJWKSetEndpoint() throws Exception {
    JWK jwk = new RSAKey();
    jwk.setKty("RSA");
    jwk.setKid("my-test-key");
    JWKSet jwkSet = new JWKSet();
    jwkSet.setKeys(Collections.singletonList(jwk));
    when(jwkService.getKeys()).thenReturn(Single.just(jwkSet));
    testRequest(HttpMethod.GET, "/.well-known/jwks.json", HttpStatusCode.OK_200, "OK", "{\n" + "  \"keys\" : [ {\n" + "    \"kty\" : \"RSA\",\n" + "    \"kid\" : \"my-test-key\"\n" + "  } ]\n" + "}");
}
Also used : RSAKey(io.gravitee.am.model.jose.RSAKey) JWKSet(io.gravitee.am.model.oidc.JWKSet) JWK(io.gravitee.am.model.jose.JWK) Test(org.junit.Test)

Aggregations

RSAKey (io.gravitee.am.model.jose.RSAKey)18 Test (org.junit.Test)15 Client (io.gravitee.am.model.oidc.Client)10 TestObserver (io.reactivex.observers.TestObserver)10 KeyPair (java.security.KeyPair)10 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)10 JWKSet (io.gravitee.am.model.oidc.JWKSet)8 RSAPublicKey (java.security.interfaces.RSAPublicKey)8 OpenIDProviderMetadata (io.gravitee.am.gateway.handler.oidc.service.discovery.OpenIDProviderMetadata)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 KeyPairGenerator (java.security.KeyPairGenerator)3 JWEObject (com.nimbusds.jose.JWEObject)2 RSADecrypter (com.nimbusds.jose.crypto.RSADecrypter)2 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)2 SignedJWT (com.nimbusds.jwt.SignedJWT)2 ECKey (io.gravitee.am.model.jose.ECKey)2 OCTKey (io.gravitee.am.model.jose.OCTKey)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 JWSHeader (com.nimbusds.jose.JWSHeader)1 OctetKeyPair (com.nimbusds.jose.jwk.OctetKeyPair)1