本文整理汇总了Python中fastapi.security.OAuth2PasswordBearer方法的典型用法代码示例。如果您正苦于以下问题:Python security.OAuth2PasswordBearer方法的具体用法?Python security.OAuth2PasswordBearer怎么用?Python security.OAuth2PasswordBearer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fastapi.security
的用法示例。
在下文中一共展示了security.OAuth2PasswordBearer方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordBearer [as 别名]
def __call__(self, request: Request):
"""
Provides the functionality to act as a Dependency
:param Request request: The incoming request, this is set automatically
by FastAPI
:return: The user object or None
:raises: The not_authenticated_exception if set by the user
"""
if self.not_authenticated_exception is None:
self.oauth_scheme = OAuth2PasswordBearer(tokenUrl=self.tokenUrl)
else:
# we handle Exception raising
self.oauth_scheme = OAuth2PasswordBearer(tokenUrl=self.tokenUrl, auto_error=False)
token = await self.oauth_scheme(request)
if token is not None:
return await self.get_current_user(token)
# No token is present in the request and no Exception has been raised yet
raise self.not_authenticated_exception
示例2: __init__
# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordBearer [as 别名]
def __init__(self, name: str = "mock"):
super().__init__(name, logout=True)
self.scheme = OAuth2PasswordBearer("/login", auto_error=False)
示例3: __init__
# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordBearer [as 别名]
def __init__(
self,
secret: str,
lifetime_seconds: int,
tokenUrl: str = "/login",
name: str = "jwt",
):
super().__init__(name, logout=False)
self.scheme = OAuth2PasswordBearer(tokenUrl, auto_error=False)
self.secret = secret
self.lifetime_seconds = lifetime_seconds