Ejemplo n.º 1
0
def api_register(*, name, email, password):
    logging.info("enter")
    check_string(name=name)
    check_email_passwd(email, name)
    users = yield from User.findAll('email = ?', [email])
    if users:
        raise APIValueError("email", "Email is already in used")
    uid = next_id()
    sha1_passwd = '%s:%s' % (email, password)
    user = User(name=name.strip(),
                email=email,
                password=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(),
                image="/static/img/user.png",
                admin=0)
    yield from user.save()

    #make session cooike
    r = web.Response()
    r.set_cookie(COOKIE_NAME,
                 user2cookie(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
Ejemplo n.º 2
0
def api_register_user(*, email, name, password):
    if not name or not name.strip():
        raise APIValueError('name')
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    if not password or not _RE_SHA1.match(password):
        raise APIValueError('password')

    users = yield from User.find_all('email=?', [email])
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')
    uid = next_id()
    sha1_password = '******' % (uid, password)
    user = User(
        id=uid,
        name=name.strip(),
        email=email,
        password=hashlib.sha1(sha1_password.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.password = '******'
    r.content_type = 'application/json'
    r.body = json.dumps(user, ensure_ascii=False).encode('utf-8')
    return r
Ejemplo n.º 3
0
def registerUser(*, email, name, password):
    if not name or not name.strip():
        raise APIValueError('name')
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    if not password or not password.strip():
        raise APIValueError('password')
    users = yield from User.findAll('email=?', email)
    if len(users) > 0:
        raise APIError('register:failed', 'email', 'Email is already in use.')
    uid = nextId()
    sha1Password = '******' % (uid, password)
    sha1 = hashlib.sha1(sha1Password.encode('utf-8')).hexdigest()
    user = User(
        id=uid,
        name=name.strip(),
        email=email,
        password=sha1,
        image=
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1542105917178&di=d1f6b6a11859ff9a2436460ed3c691dd&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fbba1cd11728b47104c5c00e9c9cec3fdfc0323a0.jpg'
    )
    yield from user.save()
    r = web.Response()
    r.set_cookie(COOKIE_NAME,
                 user2Cookie(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
Ejemplo n.º 4
0
def register_check(*, email, password, name):
    """checking the information of the register user"""
    if not email or not email.strip():
        raise APIValueError(field='email', message='Empty field.')
    if not password or not password.strip():
        raise APIValueError(field='password', message='Empty field.')
    if not name or not name.strip():
        raise APIValueError(field='name', message='Empty field.')
    # check email
    users = yield from find_models(model='user', where='email=?', args=[email])
    if len(users) > 0:
        raise APIError(error='register:failed', data='email', message='Email is already exist.')
    uid = next_id()
    sha1_pwd = '%s:%s' % (uid, password)
    user = User(id=uid, name=name, email=email, password=hashlib.sha1(sha1_pwd.encode('utf-8')).hexdigest(),
                image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest())
    yield from save_model(user)
    # make the session cookie
    r = web.Response()
    r.set_cookie(name=COOKIE_NAME, value=user2cookie(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
Ejemplo n.º 5
0
 def test_no_password_getter(self):
     u = User(password_hash='cat')
     with self.assertTrue(AttributeError):
         u.password()