【问题标题】:How to restrict access with google oauth to one specific domain如何将使用 google oauth 的访问权限限制在一个特定的域
【发布时间】:2019-01-25 11:54:56
【问题描述】:

所以我试图创建对一个特定域的受限访问,因为我想私下使用这个应用程序。

我在互联网上浏览并尝试了一些解决方案,但到目前为止我无法让它们发挥作用。

我的代码如下:

from flask import Flask, redirect, url_for, session, request, jsonify,render_template
from flask_oauthlib.client import OAuth
from bs4 import BeautifulSoup
app = Flask(__name__)

app.config['GOOGLE_ID'] = ""
app.config['GOOGLE_SECRET'] = ""
app.debug = True
app.secret_key = 'development'
oauth = OAuth(app)

google = oauth.remote_app(
    'google',
    consumer_key=app.config.get('GOOGLE_ID'),
    consumer_secret=app.config.get('GOOGLE_SECRET'),
    request_token_params={
        'scope': 'email',
        'hd': 'exampledomain.com'
    },
    base_url='https://www.googleapis.com/oauth2/v1/',
    request_token_url=None,
    access_token_method='POST',
    access_token_url='https://accounts.google.com/o/oauth2/token',
    authorize_url='https://accounts.google.com/o/oauth2/auth',
)

@app.route('/')
def index():
    return redirect(url_for('login'))

@app.route('/login')
def login():
    return google.authorize(callback=url_for('authorized', _external=True))


@app.route('/logout')
def logout():
    session.pop('google_token', None)
    return redirect(url_for('index'))


@app.route('/login/authorized')
def authorized():
    resp = google.authorized_response()
    if resp is None:
        return 'Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']
        )
    session['google_token'] = (resp['access_token'], '')
    return render_template('index.html')


@google.tokengetter
def get_google_oauth_token():
    return session.get('google_token')

我用 hd 试过了,但我仍然可以设法用其他域登录。那么如何才能限制对一个域的访问呢?

【问题讨论】:

    标签: python flask oauth google-oauth


    【解决方案1】:

    不得不搜索了一段时间,但在以下github上找到了我的解决方案:https://github.com/vistarmedia/google_oauth_flask

    那里的代码实际上是解决方案,并且完美无缺。

    我建议使用 Flask-Dance 代替上面的解决方案,更多信息可以在这里找到:https://flask-dance.readthedocs.io/en/latest/

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 2013-12-27
      • 2016-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多