Skip to content

Added the ability to authenticate with an access token vs. username a… #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/main/java/com/springml/salesforce/wave/api/APIFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public WaveAPI waveAPI(String username, String password, String loginURL, String
return new WaveAPIImpl(new SFConfig(username, password, loginURL, apiVersion));
}

public WaveAPI waveAPIwAuthToken(String authToken, String instanceURL) throws Exception {
return this.waveAPIwAuthToken( authToken, instanceURL, WaveAPIConstants.API_VERSION);
}

public WaveAPI waveAPIwAuthToken(String authToken, String instanceURL, String apiVersion) throws Exception {
return new WaveAPIImpl(new SFConfig(true, authToken, instanceURL, apiVersion));
}


public ForceAPI forceAPI(String username, String password, String loginURL) throws Exception {
return this.forceAPI(username, password, loginURL, WaveAPIConstants.API_VERSION);
}
Expand All @@ -51,6 +60,26 @@ public ForceAPI forceAPI(String username, String password, String loginURL,
return new ForceAPIImpl(sfConfig);
}

public ForceAPI forceAPIwAuthToken(String authToken, String instanceURL) throws Exception {
return this.forceAPIwAuthToken(authToken, instanceURL, WaveAPIConstants.API_VERSION);
}

public ForceAPI forceAPIwAuthToken(String authToken, String instanceURL, String apiVersion) throws Exception {
return new ForceAPIImpl(new SFConfig(true, authToken, instanceURL, apiVersion));
}

public ForceAPI forceAPIwAuthToken(String authToken, String instanceURL,
String apiVersion, Integer batchSize) throws Exception {
return new ForceAPIImpl(new SFConfig(true, authToken, instanceURL, apiVersion, batchSize));
}

public ForceAPI forceAPIwAuthToken(String authToken, String instanceURL,
String apiVersion, Integer batchSize, Integer maxRetry) throws Exception {
SFConfig sfConfig = new SFConfig(true, authToken, instanceURL, apiVersion, batchSize);
sfConfig.setMaxRetry(maxRetry);
return new ForceAPIImpl(sfConfig);
}

public BulkAPI bulkAPI(String username, String password, String loginURL, String apiVersion) throws Exception {
SFConfig sfConfig = new SFConfig(username, password, loginURL, apiVersion);
return new BulkAPIImpl(sfConfig);
Expand All @@ -60,4 +89,15 @@ public ChatterAPI chatterAPI(String username, String password, String loginURL,
SFConfig sfConfig = new SFConfig(username, password, loginURL, apiVersion);
return new ChatterAPIImpl(sfConfig);
}

public BulkAPI bulkAPIwAuthToken(String authToken, String instanceURL, String apiVersion) throws Exception {
SFConfig sfConfig = new SFConfig(true, authToken, instanceURL, apiVersion);
return new BulkAPIImpl(sfConfig);
}

public ChatterAPI chatterAPIwAuthToken(String authToken, String instanceURL, String apiVersion) throws Exception {
SFConfig sfConfig = new SFConfig(true, authToken, instanceURL, apiVersion);
return new ChatterAPIImpl(sfConfig);
}

}
68 changes: 51 additions & 17 deletions src/main/java/com/springml/salesforce/wave/util/SFConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class SFConfig {

private String username;
private String password;
private String loginURL;
private boolean useAuthToken;
private String authToken;
private String salesforceURL;
private String apiVersion;
private Integer batchSize;
private PartnerConnection partnerConnection;
Expand All @@ -28,11 +30,27 @@ public SFConfig(String username, String password, String loginURL,
this(username, password, loginURL, apiVersion, null);
}

public SFConfig(String username, String password, String loginURL,
public SFConfig(String username, String password, String salesforceURL,
String apiVersion, Integer batchSize) {
this.useAuthToken = false;
this.username = username;
this.password = password;
this.loginURL = loginURL;
this.salesforceURL = salesforceURL;
this.apiVersion = apiVersion;
this.batchSize = batchSize;
}


public SFConfig(boolean useAuthToken, String authToken, String loginURL,
String apiVersion) {
this(useAuthToken, authToken, loginURL, apiVersion, null);
}

public SFConfig(boolean useAuthToken, String authToken, String salesforceURL,
String apiVersion, Integer batchSize) {
this.useAuthToken = true;
this.authToken = authToken;
this.salesforceURL = salesforceURL;
this.apiVersion = apiVersion;
this.batchSize = batchSize;
}
Expand All @@ -42,6 +60,7 @@ public String getUsername() {
}

public void setUsername(String username) {
this.useAuthToken = false;
this.username = username;
}

Expand All @@ -53,12 +72,19 @@ public void setPassword(String password) {
this.password = password;
}

public String getLoginURL() {
return loginURL;
public String getAuthToken() { return authToken; }

public void setAuthToken(String authToken) {
this.useAuthToken = true;
this.authToken = authToken;
}

public String getSalesforceURL() {
return salesforceURL;
}

public void setLoginURL(String loginURL) {
this.loginURL = loginURL;
public void setSalesforceURL(String salesforceURL) {
this.salesforceURL = salesforceURL;
}

public String getApiVersion() {
Expand Down Expand Up @@ -95,13 +121,22 @@ public PartnerConnection getPartnerConnection() throws Exception {

private PartnerConnection createPartnerConnection() throws Exception {
ConnectorConfig config = new ConnectorConfig();
LOG.debug("Connecting SF Partner Connection using " + username);
config.setUsername(username);
config.setPassword(password);
String authEndpoint = getAuthEndpoint(loginURL);
LOG.info("loginURL : " + authEndpoint);
config.setAuthEndpoint(authEndpoint);
config.setServiceEndpoint(authEndpoint);
if (!useAuthToken) {
LOG.debug("Connecting SF Partner Connection using " + username);
config.setUsername(username);
config.setPassword(password);
String authEndpoint = getEndpoint(salesforceURL);
LOG.info("loginURL : " + authEndpoint);
config.setAuthEndpoint(authEndpoint);
config.setServiceEndpoint(authEndpoint);

} else {
LOG.debug("Connecting SF Partner Connection using authToken");
String serviceEndpoint = getEndpoint(salesforceURL);
config.setManualLogin(true);
config.setSessionId(authToken);
config.setServiceEndpoint(serviceEndpoint);
}

try {
return Connector.newConnection(config);
Expand Down Expand Up @@ -130,11 +165,10 @@ public URI getRequestURI(PartnerConnection connection, String path, String query
path, query, null);
}

private String getAuthEndpoint(String loginURL) throws Exception {
private String getEndpoint(String loginURL) throws Exception {
URI loginURI = new URI(loginURL);

return new URI(loginURI.getScheme(), loginURI.getUserInfo(), loginURI.getHost(),
loginURI.getPort(), PATH_SOAP_ENDPOINT, null, null).toString();
loginURI.getPort(), PATH_SOAP_ENDPOINT_PRE + apiVersion, null, null).toString();
}

public void closeConnection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class WaveAPIConstants {
public static final String API_VERSION = "35.0";
public static final String PATH_WAVE_QUERY = "/wave/query";
public static final String PATH_WAVE_DATASETS = "/wave/datasets";
public static final String PATH_SOAP_ENDPOINT = "/services/Soap/u/" + API_VERSION;
public static final String PATH_SOAP_ENDPOINT_PRE = "/services/Soap/u/";
public static final String PATH_SOAP_ENDPOINT = PATH_SOAP_ENDPOINT_PRE + API_VERSION;
public static final String SERVICE_PATH_WAVE_QUERY = SERVICE_PATH + "v" + API_VERSION + PATH_WAVE_QUERY;
public static final String PATH_QUERY = "/query";
public static final String PATH_SOBJECTS = "/sobjects/";
Expand Down