Skip to content

Commit

Permalink
Do not require security token
Browse files Browse the repository at this point in the history
The original extractor from Salesforce requires a security token to
login. It appears our Salesforce setup does not have such token and we
can't just add a blank one. That's why we had to remove this token from
the checks.
  • Loading branch information
ivanovyordan committed May 15, 2023
1 parent 10143d1 commit be8644d
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions tap_salesforce/salesforce/credentials.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import threading
import logging
import requests
import threading
from collections import namedtuple
from simple_salesforce import SalesforceLogin

import requests
from simple_salesforce import SalesforceLogin

LOGGER = logging.getLogger(__name__)


OAuthCredentials = namedtuple('OAuthCredentials', (
"client_id",
"client_secret",
"refresh_token"
))
OAuthCredentials = namedtuple(
"OAuthCredentials", ("client_id", "client_secret", "refresh_token")
)

PasswordCredentials = namedtuple('PasswordCredentials', (
"username",
"password",
"security_token"
))
PasswordCredentials = namedtuple(
"PasswordCredentials",
(
"username",
"password",
),
)


def parse_credentials(config):
Expand All @@ -30,7 +30,7 @@ def parse_credentials(config):
raise Exception("Cannot create credentials from config.")


class SalesforceAuth():
class SalesforceAuth:
def __init__(self, credentials, is_sandbox=False):
self.is_sandbox = is_sandbox
self._credentials = credentials
Expand All @@ -49,8 +49,10 @@ def rest_headers(self):

@property
def bulk_headers(self):
return {"X-SFDC-Session": self._access_token,
"Content-Type": "application/json"}
return {
"X-SFDC-Session": self._access_token,
"Content-Type": "application/json",
}

@property
def instance_url(self):
Expand All @@ -73,48 +75,51 @@ class SalesforceAuthOAuth(SalesforceAuth):

@property
def _login_body(self):
return {'grant_type': 'refresh_token', **self._credentials._asdict()}
return {"grant_type": "refresh_token", **self._credentials._asdict()}

@property
def _login_url(self):
login_url = 'https://login.salesforce.com/services/oauth2/token'
login_url = "https://login.salesforce.com/services/oauth2/token"

if self.is_sandbox:
login_url = 'https://test.salesforce.com/services/oauth2/token'
login_url = "https://test.salesforce.com/services/oauth2/token"

return login_url

def login(self):
try:
LOGGER.info("Attempting login via OAuth2")

resp = requests.post(self._login_url,
data=self._login_body,
headers={"Content-Type": "application/x-www-form-urlencoded"})
resp = requests.post(
self._login_url,
data=self._login_body,
headers={"Content-Type": "application/x-www-form-urlencoded"},
)

resp.raise_for_status()
auth = resp.json()

LOGGER.info("OAuth2 login successful")
self._access_token = auth['access_token']
self._instance_url = auth['instance_url']
self._access_token = auth["access_token"]
self._instance_url = auth["instance_url"]
except Exception as e:
error_message = str(e)
if resp:
error_message = error_message + ", Response from Salesforce: {}".format(resp.text)
error_message = error_message + ", Response from Salesforce: {}".format(
resp.text
)
raise Exception(error_message) from e
finally:
LOGGER.info("Starting new login timer")
self.login_timer = threading.Timer(self.REFRESH_TOKEN_EXPIRATION_PERIOD, self.login)
self.login_timer = threading.Timer(
self.REFRESH_TOKEN_EXPIRATION_PERIOD, self.login
)
self.login_timer.start()


class SalesforceAuthPassword(SalesforceAuth):
def login(self):
login = SalesforceLogin(
sandbox=self.is_sandbox,
**self._credentials._asdict()
)
login = SalesforceLogin(sandbox=self.is_sandbox, **self._credentials._asdict())

self._access_token, host = login
self._instance_url = "https://" + host

0 comments on commit be8644d

Please sign in to comment.