def login_user( user: schemas.UserLogIn, db: Session = Depends(deps.get_db)) -> JSONResponse: """ Login user and Return Access Token""" db_user = get_active_user(email=user.email, db=db) if db_user is None: return JSONResponse(status_code=400, content={"message": "Invalid Credentials"}) else: is_password_correct = crud_login.check_username_password( email=user.email, password=user.password, db=db) if is_password_correct is False: return JSONResponse(status_code=400, content={"message": "Invalid Credentials"}) else: uid = str(uuid.uuid4().hex) crud_login.login_user(user=user, session_id=uid, db=db) access_token_expires = timedelta( minutes=ProjectSettings.ACCESS_TOKEN_EXPIRE_MINUTES) token = access_token.create_access_token( data={"sub": user.email}, expires_delta=access_token_expires) return JSONResponse(status_code=200, content={ "access_token": token, "token_type": "Bearer", "session_id": uid, "user": jsonable_encoder( get_user(email=user.email, db=db)) })
def authenticate_user(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(deps.get_db)) -> JSONResponse: """ Return Access Token""" db_user = get_active_user(email=form_data.username, db=db) if db_user is None: return JSONResponse(status_code=400, content={"message": "Invalid Credentials"}) else: is_password_correct = crud_login.check_username_password( email=form_data.username, password=form_data.password, db=db) if is_password_correct is False: return JSONResponse(status_code=400, content={"message": "Invalid Credentials"}) else: access_token_expires = timedelta( minutes=ProjectSettings.ACCESS_TOKEN_EXPIRE_MINUTES) token = access_token.create_access_token( data={ "sub": form_data.username, "perm": db_user.is_admin }, expires_delta=access_token_expires) return JSONResponse(status_code=200, content={"access_token": token, "token_type": "Bearer"})
def new_token(old_token: str = None, session_id: str = None) -> JSONResponse: """ Return Access Token""" if old_token and session_id: payload = access_token.decode_access_token(token=old_token) email = payload.get("sub") db_session = crud_login.check_active_session(session_id=session_id) session_time = datetime.strptime(str(db_session.created_timestamp), "%Y-%m-%d %H:%M:%S.%f") diff = datetime.utcnow() - session_time limit = ProjectSettings.SESSION_TOKEN_EXPIRE_SECONDS # 12 hours if email == db_session.email and ( db_session.status == "logged_in" or db_session.status == "active") \ and diff.seconds < limit: crud_login.active_user(session_id=session_id) access_token_expires = timedelta( minutes=ProjectSettings.ACCESS_TOKEN_EXPIRE_MINUTES) token = access_token.create_access_token( data={"sub": email}, expires_delta=access_token_expires) return JSONResponse(status_code=200, content={"access_token": token, "token_type": "Bearer"}) else: return JSONResponse(status_code=400, content={"message": "session ended"}) else: return JSONResponse(status_code=400, content={"message": "invalid token"})
def authenticate_user( user: schemas.UserAuthenticate, db: Session = Depends(deps.get_db) ) -> JSONResponse: db_user = crud.get_user(db, email=user.email) if db_user is None: return JSONResponse(status_code=400, content={"message": "Invalid Credentials"}) else: is_password_correct = crud.check_username_password(db, user) if is_password_correct is False: return JSONResponse(status_code=400, content={"message": "Invalid Credentials"}) else: access_token_expires = timedelta( minutes=ProjectSettings.ACCESS_TOKEN_EXPIRE_MINUTES) token = access_token.create_access_token( data={"sub": user.email}, expires_delta=access_token_expires) return JSONResponse(status_code=200, content={ "access_token": token, "token_type": "Bearer" })
def mutate(root, info, email, password, ip_address, browser): db_user = crud_base.get_active_user(email=email, db=db) auth_user = schemas.UserLogIn(email=email, ip_address=ip_address, browser=browser) if db_user is None: raise GraphQLError("Email not existed") else: is_password_correct = crud_login.check_username_password( email=email, password=password, db=db) if is_password_correct is False: raise GraphQLError("Password is not correct") else: uid = str(uuid.uuid4().hex) crud_login.login_user(user=auth_user, session_id=uid, db=db) access_token_expires = timedelta( minutes=ProjectSettings.ACCESS_TOKEN_EXPIRE_MINUTES) token = access_token.create_access_token( data={"sub": auth_user.email}, expires_delta=access_token_expires) return AuthUser(token=token, token_type="Bearer", session_id=uid)