File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/lib/util_bandwidth.py
from flask import (session,
current_app as app)
import base64
from buyercall.lib.util_crypto import AESCipher
import logging
log = logging.getLogger(__name__)
def authenticate_bw_request(auth_header):
try:
# Fetch username & password from request header
auth = auth_header.get('Authorization').replace('Basic ', '').replace(' ', '')
auth_decoded = base64.b64decode(auth).decode("ascii").split(':')
# Set variables by assigning username & password from the config file
request_usr = auth_decoded[0]
request_pwd = auth_decoded[1]
# Set variables by assigning username & password from the config file
config_usr = app.config['BANDWIDTH_CALLBACK_USERNAME']
config_pwd = app.config['BANDWIDTH_CALLBACK_PASSWORD']
# Authenticate the request by pairing username & password from config file
if request_usr == config_usr and request_pwd == config_pwd:
return True
else:
return False
except Exception as e:
log.info('Bandwidth request authentication failed. Exception: {}'.format(e))
return False
def bw_client(partnership_id, request_type=None, tn_type=None):
partnership = partnership_id
from buyercall.blueprints.partnership.models import PartnershipCpaasProviders
from buyercall.lib.bandwidth_api_v2 import Bandwidth
# Get the encryption key to decrypt credentials
encrypt_key = app.config['CRYPTO_SECRET_KEY']
# Retrieve the Bandwidth credentials
partner_cpaas = PartnershipCpaasProviders.partnership_bandwidth_credentials(partnership)
cipher = AESCipher(encrypt_key)
# Decrypt the credentials
decrypted_api_username = cipher.decrypt(partner_cpaas.cpaas_api_username)
decrypted_api_password = cipher.decrypt(partner_cpaas.cpaas_api_password)
if tn_type == 'mobile':
decrypted_account_id = app.config['SIP_BANDWIDTH_ACCOUNT_ID']
site_id = app.config['SIP_BANDWIDTH_SITE_ID']
location_id = app.config['SIP_BANDWIDTH_LOCATION_ID']
voice_application_id = app.config['SIP_BANDWIDTH_VOICE_APPLICATION_ID']
sms_application_id = app.config['SIP_BANDWIDTH_SMS_APPLICATION_ID']
decrypted_cnam_password = app.config['SIP_BANDWIDTH_CNAM_PASSWORD']
else:
decrypted_account_id = cipher.decrypt(partner_cpaas.cpaas_account_id)
if partner_cpaas.cpaas_cnam_password:
decrypted_cnam_password = cipher.decrypt(partner_cpaas.cpaas_cnam_password)
else:
decrypted_cnam_password = partner_cpaas.cpaas_cnam_password
site_id = partner_cpaas.cpaas_site_id
location_id = partner_cpaas.cpaas_location_id
sms_application_id = partner_cpaas.cpaas_sms_application_id
voice_application_id = partner_cpaas.cpaas_voice_application_id
client = Bandwidth(
request_type,
decrypted_api_username,
decrypted_api_password,
decrypted_account_id,
site_id,
location_id,
sms_application_id,
voice_application_id,
decrypted_cnam_password
)
return client