Esempio n. 1
0
def funcname(token, db:Session = Depends(get_db)):
    tokenAuth = Token().decodeToken(token)
    if not tokenAuth:
        raise HTTPException(status_code=401, detail='unauthorized')
    userRquest = crud.getUser_by_email(db, email= tokenAuth['email'])
    if not userRquest:
        raise HTTPException(status_code=401, detail='User not found')
    tokenLogout = Token().generateToken(userRquest.id, userRquest.name,userRquest.name,False)
    crud.update_token_user(db,tokenLogout,userRquest.id)
    return schemas.ServerResult(status='sucess', ok=True, forceLogin= False)
Esempio n. 2
0
def getUser(user:schemas.UserLogin,db:Session = Depends(get_db)):
    userAuth = authenticate_user(**user.dict(), db= db)
    if not userAuth:
        raise HTTPException(status_code=401,detail='User not found')
    tokenToSearch = Token().generateToken(sub=userAuth.id, userName=userAuth.name, 
                                  email= userAuth.email,forceLogin=False)
    tokenToUpdate = crud.getToken_db(db,tokenToSearch)
    token = Token().generateToken(sub=userAuth.id, userName=userAuth.name, 
                                  email= userAuth.email,forceLogin=True)
    crud.update_token_user(db,token,userAuth.id)
    return schemas.ServerResult(status='sucess', ok=True, forceLogin= True ,token=token)
Esempio n. 3
0
def edit_name_email(user:schemas.UserBase,token, db: Session = Depends(get_db)):
    tokenAuth = Token().decodeToken(token)
    if not tokenAuth:
       raise HTTPException(status_code=401, detail="unauthorized") 
    userRquest = crud.getUser_by_email(db, email= tokenAuth['email'])
    if not userRquest:
        raise HTTPException(status_code=401, detail='User not found')
    if not validate_if_logged(db,userRquest.id):
        return schemas.ServerResult(status='not logged', ok=False, forceLogin= False)
    #'userToUpdate' is the memory position
    userUpdated = crud.updateUser(db,userToUpdate= userRquest, **user.dict(),password=userRquest.password)
    newToken = Token().generateToken(sub=userUpdated.id,userName=userUpdated.name,
                                       email=userUpdated.email, forceLogin=True)
    #update to login
    crud.update_token_user(db,newToken, userRquest.id)
    return schemas.ServerResult(status='sucess', ok=True, forceLogin= True ,token=newToken)
Esempio n. 4
0
def create_user(user: schemas.UserCreate, db:Session = Depends(get_db)):
    db_user = crud.getUser_by_email(db, user.email)
    if db_user:
        raise HTTPException(status_code=400, detail='Email already registered')
    user_created = crud.create_user(db= db, user = user)
    token = Token().generateToken(user_created.id,user_created.name,user_created.email,False)
    crud.create_token(db,token,user_created.id)
    return schemas.ServerResult(status='sucess, user created', ok=True)
Esempio n. 5
0
def validate_if_logged(db: Session, idUser):
    #se esta retornanco un none....
    token_db: models.Token = crud.getToken_db(db, idUser)
    tokenDecoded = Token().decodeToken(token_db.value)
    """ if not tokenDecoded:
        raise HTTPException(status_code=400,detail='bad request') """
    isLogged = tokenDecoded["forceLogin"]
    print(isLogged)
    if isLogged:
        return True
    return False
Esempio n. 6
0
def change_password( user:schemas.UserPassword,token, db:Session = Depends(get_db)):
    userAuth = Token().decodeToken(token)
    if not userAuth:
        raise HTTPException(status_code=401, detail='unauthorized')
    userToUpdate = crud.getUser_by_email(db, userAuth['email'])
    if not userToUpdate:
        raise HTTPException(status_code=401, detail='user not found')
    if not validate_if_logged(db,userToUpdate.id):
        return schemas.ServerResult(status='not logged', ok=False, forceLogin= False)
    userUpdated = crud.updateUser(db,userToUpdate= userToUpdate,email=userToUpdate.email,name=userToUpdate.name,
                                  password=user.password)
    return schemas.ServerResult(status='sucess', ok=True, forceLogin= True)
Esempio n. 7
0
def token_is_valid(token):
    tokenInstance = Token()
    tokenDecoded = tokenInstance.decodeToken(token)
    if not tokenDecoded:
        raise HTTPException(status_code=401, detail={"unauthorized"})
    return tokenDecoded