Exemplo n.º 1
0
def get_token(
        data: OAuth2PasswordRequestForm = Depends(),
        session: Session = Depends(generate_session),
):
    email = data.username
    password = data.password

    user = authenticate_user(session, email, password)

    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )

    access_token = security.create_access_token(dict(sub=email),
                                                timedelta(hours=2))
    return SnackResponse.success(
        "User Successfully Logged In",
        {
            "access_token": access_token,
            "token_type": "bearer"
        },
    )
Exemplo n.º 2
0
async def update_user(
    id: int,
    new_data: UserBase,
    current_user: UserInDB = Depends(get_current_user),
    session: Session = Depends(generate_session),
):

    token = None
    if current_user.id == id or current_user.admin:
        db.users.update(session, id, new_data.dict())
    if current_user.id == id:
        access_token = security.create_access_token(data=dict(sub=new_data.email))
        token = {"access_token": access_token, "token_type": "bearer"}
        return token
Exemplo n.º 3
0
async def update_user(
        id: int,
        new_data: UserBase,
        current_user: UserInDB = Depends(get_current_user),
        session: Session = Depends(generate_session),
):

    token = None
    if current_user.id == id or current_user.admin:
        db.users.update(session, id, new_data.dict())
    if current_user.id == id:
        access_token = security.create_access_token(
            data=dict(sub=new_data.email), expires_delta=timedelta(hours=2))
        token = {"access_token": access_token, "token_type": "bearer"}

    return SnackResponse.success("User Updated", token)
Exemplo n.º 4
0
def get_token(
        background_tasks: BackgroundTasks,
        request: Request,
        data: OAuth2PasswordRequestForm = Depends(),
        session: Session = Depends(generate_session),
):
    email = data.username
    password = data.password

    user: UserInDB = authenticate_user(session, email, password)

    if not user:
        background_tasks.add_task(
            create_user_event, "Failed Login",
            f"Username: {email}, Source IP: '{request.client.host}'")
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            headers={"WWW-Authenticate": "Bearer"},
        )

    access_token = security.create_access_token(dict(sub=user.email))
    return {"access_token": access_token, "token_type": "bearer"}
Exemplo n.º 5
0
async def create_api_token(
    token_name: LoingLiveTokenIn,
    current_user: UserInDB = Depends(get_current_user),
    session: Session = Depends(generate_session),
):
    """ Create api_token in the Database """

    token_data = {"long_token": True, "id": current_user.id}

    five_years = timedelta(1825)
    token = create_access_token(token_data, five_years)

    token_model = CreateToken(
        name=token_name.name,
        token=token,
        parent_id=current_user.id,
    )

    new_token_in_db = db.api_tokens.create(session, token_model)

    if new_token_in_db:
        return {"token": token}
Exemplo n.º 6
0
async def refresh_token(current_user: UserInDB = Depends(get_current_user)):
    """ Use a valid token to get another token"""
    access_token = security.create_access_token(data=dict(
        sub=current_user.email))
    return {"access_token": access_token, "token_type": "bearer"}