Skip to main content

blade-auth-client

blade-auth-client 是 Blade 生态的共享 Casdoor 认证客户端包。它抽离了统一的 token 校验、FastAPI 装配和 Socket.IO 握手接口,供各个服务复用。

安装

pip install blade-auth-client[fastapi,socketio]

Casdoor 配置要求

Casdoor 的 application、scope 和 redirect URI 约束见 docs/casdoor-setup.md

快速上手(0.4.0+)

from fastapi import FastAPI, Depends
from blade_auth_client import AuthConfig, BladeAuth, CasdoorClaims, LazyProvisioner

class MyProvisioner(LazyProvisioner["MyUser"]):
    async def provision_user(self, claims: CasdoorClaims) -> "MyUser":
        # 首次登录:建用户并绑定 claims.sub;非首次:查出来刷字段
        ...

auth = BladeAuth(
    AuthConfig.from_yaml("configs/oauth_config.yaml"),
    provisioner=MyProvisioner(),
)

app = FastAPI()
app.include_router(auth.router, prefix="/api/v1/auth")

@app.get("/me")
def me(user = auth.require()):
    return user

详细流程见 docs/接入指南.md;多 app 共享鉴权 / BladeAuth 设计背景见 docs/设计/设计-API暴露面收敛.md

从 0.3.x 升级

  • 推荐迁到 BladeAuth facade(上面那段)。老零件(OidcClient / TokenVerifier / create_auth_router / make_require_auth_dep / 等)0.4.0 仍可从顶层 import,但会 DeprecationWarning,0.5.0 移除。
  • LazyProvisioner.ensure_userprovision_user。老名字继续可用且 SDK 内部自动回落,调用时 warn 一次。

Web 会话与组织

系统里有两类长期凭据,生命周期不同,别混用:

web 令牌 PAT
是什么 密码登录换来的 RS256 JWT 不透明的 sk-blade-v3-…
代表 某人这次登录 某人这个身份
logout 立即失效 不受影响
适合 网页 / 客户端会话 脚本、服务间调用

会话用 web 令牌、自动化用 PAT。拿 PAT 当会话使,用户点了登出也退不掉;拿 web 令牌做自动化,用户一登出你的定时任务就断了。

from blade_auth_client import SessionClient, OrgClient
from blade_auth_client.session import AccountLockedError
from blade_auth_client.errors import InvalidTokenError

sc = SessionClient("http://blade-oauth:19000")

try:
    token = await sc.login("alice", "secret")
except AccountLockedError:      # 429:连续失败被冻结
    ...
except InvalidTokenError:       # 401:账号或密码不对
    ...

me = await sc.me(token.access_token)     # SessionUser(含 source / source_id)
await sc.logout(token.access_token)      # 服务端拉黑,之后一律 401

logout 只作用于传入的这一枚令牌:同一用户在别处的会话、以及他的 PAT 都不受 影响。需要「所有设备下线」用管理端的 DELETE /api/v1/users/{id}/sessions。 重复登出不报错——登出表达的是意图,令牌本来就无效同样满足这个意图。

本地口令校验不过时,blade-oauth 会转发给管理员配置的外部认证源,认证通过即自动 建号;对调用方没有区别,拿到的始终是 blade-oauth 自己签的令牌。SessionUser.source 告诉你这个账号从哪来(admin_api / bootstrap / external / legacy)。

oc = OrgClient("http://blade-oauth:19000")

roster = await oc.my_members(token)              # 我同组织的人,普通用户可调
orgs = await oc.list(admin_token)                # 需要 admin
org = await oc.get(admin_token, "org-a")
members = await oc.members(admin_token, "org-a")

my_members 返回脱敏视图——OrgMember 刻意没有邮箱、手机、额度等字段, 避免花名册被当成全公司通讯录来爬。注意判断 OrgRoster.truncated:花名册上限 500 人,服务端会如实告诉你名单是否被截断。

版本策略

0.0.x 版本仅用于占位发布和联调验证,不承诺可用性。0.2.0 起采用仅校验 iss + 签名 + exp 的简化策略。

发布

仓库已经预留了 GitHub Actions 发布流水线:

  • PR 校验:.github/workflows/python-sdk-ci.yml
  • PyPI 发布:.github/workflows/python-sdk-publish.yml

首次配置 Trusted Publishing 时,在 PyPI 项目的 Publishing 页面添加 GitHub publisher,填写:

  • Owner: blade-hq
  • Repository name: blade-oauth
  • Workflow name: python-sdk-publish.yml

日常发版流程:

# 1. 修改 sdk/python/pyproject.toml 里的 version

# 2. 推送代码到默认分支后,创建并推送同版本 tag
git tag blade-auth-client-vX.Y.Z
git push origin blade-auth-client-vX.Y.Z

发布 workflow 会校验 tag 版本与 pyproject.toml 一致,然后自动构建并发布到 PyPI。

Download files

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

Source Distribution

blade_auth_client-0.4.15.tar.gz (150.3 kB view details)

Uploaded Source

Built Distribution

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

blade_auth_client-0.4.15-py3-none-any.whl (60.4 kB view details)

Uploaded Python 3

File details

Details for the file blade_auth_client-0.4.15.tar.gz.

File metadata

  • Download URL: blade_auth_client-0.4.15.tar.gz
  • Upload date:
  • Size: 150.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for blade_auth_client-0.4.15.tar.gz
Algorithm Hash digest
SHA256 307da923c795eda7c2822b71d8de2801f4f8326df22f0e35ee74b2eceb9d4444
MD5 f7f3a1adb3bfc796479f7bf16de9938f
BLAKE2b-256 3dff30547f590e412ec1b4d68ac9e851cbda438e0d81ced77d290f918d3a90e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for blade_auth_client-0.4.15.tar.gz:

Publisher: python-sdk-publish.yml on blade-hq/blade-oauth

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

File details

Details for the file blade_auth_client-0.4.15-py3-none-any.whl.

File metadata

File hashes

Hashes for blade_auth_client-0.4.15-py3-none-any.whl
Algorithm Hash digest
SHA256 20e88bf1c59970c864f75700825e1fe762d740863a3a9801690e7bffd8c5483c
MD5 88add7ce590f3ab7b03ac89629c29de9
BLAKE2b-256 be795ef9e36c673b64a56e56527e5779d5af5cd4f8788cb0c1d96fb10f49e837

See more details on using hashes here.

Provenance

The following attestation bundles were made for blade_auth_client-0.4.15-py3-none-any.whl:

Publisher: python-sdk-publish.yml on blade-hq/blade-oauth

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

Release history Release notifications | RSS feed

0.4.24

2 files

0.4.23

2 files

0.4.22

2 files

0.4.21

2 files

0.4.20

2 files

0.4.19

2 files

0.4.18

2 files

0.4.16

2 files

This release

0.4.15 This release

2 files

0.4.14

2 files

0.4.13

2 files

0.4.12

2 files

0.4.11

2 files

0.4.10

2 files

0.4.9

2 files

0.4.8

2 files

0.4.6

2 files

0.4.5

2 files

0.4.4

2 files

0.4.3

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page