Search in sources :

Example 11 with KeyObjectKeyLoader

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

the class Signature method tag.

@SuppressWarnings({ "rawtypes", "unchecked" })
public Object tag(Map attributes, Closure body) throws Exception {
    Object keyId = resolve(attributes, "keyId");
    if (keyId == null) {
        throw new RuntimeException("<g:signature> requires a keyId for signing");
    }
    Callable<Key> useLoader = null;
    Object key = resolve(attributes, "key");
    if (key == null) {
        Object keystore = resolve(attributes, "keystore");
        if (keystore == null) {
            throw new RuntimeException("<g:signature> requires a key or keystore for signing");
        }
        String password = resolve(attributes, "password", String.class);
        if (password == null) {
            throw new RuntimeException("<g:signature> requires a password when using a keystore");
        }
        String alias = resolve(attributes, "alias", String.class);
        if (alias == null) {
            throw new RuntimeException("<g:signature> requires an alias when using a keystore");
        }
        if (!(keystore instanceof KeyStore)) {
            String ksl = keystore.toString();
            KeyChainKeyLoader loader = keystores.get(ksl);
            if (loader == null) {
                URIParcel<KeyStore> keystoreParcel = new URIParcel<KeyStore>(KeyStore.class, new URI(ksl));
                keystoreParcel.setRefresh(60000);
                Map conf = new HashMap();
                conf.put("password", password);
                String type = resolve(attributes, "type", String.class);
                if (type != null) {
                    conf.put("type", type);
                }
                keystoreParcel.setConfig(conf);
                loader = new KeyChainKeyLoader(new KeyStoreKeyChainImpl(keystoreParcel, password.toCharArray()), alias);
                keystores.put(ksl, loader);
            }
            useLoader = loader;
        } else {
            useLoader = new KeyChainKeyLoader(new KeyStoreKeyChainImpl((KeyStore) keystore, password.toCharArray()), alias);
        }
    }
    if (key instanceof Callable<?>) {
        useLoader = (Callable<Key>) key;
    } else if (key instanceof Key) {
        useLoader = new KeyObjectKeyLoader((Key) key);
    }
    String useAlgorithm = "hmac-sha256";
    Object algorithm = resolve(attributes, "algorithm");
    if (algorithm != null) {
        useAlgorithm = algorithm.toString();
    }
    if (useLoader == null) {
        if (useAlgorithm.startsWith("rsa")) {
        // TODO load private key from object
        } else {
            String signingAlg = Algorithms.getSecurityAlgorithm(useAlgorithm);
            // System.out.println("Generating hmac key "+signingAlg+" with "+new String(DatatypeConverter.parseBase64Binary(key.toString())));
            useLoader = new KeyObjectKeyLoader(new SecretKeySpec(DatatypeConverter.parseBase64Binary(key.toString()), signingAlg));
        }
    }
    Object headers = resolve(attributes, "headers");
    HttpSignatureSigner signer = new HttpSignatureSigner();
    signer.setAlgorithm(useAlgorithm);
    signer.setKeyId(keyId.toString());
    signer.setKeyLoader(useLoader);
    if (headers != null) {
        if (!(headers instanceof List)) {
            throw new RuntimeException("signature tag requires that 'headers' attribut contains a List, instead found " + headers.getClass().toString());
        }
        signer.setHeaders((List) headers);
    }
    bind(body, SIGNATURE_BINDING, Optional.of(signer));
    return null;
}
Also used : URIParcel(com.disney.uriparcel.URIParcel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyStore(java.security.KeyStore) URI(java.net.URI) Callable(java.util.concurrent.Callable) KeyStoreKeyChainImpl(com.disney.http.auth.keychain.KeyStoreKeyChainImpl) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeyChainKeyLoader(com.disney.http.auth.client.keyloader.KeyChainKeyLoader) HttpSignatureSigner(com.disney.http.auth.client.signer.HttpSignatureSigner) List(java.util.List) KeyObjectKeyLoader(com.disney.http.auth.client.keyloader.KeyObjectKeyLoader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) Key(java.security.Key)

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