zy-web-gate 1.0.0 → 1.1.0
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 +2 -0
- package/dist/cookie.d.ts +52 -0
- package/dist/index.d.ts +67 -0
- package/dist/ui.d.ts +24 -0
- package/dist/verify.d.ts +44 -0
- package/package.json +6 -2
package/README.md
CHANGED
package/dist/cookie.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 由当前 host 推断可用于跨子域共享的父域。
|
|
3
|
+
*
|
|
4
|
+
* 例如当前在 a.example.com,应返回 example.com,
|
|
5
|
+
* 这样写入的 cookie 能被 my./b./x. 等所有兄弟子域读到。
|
|
6
|
+
*
|
|
7
|
+
* 注意:eu.org 在 Public Suffix List 上,浏览器禁止把 cookie 设到 eu.org,
|
|
8
|
+
* 所以父域只能取到 example.com 这一层,刚好是我们要的、也是安全的。
|
|
9
|
+
*
|
|
10
|
+
* 这里采用「去掉最左一段」的朴素策略:a.example.com -> example.com。
|
|
11
|
+
* 大多数「单层子域」场景够用;若有多级子域或想显式指定,用 options.cookieDomain 覆盖。
|
|
12
|
+
*
|
|
13
|
+
* @param {string} [hostname] 默认取 location.hostname
|
|
14
|
+
* @returns {string} 用于 cookie Domain 的父域;localhost / IP 等场景返回空串(表示按当前 host)
|
|
15
|
+
*/
|
|
16
|
+
export function inferParentDomain(hostname?: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* 读取指定名称的 cookie 值。
|
|
19
|
+
* @param {string} name
|
|
20
|
+
* @returns {string|null}
|
|
21
|
+
*/
|
|
22
|
+
export function readCookie(name: string): string | null;
|
|
23
|
+
/**
|
|
24
|
+
* 写入跨子域 cookie。
|
|
25
|
+
*
|
|
26
|
+
* @param {string} name
|
|
27
|
+
* @param {string} value
|
|
28
|
+
* @param {object} opts
|
|
29
|
+
* @param {string} opts.domain cookie 的 Domain,传空串则不写 Domain(仅当前 host)
|
|
30
|
+
* @param {number} opts.maxAgeSeconds 有效期(秒)
|
|
31
|
+
* @param {string} [opts.sameSite="Lax"]
|
|
32
|
+
* @param {boolean} [opts.secure=true] HTTPS 下应为 true;本地 http 调试时会自动放宽
|
|
33
|
+
* @param {string} [opts.path="/"]
|
|
34
|
+
*/
|
|
35
|
+
export function writeCookie(name: string, value: string, opts: {
|
|
36
|
+
domain: string;
|
|
37
|
+
maxAgeSeconds: number;
|
|
38
|
+
sameSite?: string | undefined;
|
|
39
|
+
secure?: boolean | undefined;
|
|
40
|
+
path?: string | undefined;
|
|
41
|
+
}): void;
|
|
42
|
+
/**
|
|
43
|
+
* 删除跨子域 cookie(用于「登出」)。Domain / Path 必须和写入时一致才能删掉。
|
|
44
|
+
* @param {string} name
|
|
45
|
+
* @param {object} opts
|
|
46
|
+
* @param {string} opts.domain
|
|
47
|
+
* @param {string} [opts.path="/"]
|
|
48
|
+
*/
|
|
49
|
+
export function deleteCookie(name: string, opts: {
|
|
50
|
+
domain: string;
|
|
51
|
+
path?: string | undefined;
|
|
52
|
+
}): void;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 访问门主入口。
|
|
3
|
+
*
|
|
4
|
+
* 行为:
|
|
5
|
+
* 1. 若父域 cookie 已存在有效登录态 -> 直接 resolve(放行,无 UI)。
|
|
6
|
+
* 2. 否则弹出密码页,等用户输入并通过接口校验。
|
|
7
|
+
* 3. 校验通过 -> 写父域 cookie -> resolve。
|
|
8
|
+
*
|
|
9
|
+
* 该 Promise 只在「已通过门禁」时 resolve;密码未通过时不会 resolve,
|
|
10
|
+
* 因此把它放在 createApp().mount() 之前 await,可保证未通过时真实应用绝不挂载。
|
|
11
|
+
*
|
|
12
|
+
* @param {GateOptions} [options]
|
|
13
|
+
* @returns {Promise<void>}
|
|
14
|
+
*/
|
|
15
|
+
export function ensureGate(options?: GateOptions): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* 主动登出:清除父域 cookie。调用后下次进入任一子站会重新要求输入密码。
|
|
18
|
+
* @param {GateOptions} options 至少需与 ensureGate 一致的 cookieName / cookieDomain
|
|
19
|
+
*/
|
|
20
|
+
export function logoutGate(options?: GateOptions): void;
|
|
21
|
+
export type GateOptions = {
|
|
22
|
+
/**
|
|
23
|
+
* 登录态 cookie 名,默认 "zy_web_gate"
|
|
24
|
+
*/
|
|
25
|
+
cookieName?: string | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* 写入的 cookie 值,默认 "1"
|
|
28
|
+
*/
|
|
29
|
+
cookieValue?: string | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* 显式指定父域;不传则自动推断(如 example.com)
|
|
32
|
+
*/
|
|
33
|
+
cookieDomain?: string | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* 登录态有效天数,默认 7
|
|
36
|
+
*/
|
|
37
|
+
maxAgeDays?: number | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* cookie SameSite,默认 "Lax"
|
|
40
|
+
*/
|
|
41
|
+
sameSite?: string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* cookie Secure;不传则按当前协议自动判断
|
|
44
|
+
*/
|
|
45
|
+
secure?: boolean | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* 密码页标题
|
|
48
|
+
*/
|
|
49
|
+
title?: string | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* 密码页副标题
|
|
52
|
+
*/
|
|
53
|
+
subtitle?: string | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* 输入框占位文案
|
|
56
|
+
*/
|
|
57
|
+
placeholder?: string | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* 按钮文案
|
|
60
|
+
*/
|
|
61
|
+
buttonText?: string | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* 接口超时毫秒,默认 10000
|
|
64
|
+
*/
|
|
65
|
+
timeoutMs?: number | undefined;
|
|
66
|
+
};
|
|
67
|
+
export { inferParentDomain, readCookie } from "./cookie.js";
|
package/dist/ui.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 创建并挂载密码页。返回一个对象,可用于销毁。
|
|
3
|
+
*
|
|
4
|
+
* @param {object} cfg
|
|
5
|
+
* @param {string} cfg.title
|
|
6
|
+
* @param {string} cfg.subtitle
|
|
7
|
+
* @param {string} cfg.placeholder
|
|
8
|
+
* @param {string} cfg.buttonText
|
|
9
|
+
* @param {(password: string) => Promise<{ok: boolean, message?: string}>} cfg.onSubmit
|
|
10
|
+
* 提交回调;返回 ok=true 时 UI 自动销毁,false 时显示 message。
|
|
11
|
+
* @returns {{ destroy: () => void }}
|
|
12
|
+
*/
|
|
13
|
+
export function mountPasswordGate(cfg: {
|
|
14
|
+
title: string;
|
|
15
|
+
subtitle: string;
|
|
16
|
+
placeholder: string;
|
|
17
|
+
buttonText: string;
|
|
18
|
+
onSubmit: (password: string) => Promise<{
|
|
19
|
+
ok: boolean;
|
|
20
|
+
message?: string;
|
|
21
|
+
}>;
|
|
22
|
+
}): {
|
|
23
|
+
destroy: () => void;
|
|
24
|
+
};
|
package/dist/verify.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 校验结果。
|
|
3
|
+
* @typedef {object} VerifyResult
|
|
4
|
+
* @property {boolean} ok 密码是否正确
|
|
5
|
+
* @property {boolean} [networkError] 是否因网络/接口异常导致(区别于「密码错」)
|
|
6
|
+
* @property {string} [message] 可展示给用户的提示
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* 调用校验接口。
|
|
10
|
+
*
|
|
11
|
+
* 接口约定:
|
|
12
|
+
* 请求 POST apiUrl body: {"password": "用户输入"}
|
|
13
|
+
* 通过 HTTP 200, {"code":200, "data":{"match":true}}
|
|
14
|
+
* 密码错 HTTP 401, {"code":401, "data":{}}(或任何 match !== true 的响应)
|
|
15
|
+
*
|
|
16
|
+
* 判定规则:只有「HTTP ok 且 data.match === true」才算密码正确;
|
|
17
|
+
* 其余一律当密码错。网络层异常单独标记为 networkError,便于 UI 区分提示。
|
|
18
|
+
*
|
|
19
|
+
* @param {string} apiUrl
|
|
20
|
+
* @param {string} password
|
|
21
|
+
* @param {object} [opts]
|
|
22
|
+
* @param {number} [opts.timeoutMs=10000]
|
|
23
|
+
* @returns {Promise<VerifyResult>}
|
|
24
|
+
*/
|
|
25
|
+
export function verifyPassword(apiUrl: string, password: string, opts?: {
|
|
26
|
+
timeoutMs?: number | undefined;
|
|
27
|
+
}): Promise<VerifyResult>;
|
|
28
|
+
/**
|
|
29
|
+
* 校验结果。
|
|
30
|
+
*/
|
|
31
|
+
export type VerifyResult = {
|
|
32
|
+
/**
|
|
33
|
+
* 密码是否正确
|
|
34
|
+
*/
|
|
35
|
+
ok: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* 是否因网络/接口异常导致(区别于「密码错」)
|
|
38
|
+
*/
|
|
39
|
+
networkError?: boolean | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* 可展示给用户的提示
|
|
42
|
+
*/
|
|
43
|
+
message?: string | undefined;
|
|
44
|
+
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zy-web-gate",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Framework-agnostic, pure-frontend shared-password page gate with cross-subdomain login state via a parent-domain cookie. No backend auth, no accounts — verification is delegated to a check-password API.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/zy-web-gate.umd.cjs",
|
|
7
7
|
"module": "./dist/zy-web-gate.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
8
9
|
"unpkg": "./dist/zy-web-gate.umd.cjs",
|
|
9
10
|
"jsdelivr": "./dist/zy-web-gate.umd.cjs",
|
|
10
11
|
"exports": {
|
|
11
12
|
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
12
14
|
"import": "./dist/zy-web-gate.js",
|
|
13
15
|
"require": "./dist/zy-web-gate.umd.cjs"
|
|
14
16
|
}
|
|
@@ -19,7 +21,8 @@
|
|
|
19
21
|
],
|
|
20
22
|
"scripts": {
|
|
21
23
|
"check": "node --check src/index.js && node --check src/cookie.js && node --check src/verify.js && node --check src/ui.js",
|
|
22
|
-
"build": "vite build",
|
|
24
|
+
"build": "vite build && npm run build:types",
|
|
25
|
+
"build:types": "tsc -p tsconfig.json",
|
|
23
26
|
"prepublishOnly": "npm run build"
|
|
24
27
|
},
|
|
25
28
|
"keywords": [
|
|
@@ -37,6 +40,7 @@
|
|
|
37
40
|
"license": "ISC",
|
|
38
41
|
"sideEffects": false,
|
|
39
42
|
"devDependencies": {
|
|
43
|
+
"typescript": "^6.0.3",
|
|
40
44
|
"vite": "^8.0.16"
|
|
41
45
|
}
|
|
42
46
|
}
|