本文整理匯總了Python中fastapi.Security方法的典型用法代碼示例。如果您正苦於以下問題:Python fastapi.Security方法的具體用法?Python fastapi.Security怎麽用?Python fastapi.Security使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fastapi
的用法示例。
在下文中一共展示了fastapi.Security方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _get_authorization_header
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [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
示例2: get_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [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
示例3: get_current_active_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def get_current_active_user(current_user: UserInDB = Security(get_current_user)):
if not crud.user.is_active(current_user):
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
示例4: get_current_active_superuser
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def get_current_active_superuser(current_user: UserInDB = Security(get_current_user)):
if not crud.user.is_superuser(current_user):
raise HTTPException(
status_code=400, detail="The user doesn't have enough privileges"
)
return current_user
示例5: get_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def get_current_user(oauth_header: Optional[str] = Security(oid)):
if oauth_header is None:
return None
user = User(username=oauth_header)
return user
示例6: get_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def get_current_user(oauth_header: Optional[str] = Security(api_key)):
if oauth_header is None:
return None
user = User(username=oauth_header)
return user
示例7: read_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def read_user(
user_data: Tuple[str, List[str]] = Security(get_user, scopes=["foo", "bar"]),
data: List[int] = Depends(get_data),
):
return {"user": user_data[0], "scopes": user_data[1], "data": data}
示例8: get_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def get_current_user(oauth_header: Optional[str] = Security(reusable_oauth2)):
if oauth_header is None:
return None
user = User(username=oauth_header)
return user
示例9: read_items
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def read_items(token: Optional[str] = Security(oauth2_scheme)):
return {"token": token}
示例10: read_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def read_current_user(
credentials: Optional[HTTPAuthorizationCredentials] = Security(security),
):
if credentials is None:
return {"msg": "Create an account first"}
return {"scheme": credentials.scheme, "credentials": credentials.credentials}
示例11: read_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)):
return {"scheme": credentials.scheme, "credentials": credentials.credentials}
示例12: get_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def get_current_user(oauth_header: str = Security(api_key)):
user = User(username=oauth_header)
return user
示例13: read_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def read_current_user(credentials: Optional[HTTPBasicCredentials] = Security(security)):
if credentials is None:
return {"msg": "Create an account first"}
return {"username": credentials.username, "password": credentials.password}
示例14: read_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def read_current_user(credentials: HTTPBasicCredentials = Security(security)):
return {"username": credentials.username, "password": credentials.password}
示例15: get_current_user
# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def get_current_user(oauth_header: str = Security(oid)):
user = User(username=oauth_header)
return user