/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxauth.model.crypto;

import org.apache.commons.lang.StringUtils;
import org.gluu.oxauth.model.common.WebKeyStorage;
import org.gluu.oxauth.model.configuration.AppConfiguration;

/**
 * @author Javier Rojas Blum
 * @version April 25, 2017
 */
public class CryptoProviderFactory {

    private static OxAuthCryptoProvider keyStoreProvider = null;

    public synchronized static AbstractCryptoProvider getCryptoProvider(AppConfiguration configuration) throws Exception {
        AbstractCryptoProvider cryptoProvider = null;
        WebKeyStorage webKeyStorage = configuration.getWebKeysStorage();
        if (webKeyStorage == null) {
            return null;
        }

        switch (webKeyStorage) {
            case KEYSTORE:
                cryptoProvider = getKeyStoreProvider(configuration);

                break;
            case PKCS11:
                cryptoProvider = new OxElevenCryptoProvider(
                        configuration.getOxElevenGenerateKeyEndpoint(),
                        configuration.getOxElevenSignEndpoint(),
                        configuration.getOxElevenVerifySignatureEndpoint(),
                        configuration.getOxElevenDeleteKeyEndpoint(),
                        configuration.getOxElevenTestModeToken());
                break;
        }

        return cryptoProvider;
    }

    private static AbstractCryptoProvider getKeyStoreProvider(AppConfiguration configuration) throws Exception {
        if (keyStoreProvider != null &&
                StringUtils.isNotBlank(keyStoreProvider.getKeyStoreFile()) &&
                StringUtils.isNotBlank(keyStoreProvider.getKeyStoreSecret()) &&
                StringUtils.isNotBlank(keyStoreProvider.getDnName()) &&
                keyStoreProvider.getKeyStoreFile().equals(configuration.getKeyStoreFile()) &&
                keyStoreProvider.getKeyStoreSecret().equals(configuration.getKeyStoreSecret()) &&
                keyStoreProvider.getDnName().equals(configuration.getDnName())) {
            return keyStoreProvider;
        }

        String keyStoreFile = configuration.getKeyStoreFile();
        String keyStoreSecret = configuration.getKeyStoreSecret();
        String dnName = configuration.getDnName();
        return keyStoreProvider = new OxAuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName);
    }
}

@@ -518,21 +518,8 @@
 lic 
-synchronized 
 stat
@@ -605,32 +605,32 @@
 ows Exception %7B%0A
+
         Abstract
@@ -1482,24 +1482,37 @@
     private 
+synchronized 
 static Abstr
@@ -2192,38 +2192,38 @@
      %7D%0A%0A        
-String
+return
  keyStoreFile = 
@@ -2219,79 +2219,76 @@
 tore
-File = configuration.getKeyStoreFile();%0A        String keyStoreSecret =
+Provider = new OxAuthCryptoProvider(configuration.getKeyStoreFile(),
  con
@@ -2321,33 +2321,9 @@
 et()
-;%0A        String dnName =
+,
  con
@@ -2348,105 +2348,8 @@
 me()
-;%0A        return keyStoreProvider = new OxAuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName
 );%0A 

synchronize only keystore part of provider.