package com.xero.api;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.util.Collections;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.google.api.client.auth.oauth.OAuthRsaSigner;

public class RsaSignerFactory implements SignerFactory {

  private OAuthRsaSigner signer = new OAuthRsaSigner();
  final static Logger logger = LogManager.getLogger(RsaSignerFactory.class);
  
  public RsaSignerFactory(String pathToPrivateKey, String privateKeyPassword) {
    this(RsaSignerFactory.class.getResourceAsStream(pathToPrivateKey), privateKeyPassword);
  }

  public RsaSignerFactory(InputStream privateKeyInputStream, String privateKeyPassword) {
    try {
      KeyStore oauthKeyStore;
      oauthKeyStore = KeyStore.getInstance("PKCS12");

      oauthKeyStore.load(privateKeyInputStream, privateKeyPassword.toCharArray());

      PrivateKey oauthKey = null;
      for (String alias : Collections.list(oauthKeyStore.aliases())) {
        if (oauthKeyStore.isKeyEntry(alias)) {
          oauthKey = (PrivateKey) oauthKeyStore.getKey(alias, privateKeyPassword.toCharArray());
        }
      }

      signer.privateKey = oauthKey;
    } catch (IOException | GeneralSecurityException | RuntimeException ex) {
    		logger.error(ex);
    		throw new RuntimeException(ex);
    }
  }

  @Override
  public OAuthRsaSigner createSigner(String tokenSharedSecret) {
    return signer;
  }
}

@@ -35,47 +35,9 @@
 .io.
-IOException;%0Aimport java.io.InputStream
+*
 ;%0Aim
@@ -605,50 +605,29 @@
 his(
-RsaSignerFactory.class.getResourceAsStream
+getInputStreamForPath
 (pat
@@ -668,16 +668,18 @@
 );%0A  %7D%0A%0A
+  
   public
@@ -1492,11 +1492,389 @@
 ner;%0A  %7D
+%0A%0A  private static InputStream getInputStreamForPath(String path) %7B%0A    File f = new File(path);%0A    if (f.isFile()) %7B%0A      try %7B%0A        return new FileInputStream(f);%0A      %7D catch (FileNotFoundException e) %7B%0A        logger.info(%22Could not open file from file system, defaulting to classpath.%22);%0A      %7D%0A    %7D%0A%0A    return RsaSignerFactory.class.getResourceAsStream(path);%0A  %7D
 %0A%7D%0A

Allow private key file to be on file system rather than only on classpath