Search in sources :

Example 6 with KeyObjectKeyLoader

use of com.disney.http.auth.client.keyloader.KeyObjectKeyLoader in project groovity by disney.

the class SampleClient method main.

public static void main(String[] args) throws Exception {
    try {
        HttpClientBuilder clientBuilder = HttpClients.custom();
        // /// Ways to get the private key data (RSA):
        /*
         * Import KeyStore from file/url/etc.
         *   - assumes file has password but alias does not
         *   - must set loader password and type
         */
        Map<String, Object> config = new HashMap<String, Object>();
        config.put(KeyStoreValueHandler.KEYSTORE_PASSWORD, "filePassword");
        config.put(KeyStoreValueHandler.KEYSTORE_TYPE, "JCEKS");
        URIParcel<KeyStore> ks = new URIParcel<KeyStore>(KeyStore.class, new File("client_keystore.jceks").toURI(), config);
        KeyChain chain = new KeyStoreKeyChainImpl(ks, "passwordForPrivateKey".toCharArray());
        KeyChainKeyLoader loader = new KeyChainKeyLoader(chain);
        loader.setAlias("sample_webapp");
        /*
         * Import PrivateKey from PKCS8 pem file
         *   - assumes no password protection or encryption
         */
        // ExternalKeyLoader keyLoader = new ExternalKeyLoader("/client_key.pem", localContext);
        // keyLoader.setAlgorithm("RSA");
        URIParcel<PrivateKey> keyLoader = new URIParcel<PrivateKey>(PrivateKey.class, new java.net.URI("file:client_key.pem"));
        /*
         * Create own key and to set that in the signer. Can write key to file as desired
         *
         * Here, generate a KeyPair
         *   - only RSA
         *   - can set bit size to 1024 or 2048
         *   - must save the public key for verification use
         */
        KeyPair pair = KeyUtils.generateKeyPair(2048);
        // // Write privateKey to a file (PKCS8, uses base64encoding)
        // KeyUtils.writePrivateKeyToFile(pair,"/Users/kobar004/misc/auth-backup/newKey-priv.pem");
        KeyObjectKeyLoader privateKeyLoader = new KeyObjectKeyLoader(pair.getPrivate());
        // // write public KeyStore to file.
        // String publicKeyStoreLocation = "/Users/kobar004/misc/auth-backup/newKey-pub.store";
        // KeyUtils.writePublicKeyStoreToFile(pair.getPublic(), publicKeyStoreLocation, "RSA", "rachel");
        // Ways to set the symmetric key data (HMAC):
        /*
         * Set Key value explicitly
         */
        KeyObjectKeyLoader simpleLoader = new KeyObjectKeyLoader("hmac-sha256", "someBase64Secret");
        /*
         * Configuring the HttpSignatureSigner (HttpRequestInterceptor)
         *
         *   - must set the keyId / alias
         *   - must set key/encryption/algorithm
         *   - if no headers are set, default to just using the Date header
         *   - Lastly, the signer must be added to the clientBuilder
         */
        // /// Signing for SIGNATURE Authorization with imported RSA key
        // setting the key of the singer either with a loader or a key.
        HttpSignatureSigner signer = new HttpSignatureSigner();
        signer.setKeyId("apiUser123");
        signer.setHeaders(Arrays.asList("(request-target)", "host", "x-date"));
        // set key (choose one)
        // signer.setKey(loader);
        // signer.setKey(keyLoader);
        signer.setKeyLoader(simpleLoader);
        clientBuilder.addInterceptorLast(signer);
        // ///
        CloseableHttpClient client = clientBuilder.build();
        getRequest(client, "http://localhost:8080/");
        client.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HashMap(java.util.HashMap) URIParcel(com.disney.uriparcel.URIParcel) KeyChain(com.disney.http.auth.keychain.KeyChain) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) KeyStoreKeyChainImpl(com.disney.http.auth.keychain.KeyStoreKeyChainImpl) KeyChainKeyLoader(com.disney.http.auth.client.keyloader.KeyChainKeyLoader) HttpSignatureSigner(com.disney.http.auth.client.signer.HttpSignatureSigner) KeyObjectKeyLoader(com.disney.http.auth.client.keyloader.KeyObjectKeyLoader) File(java.io.File)

Example 7 with KeyObjectKeyLoader

use of com.disney.http.auth.client.keyloader.KeyObjectKeyLoader in project groovity by disney.

the class TestKeyObjectKeyLoader method testKeyObjectKeyLoaderBadAlgorithm.

@Test(expected = NoSuchAlgorithmException.class)
public void testKeyObjectKeyLoaderBadAlgorithm() throws Exception {
    KeyObjectKeyLoader loader = new KeyObjectKeyLoader("DSA", "something else");
    loader.call();
}
Also used : KeyObjectKeyLoader(com.disney.http.auth.client.keyloader.KeyObjectKeyLoader) Test(org.junit.Test)

Example 8 with KeyObjectKeyLoader

use of com.disney.http.auth.client.keyloader.KeyObjectKeyLoader in project groovity by disney.

the class TestKeyObjectKeyLoader method testKeyObjectKeyLoaderRSA.

@Test(expected = Exception.class)
public void testKeyObjectKeyLoaderRSA() throws Exception {
    KeyObjectKeyLoader loader = new KeyObjectKeyLoader("rsa-sha1", "something else");
    loader.call();
}
Also used : KeyObjectKeyLoader(com.disney.http.auth.client.keyloader.KeyObjectKeyLoader) Test(org.junit.Test)

Example 9 with KeyObjectKeyLoader

use of com.disney.http.auth.client.keyloader.KeyObjectKeyLoader in project groovity by disney.

the class TestKeyObjectKeyLoader method testKeyObjectPrivateKey.

@Test
public void testKeyObjectPrivateKey() throws Exception {
    KeyPair pair = KeyUtils.generateKeyPair(2048);
    PrivateKey privateKey = pair.getPrivate();
    PublicKey publicKey = pair.getPublic();
    // test with private key
    KeyObjectKeyLoader loader = new KeyObjectKeyLoader(privateKey);
    PrivateKey loadedPrivateKey = (PrivateKey) loader.call();
    Assert.assertEquals(privateKey, loadedPrivateKey);
    // test with public key
    loader = new KeyObjectKeyLoader(publicKey);
    PublicKey loadedPublicKey = (PublicKey) loader.call();
    Assert.assertEquals(publicKey, loadedPublicKey);
    // test with secret key
    Key key = new SecretKeySpec(DatatypeConverter.parseBase64Binary("someString"), "HmacMD5");
    loader = new KeyObjectKeyLoader(key);
    Key loadedKey = loader.call();
    Assert.assertEquals(key, loadedKey);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeyObjectKeyLoader(com.disney.http.auth.client.keyloader.KeyObjectKeyLoader) Test(org.junit.Test)

Example 10 with KeyObjectKeyLoader

use of com.disney.http.auth.client.keyloader.KeyObjectKeyLoader in project groovity by disney.

the class TestKeyObjectKeyLoader method testKeyObjectKeyLoaderGoodAlgorithms.

@Test
public void testKeyObjectKeyLoaderGoodAlgorithms() throws Exception {
    KeyObjectKeyLoader loader = new KeyObjectKeyLoader("hmac-sha1", "something else");
    loader.call();
    loader = new KeyObjectKeyLoader("hmac-md5", "something else");
    loader.call();
}
Also used : KeyObjectKeyLoader(com.disney.http.auth.client.keyloader.KeyObjectKeyLoader) Test(org.junit.Test)

Aggregations

KeyObjectKeyLoader (com.disney.http.auth.client.keyloader.KeyObjectKeyLoader)11 Test (org.junit.Test)9 HttpSignatureSigner (com.disney.http.auth.client.signer.HttpSignatureSigner)5 KeyChainKeyLoader (com.disney.http.auth.client.keyloader.KeyChainKeyLoader)3 KeyStoreKeyChainImpl (com.disney.http.auth.keychain.KeyStoreKeyChainImpl)3 URIParcel (com.disney.uriparcel.URIParcel)3 HashMap (java.util.HashMap)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)3 SignatureAuthorization (com.disney.http.auth.SignatureAuthorization)2 ClientAuthorizationRequest (com.disney.http.auth.client.ClientAuthorizationRequest)2 KeyChain (com.disney.http.auth.keychain.KeyChain)2 File (java.io.File)2 URI (java.net.URI)1 Key (java.security.Key)1 KeyStore (java.security.KeyStore)1 List (java.util.List)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1