Compare commits
1 Commits
main
...
74c2b1b7e7
| Author | SHA1 | Date | |
|---|---|---|---|
| 74c2b1b7e7 |
@@ -1,85 +1,12 @@
|
|||||||
{
|
{
|
||||||
"permissions": {
|
"permissions": {
|
||||||
"allow": [
|
"allow": [
|
||||||
"Read(./.env.example)",
|
"Read(./.env.example)"
|
||||||
|
|
||||||
"Bash(cat:*)",
|
|
||||||
"Bash(head:*)",
|
|
||||||
"Bash(tail:*)",
|
|
||||||
"Bash(less:*)",
|
|
||||||
"Bash(wc:*)",
|
|
||||||
"Bash(ls:*)",
|
|
||||||
"Bash(tree:*)",
|
|
||||||
"Bash(find:*)",
|
|
||||||
"Bash(grep:*)",
|
|
||||||
"Bash(rg:*)",
|
|
||||||
"Bash(fd:*)",
|
|
||||||
"Bash(file:*)",
|
|
||||||
"Bash(stat:*)",
|
|
||||||
"Bash(du:*)",
|
|
||||||
"Bash(diff:*)",
|
|
||||||
"Bash(which:*)",
|
|
||||||
"Bash(whoami:*)",
|
|
||||||
"Bash(echo:*)",
|
|
||||||
"Bash(pwd:*)",
|
|
||||||
"Bash(date:*)",
|
|
||||||
"Bash(uname:*)",
|
|
||||||
|
|
||||||
"Bash(git status:*)",
|
|
||||||
"Bash(git diff:*)",
|
|
||||||
"Bash(git log:*)",
|
|
||||||
"Bash(git branch:*)",
|
|
||||||
"Bash(git show:*)",
|
|
||||||
"Bash(git remote:*)",
|
|
||||||
"Bash(git tag:*)",
|
|
||||||
"Bash(git stash list:*)",
|
|
||||||
"Bash(git blame:*)",
|
|
||||||
"Bash(git shortlog:*)",
|
|
||||||
"Bash(git rev-parse:*)",
|
|
||||||
|
|
||||||
"Task",
|
|
||||||
|
|
||||||
"Edit",
|
|
||||||
"Write",
|
|
||||||
"MultiEdit",
|
|
||||||
|
|
||||||
"Bash(pnpm install:*)",
|
|
||||||
"Bash(pnpm add:*)",
|
|
||||||
"Bash(pnpm run build:*)",
|
|
||||||
"Bash(pnpm prisma generate:*)",
|
|
||||||
"Bash(pnpm run lint:*)",
|
|
||||||
"Bash(pip install:*)",
|
|
||||||
"Bash(pip3 install:*)",
|
|
||||||
|
|
||||||
"Bash(mkdir:*)",
|
|
||||||
"Bash(cp:*)",
|
|
||||||
"Bash(mv:*)",
|
|
||||||
"Bash(rm:*)",
|
|
||||||
"Bash(touch:*)",
|
|
||||||
"Bash(chmod:*)",
|
|
||||||
"Bash(chown:*)",
|
|
||||||
"Bash(ln:*)",
|
|
||||||
"Bash(tar:*)",
|
|
||||||
"Bash(unzip:*)",
|
|
||||||
"Bash(curl:*)",
|
|
||||||
"Bash(wget:*)",
|
|
||||||
"Bash(sed:*)",
|
|
||||||
"Bash(awk:*)",
|
|
||||||
"Bash(sort:*)",
|
|
||||||
"Bash(uniq:*)",
|
|
||||||
"Bash(cut:*)",
|
|
||||||
"Bash(xargs:*)",
|
|
||||||
"Bash(tee:*)",
|
|
||||||
"Bash(jq:*)",
|
|
||||||
|
|
||||||
"mcp__ide__getDiagnostics"
|
|
||||||
],
|
],
|
||||||
"deny": [
|
"deny": [
|
||||||
"Read(./.env)",
|
"Read(./.env)",
|
||||||
"Read(./.env.development)",
|
"Read(./.env.development)",
|
||||||
"Read(./.env.production)",
|
"Read(./.env.production)"
|
||||||
|
|
||||||
"Read(./.git/config)"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,415 +0,0 @@
|
|||||||
---
|
|
||||||
name: pku-iaaa
|
|
||||||
description: |
|
|
||||||
Integrate Peking University (PKU) IAAA unified authentication system into web applications.
|
|
||||||
Use this skill whenever the user wants to implement PKU IAAA SSO login, PKU unified identity
|
|
||||||
authentication, or connect their app to iaaa.pku.edu.cn. Also trigger when the user mentions
|
|
||||||
"北大统一认证", "IAAA认证", "北大SSO", "PKU login", or wants to add campus authentication
|
|
||||||
for a Peking University application. Covers login redirect, token validation, proxy SSO,
|
|
||||||
and account verification — for any web framework (Django, Spring Boot, Express.js, Flask, etc).
|
|
||||||
---
|
|
||||||
|
|
||||||
# 北京大学 IAAA 统一身份认证集成
|
|
||||||
|
|
||||||
本 skill 指导你将北京大学 IAAA(统一安全系统)身份认证集成到任意 Web 应用中。
|
|
||||||
|
|
||||||
## 前置条件
|
|
||||||
|
|
||||||
集成前需在 IAAA 注册应用系统,准备以下信息:
|
|
||||||
- **应用系统 ID**(`appId`):英文字母开头,可含数字
|
|
||||||
- **Key**:由 IAAA 提供,用于生成消息摘要
|
|
||||||
- **服务器 IP 地址**:应用系统所在服务器的 IP
|
|
||||||
- **回调地址 URL**(`redirectUrl`):用于接收 token 的回调端点
|
|
||||||
|
|
||||||
申请方式:管理部门用户登录校内门户 → "信息服务"或"办事大厅" → 搜索"统一身份认证应用备案申请"。
|
|
||||||
注册后可在 `https://i3a.pku.edu.cn/iaaa/index.jsp` 自行管理服务器 IP 和回调 URL。
|
|
||||||
|
|
||||||
## 开始集成前,先确认需求
|
|
||||||
|
|
||||||
在动手写代码之前,先向用户确认:
|
|
||||||
|
|
||||||
1. **使用什么框架?** (Django / Spring Boot / Express.js / Flask / 其他)
|
|
||||||
2. **需要哪种级别的集成?**
|
|
||||||
- 基本认证(登录跳转 + Token 验证)— 绝大多数场景只需这个
|
|
||||||
- 代理 SSO(中心应用 + 分支应用架构)— 参见 `references/proxy-sso.md`
|
|
||||||
- 账号验证(仅校验某个学号/职工号是否存在)— 参见 `references/account-verify.md`
|
|
||||||
3. **系统是否还有非校内账号?** 如果是,需要同时保留自有登录入口(影响前端跳转参数)
|
|
||||||
|
|
||||||
## 认证流程概览
|
|
||||||
|
|
||||||
```
|
|
||||||
用户点击登录 ──POST──> iaaa.pku.edu.cn/iaaa/oauth.jsp
|
|
||||||
│
|
|
||||||
用户输入账号密码认证
|
|
||||||
│
|
|
||||||
认证成功,生成 token
|
|
||||||
│
|
|
||||||
302 重定向到 redirectUrl?token=xxx
|
|
||||||
│
|
|
||||||
应用服务端收到 token
|
|
||||||
│
|
|
||||||
服务端调用 validate.do 验证 token
|
|
||||||
│
|
|
||||||
验证成功,获取用户信息(identityId 等)
|
|
||||||
│
|
|
||||||
匹配本地用户,创建会话
|
|
||||||
```
|
|
||||||
|
|
||||||
## 第一步:登录跳转(前端)
|
|
||||||
|
|
||||||
用户点击登录时,通过 POST 表单跳转到 IAAA 统一登录页面。
|
|
||||||
|
|
||||||
### 关键参数
|
|
||||||
|
|
||||||
| 参数 | 说明 |
|
|
||||||
|------|------|
|
|
||||||
| `appID` | 注册的应用系统 ID |
|
|
||||||
| `redirectUrl` | 回调地址(验证 token 的端点),若含参数需 URLEncode |
|
|
||||||
| `redirectLogonUrl` | (可选)应用自有登录地址,当系统还有非校内账号时使用 |
|
|
||||||
|
|
||||||
### 实现要点
|
|
||||||
|
|
||||||
- 必须使用 POST 方法提交表单到 `https://iaaa.pku.edu.cn/iaaa/oauth.jsp`
|
|
||||||
- `redirectUrl` 的协议必须与实际部署一致(从当前请求动态获取,不要硬编码 `http://`)
|
|
||||||
- 回调 URL 的尾部斜杠很重要——IAAA 可能因缺少尾部斜杠而拒绝回调
|
|
||||||
|
|
||||||
### 参考实现(JavaScript 前端通用)
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
function redirectToIAAALogin(appID, callbackPath, loginPath) {
|
|
||||||
const form = document.createElement('form');
|
|
||||||
form.action = 'https://iaaa.pku.edu.cn/iaaa/oauth.jsp';
|
|
||||||
form.method = 'POST';
|
|
||||||
form.style.display = 'none';
|
|
||||||
|
|
||||||
function addField(name, value) {
|
|
||||||
const input = document.createElement('input');
|
|
||||||
input.type = 'hidden';
|
|
||||||
input.name = name;
|
|
||||||
input.value = value;
|
|
||||||
form.appendChild(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用当前页面协议,不要硬编码 http://
|
|
||||||
const baseUrl = `${location.protocol}//${location.host}`;
|
|
||||||
addField('appID', appID);
|
|
||||||
addField('redirectUrl', `${baseUrl}${callbackPath}`);
|
|
||||||
if (loginPath) {
|
|
||||||
addField('redirectLogonUrl', `${baseUrl}${loginPath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
document.body.appendChild(form);
|
|
||||||
form.submit();
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 第二步:Token 验证(服务端)
|
|
||||||
|
|
||||||
IAAA 认证成功后,会重定向浏览器到 `redirectUrl?token=<TOKEN>`。服务端需要验证这个 token。
|
|
||||||
|
|
||||||
### 验证端点
|
|
||||||
|
|
||||||
```
|
|
||||||
GET https://iaaa.pku.edu.cn/iaaa/svc/token/validate.do
|
|
||||||
?remoteAddr={REMOTE_ADDR}
|
|
||||||
&appId={APP_ID}
|
|
||||||
&token={TOKEN}
|
|
||||||
&msgAbs={MSG_ABS}
|
|
||||||
```
|
|
||||||
|
|
||||||
如果不需要返回用户详细信息(只需 identityId),可使用简单验证端点:
|
|
||||||
```
|
|
||||||
GET https://iaaa.pku.edu.cn/iaaa/svc/token/validateSimple.do?...(参数相同)
|
|
||||||
```
|
|
||||||
|
|
||||||
### 参数说明
|
|
||||||
|
|
||||||
| 参数 | 说明 |
|
|
||||||
|------|------|
|
|
||||||
| `remoteAddr` | 访问应用系统的用户**真实 IP 地址** |
|
|
||||||
| `appId` | 应用系统 ID |
|
|
||||||
| `token` | IAAA 传回的票据 |
|
|
||||||
| `msgAbs` | MD5 消息摘要(见下方计算方法) |
|
|
||||||
|
|
||||||
### msgAbs 计算方法(极其重要)
|
|
||||||
|
|
||||||
1. 取除 `msgAbs` 以外的所有请求参数
|
|
||||||
2. 按**参数名升序**排列
|
|
||||||
3. 拼接为查询字符串格式:`appId={APP_ID}&remoteAddr={REMOTE_ADDR}&token={TOKEN}`
|
|
||||||
4. 将此字符串与 Key 直接拼接(不加分隔符):`PARA_STR + Key`
|
|
||||||
5. 对拼接结果计算 MD5(UTF-8 编码,32 位十六进制,大小写不敏感)
|
|
||||||
|
|
||||||
**Python 示例:**
|
|
||||||
```python
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
para_str = f"appId={app_id}&remoteAddr={remote_addr}&token={token}"
|
|
||||||
raw = para_str + key
|
|
||||||
msg_abs = hashlib.md5(raw.encode('utf-8')).hexdigest()
|
|
||||||
```
|
|
||||||
|
|
||||||
**Java 示例:**
|
|
||||||
```java
|
|
||||||
String paraStr = "appId=" + appId + "&remoteAddr=" + remoteAddr + "&token=" + token;
|
|
||||||
String raw = paraStr + key;
|
|
||||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
||||||
byte[] digest = md.digest(raw.getBytes(StandardCharsets.UTF_8));
|
|
||||||
String msgAbs = String.format("%032x", new BigInteger(1, digest));
|
|
||||||
```
|
|
||||||
|
|
||||||
**Node.js 示例:**
|
|
||||||
```javascript
|
|
||||||
const crypto = require('crypto');
|
|
||||||
const paraStr = `appId=${appId}&remoteAddr=${remoteAddr}&token=${token}`;
|
|
||||||
const msgAbs = crypto.createHash('md5').update(paraStr + key).digest('hex');
|
|
||||||
```
|
|
||||||
|
|
||||||
### 获取客户端真实 IP
|
|
||||||
|
|
||||||
反向代理(Nginx 等)环境下,客户端 IP 通常在 `X-Forwarded-For` 头中。取第一个值(最左侧)作为真实 IP:
|
|
||||||
|
|
||||||
```python
|
|
||||||
# Python / Django
|
|
||||||
remote_addr = request.META.get('HTTP_X_FORWARDED_FOR', '').split(',')[0].strip() \
|
|
||||||
or request.META.get('REMOTE_ADDR', '')
|
|
||||||
```
|
|
||||||
|
|
||||||
```java
|
|
||||||
// Java / Spring Boot
|
|
||||||
String remoteAddr = request.getHeader("X-Forwarded-For");
|
|
||||||
if (remoteAddr != null && !remoteAddr.isEmpty()) {
|
|
||||||
remoteAddr = remoteAddr.split(",")[0].trim();
|
|
||||||
} else {
|
|
||||||
remoteAddr = request.getRemoteAddr();
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
// Node.js / Express
|
|
||||||
const remoteAddr = (req.headers['x-forwarded-for'] || '').split(',')[0].trim()
|
|
||||||
|| req.socket.remoteAddress;
|
|
||||||
```
|
|
||||||
|
|
||||||
### 返回结果
|
|
||||||
|
|
||||||
**成功:**
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"success": true,
|
|
||||||
"errCode": "0",
|
|
||||||
"errMsg": "认证成功",
|
|
||||||
"userInfo": {
|
|
||||||
"name": "用户姓名",
|
|
||||||
"status": "账号状态(开通/禁止)",
|
|
||||||
"identityId": "身份账号(学号/职工号)",
|
|
||||||
"deptId": "所在院系码",
|
|
||||||
"dept": "所在院系",
|
|
||||||
"identityType": "身份类别(学生/职工)",
|
|
||||||
"detailType": "身份细类",
|
|
||||||
"identityStatus": "身份状态(在校/减离)",
|
|
||||||
"campus": "校区"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**失败:**
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"success": false,
|
|
||||||
"errCode": "错误码",
|
|
||||||
"errMsg": "错误信息"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
简单验证端点(`validateSimple.do`)成功时 `userInfo` 中只包含 `identityId`。
|
|
||||||
|
|
||||||
### 服务端验证的完整参考实现
|
|
||||||
|
|
||||||
#### Django
|
|
||||||
|
|
||||||
```python
|
|
||||||
import hashlib
|
|
||||||
import logging
|
|
||||||
import requests
|
|
||||||
from django.contrib.auth import login, get_user_model
|
|
||||||
from django.http import HttpResponseRedirect
|
|
||||||
from django.shortcuts import render, reverse
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
User = get_user_model()
|
|
||||||
|
|
||||||
IAAA_APP_ID = "your_app_id"
|
|
||||||
IAAA_KEY = "your_key"
|
|
||||||
IAAA_USERID_FIELD = "username" # User 模型中与 identityId 对应的字段
|
|
||||||
|
|
||||||
def iaaa_callback(request):
|
|
||||||
"""IAAA 认证回调视图"""
|
|
||||||
token = request.GET.get('token')
|
|
||||||
if not token:
|
|
||||||
return render(request, 'login_failed.html', {'msg': '未收到认证票据'})
|
|
||||||
|
|
||||||
# 获取客户端真实 IP
|
|
||||||
remote_addr = (
|
|
||||||
request.META.get('HTTP_X_FORWARDED_FOR', '').split(',')[0].strip()
|
|
||||||
or request.META.get('REMOTE_ADDR', '')
|
|
||||||
)
|
|
||||||
|
|
||||||
# 计算消息摘要(参数按字母序排列)
|
|
||||||
para_str = f"appId={IAAA_APP_ID}&remoteAddr={remote_addr}&token={token}"
|
|
||||||
msg_abs = hashlib.md5((para_str + IAAA_KEY).encode('utf-8')).hexdigest()
|
|
||||||
|
|
||||||
# 调用 IAAA 验证服务
|
|
||||||
validate_url = (
|
|
||||||
f"https://iaaa.pku.edu.cn/iaaa/svc/token/validate.do"
|
|
||||||
f"?{para_str}&msgAbs={msg_abs}"
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
resp = requests.get(validate_url, timeout=10)
|
|
||||||
resp.raise_for_status()
|
|
||||||
result = resp.json()
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"IAAA token 验证请求失败: {e}")
|
|
||||||
return render(request, 'login_failed.html', {'msg': '身份认证服务暂不可用,请稍后重试'})
|
|
||||||
|
|
||||||
if not result.get('success'):
|
|
||||||
err_code = result.get('errCode', '未知')
|
|
||||||
err_msg = result.get('errMsg', '未知错误')
|
|
||||||
logger.warning(f"IAAA 认证失败: errCode={err_code}, errMsg={err_msg}")
|
|
||||||
return render(request, 'login_failed.html', {'msg': f'认证失败:{err_msg}'})
|
|
||||||
|
|
||||||
# 认证成功,提取用户身份
|
|
||||||
user_info = result.get('userInfo', {})
|
|
||||||
identity_id = user_info.get('identityId', '')
|
|
||||||
if not identity_id:
|
|
||||||
logger.warning(f"IAAA 认证成功但未返回 identityId: {result}")
|
|
||||||
return render(request, 'login_failed.html', {'msg': '无法获取您的身份账号'})
|
|
||||||
|
|
||||||
# 匹配本地用户
|
|
||||||
user = User.objects.filter(**{IAAA_USERID_FIELD: identity_id}).first()
|
|
||||||
if user is None:
|
|
||||||
logger.info(f"IAAA 用户 {identity_id} 在系统中不存在")
|
|
||||||
return render(request, 'login_failed.html', {'msg': '系统中不存在与您北大账号对应的用户'})
|
|
||||||
|
|
||||||
# 登录
|
|
||||||
login(request, user)
|
|
||||||
logger.info(f"IAAA 登录成功: {identity_id} ({user_info.get('name', '')})")
|
|
||||||
return HttpResponseRedirect(reverse('index'))
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Spring Boot
|
|
||||||
|
|
||||||
```java
|
|
||||||
@GetMapping("/iaaa/callback")
|
|
||||||
public String iaaaCallback(
|
|
||||||
@RequestParam String token,
|
|
||||||
HttpServletRequest request,
|
|
||||||
HttpSession session) throws Exception {
|
|
||||||
|
|
||||||
// 获取客户端真实 IP
|
|
||||||
String remoteAddr = request.getHeader("X-Forwarded-For");
|
|
||||||
if (remoteAddr != null && !remoteAddr.isEmpty()) {
|
|
||||||
remoteAddr = remoteAddr.split(",")[0].trim();
|
|
||||||
} else {
|
|
||||||
remoteAddr = request.getRemoteAddr();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 计算消息摘要
|
|
||||||
String paraStr = "appId=" + appId + "&remoteAddr=" + remoteAddr + "&token=" + token;
|
|
||||||
String msgAbs = md5(paraStr + key);
|
|
||||||
|
|
||||||
// 调用验证服务
|
|
||||||
String url = "https://iaaa.pku.edu.cn/iaaa/svc/token/validate.do?"
|
|
||||||
+ paraStr + "&msgAbs=" + msgAbs;
|
|
||||||
RestTemplate rest = new RestTemplate();
|
|
||||||
Map<String, Object> result = rest.getForObject(url, Map.class);
|
|
||||||
|
|
||||||
if (!(Boolean) result.get("success")) {
|
|
||||||
String errMsg = (String) result.getOrDefault("errMsg", "认证失败");
|
|
||||||
// 处理失败...
|
|
||||||
return "redirect:/login?error=" + URLEncoder.encode(errMsg, "UTF-8");
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, String> userInfo = (Map<String, String>) result.get("userInfo");
|
|
||||||
String identityId = userInfo.get("identityId");
|
|
||||||
// 查找本地用户,创建会话...
|
|
||||||
|
|
||||||
return "redirect:/";
|
|
||||||
}
|
|
||||||
|
|
||||||
private String md5(String input) throws Exception {
|
|
||||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
||||||
byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));
|
|
||||||
return String.format("%032x", new java.math.BigInteger(1, digest));
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Express.js
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const crypto = require('crypto');
|
|
||||||
const axios = require('axios');
|
|
||||||
|
|
||||||
app.get('/iaaa/callback', async (req, res) => {
|
|
||||||
const { token } = req.query;
|
|
||||||
if (!token) return res.redirect('/login?error=no_token');
|
|
||||||
|
|
||||||
const remoteAddr = (req.headers['x-forwarded-for'] || '').split(',')[0].trim()
|
|
||||||
|| req.socket.remoteAddress;
|
|
||||||
|
|
||||||
const paraStr = `appId=${APP_ID}&remoteAddr=${remoteAddr}&token=${token}`;
|
|
||||||
const msgAbs = crypto.createHash('md5').update(paraStr + KEY).digest('hex');
|
|
||||||
|
|
||||||
const url = `https://iaaa.pku.edu.cn/iaaa/svc/token/validate.do?${paraStr}&msgAbs=${msgAbs}`;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { data } = await axios.get(url);
|
|
||||||
|
|
||||||
if (!data.success) {
|
|
||||||
console.warn(`IAAA 认证失败: ${data.errCode} - ${data.errMsg}`);
|
|
||||||
return res.redirect(`/login?error=${encodeURIComponent(data.errMsg || '认证失败')}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { identityId, name } = data.userInfo;
|
|
||||||
if (!identityId) {
|
|
||||||
return res.redirect('/login?error=no_identity');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查找本地用户,创建 session...
|
|
||||||
const user = await User.findOne({ where: { username: identityId } });
|
|
||||||
if (!user) {
|
|
||||||
return res.redirect('/login?error=user_not_found');
|
|
||||||
}
|
|
||||||
|
|
||||||
req.session.userId = user.id;
|
|
||||||
console.log(`IAAA 登录成功: ${identityId} (${name})`);
|
|
||||||
res.redirect('/');
|
|
||||||
} catch (err) {
|
|
||||||
console.error('IAAA 验证请求失败:', err.message);
|
|
||||||
res.redirect('/login?error=service_unavailable');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## 常见易错点
|
|
||||||
|
|
||||||
实现时特别注意以下几点,这些是实际集成中最常见的 bug 来源:
|
|
||||||
|
|
||||||
1. **协议硬编码**:`redirectUrl` 中不要写死 `http://`,应从当前请求或配置动态获取。生产环境通常是 HTTPS,协议不匹配会导致回调失败。
|
|
||||||
|
|
||||||
2. **msgAbs 参数排序**:参数必须严格按参数名字母升序拼接。当前 `validate.do` 只有三个参数(`appId`, `remoteAddr`, `token`),恰好已是字母序。但如果使用代理认证等其他端点(参数更多),一定要排序。
|
|
||||||
|
|
||||||
3. **错误响应处理**:IAAA 返回 `success: false` 时,务必提取 `errCode` 和 `errMsg` 记录到日志并展示给用户,不要只返回一个笼统的"认证失败"。
|
|
||||||
|
|
||||||
4. **客户端 IP**:`remoteAddr` 必须是用户的真实 IP。如果应用在反向代理之后,使用 `X-Forwarded-For` 头的第一个值。IP 不匹配会导致 token 验证失败。
|
|
||||||
|
|
||||||
5. **回调 URL 尾部斜杠**:IAAA 在注册时绑定了精确的回调 URL,运行时传入的 `redirectUrl` 必须完全匹配(包括尾部斜杠)。
|
|
||||||
|
|
||||||
6. **服务端可达性**:应用服务器必须能访问 `iaaa.pku.edu.cn` 的 443 端口(HTTPS)。
|
|
||||||
|
|
||||||
## 高级功能
|
|
||||||
|
|
||||||
对于需要代理 SSO(中心应用 + 分支应用)或账号验证的场景,参阅:
|
|
||||||
|
|
||||||
- `references/proxy-sso.md` — 代理身份认证(单点登录多个应用)
|
|
||||||
- `references/account-verify.md` — 验证北京大学账号是否存在
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
# 北京大学账号验证
|
|
||||||
|
|
||||||
用于验证某个学号/职工号是否为有效的北京大学账号,不涉及登录流程。
|
|
||||||
|
|
||||||
## API
|
|
||||||
|
|
||||||
```
|
|
||||||
GET https://iaaa.pku.edu.cn/iaaa/svc/pub/validate.do
|
|
||||||
?userId={USERID}
|
|
||||||
&userName={USERNAME}
|
|
||||||
&appId={APPID}
|
|
||||||
&msgAbs={MD5MSG}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 参数
|
|
||||||
|
|
||||||
| 参数 | 说明 |
|
|
||||||
|------|------|
|
|
||||||
| `userId` | 学号或职工号 |
|
|
||||||
| `userName` | 账号姓名(传输时需 URLEncode,但计算 MD5 摘要时使用原始值,不做 URLEncode) |
|
|
||||||
| `appId` | 应用 ID |
|
|
||||||
| `msgAbs` | MD5 消息摘要 |
|
|
||||||
|
|
||||||
## msgAbs 计算
|
|
||||||
|
|
||||||
与其他端点规则一致:除 `msgAbs` 外所有参数按参数名升序排列拼接,再拼接 Key,取 MD5。
|
|
||||||
|
|
||||||
PARA_STR 排序后为:`appId={APPID}&userId={USERID}&userName={USERNAME}`
|
|
||||||
|
|
||||||
注意:`userName` 在 PARA_STR 中使用**原始值**(不做 URLEncode),只有在 URL 传输时才 URLEncode。
|
|
||||||
|
|
||||||
## 返回
|
|
||||||
|
|
||||||
**正常:**
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"success": true,
|
|
||||||
"valid": true,
|
|
||||||
"userType": "用户身份类别"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
`valid` 为 `true` 表示账号存在,`false` 表示不存在。
|
|
||||||
|
|
||||||
**异常:**
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"success": false,
|
|
||||||
"errCode": "错误代码",
|
|
||||||
"errMsg": "错误信息"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Python 示例
|
|
||||||
|
|
||||||
```python
|
|
||||||
import hashlib
|
|
||||||
import urllib.parse
|
|
||||||
import requests
|
|
||||||
|
|
||||||
def verify_pku_account(user_id: str, user_name: str, app_id: str, key: str) -> dict:
|
|
||||||
"""验证北大账号是否存在"""
|
|
||||||
# msgAbs 中 userName 使用原始值
|
|
||||||
para_str = f"appId={app_id}&userId={user_id}&userName={user_name}"
|
|
||||||
msg_abs = hashlib.md5((para_str + key).encode('utf-8')).hexdigest()
|
|
||||||
|
|
||||||
# URL 中 userName 需要 URLEncode
|
|
||||||
url = (
|
|
||||||
f"https://iaaa.pku.edu.cn/iaaa/svc/pub/validate.do"
|
|
||||||
f"?appId={app_id}"
|
|
||||||
f"&userId={user_id}"
|
|
||||||
f"&userName={urllib.parse.quote(user_name)}"
|
|
||||||
f"&msgAbs={msg_abs}"
|
|
||||||
)
|
|
||||||
resp = requests.get(url, timeout=10)
|
|
||||||
resp.raise_for_status()
|
|
||||||
return resp.json()
|
|
||||||
```
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
# 代理身份认证(Proxy SSO)
|
|
||||||
|
|
||||||
适用场景:应用 O(中心应用)登录后,用户点击进入应用 A、B、C(分支应用)时无需重新登录。
|
|
||||||
|
|
||||||
## 流程
|
|
||||||
|
|
||||||
```
|
|
||||||
用户登录中心应用 O
|
|
||||||
│
|
|
||||||
├─ O 使用 validateWithProxy.do 验证 token → 获得 grantToken
|
|
||||||
│
|
|
||||||
├─ 用户点击分支应用 A
|
|
||||||
│ ├─ O 调用 validateProxyGrantToken.do → 获得分支应用 A 的 token
|
|
||||||
│ └─ 将 token 传给分支应用 A → A 用 validate.do 验证
|
|
||||||
│
|
|
||||||
└─ 用户退出中心应用 O
|
|
||||||
└─ O 调用 expireProxy.do 注销 grantToken
|
|
||||||
```
|
|
||||||
|
|
||||||
## API 详情
|
|
||||||
|
|
||||||
### 1. 中心应用验证(获取 grantToken)
|
|
||||||
|
|
||||||
```
|
|
||||||
GET https://iaaa.pku.edu.cn/iaaa/svc/token/validateWithProxy.do
|
|
||||||
?remoteAddr={REMOTE_ADDR}
|
|
||||||
&appId={APP_ID}
|
|
||||||
&token={TOKEN}
|
|
||||||
&msgAbs={MSG_ABS}
|
|
||||||
```
|
|
||||||
|
|
||||||
参数和 msgAbs 计算方式与标准 `validate.do` 相同。
|
|
||||||
|
|
||||||
**成功返回:**
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"success": true,
|
|
||||||
"errCode": "0",
|
|
||||||
"errMsg": "认证成功",
|
|
||||||
"userInfo": {
|
|
||||||
"name": "用户姓名",
|
|
||||||
"status": "账号状态",
|
|
||||||
"identityId": "身份账号",
|
|
||||||
"deptId": "所在院系码",
|
|
||||||
"dept": "所在院系",
|
|
||||||
"identityType": "身份类别",
|
|
||||||
"detailType": "身份细类",
|
|
||||||
"identityStatus": "身份状态"
|
|
||||||
},
|
|
||||||
"grantToken": "代理票据"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. 获取分支应用 Token
|
|
||||||
|
|
||||||
```
|
|
||||||
GET https://iaaa.pku.edu.cn/iaaa/svc/token/validateProxyGrantToken.do
|
|
||||||
?remoteAddr={REMOTE_ADDR}
|
|
||||||
&appId={APP_ID}
|
|
||||||
&targetAppId={TARGET_APP_ID}
|
|
||||||
&grantToken={GRANT_TOKEN}
|
|
||||||
×tamp={TIMESTAMP}
|
|
||||||
&msgAbs={MSG_ABS}
|
|
||||||
```
|
|
||||||
|
|
||||||
**参数:**
|
|
||||||
|
|
||||||
| 参数 | 说明 |
|
|
||||||
|------|------|
|
|
||||||
| `remoteAddr` | 客户端 IP |
|
|
||||||
| `appId` | 中心应用 ID |
|
|
||||||
| `targetAppId` | 分支应用 ID |
|
|
||||||
| `grantToken` | 上一步获得的代理票据 |
|
|
||||||
| `timestamp` | 当前时间戳(long 型整数) |
|
|
||||||
| `msgAbs` | MD5 摘要,PARA_STR 为除 msgAbs 外所有参数按参数名升序拼接 |
|
|
||||||
|
|
||||||
注意:此端点参数较多,PARA_STR 排序后为:
|
|
||||||
`appId={}&grantToken={}&remoteAddr={}&targetAppId={}×tamp={}`
|
|
||||||
|
|
||||||
**成功返回:**
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"success": true,
|
|
||||||
"errCode": "0",
|
|
||||||
"errMsg": "验证成功",
|
|
||||||
"token": "分支应用访问票据"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**错误码 12(需要手机验证):** 重定向用户到:
|
|
||||||
```
|
|
||||||
https://iaaa.pku.edu.cn/iaaa/ma4proxy.jsp
|
|
||||||
?proxyAppId={中心应用ID}
|
|
||||||
&appId={分支应用ID}
|
|
||||||
&userId={用户账号}
|
|
||||||
&grantToken={GRANT_TOKEN}
|
|
||||||
&redirectUrl={分支应用回调地址,需URLEncode}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. 分支应用验证 Token
|
|
||||||
|
|
||||||
分支应用收到 token 后,使用标准 `validate.do` 或 `validateSimple.do` 验证,与普通认证流程相同。
|
|
||||||
|
|
||||||
### 4. 注销代理 Token
|
|
||||||
|
|
||||||
中心应用退出时或必要时调用:
|
|
||||||
|
|
||||||
```
|
|
||||||
GET https://iaaa.pku.edu.cn/iaaa/svc/token/expireProxy.do
|
|
||||||
?appId={APP_ID}
|
|
||||||
&grantToken={GRANT_TOKEN}
|
|
||||||
&msgAbs={MSG_ABS}
|
|
||||||
```
|
|
||||||
|
|
||||||
msgAbs 的 PARA_STR:`appId={}&grantToken={}`(按参数名升序)
|
|
||||||
|
|
||||||
**返回:**
|
|
||||||
```json
|
|
||||||
{"success": true, "errCode": "0", "errMsg": "注销成功"}
|
|
||||||
```
|
|
||||||
@@ -50,8 +50,6 @@ RUN apt-get update && apt-get install -y \
|
|||||||
imagemagick \
|
imagemagick \
|
||||||
zsh \
|
zsh \
|
||||||
vim \
|
vim \
|
||||||
rsync \
|
|
||||||
openjdk-17-jdk \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# 安装 Python 3.12
|
# 安装 Python 3.12
|
||||||
@@ -98,13 +96,10 @@ RUN curl -fsSL https://code-server.dev/install.sh | sh -s -- --version=${CODE_SE
|
|||||||
|
|
||||||
# 安装 npm 全局包
|
# 安装 npm 全局包
|
||||||
RUN npm install -g \
|
RUN npm install -g \
|
||||||
|
@anthropic-ai/claude-code \
|
||||||
@musistudio/claude-code-router \
|
@musistudio/claude-code-router \
|
||||||
pm2
|
pm2
|
||||||
|
|
||||||
# 安装 claude code 编程代理
|
|
||||||
RUN curl -fsSL https://claude.ai/install.sh | bash && \
|
|
||||||
echo 'eval "$(ccr activate)"' >> /root/.zshrc
|
|
||||||
|
|
||||||
# 创建工作目录
|
# 创建工作目录
|
||||||
RUN mkdir -p /workspace /root/.local/share/code-server/User
|
RUN mkdir -p /workspace /root/.local/share/code-server/User
|
||||||
|
|
||||||
@@ -120,7 +115,7 @@ RUN code-server --install-extension ms-ceintl.vscode-language-pack-zh-hans \
|
|||||||
&& code-server --install-extension dbaeumer.vscode-eslint \
|
&& code-server --install-extension dbaeumer.vscode-eslint \
|
||||||
&& code-server --install-extension prisma.prisma \
|
&& code-server --install-extension prisma.prisma \
|
||||||
&& code-server --install-extension ecmel.vscode-html-css \
|
&& code-server --install-extension ecmel.vscode-html-css \
|
||||||
&& code-server --install-extension cweijan.vscode-database-client2 \
|
&& code-server --install-extension cweijan.vscode-redis-client \
|
||||||
&& code-server --install-extension anthropic.claude-code
|
&& code-server --install-extension anthropic.claude-code
|
||||||
|
|
||||||
# 配置 code-server (密码将在启动时设置)
|
# 配置 code-server (密码将在启动时设置)
|
||||||
|
|||||||
@@ -23,8 +23,6 @@ services:
|
|||||||
- claude-code-router:/root/.claude-code-router
|
- claude-code-router:/root/.claude-code-router
|
||||||
# SSH 配置
|
# SSH 配置
|
||||||
- ssh:/root/.ssh
|
- ssh:/root/.ssh
|
||||||
# 零碎文件持久化
|
|
||||||
- persist:/root/.hair-keeper-persist
|
|
||||||
environment:
|
environment:
|
||||||
- NODE_ENV=development
|
- NODE_ENV=development
|
||||||
- TZ=Asia/Shanghai
|
- TZ=Asia/Shanghai
|
||||||
@@ -42,12 +40,6 @@ services:
|
|||||||
memory: 4G
|
memory: 4G
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
code-server-config:
|
|
||||||
code-server-data:
|
|
||||||
claude:
|
|
||||||
claude-code-router:
|
|
||||||
ssh:
|
|
||||||
persist:
|
|
||||||
# node_modules 卷,避免主机和容器之间的文件系统差异
|
# node_modules 卷,避免主机和容器之间的文件系统差异
|
||||||
node_modules:
|
node_modules:
|
||||||
# pnpm store 卷,加速依赖安装
|
# pnpm store 卷,加速依赖安装
|
||||||
|
|||||||
@@ -1,40 +1,5 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# ============================================
|
|
||||||
# 文件持久化:启动时恢复 + 后台定期同步到命名卷
|
|
||||||
# ============================================
|
|
||||||
PERSIST_DIR="/root/.hair-keeper-persist"
|
|
||||||
PERSIST_FILES=(
|
|
||||||
"/root/.claude.json"
|
|
||||||
"/root/.zsh_history"
|
|
||||||
"/root/.bash_history"
|
|
||||||
)
|
|
||||||
|
|
||||||
mkdir -p "$PERSIST_DIR"
|
|
||||||
|
|
||||||
# 启动时恢复:持久化目录中的文件优先覆盖到原路径
|
|
||||||
for filepath in "${PERSIST_FILES[@]}"; do
|
|
||||||
filename=$(basename "$filepath")
|
|
||||||
persist_path="$PERSIST_DIR/$filename"
|
|
||||||
if [ -f "$persist_path" ] && [ -s "$persist_path" ]; then
|
|
||||||
cp "$persist_path" "$filepath"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
# 后台定期同步:将原路径文件同步回持久化目录(每60秒)
|
|
||||||
(
|
|
||||||
while true; do
|
|
||||||
sleep 60
|
|
||||||
for filepath in "${PERSIST_FILES[@]}"; do
|
|
||||||
filename=$(basename "$filepath")
|
|
||||||
persist_path="$PERSIST_DIR/$filename"
|
|
||||||
if [ -f "$filepath" ]; then
|
|
||||||
rsync -a "$filepath" "$persist_path"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
done
|
|
||||||
) &
|
|
||||||
|
|
||||||
# 设置默认密码
|
# 设置默认密码
|
||||||
DEV_PASSWORD=${DEV_PASSWORD:-clouddev}
|
DEV_PASSWORD=${DEV_PASSWORD:-clouddev}
|
||||||
|
|
||||||
@@ -77,20 +42,5 @@ echo ""
|
|||||||
echo "提示: 可通过环境变量 DEV_PASSWORD 自定义密码"
|
echo "提示: 可通过环境变量 DEV_PASSWORD 自定义密码"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
# 容器停止时执行最终同步
|
|
||||||
cleanup() {
|
|
||||||
echo "Syncing persistent files before shutdown..."
|
|
||||||
for filepath in "${PERSIST_FILES[@]}"; do
|
|
||||||
filename=$(basename "$filepath")
|
|
||||||
persist_path="$PERSIST_DIR/$filename"
|
|
||||||
if [ -f "$filepath" ]; then
|
|
||||||
rsync -a "$filepath" "$persist_path"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
exit 0
|
|
||||||
}
|
|
||||||
trap cleanup SIGTERM SIGINT
|
|
||||||
|
|
||||||
# 保持容器运行
|
# 保持容器运行
|
||||||
tail -f /dev/null &
|
tail -f /dev/null
|
||||||
wait $!
|
|
||||||
@@ -51,14 +51,6 @@ NEXTAUTH_SECRET=
|
|||||||
PKUAI_API_KEY=
|
PKUAI_API_KEY=
|
||||||
PKUAI_API_BASE=
|
PKUAI_API_BASE=
|
||||||
|
|
||||||
# 北京大学 IAAA 统一认证(可选,不配置则不启用)
|
|
||||||
## 在 IAAA 注册的应用系统 ID
|
|
||||||
IAAA_APP_ID=
|
|
||||||
## IAAA 提供的密钥,用于生成消息摘要
|
|
||||||
IAAA_KEY=
|
|
||||||
## 设为 true 则进入登录页后自动跳转到统一认证(默认 false)
|
|
||||||
IAAA_AUTO_REDIRECT=
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -52,6 +52,3 @@ tasks.md
|
|||||||
# lock 一般的项目需要用git管理,但这个是模板项目就不管理
|
# lock 一般的项目需要用git管理,但这个是模板项目就不管理
|
||||||
package-lock.json
|
package-lock.json
|
||||||
pnpm-lock.yaml
|
pnpm-lock.yaml
|
||||||
|
|
||||||
# claude
|
|
||||||
.claude/*.local.*
|
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||||
|
|
||||||
## 项目说明
|
## 项目说明
|
||||||
本项目模板(Hair Keeper v1.3.0)是一个高度集成、深度定制、约定优于配置的全栈Web应用模板,旨在保持灵活性的同时提供一套基于成熟架构的开发底座,自带身份认证(支持SSO单点登录)、权限控制、丰富前端组件、文件上传、后台任务、智能体开发等丰富功能,提供AI开发辅助,免于纠结功能如何实现,可快速上手专注于业务逻辑。
|
本项目模板(Hair Keeper v1.2.0)是一个高度集成、深度定制、约定优于配置的全栈Web应用模板,旨在保持灵活性的同时提供一套基于成熟架构的开发底座,自带身份认证、权限控制、丰富前端组件、文件上传、后台任务、智能体开发等丰富功能,提供AI开发辅助,免于纠结功能如何实现,可快速上手专注于业务逻辑。
|
||||||
|
|
||||||
Hair Keeper是个诙谐有趣的名称,和项目内容毫无关系。
|
Hair Keeper是个诙谐有趣的名称,和项目内容毫无关系。
|
||||||
|
|
||||||
@@ -77,8 +77,6 @@ Hair Keeper是个诙谐有趣的名称,和项目内容毫无关系。
|
|||||||
- `src/server/queues/`:消息队列和worker,通过其中的index.ts统一导出,任务状态更新采用trpc SSE subscription,接口定义在`server/routers/jobs.ts`中
|
- `src/server/queues/`:消息队列和worker,通过其中的index.ts统一导出,任务状态更新采用trpc SSE subscription,接口定义在`server/routers/jobs.ts`中
|
||||||
- `src/server/agents`:LLM的对接和使用
|
- `src/server/agents`:LLM的对接和使用
|
||||||
- `src/server/service/`:服务层模块集合,封装后端业务逻辑和系统服务
|
- `src/server/service/`:服务层模块集合,封装后端业务逻辑和系统服务
|
||||||
- `src/server/service/session.ts`:会话管理服务,基于Redis实现权限变更后强制重新登录
|
|
||||||
- `src/server/service/iaaa.ts`:北京大学统一认证(IAAA)对接服务,通过环境变量配置即可启用
|
|
||||||
- `src/server/service/dev/`:开发模式下的辅助功能需要的后台服务
|
- `src/server/service/dev/`:开发模式下的辅助功能需要的后台服务
|
||||||
- `src/server/utils/`:服务端专用工具函数库,为后端业务逻辑提供基础设施支持
|
- `src/server/utils/`:服务端专用工具函数库,为后端业务逻辑提供基础设施支持
|
||||||
- `src/api/dev/`:开发模式下的辅助功能需要的api
|
- `src/api/dev/`:开发模式下的辅助功能需要的api
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
# CLAUDE.md
|
|
||||||
|
|
||||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
||||||
|
|
||||||
## 项目说明
|
## 项目说明
|
||||||
本项目模板(Hair Keeper v1.3.0)是一个高度集成、深度定制、约定优于配置的全栈Web应用模板,旨在保持灵活性的同时提供一套基于成熟架构的开发底座,自带身份认证(支持SSO单点登录)、权限控制、丰富前端组件、文件上传、后台任务、智能体开发等丰富功能,提供AI开发辅助,免于纠结功能如何实现,可快速上手专注于业务逻辑。
|
本项目模板(Hair Keeper v1.2.0)是一个高度集成、深度定制、约定优于配置的全栈Web应用模板,旨在保持灵活性的同时提供一套基于成熟架构的开发底座,自带身份认证、权限控制、丰富前端组件、文件上传、后台任务、智能体开发等丰富功能,提供AI开发辅助,免于纠结功能如何实现,可快速上手专注于业务逻辑。
|
||||||
|
|
||||||
Hair Keeper是个诙谐有趣的名称,和项目内容毫无关系。
|
Hair Keeper是个诙谐有趣的名称,和项目内容毫无关系。
|
||||||
|
|
||||||
@@ -77,8 +73,6 @@ Hair Keeper是个诙谐有趣的名称,和项目内容毫无关系。
|
|||||||
- `src/server/queues/`:消息队列和worker,通过其中的index.ts统一导出,任务状态更新采用trpc SSE subscription,接口定义在`server/routers/jobs.ts`中
|
- `src/server/queues/`:消息队列和worker,通过其中的index.ts统一导出,任务状态更新采用trpc SSE subscription,接口定义在`server/routers/jobs.ts`中
|
||||||
- `src/server/agents`:LLM的对接和使用
|
- `src/server/agents`:LLM的对接和使用
|
||||||
- `src/server/service/`:服务层模块集合,封装后端业务逻辑和系统服务
|
- `src/server/service/`:服务层模块集合,封装后端业务逻辑和系统服务
|
||||||
- `src/server/service/session.ts`:会话管理服务,基于Redis实现权限变更后强制重新登录
|
|
||||||
- `src/server/service/iaaa.ts`:北京大学统一认证(IAAA)对接服务,通过环境变量配置即可启用
|
|
||||||
- `src/server/service/dev/`:开发模式下的辅助功能需要的后台服务
|
- `src/server/service/dev/`:开发模式下的辅助功能需要的后台服务
|
||||||
- `src/server/utils/`:服务端专用工具函数库,为后端业务逻辑提供基础设施支持
|
- `src/server/utils/`:服务端专用工具函数库,为后端业务逻辑提供基础设施支持
|
||||||
- `src/api/dev/`:开发模式下的辅助功能需要的api
|
- `src/api/dev/`:开发模式下的辅助功能需要的api
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hair-keeper",
|
"name": "hair-keeper",
|
||||||
"version": "1.3.0",
|
"version": "1.2.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev -p 3000 --turbo",
|
"dev": "next dev -p 3000 --turbo",
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
* `ccr code` 打开一个新对话
|
* `ccr code` 打开一个新对话
|
||||||
* `ccr code --resume` 回到之前某个对话
|
* `ccr code --resume` 回到之前某个对话
|
||||||
* `ccr model` 切换编程代理使用的模型,也可以添加新模型
|
|
||||||
|
|
||||||
对话中:
|
对话中:
|
||||||
|
|
||||||
|
|||||||
@@ -316,11 +316,7 @@ EOF
|
|||||||
|
|
||||||
## 项目说明
|
## 项目说明
|
||||||
|
|
||||||
本项目基于 Hair Keeper 模板构建(详见 @TEMPLATE_README.md),目前尚未实现业务功能。
|
本项目基于 Hair Keeper 模板构建(详见 @TEMPLATE_README.md),目前尚未实现业务功能
|
||||||
|
|
||||||
## 重要提示
|
|
||||||
- 完成用户的任务后,务必运行`pnpm run lint`检查代码错误并修复,不要运行build
|
|
||||||
- 尽量使用pnpm执行npm相关的命令
|
|
||||||
EOF
|
EOF
|
||||||
print_success "新的 CLAUDE.md 已创建"
|
print_success "新的 CLAUDE.md 已创建"
|
||||||
|
|
||||||
@@ -350,8 +346,6 @@ EOF
|
|||||||
"api_base_url": "${PKUAI_API_BASE}api/anthropic/v1/messages",
|
"api_base_url": "${PKUAI_API_BASE}api/anthropic/v1/messages",
|
||||||
"api_key": "${PKUAI_API_KEY}",
|
"api_key": "${PKUAI_API_KEY}",
|
||||||
"models": [
|
"models": [
|
||||||
"claude-opus-4-6",
|
|
||||||
"claude-sonnet-4-6",
|
|
||||||
"claude-sonnet-4-5-20250929",
|
"claude-sonnet-4-5-20250929",
|
||||||
"claude-opus-4-5-20251101",
|
"claude-opus-4-5-20251101",
|
||||||
"claude-haiku-4-5-20251001"
|
"claude-haiku-4-5-20251001"
|
||||||
@@ -374,13 +368,13 @@ EOF
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Router": {
|
"Router": {
|
||||||
"default": "pku-anthropic,claude-opus-4-6",
|
"default": "pku-anthropic,claude-opus-4-5-20251101",
|
||||||
"background": "pku-anthropic,claude-haiku-4-5-20251001",
|
"background": "pku-anthropic,claude-haiku-4-5-20251001",
|
||||||
"think": "pku-anthropic,-claude-opus4-6",
|
"think": "pku-anthropic,claude-opus-4-5-20251101",
|
||||||
"longContext": "pku-anthropic,claude-opus-4-6",
|
"longContext": "pku-anthropic,claude-opus-4-5-20251101",
|
||||||
"longContextThreshold": 80000,
|
"longContextThreshold": 80000,
|
||||||
"webSearch": "",
|
"webSearch": "",
|
||||||
"image": "pku-anthropic,claude-opus-4-6"
|
"image": "pku-anthropic,claude-opus-4-5-20251101"
|
||||||
},
|
},
|
||||||
"CUSTOM_ROUTER_PATH": ""
|
"CUSTOM_ROUTER_PATH": ""
|
||||||
}
|
}
|
||||||
@@ -407,7 +401,6 @@ EOF
|
|||||||
echo ""
|
echo ""
|
||||||
git init
|
git init
|
||||||
git config init.defaultBranch main
|
git config init.defaultBranch main
|
||||||
git config core.pager "less -F -X"
|
|
||||||
print_info "设置 Git 用户配置..."
|
print_info "设置 Git 用户配置..."
|
||||||
git config user.email "$GIT_USER_EMAIL"
|
git config user.email "$GIT_USER_EMAIL"
|
||||||
git config user.name "$GIT_USER_NAME"
|
git config user.name "$GIT_USER_NAME"
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ TEMP_DIR=$(mktemp -d)
|
|||||||
trap "rm -rf $TEMP_DIR" EXIT
|
trap "rm -rf $TEMP_DIR" EXIT
|
||||||
|
|
||||||
# 复制必要文件
|
# 复制必要文件
|
||||||
cp -r .next-prod $TEMP_DIR/
|
cp -r .next $TEMP_DIR/
|
||||||
cp -r public $TEMP_DIR/
|
cp -r public $TEMP_DIR/
|
||||||
cp -r prisma $TEMP_DIR/
|
cp -r prisma $TEMP_DIR/
|
||||||
cp package.json $TEMP_DIR/
|
cp package.json $TEMP_DIR/
|
||||||
|
|||||||
@@ -1,243 +0,0 @@
|
|||||||
'use client'
|
|
||||||
|
|
||||||
import { useEffect, useRef, useState } from "react"
|
|
||||||
import { useForm } from "react-hook-form"
|
|
||||||
import { zodResolver } from "@hookform/resolvers/zod"
|
|
||||||
import { signIn } from "next-auth/react"
|
|
||||||
import { useRouter, useSearchParams } from "next/navigation"
|
|
||||||
import { z } from "zod"
|
|
||||||
import { Button } from "@/components/ui/button"
|
|
||||||
import { Input } from "@/components/ui/input"
|
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
|
||||||
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"
|
|
||||||
import { Alert, AlertDescription } from "@/components/ui/alert"
|
|
||||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"
|
|
||||||
import { AlertTriangle, ExternalLink } from "lucide-react"
|
|
||||||
import type { IaaaClientConfig } from "./page"
|
|
||||||
|
|
||||||
// 登录表单验证 schema
|
|
||||||
const loginSchema = z.object({
|
|
||||||
id: z.string().min(1, "请输入用户ID"),
|
|
||||||
password: z.string().min(1, "请输入密码"),
|
|
||||||
})
|
|
||||||
|
|
||||||
type LoginFormData = z.infer<typeof loginSchema>
|
|
||||||
|
|
||||||
/** IAAA 回调错误码到用户提示的映射 */
|
|
||||||
const IAAA_ERROR_MESSAGES: Record<string, string> = {
|
|
||||||
iaaa_no_token: '未收到认证票据,请重试',
|
|
||||||
iaaa_validate_failed: '统一认证验证失败,请重试',
|
|
||||||
iaaa_user_not_found: '系统中不存在与您北大账号对应的用户,请联系管理员',
|
|
||||||
iaaa_server_error: '服务器内部错误,请稍后重试',
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 创建隐藏表单 POST 跳转到 IAAA 统一认证页面 */
|
|
||||||
function redirectToIaaa(appId: string, callbackPath: string) {
|
|
||||||
const form = document.createElement('form')
|
|
||||||
form.action = 'https://iaaa.pku.edu.cn/iaaa/oauth.jsp'
|
|
||||||
form.method = 'POST'
|
|
||||||
form.style.display = 'none'
|
|
||||||
|
|
||||||
function addField(name: string, value: string) {
|
|
||||||
const input = document.createElement('input')
|
|
||||||
input.type = 'hidden'
|
|
||||||
input.name = name
|
|
||||||
input.value = value
|
|
||||||
form.appendChild(input)
|
|
||||||
}
|
|
||||||
|
|
||||||
const baseUrl = `${location.protocol}//${location.host}`
|
|
||||||
addField('appID', appId)
|
|
||||||
addField('redirectUrl', `${baseUrl}${callbackPath}`)
|
|
||||||
// 保留应用自有登录入口,以便用户可以选择回到密码登录
|
|
||||||
addField('redirectLogonUrl', `${baseUrl}/login`)
|
|
||||||
|
|
||||||
document.body.appendChild(form)
|
|
||||||
form.submit()
|
|
||||||
}
|
|
||||||
|
|
||||||
export function LoginForm({ iaaaConfig }: { iaaaConfig: IaaaClientConfig }) {
|
|
||||||
const router = useRouter()
|
|
||||||
const searchParams = useSearchParams()
|
|
||||||
const autoRedirected = useRef(false)
|
|
||||||
const [showDevHint, setShowDevHint] = useState(false)
|
|
||||||
|
|
||||||
const form = useForm<LoginFormData>({
|
|
||||||
resolver: zodResolver(loginSchema),
|
|
||||||
defaultValues: {
|
|
||||||
id: "",
|
|
||||||
password: "",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// IAAA 回调错误提示(使用 iaaa_error 参数名,避免与 next-auth 的 error 参数冲突)
|
|
||||||
const iaaaErrorCode = searchParams.get('iaaa_error')
|
|
||||||
const iaaaError = iaaaErrorCode ? IAAA_ERROR_MESSAGES[iaaaErrorCode] : null
|
|
||||||
|
|
||||||
// 自动跳转:配置开启且 IAAA 已启用时,进入登录页自动跳转到统一认证
|
|
||||||
// 仅在非错误回调场景下触发(避免验证失败后死循环)
|
|
||||||
useEffect(() => {
|
|
||||||
if (
|
|
||||||
iaaaConfig.enabled &&
|
|
||||||
iaaaConfig.autoRedirect &&
|
|
||||||
!iaaaErrorCode &&
|
|
||||||
!autoRedirected.current
|
|
||||||
) {
|
|
||||||
autoRedirected.current = true
|
|
||||||
redirectToIaaa(iaaaConfig.appId, iaaaConfig.callbackPath)
|
|
||||||
}
|
|
||||||
}, [iaaaConfig, iaaaErrorCode])
|
|
||||||
|
|
||||||
const onSubmit = async (data: LoginFormData) => {
|
|
||||||
try {
|
|
||||||
const result = await signIn("credentials", {
|
|
||||||
id: data.id,
|
|
||||||
password: data.password,
|
|
||||||
redirect: false,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (result?.error) {
|
|
||||||
form.setError("root", {
|
|
||||||
type: "manual",
|
|
||||||
message: "用户ID或密码错误"
|
|
||||||
})
|
|
||||||
} else if (result?.ok) {
|
|
||||||
router.push("/")
|
|
||||||
router.refresh()
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
form.setError("root", {
|
|
||||||
type: "manual",
|
|
||||||
message: "登录失败,请重试"
|
|
||||||
})
|
|
||||||
console.error("Login error:", error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// IAAA 是否可见:已启用,或开发模式下需要显示配置提醒
|
|
||||||
const showIaaa = iaaaConfig.enabled || iaaaConfig.showInDev
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
|
||||||
<Card className="w-full max-w-md">
|
|
||||||
<CardHeader className="space-y-1">
|
|
||||||
<CardTitle className="text-2xl text-center">登录</CardTitle>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="space-y-4">
|
|
||||||
{/* IAAA 回调错误提示 */}
|
|
||||||
{iaaaError && (
|
|
||||||
<Alert variant="destructive">
|
|
||||||
<AlertTriangle className="h-4 w-4" />
|
|
||||||
<AlertDescription>{iaaaError}</AlertDescription>
|
|
||||||
</Alert>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* 密码登录表单 */}
|
|
||||||
<Form {...form}>
|
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="id"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>用户ID</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
type="text"
|
|
||||||
placeholder="请输入用户ID"
|
|
||||||
disabled={form.formState.isSubmitting}
|
|
||||||
{...field}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<FormField
|
|
||||||
control={form.control}
|
|
||||||
name="password"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>密码</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
type="password"
|
|
||||||
placeholder="请输入密码"
|
|
||||||
disabled={form.formState.isSubmitting}
|
|
||||||
{...field}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
{form.formState.errors.root && (
|
|
||||||
<div className="text-sm text-red-600 text-center">
|
|
||||||
{form.formState.errors.root.message}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<Button
|
|
||||||
type="submit"
|
|
||||||
className="w-full"
|
|
||||||
disabled={form.formState.isSubmitting}
|
|
||||||
>
|
|
||||||
{form.formState.isSubmitting ? "登录中..." : "登录"}
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
|
|
||||||
{/* IAAA 统一认证登录 */}
|
|
||||||
{showIaaa && (
|
|
||||||
<>
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
variant="outline"
|
|
||||||
className="w-full"
|
|
||||||
onClick={() => {
|
|
||||||
if (iaaaConfig.enabled) {
|
|
||||||
redirectToIaaa(iaaaConfig.appId, iaaaConfig.callbackPath)
|
|
||||||
} else {
|
|
||||||
setShowDevHint(true)
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<ExternalLink className="mr-2 h-4 w-4" />
|
|
||||||
北京大学统一认证登录
|
|
||||||
</Button>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* 开发模式下未配置 IAAA 的提示对话框(仅开发模式渲染,避免泄露内部架构) */}
|
|
||||||
{iaaaConfig.showInDev && <Dialog open={showDevHint} onOpenChange={setShowDevHint}>
|
|
||||||
<DialogContent>
|
|
||||||
<DialogHeader>
|
|
||||||
<DialogTitle>北大统一认证未配置</DialogTitle>
|
|
||||||
<DialogDescription>
|
|
||||||
IAAA 统一认证功能需要配置以下环境变量才能使用:
|
|
||||||
</DialogDescription>
|
|
||||||
</DialogHeader>
|
|
||||||
<div className="space-y-3 text-sm">
|
|
||||||
<p className="font-medium">1. 在 IAAA 平台注册应用时填写:</p>
|
|
||||||
<div className="rounded-md bg-muted p-3 font-mono text-xs space-y-1">
|
|
||||||
<div>系统URL:<span className="text-muted-foreground">https://你的域名</span></div>
|
|
||||||
<div>服务器IP:<span className="text-muted-foreground">应用服务器的出口IP</span></div>
|
|
||||||
<div>回调URL:<span className="text-muted-foreground">https://你的域名/api/auth/iaaa/callback</span></div>
|
|
||||||
</div>
|
|
||||||
<p className="font-medium">2. 将获取的凭据写入 <code className="bg-muted px-1 py-0.5 rounded">.env</code> 文件:</p>
|
|
||||||
<div className="rounded-md bg-muted p-3 font-mono text-xs space-y-1">
|
|
||||||
<div>IAAA_APP_ID=你的应用ID</div>
|
|
||||||
<div>IAAA_KEY=你的密钥</div>
|
|
||||||
<div className="text-muted-foreground"># 可选:进入登录页自动跳转统一认证</div>
|
|
||||||
<div>IAAA_AUTO_REDIRECT=true</div>
|
|
||||||
</div>
|
|
||||||
<p className="text-muted-foreground">
|
|
||||||
配置完成后重启开发服务器即可生效。
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</DialogContent>
|
|
||||||
</Dialog>}
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,23 +1,122 @@
|
|||||||
import { getIaaaClientConfig } from "@/server/service/iaaa"
|
'use client'
|
||||||
import { LoginForm } from "./login-form"
|
|
||||||
|
|
||||||
export interface IaaaClientConfig {
|
import { useForm } from "react-hook-form"
|
||||||
enabled: boolean
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
appId: string
|
import { signIn } from "next-auth/react"
|
||||||
callbackPath: string
|
import { useRouter } from "next/navigation"
|
||||||
autoRedirect: boolean
|
import { z } from "zod"
|
||||||
/** 开发模式下未配置 IAAA,需要显示配置提醒 */
|
import { Button } from "@/components/ui/button"
|
||||||
showInDev: boolean
|
import { Input } from "@/components/ui/input"
|
||||||
}
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
||||||
|
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"
|
||||||
|
|
||||||
|
// 登录表单验证 schema
|
||||||
|
const loginSchema = z.object({
|
||||||
|
id: z.string().min(1, "请输入用户ID"),
|
||||||
|
password: z.string().min(1, "请输入密码"),
|
||||||
|
})
|
||||||
|
|
||||||
|
type LoginFormData = z.infer<typeof loginSchema>
|
||||||
|
|
||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
const config = getIaaaClientConfig()
|
const router = useRouter()
|
||||||
const isDev = process.env.NODE_ENV === 'development'
|
|
||||||
|
|
||||||
const iaaaConfig: IaaaClientConfig = {
|
const form = useForm<LoginFormData>({
|
||||||
...config,
|
resolver: zodResolver(loginSchema),
|
||||||
showInDev: isDev && !config.enabled,
|
defaultValues: {
|
||||||
|
id: "",
|
||||||
|
password: "",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const onSubmit = async (data: LoginFormData) => {
|
||||||
|
try {
|
||||||
|
const result = await signIn("credentials", {
|
||||||
|
id: data.id,
|
||||||
|
password: data.password,
|
||||||
|
redirect: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (result?.error) {
|
||||||
|
form.setError("root", {
|
||||||
|
type: "manual",
|
||||||
|
message: "用户ID或密码错误"
|
||||||
|
})
|
||||||
|
} else if (result?.ok) {
|
||||||
|
// 登录成功,重定向到首页
|
||||||
|
router.push("/")
|
||||||
|
router.refresh()
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
form.setError("root", {
|
||||||
|
type: "manual",
|
||||||
|
message: "登录失败,请重试"
|
||||||
|
})
|
||||||
|
console.error("Login error:", error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return <LoginForm iaaaConfig={iaaaConfig} />
|
return (
|
||||||
}
|
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
||||||
|
<Card className="w-full max-w-md">
|
||||||
|
<CardHeader className="space-y-1">
|
||||||
|
<CardTitle className="text-2xl text-center">登录</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<Form {...form}>
|
||||||
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="id"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>用户ID</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
placeholder="请输入用户ID"
|
||||||
|
disabled={form.formState.isSubmitting}
|
||||||
|
{...field}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="password"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>密码</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
type="password"
|
||||||
|
placeholder="请输入密码"
|
||||||
|
disabled={form.formState.isSubmitting}
|
||||||
|
{...field}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
{form.formState.errors.root && (
|
||||||
|
<div className="text-sm text-red-600 text-center">
|
||||||
|
{form.formState.errors.root.message}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
className="w-full"
|
||||||
|
disabled={form.formState.isSubmitting}
|
||||||
|
>
|
||||||
|
{form.formState.isSubmitting ? "登录中..." : "登录"}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server'
|
|
||||||
import { encode } from 'next-auth/jwt'
|
|
||||||
import { db } from '@/server/db'
|
|
||||||
import { authOptions, buildUserJwtPayload, userAuthInclude } from '@/server/auth'
|
|
||||||
import { validateIaaaToken } from '@/server/service/iaaa'
|
|
||||||
import { clearSessionInvalidation } from '@/server/service/session'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* IAAA 统一认证回调路由
|
|
||||||
*
|
|
||||||
* IAAA 认证成功后会 302 重定向到此路由,携带 token 参数。
|
|
||||||
* 本路由验证 token、查找本地用户、生成 JWT session cookie,然后重定向到首页。
|
|
||||||
*/
|
|
||||||
export async function GET(req: NextRequest) {
|
|
||||||
const token = req.nextUrl.searchParams.get('token')
|
|
||||||
const loginUrl = new URL('/login', req.url)
|
|
||||||
|
|
||||||
if (!token) {
|
|
||||||
loginUrl.searchParams.set('iaaa_error', 'iaaa_no_token')
|
|
||||||
return NextResponse.redirect(loginUrl)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取客户端真实 IP(反向代理场景取 X-Forwarded-For 第一个值)
|
|
||||||
const remoteAddr =
|
|
||||||
req.headers.get('x-forwarded-for')?.split(',')[0].trim() ||
|
|
||||||
req.headers.get('x-real-ip') ||
|
|
||||||
'127.0.0.1'
|
|
||||||
|
|
||||||
// 调用 IAAA 验证
|
|
||||||
const userInfo = await validateIaaaToken(token, remoteAddr)
|
|
||||||
if (!userInfo) {
|
|
||||||
loginUrl.searchParams.set('iaaa_error', 'iaaa_validate_failed')
|
|
||||||
return NextResponse.redirect(loginUrl)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 用 identityId(学号/职工号)匹配本地用户
|
|
||||||
const user = await db.user.findUnique({
|
|
||||||
where: { id: userInfo.identityId },
|
|
||||||
include: userAuthInclude,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
console.warn(`[IAAA] 用户 ${userInfo.identityId}(${userInfo.name})在系统中不存在`)
|
|
||||||
loginUrl.searchParams.set('iaaa_error', 'iaaa_user_not_found')
|
|
||||||
return NextResponse.redirect(loginUrl)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新最近登录时间
|
|
||||||
await db.user.update({
|
|
||||||
where: { id: user.id },
|
|
||||||
data: { lastLoginAt: new Date() },
|
|
||||||
})
|
|
||||||
|
|
||||||
// 清除会话失效标记
|
|
||||||
await clearSessionInvalidation(user.id)
|
|
||||||
|
|
||||||
// 构建 JWT payload(与密码登录完全一致)
|
|
||||||
const payload = buildUserJwtPayload(user)
|
|
||||||
|
|
||||||
// 生成 next-auth 兼容的 JWT
|
|
||||||
const secret = authOptions.secret || process.env.NEXTAUTH_SECRET
|
|
||||||
if (!secret) {
|
|
||||||
console.error('[IAAA] NEXTAUTH_SECRET 未配置')
|
|
||||||
loginUrl.searchParams.set('iaaa_error', 'iaaa_server_error')
|
|
||||||
return NextResponse.redirect(loginUrl)
|
|
||||||
}
|
|
||||||
|
|
||||||
const maxAge = authOptions.session?.maxAge ?? 30 * 24 * 60 * 60
|
|
||||||
const encodedToken = await encode({
|
|
||||||
token: { ...payload, sub: payload.id },
|
|
||||||
secret,
|
|
||||||
maxAge,
|
|
||||||
})
|
|
||||||
|
|
||||||
// 设置 session cookie 并重定向到首页
|
|
||||||
// next-auth 在 HTTPS 环境下使用 __Secure- 前缀
|
|
||||||
const useSecureCookie = req.nextUrl.protocol === 'https:'
|
|
||||||
const cookieName = useSecureCookie
|
|
||||||
? '__Secure-next-auth.session-token'
|
|
||||||
: 'next-auth.session-token'
|
|
||||||
|
|
||||||
const response = NextResponse.redirect(new URL('/', req.url))
|
|
||||||
response.cookies.set(cookieName, encodedToken, {
|
|
||||||
httpOnly: true,
|
|
||||||
secure: useSecureCookie,
|
|
||||||
sameSite: 'lax',
|
|
||||||
path: '/',
|
|
||||||
maxAge,
|
|
||||||
})
|
|
||||||
|
|
||||||
console.info(`[IAAA] 登录成功: ${userInfo.identityId}(${userInfo.name})`)
|
|
||||||
return response
|
|
||||||
}
|
|
||||||
@@ -1,32 +1,7 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { SessionProvider as NextAuthSessionProvider, signOut, useSession } from "next-auth/react"
|
import { SessionProvider as NextAuthSessionProvider } from "next-auth/react"
|
||||||
import { useEffect, useRef } from "react"
|
|
||||||
import { usePathname } from "next/navigation"
|
|
||||||
|
|
||||||
/** 检测会话失效并自动登出 */
|
|
||||||
function SessionInvalidationGuard({ children }: { children: React.ReactNode }) {
|
|
||||||
const { data: session, status } = useSession()
|
|
||||||
const pathname = usePathname()
|
|
||||||
const signingOut = useRef(false)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
// 已认证但 session 中没有 user(会话被标记失效),自动登出清除 cookie
|
|
||||||
if (status === 'authenticated' && !(session as any)?.user?.id && !signingOut.current && pathname !== '/login') {
|
|
||||||
signingOut.current = true
|
|
||||||
signOut({ callbackUrl: '/login' })
|
|
||||||
}
|
|
||||||
}, [status, session, pathname])
|
|
||||||
|
|
||||||
return <>{children}</>
|
|
||||||
}
|
|
||||||
|
|
||||||
export function SessionProvider({ children }: { children: React.ReactNode }) {
|
export function SessionProvider({ children }: { children: React.ReactNode }) {
|
||||||
return (
|
return <NextAuthSessionProvider>{children}</NextAuthSessionProvider>
|
||||||
<NextAuthSessionProvider>
|
}
|
||||||
<SessionInvalidationGuard>
|
|
||||||
{children}
|
|
||||||
</SessionInvalidationGuard>
|
|
||||||
</NextAuthSessionProvider>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -3,4 +3,4 @@
|
|||||||
*/
|
*/
|
||||||
export const SITE_NAME = 'Hair Keeper'
|
export const SITE_NAME = 'Hair Keeper'
|
||||||
export const SITE_DESCRIPTION = '高度集成、深度定制、约定优于配置的全栈Web应用模板,旨在保持灵活性的同时提供一套基于成熟架构的开发底座,自带身份认证、权限控制、丰富前端组件、文件上传、后台任务、智能体开发等丰富功能,提供AI开发辅助,免于纠结功能如何实现,可快速上手专注于业务逻辑'
|
export const SITE_DESCRIPTION = '高度集成、深度定制、约定优于配置的全栈Web应用模板,旨在保持灵活性的同时提供一套基于成熟架构的开发底座,自带身份认证、权限控制、丰富前端组件、文件上传、后台任务、智能体开发等丰富功能,提供AI开发辅助,免于纠结功能如何实现,可快速上手专注于业务逻辑'
|
||||||
export const SITE_VERSION = 'v1.3.0'
|
export const SITE_VERSION = 'v1.2.0'
|
||||||
@@ -9,8 +9,8 @@ export default withAuth(
|
|||||||
const token = req.nextauth.token
|
const token = req.nextauth.token
|
||||||
const pathname = req.nextUrl.pathname
|
const pathname = req.nextUrl.pathname
|
||||||
|
|
||||||
// 如果用户已登录且访问登录页面,重定向到首页(会话失效的用户除外,需留在登录页)
|
// 如果用户已登录且访问登录页面,重定向到首页
|
||||||
if (pathname === "/login" && token && !token.sessionInvalid) {
|
if (pathname === "/login" && token) {
|
||||||
return NextResponse.redirect(new URL("/", req.url))
|
return NextResponse.redirect(new URL("/", req.url))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,12 +49,7 @@ export default withAuth(
|
|||||||
if (req.nextUrl.pathname === "/login") {
|
if (req.nextUrl.pathname === "/login") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 会话已被标记失效,强制重新登录
|
|
||||||
if (token?.sessionInvalid) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// 其他路由需要有效的 token
|
// 其他路由需要有效的 token
|
||||||
return !!token
|
return !!token
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,37 +4,6 @@ import type { User as NextAuthUser } from "next-auth"
|
|||||||
import CredentialsProvider from "next-auth/providers/credentials"
|
import CredentialsProvider from "next-auth/providers/credentials"
|
||||||
import bcrypt from "bcryptjs"
|
import bcrypt from "bcryptjs"
|
||||||
import { db } from "./db"
|
import { db } from "./db"
|
||||||
import { clearSessionInvalidation, isSessionInvalidated } from "./service/session"
|
|
||||||
|
|
||||||
/** 用户查询时需要 include 的关联 */
|
|
||||||
export const userAuthInclude = {
|
|
||||||
roles: { include: { permissions: true } },
|
|
||||||
dept: true,
|
|
||||||
} as const
|
|
||||||
|
|
||||||
/** 从数据库用户对象构建 JWT payload(供密码登录和 IAAA 登录共用) */
|
|
||||||
export function buildUserJwtPayload(user: {
|
|
||||||
id: string
|
|
||||||
name: string | null
|
|
||||||
status: string | null
|
|
||||||
deptCode: string | null
|
|
||||||
isSuperAdmin: boolean
|
|
||||||
roles: Array<{ name: string; permissions: Array<{ name: string }> }>
|
|
||||||
}) {
|
|
||||||
const roles = user.roles.map((r) => r.name)
|
|
||||||
const permissions = Array.from(
|
|
||||||
new Set(user.roles.flatMap((r) => r.permissions.map((p) => p.name)))
|
|
||||||
)
|
|
||||||
return {
|
|
||||||
id: user.id,
|
|
||||||
name: user.name,
|
|
||||||
status: user.status,
|
|
||||||
deptCode: user.deptCode,
|
|
||||||
roles,
|
|
||||||
permissions,
|
|
||||||
isSuperAdmin: user.isSuperAdmin,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const authOptions: NextAuthOptions = {
|
export const authOptions: NextAuthOptions = {
|
||||||
providers: [
|
providers: [
|
||||||
@@ -52,8 +21,15 @@ export const authOptions: NextAuthOptions = {
|
|||||||
try {
|
try {
|
||||||
// 查找用户
|
// 查找用户
|
||||||
const user = await db.user.findUnique({
|
const user = await db.user.findUnique({
|
||||||
where: { id: credentials.id },
|
where: {
|
||||||
include: userAuthInclude,
|
id: credentials.id
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
roles: {
|
||||||
|
include: { permissions: true }
|
||||||
|
},
|
||||||
|
dept: true
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
@@ -62,6 +38,7 @@ export const authOptions: NextAuthOptions = {
|
|||||||
|
|
||||||
// 验证密码
|
// 验证密码
|
||||||
const isPasswordValid = await bcrypt.compare(credentials.password, user.password)
|
const isPasswordValid = await bcrypt.compare(credentials.password, user.password)
|
||||||
|
|
||||||
|
|
||||||
if (!isPasswordValid) {
|
if (!isPasswordValid) {
|
||||||
return null
|
return null
|
||||||
@@ -72,11 +49,23 @@ export const authOptions: NextAuthOptions = {
|
|||||||
where: { id: user.id },
|
where: { id: user.id },
|
||||||
data: { lastLoginAt: new Date() }
|
data: { lastLoginAt: new Date() }
|
||||||
})
|
})
|
||||||
|
|
||||||
// 清除会话失效标记(用户已重新登录,获得最新权限)
|
// 返回用户信息、角色和权限
|
||||||
await clearSessionInvalidation(user.id)
|
const roles = user.roles.map((r) => r.name)
|
||||||
|
const permissions = Array.from(
|
||||||
return buildUserJwtPayload(user) as any
|
new Set(user.roles.flatMap((r) =>
|
||||||
|
r.permissions.map((p) => p.name)
|
||||||
|
))
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
id: user.id,
|
||||||
|
name: user.name,
|
||||||
|
status: user.status,
|
||||||
|
deptCode: user.deptCode,
|
||||||
|
roles,
|
||||||
|
permissions,
|
||||||
|
isSuperAdmin: user.isSuperAdmin
|
||||||
|
} as any
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Auth error:", error)
|
console.error("Auth error:", error)
|
||||||
return null
|
return null
|
||||||
@@ -109,20 +98,10 @@ export const authOptions: NextAuthOptions = {
|
|||||||
permissions: u.permissions,
|
permissions: u.permissions,
|
||||||
isSuperAdmin: u.isSuperAdmin,
|
isSuperAdmin: u.isSuperAdmin,
|
||||||
}
|
}
|
||||||
} else if (token.id) {
|
|
||||||
// 后续请求:检查会话是否已被标记失效
|
|
||||||
const invalidated = await isSessionInvalidated(token.id as string)
|
|
||||||
if (invalidated) {
|
|
||||||
token.sessionInvalid = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return token
|
return token
|
||||||
},
|
},
|
||||||
async session({ session, token }) {
|
async session({ session, token }) {
|
||||||
// 会话已被标记失效,返回不含用户信息的session
|
|
||||||
if (token.sessionInvalid) {
|
|
||||||
return { expires: session.expires } as any
|
|
||||||
}
|
|
||||||
// 将JWT token中的信息传递给session
|
// 将JWT token中的信息传递给session
|
||||||
if (session.user) {
|
if (session.user) {
|
||||||
const t = token as any
|
const t = token as any
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import 'server-only'
|
import 'server-only'
|
||||||
import { Client } from 'minio';
|
import { Client } from 'minio';
|
||||||
|
|
||||||
// 内部客户端:用于 statObject、removeObject 等服务端直连操作,以及 presignedPostPolicy
|
// 初始化 MinIO 客户端
|
||||||
export const minioClient = new Client({
|
export const minioClient = new Client({
|
||||||
endPoint: process.env.MINIO_ENDPOINT || 'localhost',
|
endPoint: process.env.MINIO_ENDPOINT || 'localhost',
|
||||||
port: parseInt(process.env.MINIO_API_PORT || '9000'),
|
port: parseInt(process.env.MINIO_API_PORT || '9000'),
|
||||||
@@ -10,32 +10,6 @@ export const minioClient = new Client({
|
|||||||
secretKey: process.env.MINIO_ROOT_PASSWORD || '',
|
secretKey: process.env.MINIO_ROOT_PASSWORD || '',
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* 公网客户端:用于生成 presigned GET/PUT URL
|
|
||||||
*
|
|
||||||
* AWS Signature V4 会将 Host 头纳入签名计算,因此 presigned GET/PUT URL
|
|
||||||
* 必须使用与浏览器访问一致的公网地址生成,否则签名校验失败。
|
|
||||||
* 而 POST Policy 不含 Host 签名,只需替换返回的 URL 地址即可。
|
|
||||||
*/
|
|
||||||
function createPresignClient(): Client {
|
|
||||||
const serverUrl = process.env.MINIO_SERVER_URL;
|
|
||||||
if (!serverUrl) return minioClient;
|
|
||||||
|
|
||||||
const parsed = new URL(serverUrl);
|
|
||||||
const useSSL = parsed.protocol === 'https:';
|
|
||||||
const port = parsed.port ? parseInt(parsed.port) : (useSSL ? 443 : 80);
|
|
||||||
|
|
||||||
return new Client({
|
|
||||||
endPoint: parsed.hostname,
|
|
||||||
port,
|
|
||||||
useSSL,
|
|
||||||
accessKey: process.env.MINIO_ROOT_USER || '',
|
|
||||||
secretKey: process.env.MINIO_ROOT_PASSWORD || '',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const presignClient = createPresignClient();
|
|
||||||
|
|
||||||
export const BUCKET_NAME = process.env.MINIO_BUCKET || 'app-files';
|
export const BUCKET_NAME = process.env.MINIO_BUCKET || 'app-files';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,9 +166,6 @@ export async function generatePresignedPostPolicy(
|
|||||||
// 精确匹配
|
// 精确匹配
|
||||||
policy.setContentType(allowedContentType);
|
policy.setContentType(allowedContentType);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// 未指定类型限制时,允许任意 Content-Type(客户端上传时会设置此字段,Policy 中必须声明)
|
|
||||||
policy.policy.conditions.push(['starts-with', '$Content-Type', '']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allowOriginalFilename) {
|
if (allowOriginalFilename) {
|
||||||
@@ -284,8 +255,8 @@ export async function generatePresignedGetObject(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用公网客户端生成预签名 URL,确保 V4 签名中的 Host 与浏览器访问地址一致
|
// 生成预签名 URL
|
||||||
const url = await presignClient.presignedGetObject(
|
const url = await minioClient.presignedGetObject(
|
||||||
BUCKET_NAME,
|
BUCKET_NAME,
|
||||||
objectName,
|
objectName,
|
||||||
expirySeconds,
|
expirySeconds,
|
||||||
@@ -293,7 +264,7 @@ export async function generatePresignedGetObject(
|
|||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
url,
|
url: replaceUrlBase(url),
|
||||||
expiresIn: expirySeconds,
|
expiresIn: expirySeconds,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import bcrypt from 'bcryptjs'
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { inferProcedureOutput, TRPCError } from '@trpc/server'
|
import { inferProcedureOutput, TRPCError } from '@trpc/server'
|
||||||
import pLimit from 'p-limit'
|
import pLimit from 'p-limit'
|
||||||
import { invalidateUserSessions, invalidateSessionsByRoleId } from '@/server/service/session'
|
|
||||||
|
|
||||||
// 从环境变量获取并发限制,默认为16
|
// 从环境变量获取并发限制,默认为16
|
||||||
const dbParallelLimit = pLimit(parseInt(process.env.DB_PARALLEL_LIMIT || '16', 10))
|
const dbParallelLimit = pLimit(parseInt(process.env.DB_PARALLEL_LIMIT || '16', 10))
|
||||||
@@ -117,7 +116,7 @@ export const usersRouter = createTRPCRouter({
|
|||||||
throw new TRPCError({ code: 'BAD_REQUEST', message: '角色名称已被其他角色使用' })
|
throw new TRPCError({ code: 'BAD_REQUEST', message: '角色名称已被其他角色使用' })
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedRole = await ctx.db.role.update({
|
return ctx.db.role.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
name,
|
name,
|
||||||
@@ -130,11 +129,6 @@ export const usersRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// 标记持有该角色的所有用户会话失效
|
|
||||||
await invalidateSessionsByRoleId(id)
|
|
||||||
|
|
||||||
return updatedRole
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
deleteRole: permissionRequiredProcedure(Permissions.USER_MANAGE)
|
deleteRole: permissionRequiredProcedure(Permissions.USER_MANAGE)
|
||||||
@@ -211,10 +205,7 @@ export const usersRouter = createTRPCRouter({
|
|||||||
|
|
||||||
processedCount += batch.length
|
processedCount += batch.length
|
||||||
}
|
}
|
||||||
|
|
||||||
// 标记所有受影响用户的会话失效
|
|
||||||
await invalidateUserSessions(users.map(u => u.id))
|
|
||||||
|
|
||||||
return { count: processedCount }
|
return { count: processedCount }
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -274,7 +265,7 @@ export const usersRouter = createTRPCRouter({
|
|||||||
updateData.password = await bcrypt.hash(password, 12)
|
updateData.password = await bcrypt.hash(password, 12)
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedUser = await ctx.db.user.update({
|
return ctx.db.user.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: updateData,
|
data: updateData,
|
||||||
include: {
|
include: {
|
||||||
@@ -282,11 +273,6 @@ export const usersRouter = createTRPCRouter({
|
|||||||
dept: true
|
dept: true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// 标记用户会话失效,强制重新登录以获取最新权限
|
|
||||||
await invalidateUserSessions([id])
|
|
||||||
|
|
||||||
return updatedUser
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getById: permissionRequiredProcedure(Permissions.USER_MANAGE).input(z.object({ id: z.string() })).query(async ({ ctx, input }) => {
|
getById: permissionRequiredProcedure(Permissions.USER_MANAGE).input(z.object({ id: z.string() })).query(async ({ ctx, input }) => {
|
||||||
@@ -314,9 +300,6 @@ export const usersRouter = createTRPCRouter({
|
|||||||
throw new TRPCError({ code: 'BAD_REQUEST', message: '不能删除自己的账户' })
|
throw new TRPCError({ code: 'BAD_REQUEST', message: '不能删除自己的账户' })
|
||||||
}
|
}
|
||||||
|
|
||||||
// 标记用户会话失效
|
|
||||||
await invalidateUserSessions([id])
|
|
||||||
|
|
||||||
// 删除用户
|
// 删除用户
|
||||||
return ctx.db.user.delete({
|
return ctx.db.user.delete({
|
||||||
where: { id },
|
where: { id },
|
||||||
|
|||||||
@@ -1,89 +0,0 @@
|
|||||||
import 'server-only'
|
|
||||||
import crypto from 'crypto'
|
|
||||||
|
|
||||||
const IAAA_VALIDATE_URL = 'https://iaaa.pku.edu.cn/iaaa/svc/token/validate.do'
|
|
||||||
|
|
||||||
export interface IaaaUserInfo {
|
|
||||||
name: string
|
|
||||||
status: string
|
|
||||||
identityId: string
|
|
||||||
deptId: string
|
|
||||||
dept: string
|
|
||||||
identityType: string
|
|
||||||
detailType: string
|
|
||||||
identityStatus: string
|
|
||||||
campus: string
|
|
||||||
}
|
|
||||||
|
|
||||||
interface IaaaValidateResult {
|
|
||||||
success: boolean
|
|
||||||
errCode: string
|
|
||||||
errMsg: string
|
|
||||||
userInfo?: IaaaUserInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 检查 IAAA 统一认证是否已配置启用 */
|
|
||||||
export function isIaaaEnabled(): boolean {
|
|
||||||
return !!(process.env.IAAA_APP_ID && process.env.IAAA_KEY)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 获取 IAAA 配置(仅返回前端需要的非敏感信息) */
|
|
||||||
export function getIaaaClientConfig() {
|
|
||||||
return {
|
|
||||||
enabled: isIaaaEnabled(),
|
|
||||||
appId: process.env.IAAA_APP_ID || '',
|
|
||||||
callbackPath: '/api/auth/iaaa/callback',
|
|
||||||
autoRedirect: process.env.IAAA_AUTO_REDIRECT === 'true',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 验证 IAAA Token
|
|
||||||
* @param token IAAA 回调携带的 token
|
|
||||||
* @param remoteAddr 用户真实 IP 地址
|
|
||||||
* @returns 验证成功返回用户信息,失败返回 null 并记录日志
|
|
||||||
*/
|
|
||||||
export async function validateIaaaToken(
|
|
||||||
token: string,
|
|
||||||
remoteAddr: string
|
|
||||||
): Promise<IaaaUserInfo | null> {
|
|
||||||
const appId = process.env.IAAA_APP_ID
|
|
||||||
const key = process.env.IAAA_KEY
|
|
||||||
|
|
||||||
if (!appId || !key) {
|
|
||||||
console.error('[IAAA] 环境变量 IAAA_APP_ID 或 IAAA_KEY 未配置')
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
// 参数按字母序拼接:appId, remoteAddr, token
|
|
||||||
const paraStr = `appId=${appId}&remoteAddr=${remoteAddr}&token=${token}`
|
|
||||||
const msgAbs = crypto.createHash('md5').update(paraStr + key).digest('hex')
|
|
||||||
|
|
||||||
const url = `${IAAA_VALIDATE_URL}?${paraStr}&msgAbs=${msgAbs}`
|
|
||||||
|
|
||||||
try {
|
|
||||||
const resp = await fetch(url, { signal: AbortSignal.timeout(10000) })
|
|
||||||
if (!resp.ok) {
|
|
||||||
console.error(`[IAAA] 验证请求失败: HTTP ${resp.status}`)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const result: IaaaValidateResult = await resp.json()
|
|
||||||
|
|
||||||
if (!result.success) {
|
|
||||||
console.warn(`[IAAA] 认证失败: errCode=${result.errCode}, errMsg=${result.errMsg}`)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const userInfo = result.userInfo
|
|
||||||
if (!userInfo?.identityId) {
|
|
||||||
console.warn('[IAAA] 认证成功但未返回 identityId')
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return userInfo
|
|
||||||
} catch (error) {
|
|
||||||
console.error('[IAAA] Token 验证请求异常:', error)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
import 'server-only'
|
|
||||||
import { getRedisClient } from '@/server/redis'
|
|
||||||
import { db } from '@/server/db'
|
|
||||||
|
|
||||||
const SESSION_INVALID_PREFIX = 'session:invalid:'
|
|
||||||
const SESSION_INVALID_TTL = 30 * 24 * 60 * 60 // 30天,与JWT maxAge一致
|
|
||||||
|
|
||||||
/** 标记指定用户的会话失效 */
|
|
||||||
export async function invalidateUserSessions(userIds: string[]) {
|
|
||||||
if (userIds.length === 0) return
|
|
||||||
const redis = getRedisClient()
|
|
||||||
const pipeline = redis.pipeline()
|
|
||||||
for (const id of userIds) {
|
|
||||||
pipeline.set(`${SESSION_INVALID_PREFIX}${id}`, '1', 'EX', SESSION_INVALID_TTL)
|
|
||||||
}
|
|
||||||
await pipeline.exec()
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 检查用户会话是否已被标记失效 */
|
|
||||||
export async function isSessionInvalidated(userId: string): Promise<boolean> {
|
|
||||||
const redis = getRedisClient()
|
|
||||||
const result = await redis.exists(`${SESSION_INVALID_PREFIX}${userId}`)
|
|
||||||
return result === 1
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 用户重新登录后清除失效标记 */
|
|
||||||
export async function clearSessionInvalidation(userId: string) {
|
|
||||||
const redis = getRedisClient()
|
|
||||||
await redis.del(`${SESSION_INVALID_PREFIX}${userId}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 标记某角色下所有用户的会话失效 */
|
|
||||||
export async function invalidateSessionsByRoleId(roleId: number) {
|
|
||||||
const users = await db.user.findMany({
|
|
||||||
where: { roles: { some: { id: roleId } } },
|
|
||||||
select: { id: true },
|
|
||||||
})
|
|
||||||
await invalidateUserSessions(users.map(u => u.id))
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,7 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ES2017",
|
"target": "ES2017",
|
||||||
"lib": [
|
"lib": ["dom", "dom.iterable", "esnext"],
|
||||||
"dom",
|
|
||||||
"dom.iterable",
|
|
||||||
"esnext"
|
|
||||||
],
|
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
@@ -23,19 +19,9 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": [
|
"@/*": ["./src/*"]
|
||||||
"./src/*"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": [
|
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||||
"**/*.ts",
|
"exclude": ["node_modules"]
|
||||||
"**/*.tsx",
|
|
||||||
".next/types/**/*.ts",
|
|
||||||
"next-env.d.ts",
|
|
||||||
".next-prod/types/**/*.ts"
|
|
||||||
],
|
|
||||||
"exclude": [
|
|
||||||
"node_modules"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user