当前位置: 首页>>代码示例>>Python>>正文


Python status.HTTP_403_FORBIDDEN属性代码示例

本文整理汇总了Python中starlette.status.HTTP_403_FORBIDDEN属性的典型用法代码示例。如果您正苦于以下问题:Python status.HTTP_403_FORBIDDEN属性的具体用法?Python status.HTTP_403_FORBIDDEN怎么用?Python status.HTTP_403_FORBIDDEN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在starlette.status的用法示例。


在下文中一共展示了status.HTTP_403_FORBIDDEN属性的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __call__

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def __call__(
        self, request: Request
    ) -> Optional[HTTPAuthorizationCredentials]:
        authorization: str = request.headers.get("Authorization")
        scheme, credentials = get_authorization_scheme_param(authorization)
        if not (authorization and scheme and credentials):
            if self.auto_error:
                raise HTTPException(
                    status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
                )
            else:
                return None
        if scheme.lower() != "bearer":
            if self.auto_error:
                raise HTTPException(
                    status_code=HTTP_403_FORBIDDEN,
                    detail="Invalid authentication credentials",
                )
            else:
                return None
        return HTTPAuthorizationCredentials(scheme=scheme, credentials=credentials) 
开发者ID:tiangolo,项目名称:fastapi,代码行数:23,代码来源:http.py

示例2: _get_authorization_header

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def _get_authorization_header(
    api_key: str = Security(RWAPIKeyHeader(name=HEADER_KEY)),
) -> str:
    try:
        token_prefix, token = api_key.split(" ")
    except ValueError:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN, detail=strings.WRONG_TOKEN_PREFIX,
        )

    if token_prefix != JWT_TOKEN_PREFIX:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN, detail=strings.WRONG_TOKEN_PREFIX,
        )

    return token 
开发者ID:nsidnev,项目名称:fastapi-realworld-example-app,代码行数:18,代码来源:authentication.py

示例3: _get_current_user

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
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,
        ) 
开发者ID:nsidnev,项目名称:fastapi-realworld-example-app,代码行数:19,代码来源:authentication.py

示例4: test_forbidden_exception

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def test_forbidden_exception():
    detail = "You have no rights, peasant."
    with pytest.raises(ForbiddenError) as excinfo:
        raise ForbiddenError(
            detail=detail
        )

    exc = excinfo.value
    assert exc.error_code == status.HTTP_403_FORBIDDEN
    assert exc.status_code == status.HTTP_403_FORBIDDEN
    assert exc.detail == detail

    error_code = 444
    with pytest.raises(ForbiddenError) as excinfo:
        raise ForbiddenError(
            detail=detail,
            error_code=error_code
        )

    exc = excinfo.value
    assert exc.error_code == error_code
    assert exc.status_code == status.HTTP_403_FORBIDDEN
    assert exc.detail == detail 
开发者ID:identixone,项目名称:fastapi_contrib,代码行数:25,代码来源:test_exceptions.py

示例5: test_permissions_dependency_as_class

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def test_permissions_dependency_as_class(dumb_request):
    class FailPermission(BasePermission):

        def has_required_permisions(self, request: Request) -> bool:
            return False

    class AllowPermission(BasePermission):

        def has_required_permisions(self, request: Request) -> bool:
            return True

    dependency = PermissionsDependency(permissions_classes=[AllowPermission])
    dependency(request=dumb_request)

    dependency = PermissionsDependency(
        permissions_classes=[AllowPermission, FailPermission])

    with pytest.raises(HTTPException) as excinfo:
        dependency(request=dumb_request)

    assert excinfo.value.status_code == status.HTTP_403_FORBIDDEN
    assert excinfo.value.detail == "Forbidden." 
开发者ID:identixone,项目名称:fastapi_contrib,代码行数:24,代码来源:test_permissions.py

示例6: get_current_user

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def get_current_user(token: str = Security(reusable_oauth2)):
    try:
        payload = jwt.decode(token, config.SECRET_KEY, algorithms=[ALGORITHM])
        token_data = TokenPayload(**payload)
    except PyJWTError:
        raise HTTPException(
            status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials"
        )
    bucket = get_default_bucket()
    user = crud.user.get(bucket, username=token_data.username)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    return user 
开发者ID:tiangolo,项目名称:full-stack-fastapi-couchbase,代码行数:15,代码来源:security.py

示例7: __call__

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def __call__(self, request: Request) -> Optional[str]:
        authorization: str = request.headers.get("Authorization")
        if not authorization:
            if self.auto_error:
                raise HTTPException(
                    status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
                )
            else:
                return None
        return authorization 
开发者ID:tiangolo,项目名称:fastapi,代码行数:12,代码来源:oauth2.py

示例8: __call__

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def __call__(self, request: Request) -> Optional[str]:
        api_key: str = request.headers.get(self.model.name)
        if not api_key:
            if self.auto_error:
                raise HTTPException(
                    status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
                )
            else:
                return None
        return api_key 
开发者ID:tiangolo,项目名称:fastapi,代码行数:12,代码来源:api_key.py

示例9: check_comment_modification_permissions

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def check_comment_modification_permissions(
    comment: Comment = Depends(get_comment_by_id_from_path),
    user: User = Depends(authentication.get_current_user_authorizer()),
) -> None:
    if not check_user_can_modify_comment(comment, user):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=strings.USER_IS_NOT_AUTHOR_OF_ARTICLE,
        ) 
开发者ID:nsidnev,项目名称:fastapi-realworld-example-app,代码行数:11,代码来源:comments.py

示例10: check_article_modification_permissions

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def check_article_modification_permissions(
    current_article: Article = Depends(get_article_by_slug_from_path),
    user: User = Depends(get_current_user_authorizer()),
) -> None:
    if not check_user_can_modify_article(current_article, user):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=strings.USER_IS_NOT_AUTHOR_OF_ARTICLE,
        ) 
开发者ID:nsidnev,项目名称:fastapi-realworld-example-app,代码行数:11,代码来源:articles.py

示例11: test_base_permission_no_permission_raises_403

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def test_base_permission_no_permission_raises_403(dumb_request):
    class FailPermission(BasePermission):

        def has_required_permisions(self, request: Request) -> bool:
            return False

    with pytest.raises(HTTPException) as excinfo:
        FailPermission(request=dumb_request)

    assert excinfo.value.status_code == status.HTTP_403_FORBIDDEN
    assert excinfo.value.detail == "Forbidden." 
开发者ID:identixone,项目名称:fastapi_contrib,代码行数:13,代码来源:test_permissions.py

示例12: get_current_user

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def get_current_user(
    db: Session = Depends(get_db), token: str = Security(reusable_oauth2)
):
    try:
        payload = jwt.decode(token, config.SECRET_KEY, algorithms=[ALGORITHM])
        token_data = TokenPayload(**payload)
    except PyJWTError:
        raise HTTPException(
            status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials"
        )
    user = crud.user.get(db, id=token_data.user_id)
    if not user:
        raise HTTPException(status_code=400, detail="User not found")
    return user 
开发者ID:QAX-A-Team,项目名称:LuWu,代码行数:16,代码来源:security.py

示例13: __call__

# 需要导入模块: from starlette import status [as 别名]
# 或者: from starlette.status import HTTP_403_FORBIDDEN [as 别名]
def __call__(
        self, request: Request, security_scopes: SecurityScopes, return_token=False
    ):
        if not self.enabled:
            return None
        if security_scopes.scopes:
            authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
        else:
            authenticate_value = f"Bearer"
        token: str = await oauth2_scheme(request) if not self.token else self.token
        data = (
            await models.User.join(models.Token)
            .select(models.Token.id == token)
            .gino.load((models.User, models.Token))
            .first()
        )
        if data is None:
            raise HTTPException(
                status_code=HTTP_401_UNAUTHORIZED,
                detail="Could not validate credentials",
                headers={"WWW-Authenticate": authenticate_value},
            )
        user, token = data  # first validate data, then unpack
        forbidden_exception = HTTPException(
            status_code=HTTP_403_FORBIDDEN,
            detail="Not enough permissions",
            headers={"WWW-Authenticate": authenticate_value},
        )
        if "full_control" not in token.permissions:
            for scope in security_scopes.scopes:
                if scope not in token.permissions and not check_selective_scopes(
                    request, scope, token
                ):
                    raise forbidden_exception
        if "server_management" in security_scopes.scopes and not user.is_superuser:
            raise forbidden_exception
        if return_token:
            return user, token
        return user 
开发者ID:MrNaif2018,项目名称:bitcart,代码行数:41,代码来源:utils.py


注:本文中的starlette.status.HTTP_403_FORBIDDEN属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。