package org.knowm.xchange.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Properties;

import org.knowm.xchange.ExchangeSpecification;

public class AuthUtils {

  /**
   * Generates a BASE64 Basic Authentication String
   *
   * @return BASE64 Basic Authentication String
   */
  public static String getBasicAuth(String user, final String pass) {

    return "Basic " + net.iharder.Base64.encodeBytes((user + ":" + pass).getBytes());
  }

  /**
   * Read the API & Secret key from a resource called {@code secret.keys}.
   * 
   * NOTE: This file MUST NEVER be commited to source control. It is therefore added to .gitignore. 
   */
  public static void setApiAndSecretKey(ExchangeSpecification exchangeSpec) {

    setApiAndSecretKey(exchangeSpec, null);
  }

  /**
   * Read the API & Secret key from a resource called {@code prefix}-{@code secret.keys}.
   * 
   * NOTE: This file MUST NEVER be commited to source control. It is therefore added to .gitignore. 
   */
  public static void setApiAndSecretKey(ExchangeSpecification exchangeSpec, String prefix) {

    Properties props = getSecretProperties(prefix);

    if (props != null) {
      exchangeSpec.setApiKey(props.getProperty("apiKey"));
      exchangeSpec.setSecretKey(props.getProperty("secretKey"));
    }
  }

  /**
   * Read the secret properties from a resource called {@code prefix}-{@code secret.keys}.
   * 
   * NOTE: This file MUST NEVER be commited to source control. It is therefore added to .gitignore. 
   * @return The properties or null
   */
  public static Properties getSecretProperties(String prefix) {

    String resource = prefix != null ? prefix + "-secret.keys" : "secret.keys";

    // First try to find the keys in the classpath 
    InputStream inStream = AuthUtils.class.getResourceAsStream("/" + resource);
    
    // Next try to find the keys in the user's home/.ssh dir
    File keyfile = Paths.get(System.getProperty("user.home"), ".ssh", resource).toFile();
    if (inStream == null && keyfile.isFile()) {
      try {
        inStream = new FileInputStream(keyfile);
      } catch (IOException e) {
        // do nothing
      }
    }
    
    Properties props = null;
    if (inStream != null) {
      try {
        props = new Properties();
        props.load(inStream);
        return props;
      } catch (IOException e) {
        // do nothing
      }
    }
    return props;
  }
}

@@ -140,36 +140,8 @@
 am;%0A
-import java.nio.file.Paths;%0A
 impo
@@ -1971,17 +1971,16 @@
 e = 
-Paths.get
+new File
 (Sys
@@ -2007,18 +2007,24 @@
 r.home%22)
-,
  
++ %22/%22 +
 %22.ssh%22, 
@@ -2036,17 +2036,8 @@
 rce)
-.toFile()
 ;%0A  

Update AuthUtils.java