Beispiel #1
0
async def login_with_google(google_token_id:str,db: Session = Depends(deps.get_db) ):
    '''
    Login with  gmail
    '''
    try:

        idinfo = json.loads(Request.get('https://oauth2.googleapis.com/tokeninfo?id_token='+google_token_id).text)
        
        curent_user=crud_user.get_user_by_username(db=db,user_name=idinfo["email"])
        if sercurity.check_email(idinfo["email"]) is False:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Invalid email"
            ) 
        if curent_user is None :
            crud_user.create_new_user(db=db,user_name=idinfo["email"],role="executor")

        access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
        access_token =sercurity.create_access_token(
            data={"sub": curent_user.user_name,
            "role":curent_user.role,
            "id":curent_user.id,
            "scopes":get_scopes(curent_user.role)
            }, expires_delta=access_token_expires
        )
    except ValueError:
        raise HTTPException(
                status_code=status.HTTP_502_BAD_GATEWAY,
                detail="token id error ",
                headers={"WWW-Authenticate": "Bearer"},
            ) 
    pass
    return {"access_token": access_token,"token_type": "bearer"}
Beispiel #2
0
def All_users(user_name: str,
              role: str,
              token: Optional[str] = Header(None),
              db: Session = Depends(deps.get_db)):
    '''
    View All manager User
    '''
    try:
        check_sercurity_scopes(token=token, scopes=settings.ADD_NEW_USER)

        if not crud_user.get_user_by_username(db=db,
                                              user_name=user_name) is None:
            raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY,
                                detail="User already exist ")
        if sercurity.check_email(user_name) is False:
            raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY,
                                detail="Invalid user name ")
    except (JWTError, ValidationError):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Unauthorized ",
            headers={"WWW-Authenticate": "Bearer"},
        )
    except (mysql.connector.Error):
        raise HTTPException(
            status_code=status.HTTP_502_BAD_GATEWAY,
            detail="My sql connection error ",
            headers={"WWW-Authenticate": "Bearer"},
        )
    return {"message": "success"}
Beispiel #3
0
def update_url(url_id: str,
               new_url: str,
               current_user=Security(deps.get_current_active_user,
                                     scopes=["url"]),
               db: Session = Depends(deps.get_db)):
    '''
    Update URL
    '''
    if sercurity.check_email(new_url) is None:
        raise UnicornException(messages="INVALID URL FORMAT", name=new_url)
    if crud_url.get_url(db=db, id=url_id) is None:
        raise UnicornException(messages="URL Not Found", name=url_id)
    crud_url.update_url(db=db, id=url_id, new_url=new_url)
    return {"message": " success"}
Beispiel #4
0
def Create_new_user(new_user: user_schema.UserCreate,
                    current_user=Security(deps.get_current_active_user,
                                          scopes=["read_user"]),
                    db: Session = Depends(deps.get_db)):
    '''
    Create_new_user
    '''
    if sercurity.check_email(new_user.user_name) is False:
        raise UnicornException(messages="Invalid Email",
                               name=new_user.user_name)
    if not crud_user.get_user_by_username(
            db=db, user_name=new_user.user_name) is None:
        raise UnicornException(messages="Email already exist",
                               name=new_user.user_name)
    if new_user.role not in ['executor', 'manager']:
        raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY,
                            detail="Invalid Role")
    return crud_user.create_user(db=db, users=new_user)