本文整理匯總了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