@ohoooho/taoxian-runtime-sdk 0.1.0 → 0.1.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.2
4
+
5
+ - `return_to` 不是 OAuth/OIDC 标准,回跳参数改回更短的内部约定 `next`。
6
+ - 仍然只接受 Runtime 同源相对路径,拒绝完整 URL、`//host`、反斜杠和控制字符。
7
+ - `nextPath` 为主 API;0.1.1 的 `returnTo` 保留为 deprecated alias。
8
+ - 原生 `node:http` 示例继续保留,不引入 Express。
9
+
10
+ ## 0.1.1
11
+
12
+ - 修复登录回跳参数:移除随意定义的 `next`,改为 `return_to`,仅接受 Runtime 同源相对路径。
13
+ - `runtimeLoginUrl()` 不再默认把任意 `req.originalUrl` 放进回跳参数。
14
+ - `createFenshenAuth()` 新增 `loginReturnTo`,默认回到 Runtime `/`;跨端口回跳留给正式 handoff。
15
+ - 示例改为原生 `node:http`,不引入 Express。
16
+
3
17
  ## 0.1.0
4
18
 
5
19
  - 首个公开 SDK 包结构:`runtime`、`fenshen`、`api-key`、`browser` 四个入口。
package/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  npm install @ohoooho/taoxian-runtime-sdk
7
7
  ```
8
8
 
9
- 当前版本:`0.1.0`,license:MIT。Node.js >= 20。浏览器请使用 `@ohoooho/taoxian-runtime-sdk/browser`。
9
+ 当前版本:`0.1.2`,license:MIT。Node.js >= 20。浏览器请使用 `@ohoooho/taoxian-runtime-sdk/browser`。
10
10
  包是 **ESM-only**;CommonJS 项目请用动态 `import()` 或先转译。自带 TypeScript 声明。
11
11
 
12
12
  ## 入口
@@ -36,21 +36,42 @@ console.log(status.version, status.db.ready);
36
36
 
37
37
  分身后端拿到浏览器的 Cookie header,服务端转发给 Runtime:
38
38
 
39
+ 不依赖 Express,原生 `node:http` 就够了:
40
+
39
41
  ```js
40
- import { createFenshenAuth } from '@ohoooho/taoxian-runtime-sdk/fenshen';
42
+ import { createServer } from 'node:http';
43
+ import { createFenshenAuth, runtimeLoginUrl } from '@ohoooho/taoxian-runtime-sdk/fenshen';
41
44
 
42
45
  const auth = createFenshenAuth({
43
46
  runtimeUrl: process.env.TAOXIAN_RUNTIME_URL || 'http://127.0.0.1:4319',
44
47
  });
45
48
 
46
- app.use(auth.middleware);
47
- // 登录后 req.user = { id, username, display_name, ... }
49
+ const server = createServer(async (req, res) => {
50
+ if (req.url === '/health') {
51
+ res.writeHead(200, { 'Content-Type': 'application/json' });
52
+ res.end(JSON.stringify({ ok: true }));
53
+ return;
54
+ }
55
+
56
+ const user = await auth.authenticate(req);
57
+ if (!user) {
58
+ res.writeHead(302, {
59
+ Location: runtimeLoginUrl(req, { nextPath: '/' }),
60
+ });
61
+ res.end();
62
+ return;
63
+ }
64
+
65
+ res.end(`你好,${user.display_name || user.username}`);
66
+ });
48
67
  ```
49
68
 
50
69
  - 未登录时 302 到同一个 host 的 Runtime 登录页
51
70
  - Runtime 不可用时返回 503,不会重定向循环
52
71
  - 不要自己再实现一套登录页
53
72
  - 不要把 Cookie 暴露给浏览器 JS 或日志
73
+ - `next` 只接受 Runtime 同源的相对路径;跨端口回跳需要正式的 handoff,不用裸 URL 参数
74
+ - `next` 是内部约定,不是 OAuth 标准;接 Logto/OIDC 时用 `state` + `redirect_uri`
54
75
 
55
76
  完整契约见 [`../docs/FENSHEN-INTEGRATION.md`](../docs/FENSHEN-INTEGRATION.md)。
56
77
 
@@ -115,7 +136,7 @@ const url = resolveFenshenUiUrl(fenshens[0]);
115
136
 
116
137
  ```html
117
138
  <script type="module">
118
- import { createRuntimeBrowserClient } from 'https://cdn.jsdelivr.net/npm/@ohoooho/taoxian-runtime-sdk@0.1.0/browser.mjs';
139
+ import { createRuntimeBrowserClient } from 'https://cdn.jsdelivr.net/npm/@ohoooho/taoxian-runtime-sdk@0.1.2/browser.mjs';
119
140
  const client = createRuntimeBrowserClient();
120
141
  console.log(await client.getStatus());
121
142
  </script>
@@ -123,7 +144,7 @@ const url = resolveFenshenUiUrl(fenshens[0]);
123
144
 
124
145
  ## 直接从仓库使用
125
146
 
126
- 还没发布 npm 时,可以在本仓内按相对路径引用:
147
+ 已发布到 npm;本仓开发时也可以按相对路径引用源码:
127
148
 
128
149
  ```js
129
150
  import { createFenshenAuth } from './sdk/fenshen.mjs';
@@ -0,0 +1,34 @@
1
+ import { createServer } from 'node:http';
2
+ import { createFenshenAuth, runtimeLoginUrl } from '@ohoooho/taoxian-runtime-sdk/fenshen';
3
+
4
+ const port = Number(process.env.PORT || process.env.TAOXIAN_FENSHEN_PORT || 4173);
5
+ const runtimeUrl = process.env.TAOXIAN_RUNTIME_URL || 'http://127.0.0.1:4319';
6
+ const auth = createFenshenAuth({ runtimeUrl });
7
+
8
+ const server = createServer(async (req, res) => {
9
+ if (req.url === '/health') {
10
+ res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
11
+ res.end(JSON.stringify({ ok: true, schema_version: 1 }));
12
+ return;
13
+ }
14
+
15
+ const user = await auth.authenticate(req);
16
+ if (!user) {
17
+ res.writeHead(302, {
18
+ Location: runtimeLoginUrl(req, { runtimeUrl, nextPath: '/' }),
19
+ 'Cache-Control': 'no-store',
20
+ });
21
+ res.end();
22
+ return;
23
+ }
24
+
25
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
26
+ res.end(`<!doctype html>
27
+ <meta name="viewport" content="width=device-width,initial-scale=1">
28
+ <h1>你好,${user.display_name || user.username}</h1>
29
+ <p>这是原生 node:http 分身示例。</p>`);
30
+ });
31
+
32
+ server.listen(port, '0.0.0.0', () => {
33
+ console.log(`fenshen listening on ${port}`);
34
+ });
package/fenshen.mjs CHANGED
@@ -54,15 +54,29 @@ export function runtimePublicUrl(req, {
54
54
  return url.toString();
55
55
  }
56
56
 
57
+ export function normalizeNextPath(value, { fallback = '/' } = {}) {
58
+ if (!value) return fallback;
59
+ const text = String(value);
60
+ if (!text.startsWith('/') || text.startsWith('//') || text.includes('\\')) return fallback;
61
+ if (/[\u0000-\u001f\u007f]/.test(text)) return fallback;
62
+ try {
63
+ const url = new URL(text, 'http://taoxian.invalid');
64
+ return `${url.pathname}${url.search}${url.hash}`;
65
+ } catch {
66
+ return fallback;
67
+ }
68
+ }
69
+
57
70
  export function runtimeLoginUrl(req, {
58
71
  runtimeUrl = process.env.TAOXIAN_RUNTIME_URL || DEFAULT_RUNTIME_URL,
59
72
  runtimePort,
60
73
  trustProxy = false,
61
74
  nextPath,
75
+ returnTo, // deprecated alias
62
76
  } = {}) {
63
77
  const url = runtimeBaseUrl(req, { runtimeUrl, runtimePort, trustProxy });
64
- const path = nextPath || req?.originalUrl || req?.url;
65
- if (path && path !== '/') url.searchParams.set('next', path);
78
+ const path = normalizeNextPath(nextPath ?? returnTo);
79
+ if (path !== '/') url.searchParams.set('next', path);
66
80
  return url.toString();
67
81
  }
68
82
 
@@ -89,6 +103,10 @@ export function createFenshenAuth({
89
103
  timeoutMs = 2_000,
90
104
  fetchImpl = globalThis.fetch,
91
105
  trustProxy = false,
106
+ // For now this is a Runtime-relative path. Cross-service return (fenshen
107
+ // origin + port) needs a proper handoff/state flow, not a raw URL param.
108
+ loginNextPath,
109
+ loginReturnTo, // deprecated alias
92
110
  onUnauthorized,
93
111
  onUnavailable,
94
112
  } = {}) {
@@ -102,7 +120,11 @@ export function createFenshenAuth({
102
120
  if (!user) {
103
121
  if (onUnauthorized) return onUnauthorized(req, res);
104
122
  res.statusCode = 302;
105
- res.setHeader('Location', runtimeLoginUrl(req, { runtimeUrl, trustProxy }));
123
+ res.setHeader('Location', runtimeLoginUrl(req, {
124
+ runtimeUrl,
125
+ trustProxy,
126
+ nextPath: loginNextPath ?? loginReturnTo ?? '/',
127
+ }));
106
128
  return res.end();
107
129
  }
108
130
  req.user = user;
@@ -122,3 +144,6 @@ export function createFenshenAuth({
122
144
  middleware,
123
145
  };
124
146
  }
147
+
148
+ // Deprecated alias kept for 0.1.1 consumers.
149
+ export const normalizeReturnTo = normalizeNextPath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ohoooho/taoxian-runtime-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Taoxian Runtime, fenshen auth and static API-key helpers for Node.js and browsers",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -5,6 +5,9 @@ export interface FenshenAuthOptions {
5
5
  timeoutMs?: number;
6
6
  fetchImpl?: FetchLike;
7
7
  trustProxy?: boolean;
8
+ loginNextPath?: string;
9
+ /** @deprecated use loginNextPath */
10
+ loginReturnTo?: string;
8
11
  onUnauthorized?: (req: any, res: any) => any;
9
12
  onUnavailable?: (req: any, res: any, error: unknown) => any;
10
13
  }
@@ -24,6 +27,8 @@ export interface RuntimePublicUrlOptions {
24
27
 
25
28
  export interface RuntimeLoginUrlOptions extends Omit<RuntimePublicUrlOptions, 'path'> {
26
29
  nextPath?: string;
30
+ /** @deprecated use nextPath */
31
+ returnTo?: string;
27
32
  }
28
33
 
29
34
  export class FenshenAuthError extends Error {
@@ -31,6 +36,9 @@ export class FenshenAuthError extends Error {
31
36
  cause?: unknown;
32
37
  }
33
38
 
39
+ export function normalizeNextPath(value: unknown, options?: { fallback?: string }): string;
40
+ /** @deprecated use normalizeNextPath */
41
+ export function normalizeReturnTo(value: unknown, options?: { fallback?: string }): string;
34
42
  export function runtimePublicUrl(req: any, options?: RuntimePublicUrlOptions): string;
35
43
  export function runtimeLoginUrl(req: any, options?: RuntimeLoginUrlOptions): string;
36
44
  export function getFenshenUser(req: any, options?: Omit<FenshenAuthOptions, 'trustProxy' | 'onUnauthorized' | 'onUnavailable'>): Promise<LocalUser | null>;
@@ -1,27 +0,0 @@
1
- import express from 'express';
2
- import { createFenshenAuth } from '@ohoooho/taoxian-runtime-sdk/fenshen';
3
-
4
- const app = express();
5
- const port = Number(process.env.PORT || 4173);
6
- const auth = createFenshenAuth({
7
- runtimeUrl: process.env.TAOXIAN_RUNTIME_URL || 'http://127.0.0.1:4319',
8
- });
9
-
10
- app.get('/health', (req, res) => {
11
- res.json({ ok: true, schema_version: 1 });
12
- });
13
-
14
- // Browser cookie is validated by the fenshen backend, not by browser JS.
15
- app.use(auth.middleware);
16
-
17
- app.get('/', (req, res) => {
18
- const name = req.user.display_name || req.user.username;
19
- res.type('html').send(`<!doctype html>
20
- <meta name="viewport" content="width=device-width,initial-scale=1">
21
- <h1>你好,${name}</h1>
22
- <p>这是分身自己的业务页面。</p>`);
23
- });
24
-
25
- app.listen(port, '0.0.0.0', () => {
26
- console.log(`fenshen listening on ${port}`);
27
- });