Beispiel #1
0
def revoke(storyid):
    auth = get_authorization(request)
    try:
        query(db.revoke_story, auth, storyid)
        return app.response_class(status=200)
    except DBError as e:
        return app.response_class(e.response,
                                  e.status,
                                  mimetype='application/json')
def compile_titles():
    titles = db.query(db.generate_titles_json)
    bucket = storage_client.bucket(BUCKET_NAME)
    blob = bucket.blob("titles.json")
    try:
        blob.delete()
    except NotFound:
        pass
    blob = bucket.blob("titles.json")
    blob.upload_from_string(titles)
Beispiel #3
0
def all_stories():
    auth = get_authorization(request)
    try:
        stories = query(db.get_all, auth)
        return app.response_class(json.dumps(stories),
                                  mimetype='application/json')
    except DBError as e:
        return app.response_class(e.response,
                                  e.status,
                                  mimetype='application/json')
Beispiel #4
0
def pending():
    auth = get_authorization(request)
    try:
        pending = query(db.get_pending, auth)
        return app.response_class(json.dumps(pending),
                                  mimetype='application/json')
    except DBError as e:
        return app.response_class(e.response,
                                  e.status,
                                  mimetype='application/json')
Beispiel #5
0
def get_story(storyid):
    auth = get_authorization(request)
    try:
        result = query(db.get_story, auth, storyid)
        return app.response_class(json.dumps(result),
                                  mimetype='application/json')
    except DBError as e:
        return app.response_class(e.response,
                                  e.status,
                                  mimetype='application/json')
Beispiel #6
0
def login():
    code = request.json.get('code')
    if code:
        payload = {
            'grant_type': 'authorization_code',
            'code': code,
            'client_id': CLIENT_ID,
            'client_secret': CLIENT_SECRET
        }
        r = requests.post('https://api.amazon.com/auth/o2/token', data=payload)
        if r.status_code == 200:
            response = r.json()
            user_info = requests.get(
                'https://api.amazon.com/user/profile?access_token=%s' %
                response['access_token'])
            if user_info.status_code == 200:
                user_data = user_info.json()
                token = secrets.token_urlsafe(32)
                try:
                    access_level = query(db.login, user_data['user_id'],
                                         user_data['name'], user_data['email'],
                                         token)
                    return app.response_class(json.dumps({
                        'token': token,
                        'access': access_level
                    }),
                                              200,
                                              mimetype='application/json')
                except DBError as e:
                    return app.response_class(e.response,
                                              e.status,
                                              mimetype='application/json')
            else:
                return app.response_class(
                    status=status.HTTP_503_SERVICE_UNAVAILABLE,
                    response=json.dumps({
                        'error':
                        ' Unable to get user information from Amazon'
                    }),
                    mimetype='application/json')
    return app.response_class(status=400,
                              response=json.dumps({
                                  'error':
                                  'No code was provided for authentication'
                              }),
                              mimetype='application/json')