Ejemplo n.º 1
0
def test_create_access_token(expires_delta):
    subject = "user_id"
    token = create_access_token(subject, expires_delta)
    payload = jwt.decode(token,
                         settings.SECRET_KEY,
                         algorithms=[security.ALGORITHM])
    assert payload["sub"] == subject
Ejemplo n.º 2
0
def login(
        form_data: OAuth2PasswordRequestForm = Depends(),
        db: Session = Depends(deps.get_db),
) -> schemas.Token:
    """
    OAuth2 compatible token login, get an access token for future requests
    """
    user: models.User = db.query(models.User).filter(
        models.User.email == form_data.username.lower()).first()
    if not user or not verify_password(form_data.password, user.password):
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail="Incorrect email or password")
    if not user.active:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail="Inactive user")

    token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)

    return schemas.Token(
        access_token=create_access_token(user.id,
                                         user.email,
                                         user.username,
                                         expires_delta=token_expires),
        token_type="bearer",
    )
Ejemplo n.º 3
0
async def login_access_token(
        db: AsyncSession = Depends(deps.get_db),
        form_data: OAuth2PasswordRequestForm = Depends(),
) -> Any:
    """
    OAuth2 compatible token login, get an access token for future requests
    """
    user = await crud.user.authenticate(db,
                                        email=form_data.username,
                                        password=form_data.password)
    if not user:
        raise HTTPException(status_code=400,
                            detail="Incorrect email or password")
    elif not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    access_token_expires = timedelta(
        minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
    return {
        "access_token":
        security.create_access_token(user.id,
                                     expires_delta=access_token_expires,
                                     scopes=form_data.scopes),
        "token_type":
        "bearer",
    }
Ejemplo n.º 4
0
async def login_access_token(
        db: AsyncSession = Depends(deps.get_db),
        form_data: OAuth2PasswordRequestForm = Depends(),
) -> Any:
    if user := await crud.user.authenticate(db,
                                            email_or_cpf=form_data.username,
                                            password=form_data.password):
        access_token_expires = timedelta(
            minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
        return {
            "access_token":
            security.create_access_token(user.id,
                                         expires_delta=access_token_expires),
            "token_type":
            "bearer",
        }
Ejemplo n.º 5
0
def access_token(create_user):
    return security.create_access_token(create_user.id)