def api_user_sign_up(*, name, password):
"""
password = sha1((uid + ':' + password).encode(utf-8))
"""
if not name or not name.strip():
raise APIValueError('name')
if not password or not _RE_SHA1.match(password):
raise APIValueError('password')
all_users = yield from User.find_all('name=?', (name,))
if len(all_users):
raise APIError('sign up failed', 'name', 'User name already exist')
uid = next_id()
sha1_password = '%s:%s' % (uid, password)
password = hashlib.sha1(sha1_password.encode('utf-8')).hexdigest()
user = User(id=uid, name=name.strip(), password=password, is_admin=True)
yield from user.save()
r = web.Response()
cookie_name = configs['cookie_name']
r.set_cookie(
cookie_name, generate_cookie(user, 86400), max_age=86400, httponly=True
)
user.password = '******'
r.content_type = 'application/json'
r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
return r
评论列表
文章目录