本文整理汇总了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