当前位置: 首页>>代码示例>>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;未经允许,请勿转载。