// Path: mujina-common/src/main/java/mujina/saml/KeyStoreLocator.java
// public class KeyStoreLocator {
// 
//   private static CertificateFactory certificateFactory;
// 
//   static {
//     try {
//       certificateFactory = CertificateFactory.getInstance("X.509");
//     } catch (CertificateException e) {
//       throw new RuntimeException(e);
//     }
//   }
// 
//   public static KeyStore createKeyStore(String pemPassPhrase) {
//     try {
//       KeyStore keyStore = KeyStore.getInstance("JKS");
//       keyStore.load(null, pemPassPhrase.toCharArray());
//       return keyStore;
//     } catch (Exception e) {
//       //too many exceptions we can't handle, so brute force catch
//       throw new RuntimeException(e);
//     }
//   }
// 
//   //privateKey must be in the DER unencrypted PKCS#8 format. See README.md
//   public static void addPrivateKey(KeyStore keyStore, String alias, String privateKey, String certificate, String password) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException, CertificateException {
//     String wrappedCert = wrapCert(certificate);
//     byte[] decodedKey = Base64.getDecoder().decode(privateKey.getBytes());
// 
//     char[] passwordChars = password.toCharArray();
//     Certificate cert = certificateFactory.generateCertificate(new ByteArrayInputStream(wrappedCert.getBytes()));
//     ArrayList<Certificate> certs = new ArrayList<>();
//     certs.add(cert);
// 
//     byte[] privKeyBytes = IOUtils.toByteArray(new ByteArrayInputStream(decodedKey));
// 
//     KeySpec ks = new PKCS8EncodedKeySpec(privKeyBytes);
//     RSAPrivateKey privKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(ks);
//     keyStore.setKeyEntry(alias, privKey, passwordChars, certs.toArray(new Certificate[certs.size()]));
//   }
// 
//   private static String wrapCert(String certificate) {
//     return "-----BEGIN CERTIFICATE-----\n" + certificate + "\n-----END CERTIFICATE-----";
//   }
// 
// }


import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import mujina.saml.KeyStoreLocator;
import org.opensaml.xml.Configuration;
import org.opensaml.xml.security.BasicSecurityConfiguration;
import org.opensaml.xml.signature.SignatureConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.saml.key.JKSKeyManager;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableEntryException;
import java.util.Enumeration;
  private String defaultSignatureAlgorithm = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256;
  private String signatureAlgorithm;
  private String entityId;

  public SharedConfiguration(JKSKeyManager keyManager) {
    this.keyManager = keyManager;
  }

  public abstract void reset();

  public void setEntityId(String newEntityId, boolean addTokenToStore) {
    if (addTokenToStore) {
      try {
        KeyStore keyStore = keyManager.getKeyStore();
        KeyStore.PasswordProtection passwordProtection = new KeyStore.PasswordProtection(keystorePassword.toCharArray());
        KeyStore.Entry keyStoreEntry = keyStore.getEntry(this.entityId, passwordProtection);
        keyStore.setEntry(newEntityId, keyStoreEntry, passwordProtection);
      } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableEntryException e) {
        throw new RuntimeException(e);
      }
    }
    this.entityId = newEntityId;
  }

  public void injectCredential(final String certificate, final String pemKey) {
    try {
      KeyStore keyStore = keyManager.getKeyStore();
      if (keyStore.containsAlias(entityId)) {
        keyStore.deleteEntry(entityId);
      }
// Path: mujina-common/src/main/java/mujina/saml/KeyStoreLocator.java
// public class KeyStoreLocator {
// 
//   private static CertificateFactory certificateFactory;
// 
//   static {
//     try {
//       certificateFactory = CertificateFactory.getInstance("X.509");
//     } catch (CertificateException e) {
//       throw new RuntimeException(e);
//     }
//   }
// 
//   public static KeyStore createKeyStore(String pemPassPhrase) {
//     try {
//       KeyStore keyStore = KeyStore.getInstance("JKS");
//       keyStore.load(null, pemPassPhrase.toCharArray());
//       return keyStore;
//     } catch (Exception e) {
//       //too many exceptions we can't handle, so brute force catch
//       throw new RuntimeException(e);
//     }
//   }
// 
//   //privateKey must be in the DER unencrypted PKCS#8 format. See README.md
//   public static void addPrivateKey(KeyStore keyStore, String alias, String privateKey, String certificate, String password) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException, CertificateException {
//     String wrappedCert = wrapCert(certificate);
//     byte[] decodedKey = Base64.getDecoder().decode(privateKey.getBytes());
// 
//     char[] passwordChars = password.toCharArray();
//     Certificate cert = certificateFactory.generateCertificate(new ByteArrayInputStream(wrappedCert.getBytes()));
//     ArrayList<Certificate> certs = new ArrayList<>();
//     certs.add(cert);
// 
//     byte[] privKeyBytes = IOUtils.toByteArray(new ByteArrayInputStream(decodedKey));
// 
//     KeySpec ks = new PKCS8EncodedKeySpec(privKeyBytes);
//     RSAPrivateKey privKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(ks);
//     keyStore.setKeyEntry(alias, privKey, passwordChars, certs.toArray(new Certificate[certs.size()]));
//   }
// 
//   private static String wrapCert(String certificate) {
//     return "-----BEGIN CERTIFICATE-----\n" + certificate + "\n-----END CERTIFICATE-----";
//   }
// 
// }


// Path: mujina-common/src/main/java/mujina/api/SharedConfiguration.java
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import mujina.saml.KeyStoreLocator;
import org.opensaml.xml.Configuration;
import org.opensaml.xml.security.BasicSecurityConfiguration;
import org.opensaml.xml.signature.SignatureConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.saml.key.JKSKeyManager;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableEntryException;
import java.util.Enumeration;

  private String defaultSignatureAlgorithm = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256;
  private String signatureAlgorithm;
  private String entityId;

  public SharedConfiguration(JKSKeyManager keyManager) {
    this.keyManager = keyManager;
  }

  public abstract void reset();

  public void setEntityId(String newEntityId, boolean addTokenToStore) {
    if (addTokenToStore) {
      try {
        KeyStore keyStore = keyManager.getKeyStore();
        KeyStore.PasswordProtection passwordProtection = new KeyStore.PasswordProtection(keystorePassword.toCharArray());
        KeyStore.Entry keyStoreEntry = keyStore.getEntry(this.entityId, passwordProtection);
        keyStore.setEntry(newEntityId, keyStoreEntry, passwordProtection);
      } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableEntryException e) {
        throw new RuntimeException(e);
      }
    }
    this.entityId = newEntityId;
  }

  public void injectCredential(final String certificate, final String pemKey) {
    try {
      KeyStore keyStore = keyManager.getKeyStore();
      if (keyStore.containsAlias(entityId)) {
        keyStore.deleteEntry(entityId);
      }
      KeyStoreLocator.addPrivateKey(keyStore, entityId, pemKey, certificate, keystorePassword);