Skip to main content

bit-login 北理工统一身份认证登录模块

北理工统一身份认证登录模块

📥 安装

git clone https://github.com/BIT101-dev/bit-login.git
cd bit-login
pip install -e '.[captcha]'

🚀 快速开始 (Python SDK)

基础登录

import bit_login

username = "your_username"
password = "your_password"

# 1. 登录 WebVPN
webvpn = bit_login.webvpn_login().login(username, password)
session = webvpn.get_session()
# 使用 session 访问校内资源
response = session.get("https://webvpn.bit.edu.cn/...")

# 2. 登录教务系统 (JWB)
jwb_login = bit_login.jwb_login().login(username, password)
# 获取成绩
scores = bit_login.jwb.score(jwb_login.get_session()).get_all_score()

# 3. 登录教学中心/一站式大厅 (JXZXEHALL)
hall_login = bit_login.jxzxehall_login().login(username, password)
# 获取学分信息
credits = bit_login.jxzxehall.credit(hall_login.get_session()).get_credit()
# 获取课程表
courses = bit_login.jxzxehall.courses(hall_login.get_session()).get_courses()

# 4. 其他服务支持
# - bit_login.ibit_login()      # iBIT
# - bit_login.yanhekt_login()   # 延河课堂
# - bit_login.library_login()   # 图书馆

🌐 RESTful API 服务

本项目提供 FastAPI 服务。

启动服务

bash server/start.sh

# 或者手动启动
gunicorn server:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:16384

🐳 Docker 部署

1. 构建镜像

在项目根目录下执行以下命令:

docker build -t bit-login-server -f server/Dockerfile .

2. 启动容器

docker run -d -p 16384:16384 \
  -v bit-login-data:/app/data \
  --name bit-login-server bit-login-server

通过 http://localhost:16384 访问服务。

3. 环境变量配置

  • WORKERS: Gunicorn 工作进程数 (默认: 4)
  • PORT: 服务端口 (默认: 16384)
  • HOST: 监听地址 (默认: 0.0.0.0)
  • AUTH_DB_PATH: SQLite 路径(Docker 默认 /app/data/auth.db,本机默认 /tmp/bit-login/auth.db
  • AUTH_CHALLENGE_TTL: 等待短信验证码的秒数(默认 300)
  • AUTH_SESSION_TTL: 下游 Session 保留秒数(默认 1800)

接口文档

短信 challenge

# 1. 开始登录,可一次建立多个下游 Session
curl -X POST http://localhost:16384/api/auth/start \
  -H 'Content-Type: application/json' \
  -d '{"username":"学号","password":"密码","services":["jwb","jxzxehall"]}'

# 2. status=waiting_sms 后提交验证码
curl -X POST http://localhost:16384/api/auth/CHALLENGE_ID/sms \
  -H 'Content-Type: application/json' \
  -H 'X-Challenge-Token: ACCESS_TOKEN' \
  -d '{"code":"123456"}'

# 3. 查询直至 status=authenticated
curl http://localhost:16384/api/auth/CHALLENGE_ID \
  -H 'X-Challenge-Token: ACCESS_TOKEN'

# 4. 获取下游登录结果
curl http://localhost:16384/api/auth/CHALLENGE_ID/services/yanhekt \
  -H 'X-Challenge-Token: ACCESS_TOKEN'

# 5. 使用已建立的 JWB Session,不再发送账号密码
curl -X POST http://localhost:16384/api/jwb/all_score \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -d '{"challenge_id":"CHALLENGE_ID","detailed":false}'

支持的 challenge service 名称:webvpnjwbjwb_cjdjxzxehallibityanhektlibrarydektcxcy

注册 JWT

注册认证直接复用上面的 challenge。若不需要其他下游服务,第一步传 "services":["webvpn"] 即可。轮询到 status=authenticated 后,使用同一个 ACCESS_TOKEN 换取默认有效期为 5 分钟的注册 JWT:

curl -X POST http://localhost:16384/api/auth/CHALLENGE_ID/registration-token \
  -H 'X-Challenge-Token: ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{"audience":"bit101-main"}'

生成并配置 Ed25519 私钥:

openssl genpkey -algorithm ED25519 -out registration-private.pem
openssl pkey -in registration-private.pem -pubout -out registration-public.pem

export REGISTRATION_JWT_PRIVATE_KEY_FILE=/path/to/registration-private.pem
export REGISTRATION_JWT_ISSUER=bit-login
export REGISTRATION_JWT_ALLOWED_AUDIENCES=bit101-main,course-app
export REGISTRATION_JWT_TTL=300
export REGISTRATION_JWT_KEY_ID=registration-1

私钥只部署在服务器上,公钥可公开。每个主程序只接受自己的 aud; 未列入 REGISTRATION_JWT_ALLOWED_AUDIENCES 的 audience 无法签发。

🔗 参考仓库

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

bit_login-4.0.0-py3-none-any.whl (45.3 kB view details)

Uploaded Python 3

File details

Details for the file bit_login-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: bit_login-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 45.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for bit_login-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a6530fb7515c7591cda992199f42f4aab2bf092f6123b227ecf21afe57de482
MD5 66e48804e9fac8733d909b78c25f6d9e
BLAKE2b-256 b95180705509a23f76e21c1272f100df1075f783dfa7cd46548abe52e6aeecf3

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

4.0.0 This release

1 file

3.4.3

1 file

3.4.2

1 file

3.4.1

1 file

3.4.0

1 file

3.3.1

1 file

3.3.0

1 file

3.2.1

1 file

3.2.0

1 file

3.1.2

1 file

3.1.1

1 file

3.1.0

1 file

3.0.0

1 file

2.0.0

1 file

1.2.0

1 file

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.3

2 files

1.0.1

2 files

1.0.0

1 file

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