예제 #1
0
파일: handlers.py 프로젝트: zero530/fb4u
def api_register_fbuser(*, email, name, passwd, number, birthday):
    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')
    if not number.isdigit():
        raise APIValueError('number should > 0')
 	#if not birthday:
     #   raise APIValueError('birthday') 
    print("number:" + number)
    #validation user          
    fbusers = yield from FBUser.findAll('email=?', [email])
    if len(fbusers) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')

    uid = next_id()
    sha1_passwd = '%s:%s' % (uid, passwd)

    fbuser = FBUser(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), number=number, birthday=birthday.strip())
    yield from fbuser.save()

    # make session cookie:
    r = web.Response()
    r.set_cookie(COOKIE_NAME, user2cookie(fbuser, 86400), max_age=86400, httponly=True)
    fbuser.passwd = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(fbuser, cls=CJsonEncoder, ensure_ascii=False).encode('utf-8')
    return r
예제 #2
0
파일: handlers.py 프로젝트: zero530/fb4u
def authenticate(*, email, passwd):
    if not email:
        raise APIValueError('email', 'Invalid email.')
    if not passwd:
        raise APIValueError('passwd', 'Invalid password.')
    fbusers = yield from FBUser.findAll('email=?', [email])
    if len(fbusers) == 0:
        raise APIValueError('email', 'Login failed, Email not exist.')
    fbuser = fbusers[0]
    # check passwd:
    sha1 = hashlib.sha1()
    sha1.update(fbuser.id.encode('utf-8'))
    sha1.update(b':')
    sha1.update(passwd.encode('utf-8'))
    if fbuser.passwd != sha1.hexdigest():
        raise APIValueError('passwd', 'Invalid password.')
    # authenticate ok, set cookie:
    r = web.Response()
    r.set_cookie(COOKIE_NAME,
                 user2cookie(fbuser, 86400),
                 max_age=86400,
                 httponly=True)
    fbuser.passwd = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(fbuser, cls=CJsonEncoder).encode('utf-8')

    return r
예제 #3
0
파일: handlers.py 프로젝트: zero530/fb4u
def api_register_fbuser(*, 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 FBUser.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)
    fbuser = FBUser(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 fbuser.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
예제 #4
0
파일: handlers.py 프로젝트: zero530/fb4u
def api_get_fbusers(*, page='1'):
    page_index = get_page_index(page)
    num = yield from FBUser.findNumber('count(id)')
    p = Page(num, page_index)
    if num == 0:
        return dict(page=p, users=())
    fbusers = yield from FBUser.findAll(orderBy='created_at desc', limit=(p.offset, p.limit))
    for u in fbusers:
        u.passwd = '******'
    return dict(page=p, users=fbusers)
예제 #5
0
파일: handlers.py 프로젝트: zero530/fb4u
def api_get_fbusers(*, page='1'):
    page_index = get_page_index(page)
    num = yield from FBUser.findNumber('count(id)')
    p = Page(num, page_index)
    if num == 0:
        return dict(page=p, users=())
    fbusers = yield from FBUser.findAll(orderBy='created_at desc',
                                        limit=(p.offset, p.limit))
    for u in fbusers:
        u.passwd = '******'
    return dict(page=p, users=fbusers)
예제 #6
0
파일: handlers.py 프로젝트: zero530/fb4u
def api_register_fbuser(*, email, name, passwd, number, birthday):
    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')
    if not number.isdigit():
        raise APIValueError('number should > 0')

#if not birthday:
#   raise APIValueError('birthday')
    print("number:" + number)
    #validation user
    fbusers = yield from FBUser.findAll('email=?', [email])
    if len(fbusers) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')

    uid = next_id()
    sha1_passwd = '%s:%s' % (uid, passwd)

    fbuser = FBUser(id=uid,
                    name=name.strip(),
                    email=email,
                    passwd=hashlib.sha1(
                        sha1_passwd.encode('utf-8')).hexdigest(),
                    number=number,
                    birthday=birthday.strip())
    yield from fbuser.save()

    # make session cookie:
    r = web.Response()
    r.set_cookie(COOKIE_NAME,
                 user2cookie(fbuser, 86400),
                 max_age=86400,
                 httponly=True)
    fbuser.passwd = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(fbuser, cls=CJsonEncoder,
                        ensure_ascii=False).encode('utf-8')
    return r
예제 #7
0
파일: handlers.py 프로젝트: zero530/fb4u
def api_register_fbuser(*, 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 FBUser.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)
    fbuser = FBUser(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 fbuser.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
예제 #8
0
파일: handlers.py 프로젝트: zero530/fb4u
def authenticate(*, email, passwd):
    if not email:
        raise APIValueError('email', 'Invalid email.')
    if not passwd:
        raise APIValueError('passwd', 'Invalid password.')
    fbusers = yield from FBUser.findAll('email=?', [email])
    if len(fbusers) == 0:
        raise APIValueError('email', 'Login failed, Email not exist.')
    fbuser = fbusers[0]
    # check passwd:
    sha1 = hashlib.sha1()
    sha1.update(fbuser.id.encode('utf-8'))
    sha1.update(b':')
    sha1.update(passwd.encode('utf-8'))
    if fbuser.passwd != sha1.hexdigest():
        raise APIValueError('passwd', 'Invalid password.')
    # authenticate ok, set cookie:
    r = web.Response()
    r.set_cookie(COOKIE_NAME, user2cookie(fbuser, 86400), max_age=86400, httponly=True)
    fbuser.passwd = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(fbuser, cls=CJsonEncoder).encode('utf-8')

    return r