Ejemplo n.º 1
0
def test_error_when_wrong_token_shape() -> None:
    token = create_jwt_token(
        jwt_content={"content": "payload"},
        secret_key="secret",
        expires_delta=timedelta(minutes=1),
    )
    with pytest.raises(ValueError):
        get_username_from_token(token, "secret")
Ejemplo n.º 2
0
async def _get_current_user(
        token: str = Depends(_get_authorization_header_retriever()), ) -> User:
    try:
        username = jwt.get_username_from_token(token, str(SECRET_KEY))
    except ValueError:
        raise forbidden_exception(strings.MALFORMED_PAYLOAD)

    user = await User.get_or_none(username=username)
    if user is None:
        raise forbidden_exception(strings.MALFORMED_PAYLOAD)
    return user
async def _get_current_user(
        users_repo: UsersRepository = Depends(get_repository(UsersRepository)),
        token: str = Depends(_get_authorization_header_retriever()),
) -> User:
    try:
        username = jwt.get_username_from_token(token, str(SECRET_KEY))
    except ValueError:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail=strings.MALFORMED_PAYLOAD)

    try:
        return await users_repo.get_user_by_username(username=username)
    except EntityDoesNotExist:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail=strings.MALFORMED_PAYLOAD)
Ejemplo n.º 4
0
def test_error_when_wrong_token() -> None:
    with pytest.raises(ValueError):
        get_username_from_token("asdf", "asdf")
Ejemplo n.º 5
0
def test_retrieving_token_from_user(test_user: UserInDB) -> None:
    token = create_access_token_for_user(user=test_user, secret_key="secret")
    username = get_username_from_token(token, "secret")
    assert username == test_user.username