def test(loop): print('我我',loop) yield from webapp.www.orm.create_pool(loop=loop,user='******', password='******', db='awesome') u = User(name='Test', email='*****@*****.**', passwd='1234567890', image='about:blank',created_at=3) print('我', u) yield from u.save()
def api_register_user(*, email, name, passwd): if not name or not name.strip(): raise APIValueError('name', 'invalid name') if not email or not _RE_EMAIL.match(email): raise APIValueError('email', 'invalid email') if not passwd or not _RE_SHA1.match(passwd): raise APIValueError('passwd') users = yield from User.findAll('email=?', [email]) if len(users) > 0: raise APIError('register:failed', 'email', 'Email is already in use') uid = next_id() sha1_passwd = '%s:%s' % (uid, passwd) user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest()) yield from user.save() # make session cookie: r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
async def api_register_user(*, email, name, passwd): helper.check_string(name=name) helper.check_email_password(email, passwd) users = await User.findall('email=?', [email]) if len(users) > 0: raise APIError('register: failed', 'email', 'Email is already in use.') uid = ghelper.get_unique_id() # helper.get_unique_id passwd_sha1 = helper.make_passwd_sha1(uid, passwd) email_md5 = hashlib.md5(email.encode('utf-8')).hexdigest() image_str = 'http://www.gravatar.com/avatar/%s?d=mm&s=120' % email_md5 user = User(id=uid, name=name.strip(), email=email, passwd=passwd_sha1, image=image_str) await user.save() # 保存注册用户的信息 # make session cookie: r = web.Response() cookie_name = ghelper.get_cookie_name() r.set_cookie(cookie_name, helper.user2cookie(user, 86400), max_age=86400, httponly=True) # 86400s = 24h = a day user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def api_get_users(*,page='1'): page_index = get_page_index(page) num = yield from User.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p,users=()) users = yield from User.findAll(orderBy='created_at desc',limit=(p.offset,p.limit)) for u in users: u.passwd = '******' return dict(page=p,users=users)
def api_get_users(*, page='1'): page_index = get_page_index(page) num = yield from User.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, users=()) users = yield from User.findAll(orderBy='created_at desc', limit=(p.offset, p.limit)) for u in users: u.passwd = '******' return dict(page=p, users=users)
def cookie2user(cookie_str): ''' Parse cookie and load user if cookie is valid :param cookie_str: :return: ''' if not cookie_str: return None try: L = cookie_str.split('-') if len(L) != 3: return None uid, expires, sha1 = L if int(expires) < time.time(): return None user = yield from User.find(uid) if user is None: return None s = '%s-%s-%s-%s' % (uid, user.passwd, expires, _COOKIE_KEY) if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest(): logging.info('invalid sha1') return None user.passwd = '******' return user except Exception as e: logging.exception(e) return None
def authenticate(*, email, passwd): if not email: raise APIValueError('email', 'Invalid email.') if not passwd: raise APIValueError('passwd', 'Invalid password.') users = yield from User.findAll('email=?', [email]) if len(users) == 0: raise APIValueError('email', 'Email not exist.') user = users[0] # check passwd: sha1 = hashlib.sha1() sha1.update(user.id.encode('utf-8')) sha1.update(b':') sha1.update(passwd.encode('utf-8')) if user.passwd != sha1.hexdigest(): raise APIValueError('passwd', 'Invalid password.') # authenticate ok, set cookie: r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def api_register_user(*, email, name, passwd): if not name or not name.strip(): raise APIValueError('name') if not email or not _RE_EMAIL.match(email): raise APIValueError('email') if not passwd or not _RE_SHA1.match(passwd): raise APIValueError('passwd') users = yield from User.findAll('email=?',[email]) if len(users)>0: raise APIError('register:failed','email','Email is already in use.') uid = next_id() sha1_passwd = "%s:%s" % (uid,passwd) user = User(id=uid,name = name.strip(),email=email,passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(),image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest()) yield from user.save() r = web.Response() r.set_cookie(COOKIE_NAME,user2cookie(user,86400),max_age=86400,httponly=True) user.passwd='******' r.content_type='application/json' r.body=json.dumps(user,ensure_ascii=False).encode('utf-8') return r
async def test(in_loop): await orm.create_pool(loop=in_loop, user='******', password='******', db='awesome') test_name, test_email, test_passwd = gen_rand_string() u = User(name=test_name, email=test_email, passwd=test_passwd, image='about:blank') print(u) await u.save() print('tested ok...')
def test(loop): yield from create_pool(loop, user='******', password='******', db='awesome') u = User(name='Test', email='*****@*****.**', passwd='1234567890', image='about:blank') yield from u.save() u = User(name='Tom', email='*****@*****.**', passwd='1234567890', image='about:blank') yield from u.save() u = User(name='Jackie', email='*****@*****.**', passwd='1234567890', image='about:blank') yield from u.save()
async def create_admin_user(name, email, password): password_str = '%s:%s' % (email, password) # email: password sha1 = hashlib.sha1() sha1.update(password_str.encode('utf-8')) password = sha1.hexdigest() uid = ghelper.get_unique_id() passwd_sha1 = make_passwd_sha1(uid, password) email_md5 = hashlib.md5(email.encode('utf-8')).hexdigest() image_str = 'http://www.gravatar.com/avatar/%s?d=mm&s=120' % email_md5 user = User(id=uid, name=name.strip(), email=email, passwd=passwd_sha1, image=image_str, admin=True) await user.save()
def authenticate(*,email,passwd): if not email: raise APIValueError('email','Invalid email.') if not passwd: raise APIValueError('passwd','Invalid email.') users = yield from User.findAll('email=?',[email]) if len(users) == 0: raise APIValueError('email','email not exist') user = users[0] sha1=hashlib.sha1() sha1.update(user.id.encode('utf-8')) sha1.update(b':') sha1.update(passwd.encode('utf-8')) if user.passwd != sha1.hexdigest(): raise APIValueError('passwd','Invalid passwd') r = web.Response() r.set_cookie(COOKIE_NAME,user2cookie(user,86400),max_age=86400,httponly=True) user.passwd='******' r.content_type='application/json' r.body = json.dumps(user,ensure_ascii=False).encode('utf-8') return r
def cookie2user(cookie_str): if not cookie_str: return None try: L = cookie_str.split('-') if len(L) != 3: return None uid,expires,sha1=L if int(expires) < time.time(): return None user = yield from User.find(uid) if user is None: return None s = '%s-%s-%s-%s' % (uid,user.passwd,expires,_COOKIE_KEY) if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest(): logging.info('invalid sha1') return None user.passwd = '******' return user except Exception as e: logging.exception(e) return None