Skip to content
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

[#3970]Feature/Docusign Auth Module #4100

Merged
merged 1 commit into from
Aug 29, 2023
Merged
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
63 changes: 63 additions & 0 deletions cla-backend/cla/docusign_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright The Linux Foundation and each contributor to CommunityBridge.
# SPDX-License-Identifier: MIT

"""
docusign_auth.py contains all necessary objects and functions to perform authentication and authorization.
"""


import requests
import os
import jwt
from time import time
import cla
import math


INTEGRATION_KEY = cla.config.DOCUSIGN_INTEGRATOR_KEY
INTEGRATION_SECRET = cla.config.DOCUSIGN_PRIVATE_KEY
USER_ID = cla.config.DOCUSIGN_USER_ID
OAUTH_BASE_URL = os.environ.get('DOCUSIGN_AUTH_SERVER')


def request_access_token() -> str:
"""
Requests an access token from the DocuSign OAuth2 service.
"""
try:
cla.log.debug('Requesting access token from DocuSign OAuth2 service...')
url = f'https://{OAUTH_BASE_URL}/oauth/token'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
claims = {
"iss": INTEGRATION_KEY,
"sub": USER_ID,
"aud": OAUTH_BASE_URL,
"iat": time(),
"exp": time() + 3600,
"scope": "signature impersonation"
}
cla.log.debug(f'Claims: {claims}')
encoded_jwt = jwt.encode(claims, INTEGRATION_SECRET.encode(), algorithm='RS256')

payload = {
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion': encoded_jwt
}

response = requests.post(url, headers=headers, data=payload)
data = response.json()
if 'token_type' in data and 'access_token' in data:
cla.log.debug('Successfully requested access token from DocuSign OAuth2 service.')
return data['access_token']
else:
cla.log.error('Unable to request access token from DocuSign OAuth2 service: ' + str(data))
raise Exception('Unable to request access token from DocuSign OAuth2 service: ' + str(data))

except Exception as err:
cla.log.error('Unable to request access token from DocuSign OAuth2 service: ' + str(err))
raise err



Loading