Search in sources :

Example 1 with TokenStorage

use of org.codice.ddf.security.token.storage.api.TokenStorage in project ddf by codice.

the class OAuthSecurityImplTest method setUp.

@Before
public void setUp() throws Exception {
    // Generate the RSA key pair to sign tokens
    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
    gen.initialize(2048);
    KeyPair keyPair = gen.generateKeyPair();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    JWK sigJwk = new RSAKey.Builder(publicKey).privateKey(privateKey).keyUse(KeyUse.SIGNATURE).keyID(UUID.randomUUID().toString()).build();
    String jwk = "{\"keys\": [" + sigJwk.toPublicJWK().toJSONString() + "] }";
    validAlgorithm = Algorithm.RSA256(publicKey, privateKey);
    invalidAlgorithm = Algorithm.HMAC256("WRONG");
    ResourceRetriever resourceRetriever = mock(ResourceRetriever.class);
    Resource jwkResource = new Resource(jwk, APPLICATION_JSON);
    when(resourceRetriever.retrieveResource(eq(new URL(JWK_ENDPOINT)))).thenReturn(jwkResource);
    String content = IOUtils.toString(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("metadata.json")), StandardCharsets.UTF_8);
    Resource metadataResource = new Resource(content, APPLICATION_JSON);
    when(resourceRetriever.retrieveResource(eq(new URL(METADATA_ENDPOINT)))).thenReturn(metadataResource);
    tokenStorage = mock(TokenStorage.class);
    oauthSecurity = new OAuthSecurityWithMockWebclient(tokenStorage);
    oauthSecurity.setResourceRetriever(resourceRetriever);
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) ResourceRetriever(com.nimbusds.jose.util.ResourceRetriever) Resource(com.nimbusds.jose.util.Resource) KeyPairGenerator(java.security.KeyPairGenerator) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) TokenStorage(org.codice.ddf.security.token.storage.api.TokenStorage) URL(java.net.URL) JWK(com.nimbusds.jose.jwk.JWK) Before(org.junit.Before)

Example 2 with TokenStorage

use of org.codice.ddf.security.token.storage.api.TokenStorage in project ddf by codice.

the class OAuthPluginTest method setUp.

@Before
public void setUp() throws Exception {
    // Generate the RSA key pair to sign tokens
    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
    gen.initialize(2048);
    KeyPair keyPair = gen.generateKeyPair();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    JWK sigJwk = new RSAKey.Builder(publicKey).privateKey(privateKey).keyUse(KeyUse.SIGNATURE).keyID(UUID.randomUUID().toString()).build();
    String jwk = "{\"keys\": [" + sigJwk.toPublicJWK().toJSONString() + "] }";
    validAlgorithm = Algorithm.RSA256(publicKey, privateKey);
    invalidAlgorithm = Algorithm.HMAC256("WRONG");
    ResourceRetriever resourceRetriever = mock(ResourceRetriever.class);
    Resource jwkResource = new Resource(jwk, APPLICATION_JSON);
    when(resourceRetriever.retrieveResource(eq(new URL(JWK_ENDPOINT)))).thenReturn(jwkResource);
    String content = IOUtils.toString(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("metadata.json")), StandardCharsets.UTF_8);
    Resource metadataResource = new Resource(content, APPLICATION_JSON);
    when(resourceRetriever.retrieveResource(eq(new URL(METADATA_ENDPOINT)))).thenReturn(metadataResource);
    tokenStorage = mock(TokenStorage.class);
    oauthPlugin = new OAuthPluginWithMockWebClient(tokenStorage);
    oauthPlugin.setResourceRetriever(resourceRetriever);
}
Also used : KeyPair(java.security.KeyPair) RSAPublicKey(java.security.interfaces.RSAPublicKey) ResourceRetriever(com.nimbusds.jose.util.ResourceRetriever) Resource(com.nimbusds.jose.util.Resource) KeyPairGenerator(java.security.KeyPairGenerator) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) TokenStorage(org.codice.ddf.security.token.storage.api.TokenStorage) URL(java.net.URL) DISCOVERY_URL(org.codice.ddf.security.token.storage.api.TokenStorage.DISCOVERY_URL) JWK(com.nimbusds.jose.jwk.JWK) Before(org.junit.Before)

Aggregations

JWK (com.nimbusds.jose.jwk.JWK)2 Resource (com.nimbusds.jose.util.Resource)2 ResourceRetriever (com.nimbusds.jose.util.ResourceRetriever)2 URL (java.net.URL)2 KeyPair (java.security.KeyPair)2 KeyPairGenerator (java.security.KeyPairGenerator)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 TokenStorage (org.codice.ddf.security.token.storage.api.TokenStorage)2 Before (org.junit.Before)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 DISCOVERY_URL (org.codice.ddf.security.token.storage.api.TokenStorage.DISCOVERY_URL)1