Skip to main content

Auth Client

轻量级 JWT 验证 SDK,用于接入 Auth Service 的业务项目。

安装

# 基础 JWT 校验器
pip install "seanfield-auth-client==0.3.1"

# FastAPI 依赖工厂
pip install "seanfield-auth-client[fastapi]==0.3.1"

支持 Python 3.10–3.13。包内包含 py.typed,类型检查器可直接读取公开 API 的类型信息。

快速上手

FastAPI 项目 (3 行接入)

from fastapi import FastAPI, Depends
from auth_service_client import JWTValidator, require_auth, require_scopes

# 三项安全约束不能省略:签发者、本应用 audience、access token 类型
validator = JWTValidator(
    jwks_url="http://localhost:8100/.well-known/jwks.json",
    issuer="http://localhost:8100",
    audience="app_your_client_id",
    require_token_type="access",
)

app = FastAPI()

@app.get("/public")
async def public():
    return {"message": "anyone can see this"}

@app.get("/protected")
async def protected(user=Depends(require_auth(validator))):
    return {
        "message": "you are authenticated",
        "user_id": user.sub,
        "email": user.email,
        "app": user.aud,
    }

@app.get("/admin-only")
async def admin_only(user=Depends(require_scopes(validator, "admin"))):
    return {"message": "admin access granted"}

非 FastAPI 项目 (同步验证)

from auth_service_client import JWTValidator

validator = JWTValidator(
    jwks_url="http://localhost:8100/.well-known/jwks.json",
    issuer="http://localhost:8100",
    audience="app_your_client_id",
    require_token_type="access",
)

def verify_request(authorization_header: str):
    token = authorization_header.replace("Bearer ", "")
    user = validator.verify(token)
    print(f"User {user.sub} ({user.email}) authenticated")
    return user

配置选项

validator = JWTValidator(
    jwks_url="http://localhost:8100/.well-known/jwks.json",
    issuer="http://localhost:8100",     # 必填: 锁定 token 签发者 (iss)
    audience="app_your_client_id",      # 必填: 锁定目标应用 (aud = 你的 client_id)
    require_token_type="access",        # 必填: 拒绝 refresh token 走保护路由
    cache_ttl=300,                      # JWKS 缓存时间 (秒)
    leeway_seconds=2,                   # 可选: 容忍有界时钟偏差 (秒)
)

业务项目必须把三项 (issuer / audience / require_token_type) 全开 —— IdP 不校验 aud,由消费方自己锁定 token 是发给本应用的。详见 认证契约

leeway_seconds 会同时作用于 JWT 的 iatnbfexp 校验。默认值是 0,保持已有 严格校验行为;只有在消费服务与 Auth Service 存在已测得的轻微时钟偏差时才显式设置。 建议先校准系统时间,再使用 25 秒的最小容错,不要用较大值掩盖持续时钟漂移,因为 该值也会把过期 token 的接受窗口延长同样的秒数。

本 SDK 不连接 Redis。若业务要求跨应用账户切换或全设备退出后立即拒绝已签发 access token, 消费端还需在验签后检查 Auth Service 共享 Redis 中的 revoked_sid:{sid}revoked_user:{sub};不做该检查时,旧 access token 会保持有效到其 exp。完整规则见 接入指南

发布维护

PyPI 发布仅由仓库的 python-client-publish.yml 工作流处理。工作流只响应 auth-client-v* 标签,先运行 SDK 测试、lint、构建内容校验、twine check 与隔离安装 smoke,再通过 PyPI Trusted Publishing 上传;不会读取长期 PyPI API Token。

PyPI 项目需要配置以下 Trusted Publisher:

  • PyPI Project:seanfield-auth-client
  • Owner:HyxiaoGe
  • Repository:auth-service
  • Workflow:python-client-publish.yml
  • Environment:pypi

发布前必须同步更新 pyproject.tomlauth_service_client/__init__.py 中的版本。标签必须 与包版本完全一致,例如 0.3.1 只能由 auth-client-v0.3.1 发布。

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

seanfield_auth_client-0.3.1.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

seanfield_auth_client-0.3.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file seanfield_auth_client-0.3.1.tar.gz.

File metadata

  • Download URL: seanfield_auth_client-0.3.1.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for seanfield_auth_client-0.3.1.tar.gz
Algorithm Hash digest
SHA256 9c07b0e4aa22d8a2ea753ea4bdada03963e983d55eaef4b067b4d6f6f627f84f
MD5 7a62a60f775873ca9d4ba5328346fa8e
BLAKE2b-256 5d0b7fcc41478672c4c429ce480acc0876455c942cfba982337cf98f56c422e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for seanfield_auth_client-0.3.1.tar.gz:

Publisher: python-client-publish.yml on HyxiaoGe/auth-service

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file seanfield_auth_client-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for seanfield_auth_client-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d5ad0a3bbfc79b2defad47c0d3d5761bbcab845bb2a8d6d923bb4fbd4b5eb88
MD5 066ad1dc3973bc10ed823d926f381e06
BLAKE2b-256 856aedd6c76634001bd73cb55331421f47ba3cbfd8d825565bcd5373271221f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for seanfield_auth_client-0.3.1-py3-none-any.whl:

Publisher: python-client-publish.yml on HyxiaoGe/auth-service

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page