def __init__(self, config):
        self.config = config

        print 'Getting ssl context for oauth server'
        self.ctx = tools.get_ssl_context(self.config)
        self.__init_config()
        self.client_data = None
def call_api():
    """
    Call an api using the Access Token
    :return: the index template with the data from the api in the parameter 'data'
    """

    if 'session_id' in session:
        user = _session_store.get(session['session_id'])
        if not user:
            return redirect_with_baseurl('/')
        if 'api_endpoint' in _config:
            user.api_response = None
            if "front-end" in request.args and user.front_end_access_token:
                access_token = user.front_end_access_token
            elif user.access_token:
                access_token = user.access_token
            else:
                user.api_response = None
                print 'No access token in session'

                return redirect_with_baseurl("/")

            user_sub = get_item_from_json(user.id_token_json, 1, 'sub')
            # req_string = _config['api_endpoint'] % user.id_token_json[1]['sub']
            req_string = _config['api_endpoint'] % user_sub
            bearer_map = {
                'iss': _config['client_id'],
                'aud': _config['api_audience']
            }
            bearer_token = JwtCreator(bearer_map, 'HS256',
                                      _config).sign_compact()

            try:
                req = urllib2.Request(req_string)
                req.add_header('User-Agent', 'CurityExample/1.0')
                req.add_header("Authorization", "Bearer %s" % bearer_token)
                req.add_header("Accept", 'application/json')
                response = urllib2.urlopen(
                    req, context=tools.get_ssl_context(_config))
                user.api_response = {
                    'code': response.code,
                    'data': response.read()
                }
            except urllib2.HTTPError as e:
                user.api_response = {'code': e.code, 'data': e.read()}
            except Exception as e:
                message = e.message if len(e.message) > 0 else "unknown error"
                user.api_response = {"code": "unknown error", "data": message}
        else:
            user.api_response = None
            print 'No API endpoint configured'

    return redirect_with_baseurl('/')
예제 #3
0
def call_api():
    """
    Call an api using the Access Token
    :return: the index template with the data from the api in the parameter 'data'
    """

    if 'session_id' in session:
        user = _session_store.get(session['session_id'])
        if not user:
            return redirect_with_baseurl('/')
        if 'api_endpoint' in _config:
            user.api_response = None
            if "front-end" in request.args and user.front_end_access_token:
                access_token = user.front_end_access_token
            elif user.access_token:
                access_token = user.access_token
            else:
                user.api_response = None
                print 'No access token in session'

                return redirect_with_baseurl("/")

            try:
                req = urllib2.Request(_config['api_endpoint'])
                req.add_header('User-Agent', 'CurityExample/1.0')
                req.add_header("Authorization", "Bearer %s" % access_token)
                req.add_header("Accept", 'application/json')

                if 'subscription_key' in _config:
                    req.add_header('Ocp-Apim-Subscription-Key',
                                   _config['subscription_key'])
                    req.add_header('Ocp-Apim-Trace', 'true')

                response = urllib2.urlopen(
                    req, context=tools.get_ssl_context(_config))
                user.api_response = {
                    'code': response.code,
                    'data': response.read()
                }
            except urllib2.HTTPError as e:
                user.api_response = {'code': e.code, 'data': e.read()}
            except Exception as e:
                message = e.message if len(e.message) > 0 else "unknown error"
                user.api_response = {"code": "unknown error", "data": message}
        else:
            user.api_response = None
            print 'No API endpoint configured'

    return redirect_with_baseurl('/')
    def __init__(self, config):
        print 'Getting ssl context for jwks_uri'
        self.ctx = get_ssl_context(config)

        self.jwks_uri = config['jwks_uri']
        self.jwks = self.load_keys()