def validate_api_key(api_key_in_headers, api_key_query_params): """ Validate the provided API key. :param api_key_in_headers: API key provided via headers. :type api_key_in_headers: ``str`` :param api_key_query_params: API key provided via query params. :type api_key_query_params: ``str`` :return: TokenDB object on success. :rtype: :class:`.ApiKeyDB` """ if not api_key_in_headers and not api_key_query_params: LOG.audit('API key is not found in header or query parameters.') raise exceptions.ApiKeyNotProvidedError('API key is not provided.') if api_key_in_headers: LOG.audit('API key provided in headers') if api_key_query_params: LOG.audit('API key provided in query parameters') api_key = api_key_in_headers or api_key_query_params api_key_db = ApiKey.get(api_key) if not api_key_db.enabled: raise exceptions.ApiKeyDisabledError('API key is disabled.') LOG.audit('API key with id "%s" is validated.' % (api_key_db.id)) return api_key_db
def validate_api_key(api_key): """ Validate the provided API key. :param api_key: API key provided. :type api_key: ``str`` :return: TokenDB object on success. :rtype: :class:`.ApiKeyDB` """ api_key_db = ApiKey.get(api_key) if not api_key_db.enabled: raise exceptions.ApiKeyDisabledError('API key is disabled.') LOG.audit('API key with id "%s" is validated.' % (api_key_db.id)) return api_key_db