2

这是我的 Python EVE 代码

from eve import Eve
from eve.auth import BasicAuth

class MyBasicAuth(BasicAuth):
    def check_auth(self, username, password, allowed_roles, resource,
                   method):
        if resource == 'auth':
            user = app.data.driver.db['user']
            user = user.find_one({'email': username,'password':password})
            if user:
                return user
            else:
                False
        else:
            return True

app = Eve(auth=MyBasicAuth)

if __name__ == '__main__':
    app.run()

验证成功后,我将返回user对象,但没有返回任何内容。我哪里错了。这是settings.py文件

RESOURCE_METHODS = ['GET', 'POST']

DOMAIN = {
    'user': {
        'schema': {
            'firstname': {
                'type': 'string'
            },
            'lastname': {
                'type': 'string'
            },
            'email': {
                'type': 'string'
            },
        'password': {
        'type': 'string'
        },
            'phone': {
                'type': 'string'
            }
        }
    },
    'auth': {
        'schema': {
            'username': {
                'type': 'string'
                },
            'password': {
                'type':'string'
                }
            }
        }
}
4

1 回答 1

1

身份验证和授权与返回数据无关。您的MyBasicAuth课程只应该返回TrueFalse允许处理或拒绝当前请求。

假设您的客户端正在访问user端点,它将在以下情况下返回数据:

  1. 您的自定义身份验证类返回True

  2. 数据库上的user集合中包含文档(您没有提供datasource所以 Eve 默认将端点作为数据源名称)。

另外,我不确定您为什么要进行测试,resource == 'auth'因为您没有在域中设置auth端点。

要抢占先机,您可能需要先查看QuickStart,然后是身份验证和授权,最后是RESTful 帐户管理教程。

于 2014-12-29T13:53:20.567 回答