【问题标题】:Flask oauth 2 resource owner password flowFlask oauth 2 资源所有者密码流程
【发布时间】:2023-03-19 20:01:01
【问题描述】:

我正在使用 flask-oauthlib 模块来开发 oauth 2 客户端和提供者

使用资源所有者密码流时,提供者不会重定向到客户端的重定向 url。

这是我向提供者发送帖子的客户端代码:

@app.route('/signin', methods=['POST', 'GET'])
def signin():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        f = {'client_id': 'jCce40zAaHXLxP0prU*************',
             'client_secret': 'vzf7U219hrAjIYN70NcFo3VBQzott******',
             'grant_type': 'password', 'scope': 'email',
             'redirect_uri': 'http://localhost:8000/authorized', 'response_type': 'token'}
        data = {'username': username, 'password': password}
        encoded_url = 'http://127.0.0.1:5000/oauth/authorize?' + parse.urlencode(f)
        headers = {"Content-Type": "application/json"}
        requests.post(encoded_url, data=json.dumps(data), headers=headers)
    return render_template('signin.html')

这里是provider authorize_handler

@app.route('/oauth/authorize', methods=['GET', 'POST'])
@oauth.authorize_handler
def authorize(*args, **kwargs):
    if request.method == 'POST':
        details = json.loads(request.data)
        username = details['username']
        password = details['password']
        user = User.query.filter_by(user_name=username).first()
        if user:
            if user.check_password(password):
               session['id'] = user.id
               return True
            return False
        return False

    if request.method == 'GET':
        user = current_user()
        if not user:
            session['redirect_after_login'] = request.url
            return redirect('/home')
        client_id = kwargs.get('client_id')
        client = Client.query.filter_by(client_id=client_id).first()
        kwargs['client'] = client
        kwargs['user'] = user
        return render_template('authorize.html', **kwargs)

    confirm = request.form.get('confirm', 'no')
    return confirm == 'yes'

还有 Flask-oauthlib oauth 2 提供者日志记录

Fetched credentials from request {'response_type': 'token', 'state': None, 'client_id': 'jCce40zAaHXLxP0prU************', 'redirect_uri': 'http://localhost:8000/authorized'}.
Found redirect_uri http://localhost:8000/authorized.
Validate client 'jCce40zAaHXLxP0prU***********'
Save bearer token {'scope': 'email', 'access_token': 'y08hkm594YbLe2*****', 'expires_in': 180, 'token_type': 'Bearer'}
Authorization successful.
127.0.0.1 - - [20/Sep/2015 17:40:53] "POST /oauth/authorize?client_id=jCce40zAaHXLxP0prU*********&client_secret=vzf7U219hrAjIYN70NcFo3VBQzot**********&response_type=token&grant_type=password&scope=email&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fauthorized HTTP/1.1" 302 -

在我看来,令牌正在保存但是:-

  1. 不发生重定向

  2. 它会导致客户端永远加载,直到我重新启动它(即使我尝试访问其他路由,客户端也没有响应)

我错过了什么?

注意:

我已经实现了服务器端流程和客户端流程,它们运行良好

我对烧瓶还是很陌生

【问题讨论】:

    标签: python flask oauth-2.0 flask-oauthlib


    【解决方案1】:

    我认为您正在混合使用不同的授权类型的 OAuth2。使用 Resource Owner Password Credentials 授权,授权服务器不会进行重定向,而是向客户端提供令牌响应。

    https://www.rfc-editor.org/rfc/rfc6749#section-4.3

    redirect_uris授权码 授权相关联。

    【讨论】:

      猜你喜欢
      • 2016-04-28
      • 2016-02-22
      • 2013-10-17
      • 2018-01-29
      • 2015-05-20
      • 2019-04-08
      • 2013-11-23
      • 2017-11-14
      • 2016-03-03
      相关资源
      最近更新 更多