Exemplo n.º 1
0
def newtoken_v1():
    data = json.loads(request.data.decode('utf8'))
    print(data)
    token_old = Token.query.filter_by(token=data['token']).one_or_none()
    if token_old is None:
        return json.dumps({"status": "ERROR", "error_msg": "Wrong token"})

    tknstr = Config.SECRET_KEY + data['token']
    token_new = Token(user_id=token_old.user_id)
    token_new.generate(tknstr)

    db.session.delete(token_old)
    db.session.add(token_new)
    db.session.commit()

    return json.dumps({"status": "OK", "data": {"token": token_new.token}})
Exemplo n.º 2
0
def login_v1():
    data = json.loads(request.data.decode('utf8'))
    user = User.query.filter_by(username=data['login']).one_or_none()
    if user is None:
        return json.dumps({
            "status": "ERROR",
            "error_msg": "Login doesn't exist"
        })
    print(user.check_password(data['password']))
    if not user.check_password(data['password']):
        # need log it to analyze logs for ban bruteforcers
        return json.dumps({"status": "ERROR", "error_msg": "Password wrong"})

    tknstr = Config.SECRET_KEY + data['login'] + data['password']
    token = Token(user_id=user.id)
    token.generate(tknstr)
    db.session.add(token)
    db.session.commit()
    res = {"id": str(user.id), "token": token.token}
    return json.dumps({"status": "OK", "data": res})