示例#1
0
def google_auth_verify():
    """Finalize google authorization"""
    try:
        if 'error' in request.args:
            raise Exception(
                _format_err('Error getting authorization',
                            request.args.get('error')))

        code = _request_get_required('code')

        flow = OAuth2WebServerFlow(settings.GOOGLE_CLIENT_ID,
                                   settings.GOOGLE_CLIENT_SECRET,
                                   _GOOGLE_OAUTH_SCOPES,
                                   redirect_uri=_build_oauth_redirect(
                                       request, url_for('google_auth_verify')))
        credentials = flow.step2_exchange(code)
        # ^ this is an oauth2client.client.OAuth2Credentials object

        # Get user info
        userinfo = google.get_userinfo(
            google.get_userinfo_service(credentials))
        if not userinfo:
            raise Exception('Could not get Google user info')

        info = {
            'id': userinfo.get('id'),
            'name': userinfo.get('name'),
            'credentials': credentials.to_json()
        }
        if not info['id']:
            raise Exception('Could not get Google user ID')

        if 'stg-storymap.knightlab.com' in domains and not info[
                'id'] in allowed_ids:
            print('User id not in ALLOWED_IDS:  %s ' % info['id'])
            raise Exception(
                'You are not authorized to access this page. Please send the following information to [email protected]: storymap.knilab.com unauthorized %s'
                % info['id'])

        # Upsert user record
        uid = _get_uid('google:' + info['id'])
        user = get_user(uid)
        if user:
            user['google'] = info
        else:
            user = {'uid': uid, 'migrated': 0, 'storymaps': {}, 'google': info}
        user['uname'] = info['name']
        save_user(user)

        # Update session
        session['uid'] = uid
        url = url_for('select')

        app.logger.info("google_auth_verify url: {}".format(url))
        return redirect(url)
    except Exception as e:
        traceback.print_exc()
        return jsonify({'error': str(e)})
示例#2
0
def google_auth_verify():
    """Finalize google authorization"""
    try:
        if 'error' in request.args:
            raise Exception(_format_err(
                'Error getting authorization', request.args.get('error')))

        code = _request_get_required('code')

        flow = OAuth2WebServerFlow(
            settings.GOOGLE_CLIENT_ID,
            settings.GOOGLE_CLIENT_SECRET,
            _GOOGLE_OAUTH_SCOPES,
            redirect_uri='https://'+request.host+url_for('google_auth_verify')
        )
        credentials = flow.step2_exchange(code)
        # ^ this is an oauth2client.client.OAuth2Credentials object

        # Get user info
        userinfo = google.get_userinfo(
            google.get_userinfo_service(credentials))
        if not userinfo:
            raise Exception('Could not get Google user info')

        info = {
            'id': userinfo.get('id'),
            'name': userinfo.get('name'),
            'credentials': credentials.to_json()
        }
        if not info['id']:
            raise Exception('Could not get Google user ID')

        # Upsert user record
        uid = _get_uid('google:'+info['id'])

        user = _user.find_one({'uid': uid})
        if user:
            user['google'] = info
        else:
            user = {
                'uid': uid,
                'migrated': 0,
                'storymaps': {},
                'google': info
            }
        user['uname'] = info['name']
        _user.save(user)

        # Update session
        session['uid'] = uid

        return redirect(url_for('select'))
    except Exception, e:
        traceback.print_exc()
        return jsonify({'error': str(e)})
示例#3
0
文件: api.py 项目: jorol/StoryMapJS
def google_auth_verify():
    """Finalize google authorization"""
    try:
        if "error" in request.args:
            raise Exception(_format_err("Error getting authorization", request.args.get("error")))

        code = _request_get_required("code")

        flow = OAuth2WebServerFlow(
            settings.GOOGLE_CLIENT_ID,
            settings.GOOGLE_CLIENT_SECRET,
            _GOOGLE_OAUTH_SCOPES,
            redirect_uri="https://" + request.host + url_for("google_auth_verify"),
        )
        credentials = flow.step2_exchange(code)
        # ^ this is an oauth2client.client.OAuth2Credentials object

        # Get user info
        userinfo = google.get_userinfo(google.get_userinfo_service(credentials))
        if not userinfo:
            raise Exception("Could not get Google user info")

        info = {"id": userinfo.get("id"), "name": userinfo.get("name"), "credentials": credentials.to_json()}
        if not info["id"]:
            raise Exception("Could not get Google user ID")

        # Upsert user record
        uid = _get_uid("google:" + info["id"])

        user = _user.find_one({"uid": uid})
        if user:
            user["google"] = info
        else:
            user = {"uid": uid, "migrated": 0, "storymaps": {}, "google": info}
        user["uname"] = info["name"]
        _user.save(user)

        # Update session
        session["uid"] = uid

        return redirect(url_for("select"))
    except Exception, e:
        traceback.print_exc()
        return jsonify({"error": str(e)})