Exemplo n.º 1
0
def authorize(name):
    ui_flag = session.pop('ui', None)

    client = current_auth.create_client(name)
    try:
        token = client.authorize_access_token()
    except HTTPException:
        return render_template(
            current_app.config['AUTHENTICATION_POPUP_TEMPLATE'],
            msg=f'Access not provided to {name} service.'), 400

    configs = OAUTH_SERVICES.get(name.upper(), {})
    extra_data_method = configs.get('extra_data_method')

    # TOFIX Add error handlers for reject, auth errors, etc
    extra_data = {}
    if extra_data_method:
        extra_data = extra_data_method(client, token)

    _token = _create_or_update_token(name, token)
    _token.extra_data = extra_data

    db.session.add(_token)

    # Add extra data to user profile.
    # If user profile doesn't exist yet, it creates one.
    _profile = UserProfile.get_by_userid(current_user.id)
    if not _profile:
        _profile = UserProfile(user_id=current_user.id)
        db.session.add(_profile)

    profile_data = get_oauth_profile(name, token=_token, client=client)

    if _profile.extra_data:
        profile_services = _profile.extra_data.get("services", {})
    else:
        profile_services = {}
    profile_services[name] = profile_data
    _profile.extra_data = {"services": profile_services}
    flag_modified(_profile, "extra_data")

    db.session.commit()

    if ui_flag:
        return render_template(
            current_app.config['AUTHENTICATION_POPUP_TEMPLATE'],
            msg=f'Authorization to {name} succeeded.'), 302
    else:
        return jsonify({"message": f"Authorization to {name} succeeded."}), 200
def authorize(name):
    ui_flag = session.pop('ui', None)

    client = current_auth.create_client(name)
    token = client.authorize_access_token()

    configs = OAUTH_SERVICES.get(name.upper(), {})
    extra_data_method = configs.get('extra_data_method')

    # TOFIX Add error handlers for reject, auth errors, etc
    extra_data = {}
    if extra_data_method:
        extra_data = extra_data_method(client, token)

    _token = _create_or_update_token(name, token)
    _token.extra_data = extra_data

    db.session.add(_token)

    # Add extra data to user profile.
    # If user profile doesn't exist yet, it creates one.
    _profile = UserProfile.get_by_userid(current_user.id)
    if not _profile:
        _profile = UserProfile(user_id=current_user.id)
        db.session.add(_profile)

    profile_data = get_oauth_profile(name, token=_token, client=client)

    if _profile.extra_data:
        profile_services = _profile.extra_data.get("services", {})
    else:
        profile_services = {}
    profile_services[name] = profile_data
    _profile.extra_data = {"services": profile_services}
    flag_modified(_profile, "extra_data")

    db.session.commit()

    if ui_flag:
        if current_app.config['DEBUG']:
            redirect_url = "http://localhost:3000/settings/auth/connect"
        else:
            redirect_url = "/settings/auth/connect"
        return redirect(redirect_url)
    else:
        return jsonify(
            {"message": "Authorization to {} succeeded".format(name)}), 200