# Path: data_catalog/configuration.py
# class DCConfig(object):
# 
#     """
#     Contains a configuration of the Data Catalog.
#     No fields should  be written to be the user.
#     """
# 
#     def __init__(self):
#         """
#         Reads the configuration from environment variables set by Cloud Foundry (CF)
#         or by the developer when running locally.
#         Normally, CF sets the full configuration in environment variables,
#         e.g. VCAP_SERVICES (which is most important for us).
#         When working on a local machine, VCAP_SERVICES also needs to be set and contain some values.
#         Without them, the app couldn't work.
#         Most of configuration values have some defaults that will be set when they aren't configured
#         in the environment, but some of them, like the address of the authorization server can't
#         have a default. They need to be set manually by the developer to enable local work.
#         """
#         self._fail_if_no_configuration()
# 
#         self.app_base_path = '/rest/datasets'
# 
#         # if values aren't found we set defaults that are meant for local operations
#         self.app_port = int(os.getenv(VCAP_APP_PORT, '5000'))
#         self.log_level = os.getenv(LOG_LEVEL, 'DEBUG')
# 
#         services_config = json.loads(os.environ[VCAP_SERVICES])
#         self.elastic = ElasticConfig(services_config)
#         self.services_url = ServiceUrlsConfig(services_config)
# 
#     @staticmethod
#     def _fail_if_no_configuration():
#         """
#         Checks the environment variables to decide if the app can run.
#         :rtype: None
#         :raises NoConfigEnvError: When VCAP_SERVICES is not present and the app can't run.
#         """
#         if not VCAP_SERVICES in os.environ:
#             raise NoConfigEnvError(
#                 'VCAP_SERVICES environment variable needs to be set to run Data Catalog.')


import json
import logging
import requests
import flask
import jwt
import jwt.exceptions
from werkzeug.exceptions import BadRequest
from flask_restful import abort
from data_catalog.configuration import DCConfig
    ALGORITHMS = {
        'HS256': 'HS256', 'SHA256WITHHMAC': 'HS256',
        'HS384': 'HS384', 'SHA384WITHHMAC': 'HS384',
        'HS512': 'HS512', 'SHA512WITHHMAC': 'HS512',
        'ES256': 'ES256', 'SHA256WITHECDSA': 'ES256',
        'ES384': 'ES384', 'SHA384WITHECDSA': 'ES384',
        'ES512': 'ES512', 'SHA512WITHECDSA': 'ES512',
        'RS256': 'RS256', 'SHA256WITHRSA': 'RS256',
        'RS384': 'RS384', 'SHA384WITHRSA': 'RS384',
        'RS512': 'RS512', 'SHA512WITHRSA': 'RS512',
    }

    def parse(self, public_key):
        key = public_key['value']
        alg = self._decode_token_sign_alg(public_key['alg'])
        return key, alg

    def _decode_token_sign_alg(self, alg):
        alg = alg.upper()
        if alg in self.ALGORITHMS:
            return self.ALGORITHMS.get(alg)
        else:
            raise Exception('"{}" is not on the list of known algorithms: {}'
                            .format(alg, str(self.ALGORITHMS.keys())))


class _Authorization(object):

    def __init__(self):
        self._log = logging.getLogger(type(self).__name__)

# Path: data_catalog/configuration.py
# class DCConfig(object):
# 
#     """
#     Contains a configuration of the Data Catalog.
#     No fields should  be written to be the user.
#     """
# 
#     def __init__(self):
#         """
#         Reads the configuration from environment variables set by Cloud Foundry (CF)
#         or by the developer when running locally.
#         Normally, CF sets the full configuration in environment variables,
#         e.g. VCAP_SERVICES (which is most important for us).
#         When working on a local machine, VCAP_SERVICES also needs to be set and contain some values.
#         Without them, the app couldn't work.
#         Most of configuration values have some defaults that will be set when they aren't configured
#         in the environment, but some of them, like the address of the authorization server can't
#         have a default. They need to be set manually by the developer to enable local work.
#         """
#         self._fail_if_no_configuration()
# 
#         self.app_base_path = '/rest/datasets'
# 
#         # if values aren't found we set defaults that are meant for local operations
#         self.app_port = int(os.getenv(VCAP_APP_PORT, '5000'))
#         self.log_level = os.getenv(LOG_LEVEL, 'DEBUG')
# 
#         services_config = json.loads(os.environ[VCAP_SERVICES])
#         self.elastic = ElasticConfig(services_config)
#         self.services_url = ServiceUrlsConfig(services_config)
# 
#     @staticmethod
#     def _fail_if_no_configuration():
#         """
#         Checks the environment variables to decide if the app can run.
#         :rtype: None
#         :raises NoConfigEnvError: When VCAP_SERVICES is not present and the app can't run.
#         """
#         if not VCAP_SERVICES in os.environ:
#             raise NoConfigEnvError(
#                 'VCAP_SERVICES environment variable needs to be set to run Data Catalog.')


# Path: data_catalog/auth.py
import json
import logging
import requests
import flask
import jwt
import jwt.exceptions
from werkzeug.exceptions import BadRequest
from flask_restful import abort
from data_catalog.configuration import DCConfig

    ALGORITHMS = {
        'HS256': 'HS256', 'SHA256WITHHMAC': 'HS256',
        'HS384': 'HS384', 'SHA384WITHHMAC': 'HS384',
        'HS512': 'HS512', 'SHA512WITHHMAC': 'HS512',
        'ES256': 'ES256', 'SHA256WITHECDSA': 'ES256',
        'ES384': 'ES384', 'SHA384WITHECDSA': 'ES384',
        'ES512': 'ES512', 'SHA512WITHECDSA': 'ES512',
        'RS256': 'RS256', 'SHA256WITHRSA': 'RS256',
        'RS384': 'RS384', 'SHA384WITHRSA': 'RS384',
        'RS512': 'RS512', 'SHA512WITHRSA': 'RS512',
    }

    def parse(self, public_key):
        key = public_key['value']
        alg = self._decode_token_sign_alg(public_key['alg'])
        return key, alg

    def _decode_token_sign_alg(self, alg):
        alg = alg.upper()
        if alg in self.ALGORITHMS:
            return self.ALGORITHMS.get(alg)
        else:
            raise Exception('"{}" is not on the list of known algorithms: {}'
                            .format(alg, str(self.ALGORITHMS.keys())))


class _Authorization(object):

    def __init__(self):
        self._log = logging.getLogger(type(self).__name__)

        self._config = DCConfig()