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