|
| 1 | +import os |
| 2 | +import time |
| 3 | +import jwt |
| 4 | +import pem |
| 5 | +import requests |
| 6 | +import json |
| 7 | +import base64 |
| 8 | +from calendar import timegm |
| 9 | +from datetime import datetime, timedelta |
| 10 | +from simple_salesforce import Salesforce |
| 11 | +from os.path import join, dirname |
| 12 | +from dotenv import Dotenv |
| 13 | + |
| 14 | +""" SalesForce Authentication """ |
| 15 | +class SalesForceOAuth: |
| 16 | + |
| 17 | + """ Sets up environment values for all grant types """ |
| 18 | + def __init__(self): |
| 19 | + dotenv_path = Dotenv(join(dirname(__file__), '..', '.env')) |
| 20 | + os.environ.update(dotenv_path) |
| 21 | + |
| 22 | + self.consumer_key = os.environ.get("SALESFORCE_OAUTH_CONSUMER_TOKEN") |
| 23 | + self.consumer_secret = os.environ.get("SALESFORCE_OAUTH_CONSUMER_SECRET") |
| 24 | + self.api_domain = os.environ.get("SALESFORCE_API_DOMAIN") |
| 25 | + self.access_token_url = os.environ.get("SALESFORCE_ACCESS_TOKEN_URL") |
| 26 | + self.username = os.environ.get("SALESFORCE_USER") |
| 27 | + self.password = os.environ.get("SALESFORCE_PASSWORD") |
| 28 | + |
| 29 | + """ Determines OAuth Grant Type and runs method |
| 30 | + returns a dictionary consisinting of an instance_url and access_token |
| 31 | + """ |
| 32 | + def getAccessToken(self, grant_type): |
| 33 | + self.grant_type = grant_type |
| 34 | + if grant_type == 'password': |
| 35 | + token = self.passwordGrantType() |
| 36 | + elif grant_type == 'jwt': |
| 37 | + token = self.jwtGrantType() |
| 38 | + elif grant_type == 'web-server': |
| 39 | + self.webGrantType() |
| 40 | + else: |
| 41 | + pass |
| 42 | + |
| 43 | + return token |
| 44 | + |
| 45 | + """ Password Grant Type for user/server credentials |
| 46 | + need a client_id and client_secret |
| 47 | + returns a dictionary consisting of an instance_url and access_token |
| 48 | + used to make API calls |
| 49 | + """ |
| 50 | + def passwordGrantType(self): |
| 51 | + token = dict() |
| 52 | + headers = { |
| 53 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 54 | + } |
| 55 | + |
| 56 | + post_data = {'grant_type': self.grant_type, |
| 57 | + 'client_id': self.consumer_key, |
| 58 | + 'client_secret': self.consumer_secret, |
| 59 | + 'username': self.username, |
| 60 | + 'password': self.password } |
| 61 | + |
| 62 | + result = requests.post(self.access_token_url, data=post_data, headers=headers) |
| 63 | + data = json.loads(result.text) |
| 64 | + access_token = data['access_token'] |
| 65 | + instance_url = data['instance_url'] |
| 66 | + token = { 'instance_url' : instance_url, 'access_token' : access_token } |
| 67 | + return token |
| 68 | + |
| 69 | + """ JWT Token Grant Type - key based authentication |
| 70 | + returns a dictionary consisting of an instance_url and access_token |
| 71 | + used to make API calls |
| 72 | + """ |
| 73 | + def jwtGrantType(self): |
| 74 | + token = dict() |
| 75 | + certs = pem.parse_file(os.environ.get("SALESFORCE_CERTIFICATE")) |
| 76 | + certfile = os.environ.get("SALESFORCE_PRIVATE_KEY") |
| 77 | + |
| 78 | + with open(certfile, "r") as my_cert_file: |
| 79 | + cert = my_cert_file.read() |
| 80 | + |
| 81 | + payload = { |
| 82 | + 'alg': 'RS256', |
| 83 | + 'iss': self.consumer_key, |
| 84 | + 'sub': self.username, |
| 85 | + 'aud': 'https://login.salesforce.com', |
| 86 | + 'exp': timegm(datetime.utcnow().utctimetuple()) |
| 87 | + } |
| 88 | + pay_string = (str(payload)) |
| 89 | + |
| 90 | + encoded = jwt.encode(payload, cert, algorithm='RS256') |
| 91 | + headers = { |
| 92 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 93 | + } |
| 94 | + grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer' |
| 95 | + post_data = { 'grant_type': grant_type, |
| 96 | + 'assertion': encoded |
| 97 | + } |
| 98 | + |
| 99 | + result = requests.post(self.access_token_url, data=post_data, headers=headers) |
| 100 | + data = json.loads(result.text) |
| 101 | + access_token = data['access_token'] |
| 102 | + instance_url = data['instance_url'] |
| 103 | + token = { 'instance_url' : instance_url, 'access_token' : access_token } |
| 104 | + return token |
| 105 | + |
| 106 | + |
| 107 | + def webGrantType(self): |
| 108 | + pass |
| 109 | + |
| 110 | + def decodeToken(self): |
| 111 | + pass |
| 112 | + ##TODO: |
| 113 | + #public_key="private/public.key" |
| 114 | + #with open(pub_file, "r") as public_key: |
| 115 | + # pub = pub_file.read() |
| 116 | + #decoded = jwt.decode(encoded, pub, audience="https://login.salesforce.com") |
| 117 | + #print(decoded) |
| 118 | + |
| 119 | +sf = SalesForceOAuth() |
| 120 | +##jwt_token = sf.getAccessToken('jwt') |
| 121 | +##print(jwt_token) |
| 122 | +password_token = sf.getAccessToken('password') |
| 123 | +print(password_token) |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments