示例#1
0
def register():
    db = DB()
    print(request.data)
    print(request.data.decode())
    data = json.loads(request.data.decode())
    print(data)
    if not db.isExist(data['username']):
        username = data['username']
        salt = os.urandom(16)
        password = encrypt(data['password'], salt)
        email = data['email']
        print('DEBUG: Ready to create doc')
        if db.newDoc(
                dict(_id=username,
                     salt=base64.b64encode(salt).decode(),
                     password=base64.b64encode(password).decode(),
                     email=email)):
            response = make_response("OK")
            return response
            # return 'OK'
        else:
            response = make_response("An Error occured on the server")
            return response
            # return 'An Error occured on the server'
    else:
        response = make_response("Username Exists")
        return response
示例#2
0
def login():
    print(request.data)
    print(request.data.decode())
    data = json.loads(request.data.decode())
    db = DB()
    if not db.isExist(data['username']):
        response = make_response("No such username")
        return response
    res = db.findDoc(data['username'])
    print(base64.b64decode(res['salt'].encode()))
    print(base64.b64decode(res['password'].encode()))
    tmp = encrypt(data['password'], base64.b64decode(res['salt'].encode()))
    if base64.b64encode(tmp).decode() == res['password']:
        session['login'] = True
        response = make_response("OK")
        return response
    else:
        response = make_response("Password Wrong")
        return response