// Path: src/main/java/io/cdap/plugin/salesforce/SalesforceConstants.java
// public class SalesforceConstants {
// 
//   public static final String API_VERSION = "53.0";
//   public static final String REFERENCE_NAME_DELIMITER = ".";
// 
//   public static final String PROPERTY_CONSUMER_KEY = "consumerKey";
//   public static final String PROPERTY_CONSUMER_SECRET = "consumerSecret";
//   public static final String PROPERTY_USERNAME = "username";
//   public static final String PROPERTY_PASSWORD = "password";
//   public static final String PROPERTY_SECURITY_TOKEN = "securityToken";
//   public static final String PROPERTY_LOGIN_URL = "loginUrl";
//   public static final String PROPERTY_OAUTH_INFO = "oAuthInfo";
// 
//   public static final String CONFIG_OAUTH_TOKEN = "mapred.salesforce.oauth.token";
//   public static final String CONFIG_OAUTH_INSTANCE_URL = "mapred.salesforce.oauth.instance.url";
//   public static final String CONFIG_CONSUMER_KEY = "mapred.salesforce.consumer.key";
//   public static final String CONFIG_PASSWORD = "mapred.salesforce.password";
//   public static final String CONFIG_USERNAME = "mapred.salesforce.user";
//   public static final String CONFIG_CONSUMER_SECRET = "mapred.salesforce.consumer.secret";
//   public static final String CONFIG_LOGIN_URL = "mapred.salesforce.login.url";
// 
//   public static final int RANGE_FILTER_MIN_VALUE = 0;
//   public static final int SOQL_MAX_LENGTH = 20000;
// }
// 
// Path: src/main/java/io/cdap/plugin/salesforce/plugin/OAuthInfo.java
// public final class OAuthInfo {
// 
//   private final String accessToken;
//   private final String instanceURL;
// 
//   public OAuthInfo(String accessToken, String instanceURL) {
//     this.accessToken = accessToken;
//     this.instanceURL = instanceURL;
//   }
// 
//   public String getAccessToken() {
//     return accessToken;
//   }
// 
//   public String getInstanceURL() {
//     return instanceURL;
//   }
// }


import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.sforce.ws.ConnectorConfig;
import io.cdap.plugin.salesforce.SalesforceConstants;
import io.cdap.plugin.salesforce.plugin.OAuthInfo;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
/*
 * Copyright © 2019 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.cdap.plugin.salesforce.authenticator;


/**
 * Authentication to Salesforce via oauth2
 */
public class Authenticator {
  private static final Gson GSON = new Gson();

  /**
   * Authenticates via oauth2 to salesforce and returns a connectorConfig
   * which can be used by salesforce libraries to make a connection.
   *
   * @param credentials information to log in
   *
   * @return ConnectorConfig which can be used to create BulkConnection and PartnerConnection
   */
  public static ConnectorConfig createConnectorConfig(AuthenticatorCredentials credentials) {
    try {
      OAuthInfo oAuthInfo = getOAuthInfo(credentials);
      ConnectorConfig connectorConfig = new ConnectorConfig();
      connectorConfig.setSessionId(oAuthInfo.getAccessToken());
// Path: src/main/java/io/cdap/plugin/salesforce/SalesforceConstants.java
// public class SalesforceConstants {
// 
//   public static final String API_VERSION = "53.0";
//   public static final String REFERENCE_NAME_DELIMITER = ".";
// 
//   public static final String PROPERTY_CONSUMER_KEY = "consumerKey";
//   public static final String PROPERTY_CONSUMER_SECRET = "consumerSecret";
//   public static final String PROPERTY_USERNAME = "username";
//   public static final String PROPERTY_PASSWORD = "password";
//   public static final String PROPERTY_SECURITY_TOKEN = "securityToken";
//   public static final String PROPERTY_LOGIN_URL = "loginUrl";
//   public static final String PROPERTY_OAUTH_INFO = "oAuthInfo";
// 
//   public static final String CONFIG_OAUTH_TOKEN = "mapred.salesforce.oauth.token";
//   public static final String CONFIG_OAUTH_INSTANCE_URL = "mapred.salesforce.oauth.instance.url";
//   public static final String CONFIG_CONSUMER_KEY = "mapred.salesforce.consumer.key";
//   public static final String CONFIG_PASSWORD = "mapred.salesforce.password";
//   public static final String CONFIG_USERNAME = "mapred.salesforce.user";
//   public static final String CONFIG_CONSUMER_SECRET = "mapred.salesforce.consumer.secret";
//   public static final String CONFIG_LOGIN_URL = "mapred.salesforce.login.url";
// 
//   public static final int RANGE_FILTER_MIN_VALUE = 0;
//   public static final int SOQL_MAX_LENGTH = 20000;
// }
// 
// Path: src/main/java/io/cdap/plugin/salesforce/plugin/OAuthInfo.java
// public final class OAuthInfo {
// 
//   private final String accessToken;
//   private final String instanceURL;
// 
//   public OAuthInfo(String accessToken, String instanceURL) {
//     this.accessToken = accessToken;
//     this.instanceURL = instanceURL;
//   }
// 
//   public String getAccessToken() {
//     return accessToken;
//   }
// 
//   public String getInstanceURL() {
//     return instanceURL;
//   }
// }


// Path: src/main/java/io/cdap/plugin/salesforce/authenticator/Authenticator.java
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.sforce.ws.ConnectorConfig;
import io.cdap.plugin.salesforce.SalesforceConstants;
import io.cdap.plugin.salesforce.plugin.OAuthInfo;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;

/*
 * Copyright © 2019 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.cdap.plugin.salesforce.authenticator;


/**
 * Authentication to Salesforce via oauth2
 */
public class Authenticator {
  private static final Gson GSON = new Gson();

  /**
   * Authenticates via oauth2 to salesforce and returns a connectorConfig
   * which can be used by salesforce libraries to make a connection.
   *
   * @param credentials information to log in
   *
   * @return ConnectorConfig which can be used to create BulkConnection and PartnerConnection
   */
  public static ConnectorConfig createConnectorConfig(AuthenticatorCredentials credentials) {
    try {
      OAuthInfo oAuthInfo = getOAuthInfo(credentials);
      ConnectorConfig connectorConfig = new ConnectorConfig();
      connectorConfig.setSessionId(oAuthInfo.getAccessToken());
      String apiVersion = SalesforceConstants.API_VERSION;