當前位置: 首頁>>代碼示例>>Python>>正文


Python fastapi.Security方法代碼示例

本文整理匯總了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 
開發者ID:nsidnev,項目名稱:fastapi-realworld-example-app,代碼行數:18,代碼來源:authentication.py

示例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 
開發者ID:tiangolo,項目名稱:full-stack-fastapi-couchbase,代碼行數:15,代碼來源:security.py

示例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 
開發者ID:tiangolo,項目名稱:full-stack-fastapi-couchbase,代碼行數:6,代碼來源:security.py

示例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 
開發者ID:tiangolo,項目名稱:full-stack-fastapi-couchbase,代碼行數:8,代碼來源:security.py

示例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 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:7,代碼來源:test_security_openid_connect_optional.py

示例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 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:7,代碼來源:test_security_api_key_query_optional.py

示例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} 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:7,代碼來源:test_dependency_security_overrides.py

示例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 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:7,代碼來源:test_security_oauth2_optional.py

示例9: read_items

# 需要導入模塊: import fastapi [as 別名]
# 或者: from fastapi import Security [as 別名]
def read_items(token: Optional[str] = Security(oauth2_scheme)):
    return {"token": token} 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:4,代碼來源:test_security_oauth2_authorization_code_bearer.py

示例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} 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:8,代碼來源:test_security_http_bearer_optional.py

示例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} 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:4,代碼來源:test_security_http_bearer.py

示例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 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:5,代碼來源:test_security_api_key_header.py

示例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} 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:6,代碼來源:test_security_http_basic_optional.py

示例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} 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:4,代碼來源:test_security_http_basic_realm.py

示例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 
開發者ID:tiangolo,項目名稱:fastapi,代碼行數:5,代碼來源:test_security_openid_connect.py


注:本文中的fastapi.Security方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。