2525import io .cdap .plugin .salesforce .SalesforceConstants ;
2626import io .cdap .plugin .salesforce .authenticator .AuthenticatorCredentials ;
2727
28+ import javax .annotation .Nullable ;
29+
2830/**
2931 * Base configuration for Salesforce Streaming and Batch plugins
3032 */
@@ -50,19 +52,27 @@ public class BaseSalesforceConfig extends ReferencePluginConfig {
5052 @ Macro
5153 private String password ;
5254
55+ @ Name (SalesforceConstants .PROPERTY_SECURITY_TOKEN )
56+ @ Description ("Salesforce security token" )
57+ @ Nullable
58+ @ Macro
59+ private String securityToken ;
60+
5361 @ Name (SalesforceConstants .PROPERTY_LOGIN_URL )
5462 @ Description ("Endpoint to authenticate to" )
5563 @ Macro
5664 private String loginUrl ;
5765
5866 public BaseSalesforceConfig (String referenceName , String consumerKey , String consumerSecret ,
59- String username , String password , String loginUrl ) {
67+ String username , String password , String loginUrl ,
68+ @ Nullable String securityToken ) {
6069 super (referenceName );
6170 this .consumerKey = consumerKey ;
6271 this .consumerSecret = consumerSecret ;
6372 this .username = username ;
6473 this .password = password ;
6574 this .loginUrl = loginUrl ;
75+ this .securityToken = securityToken ;
6676 }
6777
6878 public String getConsumerKey () {
@@ -78,7 +88,7 @@ public String getUsername() {
7888 }
7989
8090 public String getPassword () {
81- return password ;
91+ return constructPasswordWithToken ( password , securityToken ) ;
8292 }
8393
8494 public String getLoginUrl () {
@@ -96,7 +106,7 @@ public void validate(FailureCollector collector) {
96106 }
97107
98108 public AuthenticatorCredentials getAuthenticatorCredentials () {
99- return SalesforceConnectionUtil .getAuthenticatorCredentials (username , password ,
109+ return SalesforceConnectionUtil .getAuthenticatorCredentials (username , getPassword () ,
100110 consumerKey , consumerSecret , loginUrl );
101111 }
102112
@@ -111,7 +121,8 @@ public boolean canAttemptToEstablishConnection() {
111121 || containsMacro (SalesforceConstants .PROPERTY_CONSUMER_SECRET )
112122 || containsMacro (SalesforceConstants .PROPERTY_USERNAME )
113123 || containsMacro (SalesforceConstants .PROPERTY_PASSWORD )
114- || containsMacro (SalesforceConstants .PROPERTY_LOGIN_URL ));
124+ || containsMacro (SalesforceConstants .PROPERTY_LOGIN_URL )
125+ || containsMacro (SalesforceConstants .PROPERTY_SECURITY_TOKEN ));
115126 }
116127
117128 private void validateConnection () {
@@ -125,4 +136,12 @@ private void validateConnection() {
125136 throw new RuntimeException ("There was issue communicating with Salesforce. " + e .getMessage (), e );
126137 }
127138 }
139+
140+ private String constructPasswordWithToken (String password , @ Nullable String securityToken ) {
141+ if (securityToken != null && !securityToken .isEmpty () && !password .endsWith (securityToken )) {
142+ return password + securityToken ;
143+ } else {
144+ return password ;
145+ }
146+ }
128147}
0 commit comments