Example #1
0
def check_verify_rate(rds, zone, number):
    now = int(time.time())
    _, ts, count = code.get_verify_code(rds, zone, number)
    if count > 10 and now - ts > 30*60:
        return True
    if now - ts > 50:
        return True

    return False
Example #2
0
def register_user():
    if not request.data:
        return INVALID_PARAM()
    
    req = json.loads(request.data)
    name = req.get('nickname')
    password = req.get('password')
    #短信验证码
    code = req.get("code")
    number = req.get("number")
    country_code = req.get("country_code")

    if not name or not password or not code \
       or not number or not country_code:
        return INVALID_PARAM()
    
    #check sms code
    if is_test_number(number):
        pass
    else:
        c2, timestamp, _ = code.get_verify_code(g.rds, country_code, number)
        if c1 != c2:
            return INVALID_CODE()
    password = generate_password_hash(password)
    phone_number = "+%s-%s"%(country_code, number)
    u = DBUser.get_user(g._db, phone_number)
    if u:
        uid = u['id']
        DBUser.save_user(g._db, uid, name, password)
    else:
        uid = DBUser.add_user(g._db, name, password, phone_number)

    #登录动作
    access_token = gobelieve.login_gobelieve(uid, name)
    if not access_token:
        return CAN_NOT_GET_TOKEN()

    tok = {
        'expires_in': 3600,
        "access_token":access_token,
        "refresh_token":random_token_generator(),
        'uid':uid
    }


    Token.save_access_token(g.rds, access_token, uid, 3600)
    Token.save_refresh_token(g.rds, tok['refresh_token'], uid)

    return make_response(200, tok)
Example #3
0
def access_token():
    if not request.data:
        return INVALID_PARAM()

    obj = json.loads(request.data)
    c1 = obj["code"]
    number = obj["number"]
    zone = obj["zone"]
    if is_test_number(number):
        pass
    else:
        c2, timestamp, _ = code.get_verify_code(g.rds, zone, number)
        if c1 != c2:
            return INVALID_CODE()

    uid = user.make_uid(zone, number)

    access_token = gobelieve.login_gobelieve(uid, "")
        
    if not access_token:
        return CAN_NOT_GET_TOKEN()

    u0 = user.get_user(g.rds, uid)
    u = user.User()
    u.uid = uid
    if u0 is None:
        u.state = "Hey!"
    else:
        u.state = u0.state

    user.save_user(g.rds, u)

    tok = {
        'expires_in': 3600,
        'token_type': 'Bearer',
        "access_token":access_token,
        "refresh_token":random_token_generator(),
        'uid':int(uid)
    }

    Token.save_access_token(g.rds, access_token, uid, 3600)
    Token.save_refresh_token(g.rds, tok['refresh_token'], uid)
    
    return make_response(200, tok)