"""Authorization Models. The authorization table in dynamodb contains the authorization token and the different information tied to this request (such as a timestamp of when it was created). """ import json import time from warnings import warn from owsrequest import connectors from owsrequest.config import logger from owsrequest.constants.environment import PROD_ENVIRONMENT from owsrequest.constants.environment import QA_ENVIRONMENT from owsrequest.utils import json as json_formatter TABLE_NAME = '{environment}-ows-request-authorization' def get_table(environment): """Get the table for the environment. Args: environment (str): the environment. Return: Table: the dynamodb table object. """ warn('HMAC Authorization is being deprecated', PendingDeprecationWarning, stacklevel=2) environment = (environment or '').lower() if environment not in [QA_ENVIRONMENT, PROD_ENVIRONMENT]: environment = QA_ENVIRONMENT return connectors.dynamodb.Table( TABLE_NAME.format(environment=environment)) def save_authorization(environment, hmac, sender, recipient, secret): """Save Authorization Token. Args: environment (str): the environment. hmac (str): the token used for the authorization. sender (str): the issuer of the token. recipient (str): the service that will receive the token. secret (str): the secret used to generate the HMAC. """ warn('HMAC Authorization is being deprecated', PendingDeprecationWarning, stacklevel=2) start_time = time.time() response = get_table(environment).put_item( Item={ 'authorization': '{recipient}:{hmac}'.format( recipient=recipient, hmac=hmac), 'sender': sender, 'created_at': int(start_time), 'secret': secret }) end_time = time.time() if request_latency(start_time, end_time): logger.info( 'Request having latency while saving Authorization with ' 'Sender- {sender} Recipient- {recipient}: '.format( recipient=recipient, sender=sender) + json.dumps( response, cls=json_formatter.DecimalEncoder)) def get_authorization(environment, token): """Get an authorization from a token. Args: environment (str): name of the environment. token (str): the token that's used to save the specific record. Returns: dict: the result of the authorization. """ warn('HMAC Authorization is being deprecated', PendingDeprecationWarning, stacklevel=2) # If the full token is provided, {sender}/{recipient}:{hmac}, we only care # about the end. sender = token.split('/')[0] token = token.split('/')[-1] recipient = token.split(':')[0] start_time = time.time() data = get_table(environment).get_item( Key={'authorization': token}, ConsistentRead=True) end_time = time.time() if request_latency(start_time, end_time): logger.info( 'Request having latency while fetching Authorization with ' 'Sender- {sender} Recipient- {recipient}: '.format( recipient=recipient, sender=sender) + json.dumps( data, cls=json_formatter.DecimalEncoder)) return data.get('Item', {}) def request_latency(start_time, end_time): """Check if request is taing more than 1 sec. Args: start_time (int): start_time of request end_time (int): end_time of request Returns: bool: the result request latency check. """ warn('HMAC Authorization is being deprecated', PendingDeprecationWarning, stacklevel=2) return end_time - start_time > 1