

@app.route('/token', methods=['POST', 'GET'])
def handleToken():
    app.logger.info(('handleToken [%s]' % request.method))
    if (request.method == 'GET'):
        access_token = request.headers.get('Authorization')
        if access_token:
            access_token = access_token.replace('Bearer ', '')
        (me, client_id, scope) = checkAccessToken(access_token)
        if ((me is None) or (client_id is None)):
            return ('Token is not valid', 400, {
                
            })
        else:
            params = {
                'me': me,
                'client_id': client_id,
            }
            if (scope is not None):
                params['scope'] = scope
            return (urllib.urlencode(params), 200, {
                'Content-Type': 'application/x-www-form-urlencoded',
            })
    elif (request.method == 'POST'):
        code = request.form.get('code')
        me = request.form.get('me')
        redirect_uri = request.form.get('redirect_uri')
        client_id = request.form.get('client_id')
        state = request.form.get('state')
        r = ninka.indieauth.validateAuthCode(code=code, client_id=me, state=state, redirect_uri=redirect_uri)
        if (r['status'] == requests.codes.ok):
            app.logger.info('token request auth code verified')
            scope = r['response']['scope']
            key = ('app-%s-%s-%s' % (me, client_id, scope))
            token = db.get(key)
            if (token is None):
                token = str(uuid.uuid4())
                token_key = ('token-%s' % token)
                db.set(key, token)
                db.set(token_key, key)
            app.logger.info(('[%s] [%s]' % (key, token)))
            params = {
                'me': me,
                'scope': scope,
                'access_token': token,
            }
            return (urllib.urlencode(params), 200, {
                'Content-Type': 'application/x-www-form-urlencoded',
            })

dataset/ETHPy150Open bear/python-indieweb/indieweb.py handleToken/original