【问题标题】:Simplest authentication with flask-restless使用flask-restless进行最简单的身份验证
【发布时间】:2016-02-23 20:38:39
【问题描述】:

我正在使用 Flask-Restless 制作一个超级简单的 REST API。我想添加身份验证,但仅用于 put/post/delete 调用,而我想公开 get 调用。

到目前为止,我在 views.py 文件中放入了这个:

manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(models.mymodel, methods=['GET', 'POST', 'DELETE'])

我查看了不同的身份验证解决方案,但它们看起来都太“大”了。我将只有一个用户应该能够进行 PUT/POST/DELETE 调用,然后“公共”用户将只使用 GET。

我认为一个简单的方法应该是这样的:

  • 进行 get 调用的公共用户:返回 api 响应,就像现在一样
  • 公共用户发出 put/post/delete 调用:返回“未授权”响应
  • 已注册用户进行 get/put/post/delete 调用:检查是否已注册并回复适当的响应。

“检查”不应该是在我的config.py 文件中存储密钥,然后将其与 api 调用标头的属性进行比较?我认为,正如我在一些教程中看到的那样,为用户创建一个完整的表,然后让 usernames+password 生成 API 令牌太“大”,在这里没有必要。因为我将是唯一可以通过身份验证的用户,我知道关键是什么,我可以在标题中放一些'secret-key' : mysecretkey。我错过了什么吗?

谢谢!

【问题讨论】:

    标签: python authentication flask python-requests flask-restless


    【解决方案1】:

    这可以通过using preprocessors argumentcreate_api 完成。例如:

    # replace the next line with your favorite way or reading config files
    access_password = 'mysecretkey'
    
    def check_credentials(**kwargs):
        if flask.request.headers.get('X-Secret-Key','') != access_password:
            raise flask_restless.ProcessingException(code=401)  # Unauthorized
    
    # create API
    manager = flask_restless.APIManager(app, flask_sqlalchemy_db=db)
    manager.create_api(models.mymodel, methods=['GET', 'POST', 'DELETE'],
                       collection_name='myobjects',
                       preprocessors={
                            'POST': [check_credentials],
                            'DELETE_SINGLE': [check_credentials],
                       })
    

    使用 Python 3.4、Flask 0.11.1、Flask-Restless 0.17.0 测试。 Flask-Restless 的旧版本可能需要 DELETE 而不是 DELETE_SINGLE,以及不同的包名称。

    如何使用 cURL 访问受密码保护的 API 部分的示例:

    curl -X POST -d '{"data":"my JSON data, etc."}' \
         -H "Content-Type: application/json" \
         -H "X-Secret-Key: mysecretkey" \
         http://localhost:5000/api/myobjects
    

    这是一个简单的方案,但如果您的应用将来有可能变得更大,我建议您使用Flask-JWT。数据库后端不必大而复杂。这是您在极简实现中的起始案例(与硬编码密码一起使用的案例):

    # Anonymous object is sufficient here, a full-blown solution would define a class User
    admin_user = type('', (object,), {"id": 1, "username": "admin", "password": "mysecret"})()
    
    def authenticate(username, password):
        if username == admin_user.username and password == admin_user.password:
            return admin_user
    
    def identity(payload):
        id = payload['identity']
        if id == admin_user.id:
            return admin_user
    
    
    jwt = JWT(app, authenticate, identity)
    
    
    @jwt_required()        
    def check_credentials(**kwargs):
        pass
    
    # create API as in the first example
    

    使用 Python 3.4、Flask 0.11.1、Flask-Restless 0.17.0、Flask-JWT 0.3.2 测试。

    【讨论】:

      【解决方案2】:

      你可以试试flask-jwt,很容易集成。它也有一个简单的例子。我还找到了一种方法来实现您的第三个要点,如果您有任何解决方案,请分享。

      【讨论】:

        【解决方案3】:

        存在 HTTP Basic Auth 时无需重新发明身份验证:

        【讨论】:

          猜你喜欢
          • 2014-01-01
          • 2017-10-07
          • 1970-01-01
          • 2011-10-12
          • 1970-01-01
          • 2011-03-05
          • 2019-12-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多