本文整理汇总了Python中auth.models.User.authenticate_user方法的典型用法代码示例。如果您正苦于以下问题:Python User.authenticate_user方法的具体用法?Python User.authenticate_user怎么用?Python User.authenticate_user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类auth.models.User
的用法示例。
在下文中一共展示了User.authenticate_user方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: auth_post
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import authenticate_user [as 别名]
def auth_post(request):
"""
An endpoint to authenticate users and retrieve their access token.
The access token is needed to authenticate requests that needs authorization::
/endpont?access_token=<access token>
returns
=======
returns 401 auth error if fails, otherswise returns::
{
'access_token': '',
'user_id': ''
}
"""
email = request.validated['email']
password = request.validated['password']
user = User.authenticate_user(email, password)
response_body = {}
if user:
# user found and authenticated
logger.debug('user:{} authenticated'.format(email))
access_token = create_access_token(user)
response_body = json.dumps({
'access_token': access_token,
'user_id': str(user.id),
})
else:
# user not found or authenticated
logger.debug('user:{} failed authentication'.format(email))
request.response.status_int = 401
response_body = json.dumps({
'status': 'error',
'message': 'user failed to authenticate',
})
request.response.body = response_body
request.response.content_type = 'application/json'
return request.response
示例2: test_user
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import authenticate_user [as 别名]
def test_user(self):
response = self.testapp.post_json('/api/v1/users', {}, status=400)
# create new user
payload = {
'email': '[email protected]',
'password': 'hello',
}
response = self.testapp.post_json('/api/v1/users', payload, status=200)
self.assertTrue(response.json['id'])
# check duplicate
response = self.testapp.post_json('/api/v1/users', payload, status=400)
# check database
user = User.get_by_email('[email protected]')
self.assertTrue(user)
self.created.append(user)
# get user
endpoint = '/api/v1/users/{}'.format(user.id)
response = self.testapp.get(endpoint, status=200)
self.assertTrue(response.json['id'])
# update user
payload = {
'active': True,
'email': '[email protected]',
}
endpoint = '/api/v1/users/{}'.format(user.id)
response = self.testapp.put_json(endpoint, payload, status=200)
user = User.get_by_id(user.id)
self.assertTrue(user.active)
# change password
payload = {
'password': 'world',
}
endpoint = '/api/v1/users/{}/password'.format(user.id)
response = self.testapp.put_json(endpoint, payload, status=200)
user = User.authenticate_user('[email protected]', 'world')
self.assertTrue(user)
示例3: authentication
# 需要导入模块: from auth.models import User [as 别名]
# 或者: from auth.models.User import authenticate_user [as 别名]
def authentication(self, user):
user2 = User.authenticate_user(user.email, 'hello')
self.assertTrue(user.id == user2.id)
self.assertFalse(User.authenticate_user(user.email, 'world'))
self.assertFalse(User.authenticate_user('[email protected]', 'hello'))