@55387.ai/uniauth-server 1.2.1 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -171
- package/package.json +2 -3
- package/INTEGRATION.md +0 -1273
package/README.md
CHANGED
|
@@ -1,244 +1,151 @@
|
|
|
1
1
|
# @55387.ai/uniauth-server
|
|
2
2
|
|
|
3
|
-
UniAuth
|
|
3
|
+
> UniAuth Backend SDK — Token verification & middleware for Node.js servers.
|
|
4
|
+
>
|
|
5
|
+
> UniAuth 后端 SDK — Node.js 服务端令牌验证和中间件。
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
**Version / 版本:** 1.2.2
|
|
8
|
+
|
|
9
|
+
## Install / 安装
|
|
6
10
|
|
|
7
11
|
```bash
|
|
8
12
|
npm install @55387.ai/uniauth-server
|
|
9
|
-
# or
|
|
13
|
+
# or / 或
|
|
10
14
|
pnpm add @55387.ai/uniauth-server
|
|
11
15
|
```
|
|
12
16
|
|
|
13
|
-
## 快速开始
|
|
17
|
+
## Quick Start / 快速开始
|
|
14
18
|
|
|
15
19
|
```typescript
|
|
16
20
|
import { UniAuthServer } from '@55387.ai/uniauth-server';
|
|
17
21
|
|
|
18
22
|
const auth = new UniAuthServer({
|
|
19
23
|
baseUrl: 'https://sso.55387.xyz',
|
|
20
|
-
clientId:
|
|
21
|
-
clientSecret:
|
|
24
|
+
clientId: process.env.UNIAUTH_CLIENT_ID!,
|
|
25
|
+
clientSecret: process.env.UNIAUTH_CLIENT_SECRET!,
|
|
22
26
|
});
|
|
23
27
|
|
|
24
|
-
// 验证令牌
|
|
28
|
+
// Verify token / 验证令牌
|
|
25
29
|
const payload = await auth.verifyToken(accessToken);
|
|
26
30
|
console.log('User ID:', payload.sub);
|
|
27
31
|
```
|
|
28
32
|
|
|
29
|
-
##
|
|
33
|
+
## Middleware / 中间件
|
|
34
|
+
|
|
35
|
+
### Express
|
|
30
36
|
|
|
31
37
|
```typescript
|
|
32
38
|
import express from 'express';
|
|
33
|
-
import { UniAuthServer } from '@55387.ai/uniauth-server';
|
|
34
|
-
|
|
35
39
|
const app = express();
|
|
36
|
-
const auth = new UniAuthServer({ ... });
|
|
37
40
|
|
|
38
|
-
// 保护 API 路由
|
|
39
41
|
app.use('/api/*', auth.middleware());
|
|
40
42
|
|
|
41
|
-
// 在路由中使用用户信息
|
|
42
43
|
app.get('/api/profile', (req, res) => {
|
|
43
44
|
res.json({ user: req.user, payload: req.authPayload });
|
|
44
45
|
});
|
|
45
46
|
```
|
|
46
47
|
|
|
47
|
-
|
|
48
|
+
### Hono
|
|
48
49
|
|
|
49
50
|
```typescript
|
|
50
51
|
import { Hono } from 'hono';
|
|
51
|
-
import { UniAuthServer } from '@55387.ai/uniauth-server';
|
|
52
|
-
|
|
53
52
|
const app = new Hono();
|
|
54
|
-
const auth = new UniAuthServer({ ... });
|
|
55
53
|
|
|
56
|
-
// 保护 API 路由
|
|
57
54
|
app.use('/api/*', auth.honoMiddleware());
|
|
58
55
|
|
|
59
|
-
// 在路由中使用用户信息
|
|
60
56
|
app.get('/api/profile', (c) => {
|
|
61
|
-
|
|
62
|
-
return c.json({ user });
|
|
57
|
+
return c.json({ user: c.get('user') });
|
|
63
58
|
});
|
|
64
59
|
```
|
|
65
60
|
|
|
66
|
-
## SSO
|
|
61
|
+
## SSO Backend Proxy / SSO 后端代理
|
|
67
62
|
|
|
68
|
-
|
|
63
|
+
When your app is a **Confidential Client**, token exchange must happen on the server.
|
|
69
64
|
|
|
70
|
-
|
|
65
|
+
当应用配置为 **机密客户端** 时,Token 交换必须在服务端完成。
|
|
71
66
|
|
|
72
67
|
```
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
68
|
+
User → Frontend → /api/auth/login → Backend → redirect to UniAuth SSO
|
|
69
|
+
↓
|
|
70
|
+
User ← Frontend ← redirect ← Backend (set cookie) ← SSO callback
|
|
71
|
+
↑
|
|
72
|
+
Backend exchanges code with client_secret
|
|
78
73
|
```
|
|
79
74
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
| 端点 | URL |
|
|
83
|
-
|------|-----|
|
|
84
|
-
| 授权端点 | `https://sso.55387.xyz/api/v1/oauth2/authorize` |
|
|
85
|
-
| Token 端点 | `https://sso.55387.xyz/api/v1/oauth2/token` |
|
|
86
|
-
| 用户信息端点 | `https://sso.55387.xyz/api/v1/oauth2/userinfo` |
|
|
87
|
-
|
|
88
|
-
### 实现示例(Hono)
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
91
|
-
import { Hono } from 'hono';
|
|
92
|
-
import { setCookie, getCookie } from 'hono/cookie';
|
|
93
|
-
|
|
94
|
-
const app = new Hono();
|
|
95
|
-
|
|
96
|
-
// 登录端点 - 重定向到 SSO
|
|
97
|
-
app.get('/api/auth/login', (c) => {
|
|
98
|
-
const origin = c.req.header('origin') || 'http://localhost:3000';
|
|
99
|
-
const redirectUri = `${origin}/api/auth/callback`;
|
|
100
|
-
|
|
101
|
-
const params = new URLSearchParams({
|
|
102
|
-
client_id: process.env.UNIAUTH_CLIENT_ID,
|
|
103
|
-
redirect_uri: redirectUri,
|
|
104
|
-
response_type: 'code',
|
|
105
|
-
scope: 'openid profile email phone',
|
|
106
|
-
state: generateRandomState(), // 生成随机 state 防止 CSRF
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
return c.redirect(`https://sso.55387.xyz/api/v1/oauth2/authorize?${params}`);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// 回调端点 - 交换 Token
|
|
113
|
-
app.get('/api/auth/callback', async (c) => {
|
|
114
|
-
const code = c.req.query('code');
|
|
115
|
-
const origin = c.req.header('referer')?.replace(/\/api\/auth\/callback.*$/, '') || 'http://localhost:3000';
|
|
116
|
-
|
|
117
|
-
// 用授权码交换 Token
|
|
118
|
-
const response = await fetch('https://sso.55387.xyz/api/v1/oauth2/token', {
|
|
119
|
-
method: 'POST',
|
|
120
|
-
headers: { 'Content-Type': 'application/json' },
|
|
121
|
-
body: JSON.stringify({
|
|
122
|
-
client_id: process.env.UNIAUTH_CLIENT_ID,
|
|
123
|
-
client_secret: process.env.UNIAUTH_CLIENT_SECRET,
|
|
124
|
-
code,
|
|
125
|
-
grant_type: 'authorization_code',
|
|
126
|
-
redirect_uri: `${origin}/api/auth/callback`,
|
|
127
|
-
}),
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
const { access_token, id_token } = await response.json();
|
|
131
|
-
|
|
132
|
-
// 将 Token 存储到 httpOnly Cookie
|
|
133
|
-
setCookie(c, 'auth_token', id_token, {
|
|
134
|
-
httpOnly: true,
|
|
135
|
-
secure: true,
|
|
136
|
-
sameSite: 'Lax',
|
|
137
|
-
maxAge: 60 * 60 * 24 * 7, // 7 天
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
return c.redirect('/');
|
|
141
|
-
});
|
|
75
|
+
See full implementation: [AI Integration Guide](../../docs/AI_INTEGRATION_GUIDE.md#2b-backend-proxy-confidential-client)
|
|
142
76
|
|
|
143
|
-
|
|
144
|
-
app.get('/api/auth/status', async (c) => {
|
|
145
|
-
const token = getCookie(c, 'auth_token');
|
|
146
|
-
if (!token) {
|
|
147
|
-
return c.json({ authenticated: false });
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// 验证 Token
|
|
151
|
-
try {
|
|
152
|
-
const payload = await auth.verifyToken(token);
|
|
153
|
-
return c.json({ authenticated: true, userId: payload.sub });
|
|
154
|
-
} catch {
|
|
155
|
-
return c.json({ authenticated: false });
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
```
|
|
77
|
+
完整实现见: [集成指南](../../docs/AI_INTEGRATION_GUIDE.md#2b-backend-proxy-confidential-client)
|
|
159
78
|
|
|
160
|
-
|
|
79
|
+
## Token Introspection / 令牌内省
|
|
161
80
|
|
|
162
|
-
|
|
163
|
-
// 触发登录
|
|
164
|
-
const handleLogin = () => {
|
|
165
|
-
window.location.href = '/api/auth/login';
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
// 检查登录状态
|
|
169
|
-
const checkAuth = async () => {
|
|
170
|
-
const response = await fetch('/api/auth/status', { credentials: 'include' });
|
|
171
|
-
const data = await response.json();
|
|
172
|
-
return data.authenticated;
|
|
173
|
-
};
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
## OAuth2 Token Introspection (RFC 7662)
|
|
81
|
+
RFC 7662 compliant token introspection:
|
|
177
82
|
|
|
178
83
|
```typescript
|
|
179
|
-
// 内省令牌(资源服务器标准验证方式)
|
|
180
84
|
const result = await auth.introspectToken(accessToken);
|
|
181
85
|
|
|
182
86
|
if (result.active) {
|
|
183
|
-
console.log('
|
|
184
|
-
console.log('
|
|
185
|
-
console.log('权限:', result.scope);
|
|
186
|
-
} else {
|
|
187
|
-
console.log('Token 无效或已过期');
|
|
87
|
+
console.log('User:', result.sub);
|
|
88
|
+
console.log('Scope:', result.scope);
|
|
188
89
|
}
|
|
189
90
|
```
|
|
190
91
|
|
|
191
|
-
## API 参考
|
|
92
|
+
## API Reference / API 参考
|
|
192
93
|
|
|
193
|
-
###
|
|
94
|
+
### Config / 配置
|
|
194
95
|
|
|
195
96
|
```typescript
|
|
196
97
|
interface UniAuthServerConfig {
|
|
197
|
-
baseUrl: string; // UniAuth
|
|
198
|
-
clientId: string; // OAuth2
|
|
199
|
-
clientSecret: string; // OAuth2
|
|
200
|
-
jwtPublicKey?: string; // JWT
|
|
98
|
+
baseUrl: string; // UniAuth server URL
|
|
99
|
+
clientId: string; // OAuth2 client ID
|
|
100
|
+
clientSecret: string; // OAuth2 client secret
|
|
101
|
+
jwtPublicKey?: string; // JWT public key (local verification)
|
|
201
102
|
}
|
|
202
103
|
```
|
|
203
104
|
|
|
204
|
-
### 方法
|
|
105
|
+
### Methods / 方法
|
|
106
|
+
|
|
107
|
+
| Method | Description / 说明 |
|
|
108
|
+
|--------|-----------|
|
|
109
|
+
| `verifyToken(token)` | Verify access token / 验证访问令牌 |
|
|
110
|
+
| `introspectToken(token)` | RFC 7662 introspection / 令牌内省 |
|
|
111
|
+
| `isTokenActive(token)` | Check if token is active / 检查令牌状态 |
|
|
112
|
+
| `getUser(userId)` | Get user info / 获取用户信息 |
|
|
113
|
+
| `middleware()` | Express middleware / Express 中间件 |
|
|
114
|
+
| `honoMiddleware()` | Hono middleware / Hono 中间件 |
|
|
115
|
+
| `clearCache()` | Clear token cache / 清除令牌缓存 |
|
|
205
116
|
|
|
206
|
-
|
|
207
|
-
|------|------|
|
|
208
|
-
| `verifyToken(token)` | 验证访问令牌 |
|
|
209
|
-
| `introspectToken(token)` | RFC 7662 令牌内省 |
|
|
210
|
-
| `isTokenActive(token)` | 检查令牌是否有效 |
|
|
211
|
-
| `getUser(userId)` | 获取用户信息 |
|
|
212
|
-
| `middleware()` | Express/Connect 中间件 |
|
|
213
|
-
| `honoMiddleware()` | Hono 中间件 |
|
|
214
|
-
| `clearCache()` | 清除令牌缓存 |
|
|
117
|
+
### Token Verification Flow / 令牌验证流程
|
|
215
118
|
|
|
216
|
-
|
|
119
|
+
```
|
|
120
|
+
verifyToken(token)
|
|
121
|
+
│
|
|
122
|
+
├─ 1. POST /api/v1/auth/verify (App Key + Secret)
|
|
123
|
+
│ ↓ success → return payload
|
|
124
|
+
│ ↓ 404 or network error
|
|
125
|
+
│
|
|
126
|
+
├─ 2. POST /api/v1/oauth2/introspect (Basic Auth, RFC 7662)
|
|
127
|
+
│ ↓ active:true → return payload
|
|
128
|
+
│ ↓ fail
|
|
129
|
+
│
|
|
130
|
+
└─ 3. Local JWT verification (if jwtPublicKey configured)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Types / 类型
|
|
217
134
|
|
|
218
135
|
```typescript
|
|
219
136
|
interface TokenPayload {
|
|
220
|
-
sub: string;
|
|
221
|
-
iss?: string;
|
|
222
|
-
aud?: string | string[];
|
|
223
|
-
exp: number;
|
|
224
|
-
iat: number;
|
|
225
|
-
scope?: string;
|
|
226
|
-
phone?: string;
|
|
227
|
-
email?: string;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
interface UserInfo {
|
|
231
|
-
id: string;
|
|
232
|
-
phone?: string;
|
|
233
|
-
email?: string;
|
|
234
|
-
nickname?: string;
|
|
235
|
-
avatar_url?: string;
|
|
236
|
-
phone_verified?: boolean;
|
|
237
|
-
email_verified?: boolean;
|
|
137
|
+
sub: string; // User ID
|
|
138
|
+
iss?: string; // Issuer
|
|
139
|
+
aud?: string | string[]; // Audience
|
|
140
|
+
exp: number; // Expiration
|
|
141
|
+
iat: number; // Issued at
|
|
142
|
+
scope?: string; // Scopes
|
|
143
|
+
phone?: string; // Phone number
|
|
144
|
+
email?: string; // Email
|
|
238
145
|
}
|
|
239
146
|
```
|
|
240
147
|
|
|
241
|
-
## 错误处理
|
|
148
|
+
## Error Handling / 错误处理
|
|
242
149
|
|
|
243
150
|
```typescript
|
|
244
151
|
import { ServerAuthError, ServerErrorCode } from '@55387.ai/uniauth-server';
|
|
@@ -248,12 +155,8 @@ try {
|
|
|
248
155
|
} catch (error) {
|
|
249
156
|
if (error instanceof ServerAuthError) {
|
|
250
157
|
switch (error.code) {
|
|
251
|
-
case ServerErrorCode.INVALID_TOKEN:
|
|
252
|
-
|
|
253
|
-
break;
|
|
254
|
-
case ServerErrorCode.TOKEN_EXPIRED:
|
|
255
|
-
// 令牌已过期
|
|
256
|
-
break;
|
|
158
|
+
case ServerErrorCode.INVALID_TOKEN: // Invalid / 令牌无效
|
|
159
|
+
case ServerErrorCode.TOKEN_EXPIRED: // Expired / 令牌过期
|
|
257
160
|
}
|
|
258
161
|
}
|
|
259
162
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@55387.ai/uniauth-server",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "UniAuth Server SDK - Token verification for Node.js backends",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,8 +8,7 @@
|
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist",
|
|
11
|
-
"README.md"
|
|
12
|
-
"INTEGRATION.md"
|
|
11
|
+
"README.md"
|
|
13
12
|
],
|
|
14
13
|
"exports": {
|
|
15
14
|
".": {
|