@dplugin/core 0.0.0-stage → 2.0.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 +316 -2
- package/dist/index.cjs +3 -0
- package/dist/index.mjs +861 -0
- package/dist/index.umd.js +3 -0
- package/package.json +50 -4
package/README.md
CHANGED
|
@@ -1,3 +1,317 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dplugin/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
DPlugin 前端登录与请求 SDK(v2)。零运行时依赖。
|
|
4
|
+
|
|
5
|
+
## 两种模式(按 origin 自动选,也可 `setup({mode})` 强制)
|
|
6
|
+
|
|
7
|
+
| | 触发条件 | 凭证 | 密钥 | 请求形态 |
|
|
8
|
+
|---|---|---|---|---|
|
|
9
|
+
| **cookie** | 页面与 `apiBase` **同源** | 服务端下发的 HttpOnly Cookie(`dp_sid` / `dp_ds` / `dp_token`) | **不生成** | 无签名参数,浏览器自动带 Cookie |
|
|
10
|
+
| **sign** | 页面与 `apiBase` 跨源 | `access`(localStorage)+ sso/域私钥 | P-256 ×2 | 每次带一个 `dsign` 信封 |
|
|
11
|
+
|
|
12
|
+
两条链路的登录态在服务端是同一套 sso/域会话,互不干扰。
|
|
13
|
+
|
|
14
|
+
**cookie 模式**
|
|
15
|
+
- 登录:`submit`(**不带 pk**)→ 前端把临时 token 种进 `dp_dvt` → 轮询 `POST /c/verify`(body 只有 `redirect`)→ `POST /c/token`(无参数,凭 `dp_code` Cookie)换 `dp_token`
|
|
16
|
+
- 业务:直接请求,浏览器带 Cookie
|
|
17
|
+
- 续期:`500900` → `POST /c/refresh`(无参数)→ 重发
|
|
18
|
+
- 退出:`POST /api/safe/dplugin/login/logout`(无参数),服务端清 `dp_sid`/`dp_token`
|
|
19
|
+
- `isLogin()` 只是本地标记(HttpOnly 读不到),真正有效性以第一次请求为准
|
|
20
|
+
|
|
21
|
+
**sign 模式**
|
|
22
|
+
- 登录:`submit`(上报 sso 公钥)→ 轮询 `POST /s/verify`(带签名)→ `code` 换 `access`(上报域公钥)
|
|
23
|
+
- 业务:每次请求带 `dsign` 信封(access/设备/时间/签名,域私钥签)
|
|
24
|
+
- 续期:`500900` → 用 sso 私钥走 `VERIFY_SID` 换 code → `TOKEN` 换新 access(**不再有 REFRESH**)
|
|
25
|
+
|
|
26
|
+
旧版的 RSA 加密、`refresh_token`、`jump`、定时续期全部移除(后端已删)。
|
|
27
|
+
|
|
28
|
+
## 安装
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# 内测阶段用 file: 引用(页面 package.json 已经是 "file:../dplugin-login-umdjs-core")
|
|
32
|
+
# ⚠️ 干净 clone 后必须先在**本目录**跑一次 npm install && npm run build:
|
|
33
|
+
# dist/ 不入库,而页面通过 exports 解析到 dist/index.mjs,缺了会报
|
|
34
|
+
# "Failed to resolve entry for package @dplugin/core"
|
|
35
|
+
npm install && npm run build
|
|
36
|
+
cnpm install @dplugin/core
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## 接入
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
// main.js —— 每个应用只调一次
|
|
43
|
+
import {setup, request, login, logout} from '@dplugin/core';
|
|
44
|
+
|
|
45
|
+
setup({
|
|
46
|
+
apiBase: import.meta.env.VITE_API_BASE_URL, // '' = 同源 → 自动走 cookie 模式
|
|
47
|
+
mode: 'auto', // auto / cookie / sign
|
|
48
|
+
loginUrl: import.meta.env.VITE_LOGIN_URL,
|
|
49
|
+
onError: ({code, message}) => Modal.error({title: '操作失败[' + code + ']', content: message}),
|
|
50
|
+
onLogout: () => router.replace('/login')
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
// 登录页:登录方式自己决定,SDK 只负责剩下的三步
|
|
56
|
+
await login('/api/unsafe/dplugin/user/login/pwd/submit', {username, password});
|
|
57
|
+
location.href = '/';
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
// 业务接口
|
|
62
|
+
export function apiUserGet() {
|
|
63
|
+
return request('/api/safe/dplugin/user/get'); // 返回值就是 data
|
|
64
|
+
}
|
|
65
|
+
export function apiUserUpdate(data) {
|
|
66
|
+
return request('/api/safe/dplugin/user/update', data);
|
|
67
|
+
}
|
|
68
|
+
export function apiLoginPageSetting() {
|
|
69
|
+
return request('/api/unsafe/dplugin/login/page/setting', {}, {auth: false}); // 匿名接口
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
默认 `enableAuth:false`:SDK 不碰 `location`,会话失效只回调 `onLogout` 并抛 `AuthError`,
|
|
74
|
+
提示与跳转都由业务处理(登录页无需额外配置)。
|
|
75
|
+
|
|
76
|
+
## 对外 API
|
|
77
|
+
|
|
78
|
+
| 导出 | 说明 |
|
|
79
|
+
|---|---|
|
|
80
|
+
| `setup(options)` | 初始化,见下表 |
|
|
81
|
+
| `request(api, data?, options?)` | 业务请求,返回 `data`;自动静默恢复 / 刷新 / 重试一次 |
|
|
82
|
+
| `upload(api, file, data?, options?)` | multipart 上传(签名参数在 query,文件在 body);`options.timeout` 单独放宽超时 |
|
|
83
|
+
| `login(submitApi, params)` | 完整登录流程,返回 `{loggedIn, sid, ds}` |
|
|
84
|
+
| `logout()` | 退出(access 过期会先续期再退,保证服务端会话被删) |
|
|
85
|
+
| `verifyLogin(options?)` | 页面加载时恢复登录态;`{force:true}` 强制换一次 token |
|
|
86
|
+
| `isLogin()` / `getState()` | `getState()` = `{loggedIn, hasAccess, sid, ds}`,**不暴露 access**;`isLogin()` 是本地提示(有 access 或 sid 即为 true) |
|
|
87
|
+
| `clear()` / `expire(reason?)` | 只清本地 / 清本地并跳登录页 |
|
|
88
|
+
| `auth` | 门面:`{login, logout, isLogin, getState, restore}` |
|
|
89
|
+
| `BizError` / `AuthError` / `CODE` / `isAuthError` | 错误类型与码值常量 |
|
|
90
|
+
| `store` | 登录态存储(access / sid / ds),一般不需要直接用 |
|
|
91
|
+
|
|
92
|
+
### options
|
|
93
|
+
|
|
94
|
+
| 字段 | 默认 | 说明 |
|
|
95
|
+
|---|---|---|
|
|
96
|
+
| `apiBase` | `''` | 接口前缀,`''` = 同源 |
|
|
97
|
+
| `loginUrl` | `/login/` | 仅当 `enableAuth:true` 时由 SDK 跳这里 |
|
|
98
|
+
| `enableAuth` | `false` | **默认不跳转**:会话失效只清本地 + `onLogout` + 抛 `AuthError`,提示/跳转由业务做 |
|
|
99
|
+
| `timeout` | `15000` | 请求超时(毫秒) |
|
|
100
|
+
| `pollInterval` | `2000` | 登录轮询间隔(毫秒) |
|
|
101
|
+
| `pollMax` | `5` | 登录轮询次数,用完还没成功即 `500900` |
|
|
102
|
+
| `nativeGuard` | `'warn'` | 原生能力体检:`warn` / `error` / `off` |
|
|
103
|
+
| `onError` | - | `({code, message, error})`,业务错误提示(每个业务失败都会回调一次) |
|
|
104
|
+
| `onLogin` | - | `(state)` 登录 / 静默恢复成功 |
|
|
105
|
+
| `onLogout` | - | `(reason)` 登出或会话失效 |
|
|
106
|
+
|
|
107
|
+
## 签名口径(统一模板 + 单参数信封)
|
|
108
|
+
|
|
109
|
+
四个场景共用**一张模板**(后端 `com.dplugin.starter.core.sign.SignUtils`,前端 `src/sign.js`,
|
|
110
|
+
两边逐字节一致,有跨语言比对测试):
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
OP \n METHOD \n PATH \n ORIGIN(小写) \n DS \n TS(毫秒) \n SUBJECT \n DNA \n REDIRECT
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
固定 9 段,**缺失的段写 `-`**。
|
|
117
|
+
|
|
118
|
+
| OP | 场景 | METHOD/PATH | SUBJECT | DNA/REDIRECT | 私钥 |
|
|
119
|
+
|---|---|---|---|---|---|
|
|
120
|
+
| `BIZ` | 业务接口(网关验) | 实际值 | access | `-` | domain |
|
|
121
|
+
| `VERIFY` | 临时 token 轮询换 code(login 验) | 实际值 | 临时 token | 都有 | sso |
|
|
122
|
+
| `VERIFY_SID` | 已登录用 sid 换 code(login 验) | 实际值 | sid | 都有 | sso |
|
|
123
|
+
| `TOKEN` | code 换 access(login 验) | `-` | 一次性 code | dna | domain |
|
|
124
|
+
|
|
125
|
+
### 单参数信封
|
|
126
|
+
|
|
127
|
+
所有签名要素压成一个值,服务端再拆:
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
dsign = OP.SUBJECT.DS.TS.SIGN
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
| 接口 | 携带位置 | 其它字段 |
|
|
134
|
+
|---|---|---|
|
|
135
|
+
| 业务 `/api/**` `/mgr/**` | URL query `dsign` | body 就是业务参数 |
|
|
136
|
+
| `logout` | URL query `dsign` | 无 body |
|
|
137
|
+
| `verify` | body `dsign` | `dna`、`redirect` |
|
|
138
|
+
| `token` | body `dsign` | `pk` |
|
|
139
|
+
|
|
140
|
+
URL 上不再有 `_dsid / _ds / _dt / dsignn` 四个参数,只有一个 `dsign`。
|
|
141
|
+
|
|
142
|
+
> **⚠️ 载体规则(硬约束)**:出现在 **URL** 上的 `dsign` **只能是 `BIZ`** —— 网关 `SignFilter`
|
|
143
|
+
> 对 URL 上的其它 OP 一律 500「无效的请求参数」。`VERIFY` / `VERIFY_SID` / `TOKEN` 必须放请求体。
|
|
144
|
+
> 测试里有一条断言守着这个不变量(`URL 上的 dsign 全部是 BIZ`)。
|
|
145
|
+
|
|
146
|
+
### 为什么 `dsign` 放 query 而不是请求头
|
|
147
|
+
|
|
148
|
+
请求体用 **`Content-Type: text/plain`**(不是 `application/json`),加上没有任何自定义头,
|
|
149
|
+
跨域时这就是一个 **CORS「简单请求」,不会发 OPTIONS 预检**——每个接口少一次往返。
|
|
150
|
+
|
|
151
|
+
框架侧 `JSONHttpMessageConverter.body()` **不看 Content-Type**,直接读流按 JSON 解析,
|
|
152
|
+
所以 `text/plain` 里放 JSON 完全正常。
|
|
153
|
+
|
|
154
|
+
⚠️ 一旦把 `dsign` 挪到 `X-Dsig` 这类请求头,就会重新触发预检,等于把省掉的往返又加回来。
|
|
155
|
+
所以「单参数信封」放在 query 上是和「无预检」配套的设计,不是随意选的。
|
|
156
|
+
|
|
157
|
+
### 其它硬约束(读代码核出来的)
|
|
158
|
+
|
|
159
|
+
- `ORIGIN` 两边都转小写;`TS` 一律毫秒;
|
|
160
|
+
- `verify` / `token` 绑定 `D-Request-IP`:登录过程中 IP 变了会 `500900`;
|
|
161
|
+
- `dna` = `SHA-256(SPKI DER)` 的 Base64URL 无填充,`verify` 必传,`token` 用上报的 `pk` 重算并比对;
|
|
162
|
+
- 时间窗 300 秒;access Redis TTL 1800 秒;临时 token 5 分钟;code 60 秒且 `GETDEL`;
|
|
163
|
+
- 设备只走信封的 `DS` 段,**没有 `X-Device-Id` 头**;
|
|
164
|
+
- 业务请求头里只有 `Content-Type: text/plain`,不加自定义头 → 不触发 CORS 预检(有测试断言)。
|
|
165
|
+
|
|
166
|
+
## 错误码与前端动作
|
|
167
|
+
|
|
168
|
+
| 码 | 含义 | SDK 已自动处理,业务侧只需 |
|
|
169
|
+
|---|---|---|
|
|
170
|
+
| `200` | 成功 | - |
|
|
171
|
+
| `202` | 登录中 | `login()` 内部轮询,业务侧看不到 |
|
|
172
|
+
| `500900` | 会话失效 | 业务请求里会自动 `refresh` 一次并重发;刷新也失败才清状态 + 跳登录页 |
|
|
173
|
+
| `500` | 通用业务错误:密码错误 / 平台不支持 / 重复请求(`重复请求`、`请勿重复提交`)/ 时钟异常 | 抛 `BizError` + `onError`,**不刷新、不登出**;按 `message` 展示即可 |
|
|
174
|
+
| `401001` | 没有权限 | 抛错 + `onError`,会话仍然有效 |
|
|
175
|
+
| `403` | 非法来源(CORS / 域名不符) | 环境配置问题,不是登录态问题 |
|
|
176
|
+
| `404` | 接口不存在 | 接口没在 api 表登记 |
|
|
177
|
+
|
|
178
|
+
> `login()` 抛出的错误直接展示即可(`code` + `message` 都来自后端)。
|
|
179
|
+
|
|
180
|
+
## 密钥与存储
|
|
181
|
+
|
|
182
|
+
| 内容 | 位置 | 说明 |
|
|
183
|
+
|---|---|---|
|
|
184
|
+
| `access` | localStorage `dplugin.access` | 业务接口信封的 SUBJECT,30 分钟 |
|
|
185
|
+
| `sid` | localStorage `dplugin.sid` | 页面刷新后用来自动恢复登录态 |
|
|
186
|
+
| `ds` | localStorage `dplugin.ds` | 设备凭证,服务端下发,**不要自己生成**;退出登录**不清**它(设备不是会话) |
|
|
187
|
+
| `exp` | localStorage `dplugin.exp` | access 到期时间,仅用于本地展示/判断 |
|
|
188
|
+
| sso 私钥 | IndexedDB `dplugin-auth/keys.sso` | 签 `verify` 的 sid 分支 |
|
|
189
|
+
| 域私钥 | IndexedDB `dplugin-auth/keys.domain` | 签所有业务请求 |
|
|
190
|
+
|
|
191
|
+
- 私钥 `extractable = false`,只能以 CryptoKey 形式存在 IndexedDB,JS 导不出原始字节。
|
|
192
|
+
- 每次登录重新生成 sso 密钥,每次换 token 重新生成域密钥(多账号不会串)。
|
|
193
|
+
- 清过 IndexedDB 后:业务请求会检测到缺私钥 → 用 `sid` 强制换一次 token(自愈);连 `sid` 都没了才回登录页。
|
|
194
|
+
|
|
195
|
+
## 行为约定(多标签页 / 失败回滚)
|
|
196
|
+
|
|
197
|
+
1. **同一页面内续期单飞**:N 个并发 500900 只发一次 VERIFY_SID,其余复用同一结果。
|
|
198
|
+
2. **跨标签页续期**:`/s/token` 有按 sso 的锁,A 标签页抢到时 B 会拿到 `500 重复请求`;
|
|
199
|
+
SDK 不会把自己登出,而是等 A 把新 access 写进 localStorage(最多 1.2 秒)后直接采用。
|
|
200
|
+
3. **登录失败会回滚密钥**:`login()` 会先生成新的 sso/domain 密钥,失败(密码错、轮询超时)
|
|
201
|
+
会把上一个会话的密钥写回去,避免"输错一次密码"把已有会话的私钥冲掉。
|
|
202
|
+
4. **`isLogin()` 只是提示**:access 过期但 sid 还在时它仍返回 true,第一次 `request()` 会自动
|
|
203
|
+
用 sid 静默换回 access;两条路都断了才跳登录页。
|
|
204
|
+
|
|
205
|
+
## 已知取舍(有意为之,不是待办)
|
|
206
|
+
|
|
207
|
+
| 取舍 | 原因 |
|
|
208
|
+
|---|---|
|
|
209
|
+
| 不做定时器 / 提前续期 | 后端规定 access 必须**已过期**才允许刷新,未过期刷新会被判异常并吊销整个 sso。只能在 500900 之后被动刷新 |
|
|
210
|
+
| 默认不跳登录页(`enableAuth:false`) | 框架不该替业务决定导航;失效时抛 `AuthError` + `onLogout`,由业务提示或跳转 |
|
|
211
|
+
| `refresh` 不对外导出 | 同上:业务侧手调会把用户全端踢下线 |
|
|
212
|
+
| 不做跨标签页登出广播 | A 标签页退出后,B 要等下一个请求才发现(然后正常跳登录页);加 `storage` 事件监听会引入全局副作用,收益不值 |
|
|
213
|
+
| 不内置 loading / 重试策略 | 交给页面自己的请求封装;SDK 只保证"登录态 + 签名"正确 |
|
|
214
|
+
| QQ / 微信扫码登录暂不支持 | 后端这两个入口还没接新协议(缺 `origin`/`api`/`pk`,微信扫码在服务端 callback 里拿不到浏览器公钥) |
|
|
215
|
+
|
|
216
|
+
## 原生能力指纹(防 fetch / indexedDB.open / subtle 被代理)
|
|
217
|
+
|
|
218
|
+
`src/native.js` 在 **模块加载那一刻**:
|
|
219
|
+
1. 把 `fetch`、`indexedDB.open`、`crypto.subtle.{generateKey,exportKey,importKey,sign,verify,digest}`
|
|
220
|
+
绑定成局部引用 —— 之后页面再替换全局,也影响不到 SDK;
|
|
221
|
+
2. 顺手给这份引用做**指纹校验**,`setup()` 时报告(`nativeGuard: 'warn' | 'error' | 'off'`)。
|
|
222
|
+
|
|
223
|
+
真机(Chrome)实测结果,`checkNative()` 的返回:
|
|
224
|
+
|
|
225
|
+
| 场景 | 返回 | 判定依据 |
|
|
226
|
+
|---|---|---|
|
|
227
|
+
| 原生 | `[]` | `function fetch() { [native code] }` |
|
|
228
|
+
| JS 包装(转发原生) | `["fetch"]` | 源码变成 `async function fetch(input, init) {…}` |
|
|
229
|
+
| **Proxy 包原生** | `["fetch"]` | Proxy 的源码是 `function () { [native code] }` —— **没有函数名** |
|
|
230
|
+
| 自造 fetch(不校验参数) | `["fetch"]` | 源码里没有 `[native code]` |
|
|
231
|
+
| `indexedDB.open` 被包 | `["indexedDB.open"]` | 源码形态 + **无参调用必须同步抛缺参数 TypeError** |
|
|
232
|
+
| 全部还原 | `[]` | - |
|
|
233
|
+
|
|
234
|
+
关键细节(实测踩出来的):
|
|
235
|
+
- **`fetch()` 无参在 Chrome 里是返回 rejected promise,不是同步抛**。所以 fetch 只用「源码形态」
|
|
236
|
+
判,不能做同步异常指纹,否则会把原生误报成被替换。
|
|
237
|
+
- `indexedDB.open()` 是同步 WebIDL 方法,无参会同步抛
|
|
238
|
+
`TypeError: Failed to execute 'open' on 'IDBFactory': 1 argument required…`,
|
|
239
|
+
所以它额外多一道异常指纹。`crypto.subtle.*` 同理是异步 reject,只比源码。
|
|
240
|
+
- 检测只对浏览器生效:Node / SSR 里 fetch、subtle 本身就是 JS 实现,必然误报。
|
|
241
|
+
|
|
242
|
+
**挡不住的**:在本模块加载之前就用 Proxy 冒充并同步伪造 `name` / 源码,或者干脆接管整个 `window`
|
|
243
|
+
(同源 XSS)。同源脚本本来就能自己开 IndexedDB、读 localStorage,patch 只是省事,所以这里是
|
|
244
|
+
「提高门槛」,真正的边界是:私钥不可导出 + 签名绑路径/设备/时间/token + 网关 lockSign + 30 分钟 TTL。
|
|
245
|
+
|
|
246
|
+
> 要更硬的隔离只有换 realm:一次性 same-origin iframe(或 Worker)取 `contentWindow.fetch` /
|
|
247
|
+
> `contentWindow.indexedDB.open`,页面对主 realm 的 patch 影响不到它。代价:CSP 放行 `frame-src`、
|
|
248
|
+
> iframe 生命周期、跨 realm 传对象。默认没做。
|
|
249
|
+
|
|
250
|
+
浏览器自检页:`test/native-check.html`(起静态服务打开,逐项替换 → 看返回)。
|
|
251
|
+
|
|
252
|
+
## 部署要求
|
|
253
|
+
|
|
254
|
+
1. **必须 https 或 localhost**:`crypto.subtle` 在非安全上下文不存在,SDK 会直接抛错。
|
|
255
|
+
2. **登录页和应用必须同源**(同 scheme+host+port):
|
|
256
|
+
- `localStorage` / `IndexedDB` 不跨源,跨子域登录拿不到会话;
|
|
257
|
+
- 后端 `sso.domain` 取的是登录时的 Origin 主机名(不是可注册域),所以每个子域各自登录。
|
|
258
|
+
把登录页放在 `login.xxx.com`、应用在 `app.xxx.com`,会变成「登录成功 → 跳应用 → 没会话 → 回登录页」的死循环。
|
|
259
|
+
3. **网关 CORS 白名单**要包含前端的 origin(`dplugin.cors.allowed_origins`)。
|
|
260
|
+
4. **登录相关接口必须在 api 表登记**(网关 `AuthFilter` 查不到就 404)。
|
|
261
|
+
5. 反向代理不要改写 `/api/**` 的路径和 query —— `dsignn` 的签名原文包含路径。
|
|
262
|
+
|
|
263
|
+
## 与旧版的差异(迁移清单)
|
|
264
|
+
|
|
265
|
+
| 旧 | 新 |
|
|
266
|
+
|---|---|
|
|
267
|
+
| `request(api, {data, loading, noToken})` | `request(api, data, options)` |
|
|
268
|
+
| `auth.setAuthData(data)` | 不需要,`login()` 内部落库 |
|
|
269
|
+
| `auth.jump(token)` / `jumpGenerate()` | **删除**(后端 jump 接口已删) |
|
|
270
|
+
| `setup({publicKey})` | 不需要(改用 WebCrypto 每次生成) |
|
|
271
|
+
| `onError(data)` | `onError({code, message, error})`,签名兼容 |
|
|
272
|
+
| 页面里 `localStorage.token` | `store.access`(键名 `dplugin.access`) |
|
|
273
|
+
| `utils/device.js` 里的 `platform` 依赖 | SDK 内置 `deviceInfo()`,`login()` 自动带 |
|
|
274
|
+
|
|
275
|
+
## 构建
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
npm run build # index.mjs(ESM) + index.umd.js(UMD/DPluginCore) + index.cjs(CJS)
|
|
279
|
+
npm test # 流程自检(mock fetch,25 次请求 / 7 个场景)
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
零依赖:`axios` / `crypto-js` / `jsencrypt` 已全部去掉,UMD 产物 ~12KB(旧版 ~200KB)。
|
|
283
|
+
|
|
284
|
+
## 加密互通自检
|
|
285
|
+
|
|
286
|
+
后端验签用的是 JCA `SHA256withECDSA`(ASN.1 DER),而 WebCrypto 产出的是 P1363 原始 64 字节 —— 对不上时后端**不报错、只返回 false**,非常难查。改动签名相关代码后跑一次:
|
|
287
|
+
|
|
288
|
+
```bash
|
|
289
|
+
node test/interop.mjs # 输出 {pk, text, sign}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
浏览器侧还要单独验一次「私钥跨页面存活」(Node 没有 IndexedDB,测不到这段):
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
python3 -m http.server 8765 --bind 127.0.0.1 # 在本目录起静态服务
|
|
296
|
+
# 打开 http://127.0.0.1:8765/test/browser.html → 生成并写入 IndexedDB
|
|
297
|
+
# 开一个新标签页 .../test/browser.html#read → 从 IndexedDB 读回并签名
|
|
298
|
+
# 两边 pk 必须一致、extractable 必须为 false
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
然后:
|
|
302
|
+
|
|
303
|
+
```java
|
|
304
|
+
import com.dplugin.core.utils.EcdsaUtils;
|
|
305
|
+
|
|
306
|
+
public class EcdsaVerify {
|
|
307
|
+
public static void main(String[] a) {
|
|
308
|
+
System.out.println(EcdsaUtils.verify(a[0], a[1], a[2]) ? "VERIFY_OK" : "VERIFY_FAIL");
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
```bash
|
|
314
|
+
JAR=$(ls ~/.m2/repository/com/dplugin/dplugin-core/*/dplugin-core-*.jar | grep -v sources | tail -1)
|
|
315
|
+
javac -cp "$JAR" EcdsaVerify.java
|
|
316
|
+
java -cp "$JAR:." EcdsaVerify '<pk>' '<text>' '<sign>'
|
|
317
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const M={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"warn"};function Ae(e){return e&&Object.keys(M).forEach(t=>{e[t]!==void 0&&(M[t]=e[t])}),M}function R(){return M}const m=_e();function Z(e,t){if(typeof e!="function")return!1;let n;try{n=Function.prototype.toString.call(e)}catch(r){return!1}const o=/^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(n);return o?o[1]===t:/\[native code\]/.test(n)&&!/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(n)}function Ce(e){try{const t=e();return t&&typeof t.then=="function"&&Promise.resolve(t).catch(()=>{}),!1}catch(t){const n=String(t&&t.message||"");return!!t&&t.name==="TypeError"&&/argument|required|参数/i.test(n)}}function _e(){const e=globalThis,t=typeof e.window!="undefined"&&e.window&&typeof e.window.document!="undefined",n={fetch:null,idbOpen:null,subtle:null,pristine:{fetch:!1,idbOpen:!1,subtle:!1}};typeof e.fetch=="function"&&(n.pristine.fetch=Z(e.fetch,"fetch"),n.fetch=e.fetch.bind(e)),typeof e.indexedDB!="undefined"&&e.indexedDB&&typeof e.indexedDB.open=="function"&&(n.pristine.idbOpen=Z(e.indexedDB.open,"open")&&(!t||Ce(()=>e.indexedDB.open())),n.idbOpen=e.indexedDB.open.bind(e.indexedDB));const o=e.crypto&&e.crypto.subtle?e.crypto.subtle:null;if(o){const r=["generateKey","exportKey","importKey","sign","verify","digest"],s={};let u=!0;r.forEach(c=>{typeof o[c]=="function"&&(s[c]=o[c].bind(o),u=u&&Z(o[c],c))}),n.subtle=s,n.pristine.subtle=u}return n}function ke(){if(typeof window=="undefined"||typeof window.document=="undefined")return[];const e=[];return m.fetch&&!m.pristine.fetch&&e.push("fetch"),m.idbOpen&&!m.pristine.idbOpen&&e.push("indexedDB.open"),m.subtle&&!m.pristine.subtle&&e.push("crypto.subtle"),e}const a={OK:"200",PENDING:"202",AUTH_EXPIRED:"500900",FORBIDDEN:"401001",CORS_DENIED:"403",API_NOT_FOUND:"404",NETWORK:"NETWORK",TIMEOUT:"TIMEOUT"};class E extends Error{constructor(t,n){super(n||"请求失败"),this.name="BizError",this.code=t}}class O extends E{constructor(t){super(a.AUTH_EXPIRED,t||"登录已失效"),this.name="AuthError"}}function Me(e){return e instanceof O||e&&e.code===a.AUTH_EXPIRED}const $="dplugin.access",J="dplugin.sid",we="dplugin.ds",Q="dplugin.exp",ee="dplugin.cookie";function Ue(e){try{return localStorage.getItem(e)}catch(t){return null}}function Ee(e,t){try{t==null||t===""?localStorage.removeItem(e):localStorage.setItem(e,t)}catch(n){}}const H={};function C(e){const t=Ue(e);return t===null?H[e]===void 0?null:H[e]:t}function _(e,t){H[e]=t,Ee(e,t)}const i={get access(){return C($)},set access(e){_($,e)},get sid(){return C(J)},set sid(e){_(J,e)},get ds(){return C(we)},set ds(e){_(we,e)},get exp(){const e=C(Q);return e?Number(e):0},set exp(e){_(Q,e?String(e):null)},get cookie(){return C(ee)==="1"},set cookie(e){_(ee,e?"1":null)},clear(){[$,J,Q,ee].forEach(e=>{H[e]=null,Ee(e,null)})}};function g(e,t){const n=R().apiBase||"";let o=e;return/^https?:\/\//i.test(e)||(o=(n?n.replace(/\/+$/,""):"")+(e.startsWith("/")?e:"/"+e)),t&&(o+=(o.includes("?")?"&":"?")+t),o}function j(e){const t=g(e);if(/^https?:\/\//i.test(t))try{return new URL(t).pathname}catch(n){return t.split("?")[0]}try{return new URL(t,"http://localhost").pathname}catch(n){return t.split("?")[0]}}let b=null;function ve(e){try{const t=typeof location!="undefined"?location.href:void 0;return new URL(e||"",t).origin}catch(t){return null}}function Le(){b=null}function ie(){if(b)return b;const e=R();if(e.mode==="sign"||e.mode==="cookie")return b=e.mode,b;const t=typeof location!="undefined"?location.origin:null;if(!t)return b="sign",b;const n=ve(e.apiBase||t);return b=n&&n===t?"cookie":"sign",b}function S(){return ie()==="cookie"}async function h(e,t,n){const o=R(),r=n||o.timeout,s=typeof AbortController!="undefined"?new AbortController:null,u=s?setTimeout(()=>s.abort(),r):null,c={method:"POST",credentials:S()?"same-origin":"omit",signal:s?s.signal:void 0};if(typeof FormData!="undefined"&&t instanceof FormData?c.body=t:t!=null&&(c.headers={"Content-Type":"text/plain"},c.body=JSON.stringify(t)),!m.fetch)throw new E(a.NETWORK,"当前环境不支持 fetch");let f;try{f=await m.fetch(e,c)}catch(y){throw u&&clearTimeout(u),y&&y.name==="AbortError"?new E(a.TIMEOUT,"请求超时,请重试"):new E(a.NETWORK,"网络异常,请检查网络后重试")}u&&clearTimeout(u);const d=await f.text();let l=null;if(d)try{l=JSON.parse(d)}catch(y){l=null}if(!f.ok)throw new E(String(f.status),l&&l.message||d||"请求失败");if(!l||l.code===null||l.code===void 0)throw new E("500","响应格式异常");return l.code=String(l.code),l}const Fe="dplugin-auth",Be=1,Y="keys",se="sso",ce="domain",Ge={name:"ECDSA",namedCurve:"P-256"},He={name:"ECDSA",hash:"SHA-256"},B=new Map;let L=null;function X(){if(!m.subtle){const e=typeof location!="undefined"&&location.origin?location.origin:"(未知 origin)";throw new Error("[dplugin/core] crypto.subtle 不存在:浏览器只在**安全上下文**里提供它(https、http://localhost、http://127.0.0.1、http://[::1]、file://);当前 origin="+e)}return m.subtle}function Ye(){return L||(L=new Promise(e=>{if(!m.idbOpen){e(null);return}let t;try{t=m.idbOpen(Fe,Be)}catch(n){e(null);return}t.onupgradeneeded=()=>{const n=t.result;n.objectStoreNames.contains(Y)||n.createObjectStore(Y)},t.onsuccess=()=>e(t.result),t.onerror=()=>e(null),t.onblocked=()=>e(null)}),L)}function me(e,t){return Ye().then(n=>n?new Promise(o=>{let r;try{r=n.transaction(Y,e)}catch(c){o(null);return}const s=r.objectStore(Y);let u=null;try{const c=t(s);c&&(c.onsuccess=()=>{u=c.result},c.onerror=()=>{u=null})}catch(c){u=null}r.oncomplete=()=>o(u),r.onerror=()=>o(null),r.onabort=()=>o(null)}):null)}function Oe(e){return B.has(e)?Promise.resolve(B.get(e)):me("readonly",t=>t.get(e)).then(t=>(t&&B.set(e,t),t||null))}function ye(e,t){return B.set(e,t),me("readwrite",n=>n.put(t,e)).then(()=>t)}async function ae(e){const n=await X().generateKey(Ge,!1,["sign","verify"]);return ye(e,n)}async function Se(e){return Oe(e)}async function he(e,t){return t?ye(e,t):null}async function Ve(e){return await Oe(e)||ae(e)}function je(){return ae(se)}function Xe(){return ae(ce)}function ue(){return Se(se)}function be(){return Se(ce)}async function te(e){const n=await X().exportKey("spki",e.publicKey);let o="";const r=new Uint8Array(n);for(let s=0;s<r.length;s++)o+=String.fromCharCode(r[s]);return btoa(o)}function Ie(e){const t=new Uint8Array(e);let n="";for(let o=0;o<t.length;o++)n+=String.fromCharCode(t[o]);return btoa(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function We(e,t){const o=await X().sign(He,e.privateKey,new TextEncoder().encode(t));return Ie(o)}async function Te(e){const t=X(),n=await t.exportKey("spki",e.publicKey),o=await t.digest("SHA-256",n);return Ie(o)}const ne={SSO:se,DOMAIN:ce},oe="-",qe="POST",N={BIZ:"BIZ",VERIFY:"VERIFY",VERIFY_SID:"VERIFY_SID",TOKEN:"TOKEN"};function p(e){return e==null||e===""?oe:String(e)}function ze(e,t){const n=p(t.origin);return[p(e),p(t.method),p(t.path),n===oe?oe:n.toLowerCase(),p(t.ds),p(t.ts),p(t.subject),p(t.dna),p(t.redirect)].join(`
|
|
2
|
+
`)}function Ze(e,t,n){return[p(e),p(t.subject),p(t.ds),p(t.ts),n].join(".")}async function x(e,t,n){const o=n==="sso"?await ue():await be();if(!o)throw Pe(n==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");const r=await We(o,ze(e,t));return Ze(e,t,r)}async function fe(e,t,n){if(!t||!n)throw Pe("缺少 access 或 ds,无法签名");const o=await x(N.BIZ,{method:qe,path:j(e),origin:K(),ds:n,ts:Date.now(),subject:t},"domain");return"dsign="+encodeURIComponent(o)}function K(){if(typeof location=="undefined")return"";if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(e){return""}}function Pe(e){const t=new Error(e);return t.code="KEY_MISSING",t}function $e(){const e=typeof navigator!="undefined"&&navigator.userAgent||"",t={device_brand:Je(e),device_model:Qe(e),browser_name:et(e),browser_version:tt(e),os_name:nt(e),os_version:ot(e)},n=i.ds;return n&&(t.device_id=n),t}function I(e,t){const n=e.match(t);return n?n[1]:""}function Je(e){return/iPhone|iPad|iPod/.test(e)?"Apple":/Huawei|HONOR/i.test(e)?"Huawei":/Xiaomi|Redmi|POCO/i.test(e)?"Xiaomi":/OPPO|realme/i.test(e)?"OPPO":/vivo/i.test(e)?"vivo":/Samsung|SM-|SAMSUNG/i.test(e)?"Samsung":/Macintosh|Mac OS X/.test(e)?"Apple":""}function Qe(e){if(/iPhone/.test(e))return"iPhone";if(/iPad/.test(e))return"iPad";const t=e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return t?t[1].trim():/Macintosh/.test(e)?"Mac":""}function et(e){return/MicroMessenger/i.test(e)?"WeChat":/Edg[A-Za-z]*\//.test(e)?"Edge":/OPR\/|Opera/.test(e)?"Opera":/Firefox\//.test(e)?"Firefox":/Chrome\/|CriOS\//.test(e)?"Chrome":/Safari\//.test(e)?"Safari":""}function tt(e){return/MicroMessenger\/([\d.]+)/.test(e)?I(e,/MicroMessenger\/([\d.]+)/):/Edg[A-Za-z]*\/([\d.]+)/.test(e)?I(e,/Edg[A-Za-z]*\/([\d.]+)/):/OPR\/([\d.]+)/.test(e)?I(e,/OPR\/([\d.]+)/):/Firefox\/([\d.]+)/.test(e)?I(e,/Firefox\/([\d.]+)/):/CriOS\/([\d.]+)/.test(e)?I(e,/CriOS\/([\d.]+)/):/Chrome\/([\d.]+)/.test(e)?I(e,/Chrome\/([\d.]+)/):/Version\/([\d.]+).*Safari/.test(e)?I(e,/Version\/([\d.]+)/):""}function nt(e){return/Windows NT/.test(e)?"Windows":/iPhone|iPad|iPod/.test(e)?"iOS":/Android/.test(e)?"Android":/Mac OS X|Macintosh/.test(e)?"macOS":/HarmonyOS/.test(e)?"HarmonyOS":/Linux/.test(e)?"Linux":""}function ot(e){if(/Windows NT 10\.0/.test(e))return"10/11";if(/Windows NT 6\.3/.test(e))return"8.1";if(/Windows NT 6\.1/.test(e))return"7";const t=e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(t)return t[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(e))return I(e,/Android[ _]([\d.]+)/);const n=e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return n?n[1].replace(/_/g,"."):""}const w={SIGN:{VERIFY:"/api/unsafe/dplugin/login/s/verify",TOKEN:"/api/unsafe/dplugin/login/s/token",REFRESH:"/api/unsafe/dplugin/login/s/refresh"},COOKIE:{VERIFY:"/api/unsafe/dplugin/login/c/verify",TOKEN:"/api/unsafe/dplugin/login/c/token",REFRESH:"/api/unsafe/dplugin/login/c/refresh"},LOGOUT:"/api/safe/dplugin/login/logout"},rt="dp_dvt",de=1800*1e3;let k=null,F=null,D=null,V=!1;function W(){return typeof location!="undefined"?location.href:""}function U(e){return new Promise(t=>setTimeout(t,e))}function q(){const e=R();typeof e.onLogin=="function"&&e.onLogin(v())}function T(e,t){return new E(e.code,e.message||t)}function it(e,t,n){if(typeof document=="undefined")return;let o=e+"="+encodeURIComponent(t)+"; path=/; SameSite=Lax";o+="; max-age="+n,document.cookie=o}function v(){return{mode:ie(),loggedIn:z(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function z(){return S()?!!i.cookie:!!(i.access||i.sid)}function le(){i.clear()}function P(e){const t=z();le();const n=R();if(t&&typeof n.onLogout=="function"&&n.onLogout(e||"expired"),V||!n.enableAuth||!n.loginUrl||typeof location=="undefined")return;V=!0;const o=n.loginUrl.includes("?")?"&":"?";location.href=n.loginUrl+o+"referer="+encodeURIComponent(location.href)}function Re(e,t){return D||(D=st(e,t).then(n=>(D=null,n),n=>{throw D=null,n}),D)}async function st(e,t){const n=R(),o=$e();if(S())return ut(e,t,o,n);const r=await ue(),s=await be();try{return await ct(e,t,o,n)}catch(u){throw await he(ne.SSO,r),await he(ne.DOMAIN,s),u}}async function ct(e,t,n,o){const r=await je(),s=await te(r),u=await h(g(e),Object.assign({},t,{device:n,pk:s}));if(u.code!==a.OK)throw T(u,"登录失败");const c=u.data&&u.data.token;if(!c)throw T(u,"登录失败");const f=await Xe(),d=await Te(f),l=await at(c,o,d),y=await h(g(w.SIGN.TOKEN),{dsign:await x(N.TOKEN,{origin:K(),ts:Date.now(),subject:l.code,dna:d},"domain"),pk:await te(f)});if(y.code!==a.OK||!y.data)throw T(y,"登录失败");return i.sid=l.sid,i.ds=y.data.ds||l.ds,i.access=y.data.token,i.exp=Date.now()+de,V=!1,q(),v()}async function at(e,t,n){const o=W(),r=Math.max(1,Number(t.pollMax)||5);for(let s=1;s<=r;s++){const u=i.ds||null,c=Date.now(),f=await x(N.VERIFY,{method:"POST",path:j(w.SIGN.VERIFY),origin:K(),ds:u,ts:c,subject:e,dna:n,redirect:o},"sso");let d;try{d=await h(g(w.SIGN.VERIFY),{dsign:f,dna:n,redirect:o})}catch(l){if((l.code===a.NETWORK||l.code===a.TIMEOUT)&&s<r){await U(t.pollInterval);continue}throw l}if(d.code===a.OK&&d.data)return d.data;if(d.code===a.PENDING){if(s<r){await U(t.pollInterval);continue}break}throw T(d,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function ut(e,t,n,o){const r=await h(g(e),Object.assign({},t,{device:n}));if(r.code!==a.OK)throw T(r,"登录失败");const s=r.data&&r.data.token;if(!s)throw T(r,"登录失败");return it(rt,s,300),await ft(o),await De(),i.cookie=!0,V=!1,q(),v()}async function ft(e){const t=W(),n=Math.max(1,Number(e.pollMax)||5);for(let o=1;o<=n;o++){let r;try{r=await h(g(w.COOKIE.VERIFY),{redirect:t})}catch(s){if((s.code===a.NETWORK||s.code===a.TIMEOUT)&&o<n){await U(e.pollInterval);continue}throw s}if(r.code===a.OK)return r.data;if(r.code===a.PENDING){if(o<n){await U(e.pollInterval);continue}break}throw T(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function De(){const e=await h(g(w.COOKIE.TOKEN));if(e.code!==a.OK)throw T(e,"登录失败");return e}async function A(e){const t=!!(e&&e.force);return F||(F=(async()=>{try{return S()?await dt(t):await gt(t)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{F=null}})()),F}async function dt(e){return!e&&i.cookie?!0:(await h(g(w.COOKIE.VERIFY),{redirect:W()})).code!==a.OK||(await De()).code!==a.OK?(i.cookie=!1,!1):(i.cookie=!0,q(),!0)}async function lt(){if(!i.sid||!i.ds||!await ue())return!1;const t=i.ds,n=await Ve(ne.DOMAIN),o=await Te(n),r=W(),s=await x(N.VERIFY_SID,{method:"POST",path:j(w.SIGN.VERIFY),origin:K(),ds:t,ts:Date.now(),subject:i.sid,dna:o,redirect:r},"sso"),u=await h(g(w.SIGN.VERIFY),{dsign:s,dna:o,redirect:r});if(u.code!==a.OK||!u.data)return!1;const c=await h(g(w.SIGN.TOKEN),{dsign:await x(N.TOKEN,{origin:K(),ts:Date.now(),subject:u.data.code,dna:o},"domain"),pk:await te(n)});return c.code!==a.OK||!c.data?!1:(i.access=c.data.token,c.data.ds&&(i.ds=c.data.ds),i.exp=Date.now()+de,q(),!0)}async function gt(e){return!e&&i.access?!0:lt()}function ge(){return k||(k=(S()?wt():ht()).then(e=>(k=null,e),e=>{throw k=null,e})),k}async function wt(){const e=await h(g(w.COOKIE.REFRESH));if(e.code===a.OK)return!0;if(e.code===a.AUTH_EXPIRED)throw new O(e.message);if(e.code==="500"&&/重复/.test(e.message||""))return await U(800),!0;throw new E(e.code,e.message)}async function ht(){const e=i.access,t=i.ds;if(!e||!t)throw new O;const n=await x(N.BIZ,{method:"POST",path:j(w.SIGN.REFRESH),origin:K(),ds:t,ts:Date.now(),subject:e},"domain"),o=await h(g(w.SIGN.REFRESH,"dsign="+encodeURIComponent(n)));if(o.code===a.OK&&o.data)return i.access=o.data.token,o.data.ds&&(i.ds=o.data.ds),i.exp=Date.now()+de,i.access;const r=await pt(e,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new O(o.message):new E(o.code,o.message)}function pt(e,t){const n=Date.now()+t;return new Promise(o=>{const r=()=>{const s=i.access;if(s&&s!==e){o(s);return}if(Date.now()>=n){o(null);return}setTimeout(r,150)};r()})}async function Ne(){try{let t=await pe();t.code===a.AUTH_EXPIRED&&(await ge(),t=await pe())}catch(t){}le();const e=R();typeof e.onLogout=="function"&&e.onLogout("logout")}async function pe(){if(S())return h(g(w.LOGOUT));const e=await fe(w.LOGOUT,i.access,i.ds);return h(g(w.LOGOUT,e))}async function Et(e,t,n){const o=n||{};return o.auth!==!1&&await xe(),G(e,t||{},o,!1)}async function xe(){if(S()){if(i.cookie||await A())return;throw P("expired"),new O}if(!i.access&&!await A())throw P("expired"),new O}async function G(e,t,n,o){const r=n.auth!==!1,s=r&&!S();let u="",c=null;if(s){c=i.access;try{u=await fe(e,c,i.ds)}catch(d){if(!o&&await A({force:!0}))return G(e,t,n,!0);throw P("expired"),new O}}const f=await h(g(e,u),t,n.timeout);if(f.code===a.OK)return f.data;if(r&&f.code===a.AUTH_EXPIRED&&!o){if(s&&c&&i.access!==c)return G(e,t,n,!0);try{await ge()}catch(d){throw d instanceof O&&P("expired"),d}return G(e,t,n,!0)}throw r&&f.code===a.AUTH_EXPIRED?(P("expired"),new O(f.message)):Ke(f)}function Ke(e){const t=new E(e.code,e.message),n=R();return n.onError&&n.onError({code:e.code,message:e.message,error:t}),t}async function mt(e,t,n,o){return(S()?!i.cookie:!i.access)&&await xe(),re(e,t,n,o||{},!1)}async function re(e,t,n,o,r){let s="";if(!S())try{s=await fe(e,i.access,i.ds)}catch(f){if(!r&&await A({force:!0}))return re(e,t,n,o,!0);throw P("expired"),new O}const u=new FormData;Object.keys(n||{}).forEach(f=>{const d=n[f];d!=null&&u.append(f,d)}),u.append("file",t);const c=await h(g(e,s),u,o.timeout);if(c.code===a.OK)return c.data;if(c.code===a.AUTH_EXPIRED&&!r){try{await ge()}catch(f){throw f instanceof O&&P("expired"),f}return re(e,t,n,o,!0)}throw Ke(c)}function Ot(e){const t=Ae(e);if(Le(),t.nativeGuard!=="off"){const n=ke();if(n.length){const o="[dplugin/core] 这些原生能力在模块加载前已被替换:"+n.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(t.nativeGuard==="error")throw new Error(o);typeof console!="undefined"&&console.warn(o)}}}const yt={login:Re,logout:Ne,isLogin:z,getState:v,restore:A};exports.AuthError=O;exports.BizError=E;exports.CODE=a;exports.auth=yt;exports.clear=le;exports.expire=P;exports.getMode=ie;exports.getState=v;exports.isAuthError=Me;exports.isLogin=z;exports.login=Re;exports.logout=Ne;exports.request=Et;exports.setup=Ot;exports.store=i;exports.upload=mt;exports.verifyLogin=A;
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,861 @@
|
|
|
1
|
+
const k = {
|
|
2
|
+
// 接口前缀。'' = 同源;也可以是 https://api.company.com
|
|
3
|
+
apiBase: "",
|
|
4
|
+
// 运行模式:'auto' 按同源自动判定;'cookie' 强制同域 Cookie;'sign' 强制跨域签名
|
|
5
|
+
mode: "auto",
|
|
6
|
+
// 登录页地址。会话彻底失效且 enableAuth = true 时跳这里
|
|
7
|
+
loginUrl: "/login/",
|
|
8
|
+
// 请求超时(毫秒)
|
|
9
|
+
timeout: 15e3,
|
|
10
|
+
// 登录轮询:间隔(毫秒)× 次数。次数用完还没成功就报「登录超时」
|
|
11
|
+
pollInterval: 2e3,
|
|
12
|
+
pollMax: 5,
|
|
13
|
+
// 会话失效时是否由 SDK 跳转到 loginUrl。
|
|
14
|
+
// 默认 false:只清本地 + 回调 onLogout + 抛 AuthError,跳转/提示由业务自己决定
|
|
15
|
+
enableAuth: !1,
|
|
16
|
+
// 业务错误统一提示(可选):({code, message, error}) => void
|
|
17
|
+
onError: null,
|
|
18
|
+
// 登录成功 / 静默恢复成功(可选)
|
|
19
|
+
onLogin: null,
|
|
20
|
+
// 登出或会话失效(可选),(reason) => void
|
|
21
|
+
onLogout: null,
|
|
22
|
+
// 原生能力体检:'warn' 只告警 / 'error' 直接抛错 / 'off' 不检查
|
|
23
|
+
nativeGuard: "warn"
|
|
24
|
+
};
|
|
25
|
+
function xe(e) {
|
|
26
|
+
return e && Object.keys(k).forEach((t) => {
|
|
27
|
+
e[t] !== void 0 && (k[t] = e[t]);
|
|
28
|
+
}), k;
|
|
29
|
+
}
|
|
30
|
+
function P() {
|
|
31
|
+
return k;
|
|
32
|
+
}
|
|
33
|
+
const E = Ae();
|
|
34
|
+
function $(e, t) {
|
|
35
|
+
if (typeof e != "function")
|
|
36
|
+
return !1;
|
|
37
|
+
let n;
|
|
38
|
+
try {
|
|
39
|
+
n = Function.prototype.toString.call(e);
|
|
40
|
+
} catch (r) {
|
|
41
|
+
return !1;
|
|
42
|
+
}
|
|
43
|
+
const o = /^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(n);
|
|
44
|
+
return o ? o[1] === t : /\[native code\]/.test(n) && !/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(n);
|
|
45
|
+
}
|
|
46
|
+
function Ke(e) {
|
|
47
|
+
try {
|
|
48
|
+
const t = e();
|
|
49
|
+
return t && typeof t.then == "function" && Promise.resolve(t).catch(() => {
|
|
50
|
+
}), !1;
|
|
51
|
+
} catch (t) {
|
|
52
|
+
const n = String(t && t.message || "");
|
|
53
|
+
return !!t && t.name === "TypeError" && /argument|required|参数/i.test(n);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function Ae() {
|
|
57
|
+
const e = globalThis, t = typeof e.window != "undefined" && e.window && typeof e.window.document != "undefined", n = {
|
|
58
|
+
fetch: null,
|
|
59
|
+
idbOpen: null,
|
|
60
|
+
subtle: null,
|
|
61
|
+
pristine: { fetch: !1, idbOpen: !1, subtle: !1 }
|
|
62
|
+
};
|
|
63
|
+
typeof e.fetch == "function" && (n.pristine.fetch = $(e.fetch, "fetch"), n.fetch = e.fetch.bind(e)), typeof e.indexedDB != "undefined" && e.indexedDB && typeof e.indexedDB.open == "function" && (n.pristine.idbOpen = $(e.indexedDB.open, "open") && (!t || Ke(() => e.indexedDB.open())), n.idbOpen = e.indexedDB.open.bind(e.indexedDB));
|
|
64
|
+
const o = e.crypto && e.crypto.subtle ? e.crypto.subtle : null;
|
|
65
|
+
if (o) {
|
|
66
|
+
const r = ["generateKey", "exportKey", "importKey", "sign", "verify", "digest"], s = {};
|
|
67
|
+
let a = !0;
|
|
68
|
+
r.forEach((c) => {
|
|
69
|
+
typeof o[c] == "function" && (s[c] = o[c].bind(o), a = a && $(o[c], c));
|
|
70
|
+
}), n.subtle = s, n.pristine.subtle = a;
|
|
71
|
+
}
|
|
72
|
+
return n;
|
|
73
|
+
}
|
|
74
|
+
function Ce() {
|
|
75
|
+
if (typeof window == "undefined" || typeof window.document == "undefined")
|
|
76
|
+
return [];
|
|
77
|
+
const e = [];
|
|
78
|
+
return E.fetch && !E.pristine.fetch && e.push("fetch"), E.idbOpen && !E.pristine.idbOpen && e.push("indexedDB.open"), E.subtle && !E.pristine.subtle && e.push("crypto.subtle"), e;
|
|
79
|
+
}
|
|
80
|
+
const u = {
|
|
81
|
+
OK: "200",
|
|
82
|
+
PENDING: "202",
|
|
83
|
+
AUTH_EXPIRED: "500900",
|
|
84
|
+
FORBIDDEN: "401001",
|
|
85
|
+
CORS_DENIED: "403",
|
|
86
|
+
API_NOT_FOUND: "404",
|
|
87
|
+
NETWORK: "NETWORK",
|
|
88
|
+
TIMEOUT: "TIMEOUT"
|
|
89
|
+
};
|
|
90
|
+
class m extends Error {
|
|
91
|
+
constructor(t, n) {
|
|
92
|
+
super(n || "请求失败"), this.name = "BizError", this.code = t;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
class O extends m {
|
|
96
|
+
constructor(t) {
|
|
97
|
+
super(u.AUTH_EXPIRED, t || "登录已失效"), this.name = "AuthError";
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function pt(e) {
|
|
101
|
+
return e instanceof O || e && e.code === u.AUTH_EXPIRED;
|
|
102
|
+
}
|
|
103
|
+
const q = "dplugin.access", z = "dplugin.sid", le = "dplugin.ds", J = "dplugin.exp", Q = "dplugin.cookie";
|
|
104
|
+
function _e(e) {
|
|
105
|
+
try {
|
|
106
|
+
return localStorage.getItem(e);
|
|
107
|
+
} catch (t) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function he(e, t) {
|
|
112
|
+
try {
|
|
113
|
+
t == null || t === "" ? localStorage.removeItem(e) : localStorage.setItem(e, t);
|
|
114
|
+
} catch (n) {
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const H = {};
|
|
118
|
+
function A(e) {
|
|
119
|
+
const t = _e(e);
|
|
120
|
+
return t === null ? H[e] === void 0 ? null : H[e] : t;
|
|
121
|
+
}
|
|
122
|
+
function C(e, t) {
|
|
123
|
+
H[e] = t, he(e, t);
|
|
124
|
+
}
|
|
125
|
+
const i = {
|
|
126
|
+
get access() {
|
|
127
|
+
return A(q);
|
|
128
|
+
},
|
|
129
|
+
set access(e) {
|
|
130
|
+
C(q, e);
|
|
131
|
+
},
|
|
132
|
+
get sid() {
|
|
133
|
+
return A(z);
|
|
134
|
+
},
|
|
135
|
+
set sid(e) {
|
|
136
|
+
C(z, e);
|
|
137
|
+
},
|
|
138
|
+
get ds() {
|
|
139
|
+
return A(le);
|
|
140
|
+
},
|
|
141
|
+
set ds(e) {
|
|
142
|
+
C(le, e);
|
|
143
|
+
},
|
|
144
|
+
/** access 的到期时间(毫秒时间戳)。仅用于界面判断,不要拿它做提前续期 */
|
|
145
|
+
get exp() {
|
|
146
|
+
const e = A(J);
|
|
147
|
+
return e ? Number(e) : 0;
|
|
148
|
+
},
|
|
149
|
+
set exp(e) {
|
|
150
|
+
C(J, e ? String(e) : null);
|
|
151
|
+
},
|
|
152
|
+
/** cookie 模式:本地只记「可能已登录」,实际有效性由第一次请求的结果决定 */
|
|
153
|
+
get cookie() {
|
|
154
|
+
return A(Q) === "1";
|
|
155
|
+
},
|
|
156
|
+
set cookie(e) {
|
|
157
|
+
C(Q, e ? "1" : null);
|
|
158
|
+
},
|
|
159
|
+
/**
|
|
160
|
+
* 清登录态。
|
|
161
|
+
*
|
|
162
|
+
* ⚠️ 故意不清 ds:设备凭证代表"这台设备",不是会话。清掉的话用户每退出登录一次,
|
|
163
|
+
* 下次登录后端就会新建一条设备记录(common_login_device 无限膨胀)。
|
|
164
|
+
*/
|
|
165
|
+
clear() {
|
|
166
|
+
[q, z, J, Q].forEach((e) => {
|
|
167
|
+
H[e] = null, he(e, null);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
function w(e, t) {
|
|
172
|
+
const n = P().apiBase || "";
|
|
173
|
+
let o = e;
|
|
174
|
+
return /^https?:\/\//i.test(e) || (o = (n ? n.replace(/\/+$/, "") : "") + (e.startsWith("/") ? e : "/" + e)), t && (o += (o.includes("?") ? "&" : "?") + t), o;
|
|
175
|
+
}
|
|
176
|
+
function V(e) {
|
|
177
|
+
const t = w(e);
|
|
178
|
+
if (/^https?:\/\//i.test(t))
|
|
179
|
+
try {
|
|
180
|
+
return new URL(t).pathname;
|
|
181
|
+
} catch (n) {
|
|
182
|
+
return t.split("?")[0];
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
return new URL(t, "http://localhost").pathname;
|
|
186
|
+
} catch (n) {
|
|
187
|
+
return t.split("?")[0];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
let b = null;
|
|
191
|
+
function ke(e) {
|
|
192
|
+
try {
|
|
193
|
+
const t = typeof location != "undefined" ? location.href : void 0;
|
|
194
|
+
return new URL(e || "", t).origin;
|
|
195
|
+
} catch (t) {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function Ue() {
|
|
200
|
+
b = null;
|
|
201
|
+
}
|
|
202
|
+
function pe() {
|
|
203
|
+
if (b)
|
|
204
|
+
return b;
|
|
205
|
+
const e = P();
|
|
206
|
+
if (e.mode === "sign" || e.mode === "cookie")
|
|
207
|
+
return b = e.mode, b;
|
|
208
|
+
const t = typeof location != "undefined" ? location.origin : null;
|
|
209
|
+
if (!t)
|
|
210
|
+
return b = "sign", b;
|
|
211
|
+
const n = ke(e.apiBase || t);
|
|
212
|
+
return b = n && n === t ? "cookie" : "sign", b;
|
|
213
|
+
}
|
|
214
|
+
function S() {
|
|
215
|
+
return pe() === "cookie";
|
|
216
|
+
}
|
|
217
|
+
async function h(e, t, n) {
|
|
218
|
+
const o = P(), r = n || o.timeout, s = typeof AbortController != "undefined" ? new AbortController() : null, a = s ? setTimeout(() => s.abort(), r) : null, c = {
|
|
219
|
+
method: "POST",
|
|
220
|
+
// cookie 模式必须带上 Cookie(同源不涉及 CORS);sign 模式主动不带,减少暴露面
|
|
221
|
+
credentials: S() ? "same-origin" : "omit",
|
|
222
|
+
signal: s ? s.signal : void 0
|
|
223
|
+
};
|
|
224
|
+
if (typeof FormData != "undefined" && t instanceof FormData ? c.body = t : t != null && (c.headers = { "Content-Type": "text/plain" }, c.body = JSON.stringify(t)), !E.fetch)
|
|
225
|
+
throw new m(u.NETWORK, "当前环境不支持 fetch");
|
|
226
|
+
let f;
|
|
227
|
+
try {
|
|
228
|
+
f = await E.fetch(e, c);
|
|
229
|
+
} catch (y) {
|
|
230
|
+
throw a && clearTimeout(a), y && y.name === "AbortError" ? new m(u.TIMEOUT, "请求超时,请重试") : new m(u.NETWORK, "网络异常,请检查网络后重试");
|
|
231
|
+
}
|
|
232
|
+
a && clearTimeout(a);
|
|
233
|
+
const d = await f.text();
|
|
234
|
+
let l = null;
|
|
235
|
+
if (d)
|
|
236
|
+
try {
|
|
237
|
+
l = JSON.parse(d);
|
|
238
|
+
} catch (y) {
|
|
239
|
+
l = null;
|
|
240
|
+
}
|
|
241
|
+
if (!f.ok)
|
|
242
|
+
throw new m(String(f.status), l && l.message || d || "请求失败");
|
|
243
|
+
if (!l || l.code === null || l.code === void 0)
|
|
244
|
+
throw new m("500", "响应格式异常");
|
|
245
|
+
return l.code = String(l.code), l;
|
|
246
|
+
}
|
|
247
|
+
const Me = "dplugin-auth", ve = 1, Y = "keys", re = "sso", ie = "domain", Le = { name: "ECDSA", namedCurve: "P-256" }, Fe = { name: "ECDSA", hash: "SHA-256" }, F = /* @__PURE__ */ new Map();
|
|
248
|
+
let v = null;
|
|
249
|
+
function j() {
|
|
250
|
+
if (!E.subtle) {
|
|
251
|
+
const e = typeof location != "undefined" && location.origin ? location.origin : "(未知 origin)";
|
|
252
|
+
throw new Error("[dplugin/core] crypto.subtle 不存在:浏览器只在**安全上下文**里提供它(https、http://localhost、http://127.0.0.1、http://[::1]、file://);当前 origin=" + e);
|
|
253
|
+
}
|
|
254
|
+
return E.subtle;
|
|
255
|
+
}
|
|
256
|
+
function Ge() {
|
|
257
|
+
return v || (v = new Promise((e) => {
|
|
258
|
+
if (!E.idbOpen) {
|
|
259
|
+
e(null);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
let t;
|
|
263
|
+
try {
|
|
264
|
+
t = E.idbOpen(Me, ve);
|
|
265
|
+
} catch (n) {
|
|
266
|
+
e(null);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
t.onupgradeneeded = () => {
|
|
270
|
+
const n = t.result;
|
|
271
|
+
n.objectStoreNames.contains(Y) || n.createObjectStore(Y);
|
|
272
|
+
}, t.onsuccess = () => e(t.result), t.onerror = () => e(null), t.onblocked = () => e(null);
|
|
273
|
+
}), v);
|
|
274
|
+
}
|
|
275
|
+
function Ee(e, t) {
|
|
276
|
+
return Ge().then((n) => n ? new Promise((o) => {
|
|
277
|
+
let r;
|
|
278
|
+
try {
|
|
279
|
+
r = n.transaction(Y, e);
|
|
280
|
+
} catch (c) {
|
|
281
|
+
o(null);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const s = r.objectStore(Y);
|
|
285
|
+
let a = null;
|
|
286
|
+
try {
|
|
287
|
+
const c = t(s);
|
|
288
|
+
c && (c.onsuccess = () => {
|
|
289
|
+
a = c.result;
|
|
290
|
+
}, c.onerror = () => {
|
|
291
|
+
a = null;
|
|
292
|
+
});
|
|
293
|
+
} catch (c) {
|
|
294
|
+
a = null;
|
|
295
|
+
}
|
|
296
|
+
r.oncomplete = () => o(a), r.onerror = () => o(null), r.onabort = () => o(null);
|
|
297
|
+
}) : null);
|
|
298
|
+
}
|
|
299
|
+
function me(e) {
|
|
300
|
+
return F.has(e) ? Promise.resolve(F.get(e)) : Ee("readonly", (t) => t.get(e)).then((t) => (t && F.set(e, t), t || null));
|
|
301
|
+
}
|
|
302
|
+
function Oe(e, t) {
|
|
303
|
+
return F.set(e, t), Ee("readwrite", (n) => n.put(t, e)).then(() => t);
|
|
304
|
+
}
|
|
305
|
+
async function se(e) {
|
|
306
|
+
const n = await j().generateKey(Le, !1, ["sign", "verify"]);
|
|
307
|
+
return Oe(e, n);
|
|
308
|
+
}
|
|
309
|
+
async function ye(e) {
|
|
310
|
+
return me(e);
|
|
311
|
+
}
|
|
312
|
+
async function we(e, t) {
|
|
313
|
+
return t ? Oe(e, t) : null;
|
|
314
|
+
}
|
|
315
|
+
async function He(e) {
|
|
316
|
+
return await me(e) || se(e);
|
|
317
|
+
}
|
|
318
|
+
function Ye() {
|
|
319
|
+
return se(re);
|
|
320
|
+
}
|
|
321
|
+
function Be() {
|
|
322
|
+
return se(ie);
|
|
323
|
+
}
|
|
324
|
+
function ce() {
|
|
325
|
+
return ye(re);
|
|
326
|
+
}
|
|
327
|
+
function Se() {
|
|
328
|
+
return ye(ie);
|
|
329
|
+
}
|
|
330
|
+
async function ee(e) {
|
|
331
|
+
const n = await j().exportKey("spki", e.publicKey);
|
|
332
|
+
let o = "";
|
|
333
|
+
const r = new Uint8Array(n);
|
|
334
|
+
for (let s = 0; s < r.length; s++)
|
|
335
|
+
o += String.fromCharCode(r[s]);
|
|
336
|
+
return btoa(o);
|
|
337
|
+
}
|
|
338
|
+
function be(e) {
|
|
339
|
+
const t = new Uint8Array(e);
|
|
340
|
+
let n = "";
|
|
341
|
+
for (let o = 0; o < t.length; o++)
|
|
342
|
+
n += String.fromCharCode(t[o]);
|
|
343
|
+
return btoa(n).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
344
|
+
}
|
|
345
|
+
async function Ve(e, t) {
|
|
346
|
+
const o = await j().sign(Fe, e.privateKey, new TextEncoder().encode(t));
|
|
347
|
+
return be(o);
|
|
348
|
+
}
|
|
349
|
+
async function Ie(e) {
|
|
350
|
+
const t = j(), n = await t.exportKey("spki", e.publicKey), o = await t.digest("SHA-256", n);
|
|
351
|
+
return be(o);
|
|
352
|
+
}
|
|
353
|
+
const te = {
|
|
354
|
+
SSO: re,
|
|
355
|
+
DOMAIN: ie
|
|
356
|
+
}, ne = "-", je = "POST", N = {
|
|
357
|
+
BIZ: "BIZ",
|
|
358
|
+
VERIFY: "VERIFY",
|
|
359
|
+
VERIFY_SID: "VERIFY_SID",
|
|
360
|
+
TOKEN: "TOKEN"
|
|
361
|
+
};
|
|
362
|
+
function p(e) {
|
|
363
|
+
return e == null || e === "" ? ne : String(e);
|
|
364
|
+
}
|
|
365
|
+
function Xe(e, t) {
|
|
366
|
+
const n = p(t.origin);
|
|
367
|
+
return [
|
|
368
|
+
p(e),
|
|
369
|
+
p(t.method),
|
|
370
|
+
p(t.path),
|
|
371
|
+
n === ne ? ne : n.toLowerCase(),
|
|
372
|
+
p(t.ds),
|
|
373
|
+
p(t.ts),
|
|
374
|
+
p(t.subject),
|
|
375
|
+
p(t.dna),
|
|
376
|
+
p(t.redirect)
|
|
377
|
+
].join(`
|
|
378
|
+
`);
|
|
379
|
+
}
|
|
380
|
+
function We(e, t, n) {
|
|
381
|
+
return [p(e), p(t.subject), p(t.ds), p(t.ts), n].join(".");
|
|
382
|
+
}
|
|
383
|
+
async function x(e, t, n) {
|
|
384
|
+
const o = n === "sso" ? await ce() : await Se();
|
|
385
|
+
if (!o)
|
|
386
|
+
throw Te(n === "sso" ? "缺少登录签名密钥" : "缺少业务签名密钥");
|
|
387
|
+
const r = await Ve(o, Xe(e, t));
|
|
388
|
+
return We(e, t, r);
|
|
389
|
+
}
|
|
390
|
+
async function ae(e, t, n) {
|
|
391
|
+
if (!t || !n)
|
|
392
|
+
throw Te("缺少 access 或 ds,无法签名");
|
|
393
|
+
const o = await x(N.BIZ, {
|
|
394
|
+
method: je,
|
|
395
|
+
path: V(e),
|
|
396
|
+
origin: K(),
|
|
397
|
+
ds: n,
|
|
398
|
+
ts: Date.now(),
|
|
399
|
+
subject: t
|
|
400
|
+
}, "domain");
|
|
401
|
+
return "dsign=" + encodeURIComponent(o);
|
|
402
|
+
}
|
|
403
|
+
function K() {
|
|
404
|
+
if (typeof location == "undefined")
|
|
405
|
+
return "";
|
|
406
|
+
if (location.origin)
|
|
407
|
+
return location.origin.toLowerCase();
|
|
408
|
+
try {
|
|
409
|
+
return new URL(location.href).origin.toLowerCase();
|
|
410
|
+
} catch (e) {
|
|
411
|
+
return "";
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
function Te(e) {
|
|
415
|
+
const t = new Error(e);
|
|
416
|
+
return t.code = "KEY_MISSING", t;
|
|
417
|
+
}
|
|
418
|
+
function Ze() {
|
|
419
|
+
const e = typeof navigator != "undefined" && navigator.userAgent || "", t = {
|
|
420
|
+
device_brand: $e(e),
|
|
421
|
+
device_model: qe(e),
|
|
422
|
+
browser_name: ze(e),
|
|
423
|
+
browser_version: Je(e),
|
|
424
|
+
os_name: Qe(e),
|
|
425
|
+
os_version: et(e)
|
|
426
|
+
}, n = i.ds;
|
|
427
|
+
return n && (t.device_id = n), t;
|
|
428
|
+
}
|
|
429
|
+
function I(e, t) {
|
|
430
|
+
const n = e.match(t);
|
|
431
|
+
return n ? n[1] : "";
|
|
432
|
+
}
|
|
433
|
+
function $e(e) {
|
|
434
|
+
return /iPhone|iPad|iPod/.test(e) ? "Apple" : /Huawei|HONOR/i.test(e) ? "Huawei" : /Xiaomi|Redmi|POCO/i.test(e) ? "Xiaomi" : /OPPO|realme/i.test(e) ? "OPPO" : /vivo/i.test(e) ? "vivo" : /Samsung|SM-|SAMSUNG/i.test(e) ? "Samsung" : /Macintosh|Mac OS X/.test(e) ? "Apple" : "";
|
|
435
|
+
}
|
|
436
|
+
function qe(e) {
|
|
437
|
+
if (/iPhone/.test(e)) return "iPhone";
|
|
438
|
+
if (/iPad/.test(e)) return "iPad";
|
|
439
|
+
const t = e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);
|
|
440
|
+
return t ? t[1].trim() : /Macintosh/.test(e) ? "Mac" : "";
|
|
441
|
+
}
|
|
442
|
+
function ze(e) {
|
|
443
|
+
return /MicroMessenger/i.test(e) ? "WeChat" : /Edg[A-Za-z]*\//.test(e) ? "Edge" : /OPR\/|Opera/.test(e) ? "Opera" : /Firefox\//.test(e) ? "Firefox" : /Chrome\/|CriOS\//.test(e) ? "Chrome" : /Safari\//.test(e) ? "Safari" : "";
|
|
444
|
+
}
|
|
445
|
+
function Je(e) {
|
|
446
|
+
return /MicroMessenger\/([\d.]+)/.test(e) ? I(e, /MicroMessenger\/([\d.]+)/) : /Edg[A-Za-z]*\/([\d.]+)/.test(e) ? I(e, /Edg[A-Za-z]*\/([\d.]+)/) : /OPR\/([\d.]+)/.test(e) ? I(e, /OPR\/([\d.]+)/) : /Firefox\/([\d.]+)/.test(e) ? I(e, /Firefox\/([\d.]+)/) : /CriOS\/([\d.]+)/.test(e) ? I(e, /CriOS\/([\d.]+)/) : /Chrome\/([\d.]+)/.test(e) ? I(e, /Chrome\/([\d.]+)/) : /Version\/([\d.]+).*Safari/.test(e) ? I(e, /Version\/([\d.]+)/) : "";
|
|
447
|
+
}
|
|
448
|
+
function Qe(e) {
|
|
449
|
+
return /Windows NT/.test(e) ? "Windows" : /iPhone|iPad|iPod/.test(e) ? "iOS" : /Android/.test(e) ? "Android" : /Mac OS X|Macintosh/.test(e) ? "macOS" : /HarmonyOS/.test(e) ? "HarmonyOS" : /Linux/.test(e) ? "Linux" : "";
|
|
450
|
+
}
|
|
451
|
+
function et(e) {
|
|
452
|
+
if (/Windows NT 10\.0/.test(e)) return "10/11";
|
|
453
|
+
if (/Windows NT 6\.3/.test(e)) return "8.1";
|
|
454
|
+
if (/Windows NT 6\.1/.test(e)) return "7";
|
|
455
|
+
const t = e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);
|
|
456
|
+
if (t) return t[1].replace(/_/g, ".");
|
|
457
|
+
if (/Android[ _]([\d.]+)/.test(e)) return I(e, /Android[ _]([\d.]+)/);
|
|
458
|
+
const n = e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);
|
|
459
|
+
return n ? n[1].replace(/_/g, ".") : "";
|
|
460
|
+
}
|
|
461
|
+
const g = {
|
|
462
|
+
SIGN: {
|
|
463
|
+
VERIFY: "/api/unsafe/dplugin/login/s/verify",
|
|
464
|
+
TOKEN: "/api/unsafe/dplugin/login/s/token",
|
|
465
|
+
REFRESH: "/api/unsafe/dplugin/login/s/refresh"
|
|
466
|
+
},
|
|
467
|
+
COOKIE: {
|
|
468
|
+
VERIFY: "/api/unsafe/dplugin/login/c/verify",
|
|
469
|
+
TOKEN: "/api/unsafe/dplugin/login/c/token",
|
|
470
|
+
REFRESH: "/api/unsafe/dplugin/login/c/refresh"
|
|
471
|
+
},
|
|
472
|
+
LOGOUT: "/api/safe/dplugin/login/logout"
|
|
473
|
+
}, tt = "dp_dvt", ue = 1800 * 1e3;
|
|
474
|
+
let _ = null, L = null, D = null, B = !1;
|
|
475
|
+
function X() {
|
|
476
|
+
return typeof location != "undefined" ? location.href : "";
|
|
477
|
+
}
|
|
478
|
+
function U(e) {
|
|
479
|
+
return new Promise((t) => setTimeout(t, e));
|
|
480
|
+
}
|
|
481
|
+
function W() {
|
|
482
|
+
const e = P();
|
|
483
|
+
typeof e.onLogin == "function" && e.onLogin(Z());
|
|
484
|
+
}
|
|
485
|
+
function T(e, t) {
|
|
486
|
+
return new m(e.code, e.message || t);
|
|
487
|
+
}
|
|
488
|
+
function nt(e, t, n) {
|
|
489
|
+
if (typeof document == "undefined")
|
|
490
|
+
return;
|
|
491
|
+
let o = e + "=" + encodeURIComponent(t) + "; path=/; SameSite=Lax";
|
|
492
|
+
o += "; max-age=" + n, document.cookie = o;
|
|
493
|
+
}
|
|
494
|
+
function Z() {
|
|
495
|
+
return {
|
|
496
|
+
mode: pe(),
|
|
497
|
+
loggedIn: fe(),
|
|
498
|
+
hasAccess: !!i.access,
|
|
499
|
+
sid: i.sid,
|
|
500
|
+
ds: i.ds
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
function fe() {
|
|
504
|
+
return S() ? !!i.cookie : !!(i.access || i.sid);
|
|
505
|
+
}
|
|
506
|
+
function Pe() {
|
|
507
|
+
i.clear();
|
|
508
|
+
}
|
|
509
|
+
function R(e) {
|
|
510
|
+
const t = fe();
|
|
511
|
+
Pe();
|
|
512
|
+
const n = P();
|
|
513
|
+
if (t && typeof n.onLogout == "function" && n.onLogout(e || "expired"), B || !n.enableAuth || !n.loginUrl || typeof location == "undefined")
|
|
514
|
+
return;
|
|
515
|
+
B = !0;
|
|
516
|
+
const o = n.loginUrl.includes("?") ? "&" : "?";
|
|
517
|
+
location.href = n.loginUrl + o + "referer=" + encodeURIComponent(location.href);
|
|
518
|
+
}
|
|
519
|
+
function ot(e, t) {
|
|
520
|
+
return D || (D = rt(e, t).then((n) => (D = null, n), (n) => {
|
|
521
|
+
throw D = null, n;
|
|
522
|
+
}), D);
|
|
523
|
+
}
|
|
524
|
+
async function rt(e, t) {
|
|
525
|
+
const n = P(), o = Ze();
|
|
526
|
+
if (S())
|
|
527
|
+
return ct(e, t, o, n);
|
|
528
|
+
const r = await ce(), s = await Se();
|
|
529
|
+
try {
|
|
530
|
+
return await it(e, t, o, n);
|
|
531
|
+
} catch (a) {
|
|
532
|
+
throw await we(te.SSO, r), await we(te.DOMAIN, s), a;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
async function it(e, t, n, o) {
|
|
536
|
+
const r = await Ye(), s = await ee(r), a = await h(w(e), Object.assign({}, t, { device: n, pk: s }));
|
|
537
|
+
if (a.code !== u.OK)
|
|
538
|
+
throw T(a, "登录失败");
|
|
539
|
+
const c = a.data && a.data.token;
|
|
540
|
+
if (!c)
|
|
541
|
+
throw T(a, "登录失败");
|
|
542
|
+
const f = await Be(), d = await Ie(f), l = await st(c, o, d), y = await h(w(g.SIGN.TOKEN), {
|
|
543
|
+
dsign: await x(N.TOKEN, {
|
|
544
|
+
origin: K(),
|
|
545
|
+
ts: Date.now(),
|
|
546
|
+
subject: l.code,
|
|
547
|
+
dna: d
|
|
548
|
+
}, "domain"),
|
|
549
|
+
pk: await ee(f)
|
|
550
|
+
});
|
|
551
|
+
if (y.code !== u.OK || !y.data)
|
|
552
|
+
throw T(y, "登录失败");
|
|
553
|
+
return i.sid = l.sid, i.ds = y.data.ds || l.ds, i.access = y.data.token, i.exp = Date.now() + ue, B = !1, W(), Z();
|
|
554
|
+
}
|
|
555
|
+
async function st(e, t, n) {
|
|
556
|
+
const o = X(), r = Math.max(1, Number(t.pollMax) || 5);
|
|
557
|
+
for (let s = 1; s <= r; s++) {
|
|
558
|
+
const a = i.ds || null, c = Date.now(), f = await x(N.VERIFY, {
|
|
559
|
+
method: "POST",
|
|
560
|
+
path: V(g.SIGN.VERIFY),
|
|
561
|
+
origin: K(),
|
|
562
|
+
ds: a,
|
|
563
|
+
ts: c,
|
|
564
|
+
subject: e,
|
|
565
|
+
dna: n,
|
|
566
|
+
redirect: o
|
|
567
|
+
}, "sso");
|
|
568
|
+
let d;
|
|
569
|
+
try {
|
|
570
|
+
d = await h(w(g.SIGN.VERIFY), { dsign: f, dna: n, redirect: o });
|
|
571
|
+
} catch (l) {
|
|
572
|
+
if ((l.code === u.NETWORK || l.code === u.TIMEOUT) && s < r) {
|
|
573
|
+
await U(t.pollInterval);
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
throw l;
|
|
577
|
+
}
|
|
578
|
+
if (d.code === u.OK && d.data)
|
|
579
|
+
return d.data;
|
|
580
|
+
if (d.code === u.PENDING) {
|
|
581
|
+
if (s < r) {
|
|
582
|
+
await U(t.pollInterval);
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
break;
|
|
586
|
+
}
|
|
587
|
+
throw T(d, "登录失败");
|
|
588
|
+
}
|
|
589
|
+
throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
|
|
590
|
+
}
|
|
591
|
+
async function ct(e, t, n, o) {
|
|
592
|
+
const r = await h(w(e), Object.assign({}, t, { device: n }));
|
|
593
|
+
if (r.code !== u.OK)
|
|
594
|
+
throw T(r, "登录失败");
|
|
595
|
+
const s = r.data && r.data.token;
|
|
596
|
+
if (!s)
|
|
597
|
+
throw T(r, "登录失败");
|
|
598
|
+
return nt(tt, s, 300), await at(o), await Re(), i.cookie = !0, B = !1, W(), Z();
|
|
599
|
+
}
|
|
600
|
+
async function at(e) {
|
|
601
|
+
const t = X(), n = Math.max(1, Number(e.pollMax) || 5);
|
|
602
|
+
for (let o = 1; o <= n; o++) {
|
|
603
|
+
let r;
|
|
604
|
+
try {
|
|
605
|
+
r = await h(w(g.COOKIE.VERIFY), { redirect: t });
|
|
606
|
+
} catch (s) {
|
|
607
|
+
if ((s.code === u.NETWORK || s.code === u.TIMEOUT) && o < n) {
|
|
608
|
+
await U(e.pollInterval);
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
throw s;
|
|
612
|
+
}
|
|
613
|
+
if (r.code === u.OK)
|
|
614
|
+
return r.data;
|
|
615
|
+
if (r.code === u.PENDING) {
|
|
616
|
+
if (o < n) {
|
|
617
|
+
await U(e.pollInterval);
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
throw T(r, "登录失败");
|
|
623
|
+
}
|
|
624
|
+
throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
|
|
625
|
+
}
|
|
626
|
+
async function Re() {
|
|
627
|
+
const e = await h(w(g.COOKIE.TOKEN));
|
|
628
|
+
if (e.code !== u.OK)
|
|
629
|
+
throw T(e, "登录失败");
|
|
630
|
+
return e;
|
|
631
|
+
}
|
|
632
|
+
async function M(e) {
|
|
633
|
+
const t = !!(e && e.force);
|
|
634
|
+
return L || (L = (async () => {
|
|
635
|
+
try {
|
|
636
|
+
return S() ? await ut(t) : await dt(t);
|
|
637
|
+
} catch (n) {
|
|
638
|
+
if (n && (n.code === u.NETWORK || n.code === u.TIMEOUT))
|
|
639
|
+
throw n;
|
|
640
|
+
return !1;
|
|
641
|
+
} finally {
|
|
642
|
+
L = null;
|
|
643
|
+
}
|
|
644
|
+
})()), L;
|
|
645
|
+
}
|
|
646
|
+
async function ut(e) {
|
|
647
|
+
return !e && i.cookie ? !0 : (await h(w(g.COOKIE.VERIFY), { redirect: X() })).code !== u.OK || (await Re()).code !== u.OK ? (i.cookie = !1, !1) : (i.cookie = !0, W(), !0);
|
|
648
|
+
}
|
|
649
|
+
async function ft() {
|
|
650
|
+
if (!i.sid || !i.ds || !await ce())
|
|
651
|
+
return !1;
|
|
652
|
+
const t = i.ds, n = await He(te.DOMAIN), o = await Ie(n), r = X(), s = await x(N.VERIFY_SID, {
|
|
653
|
+
method: "POST",
|
|
654
|
+
path: V(g.SIGN.VERIFY),
|
|
655
|
+
origin: K(),
|
|
656
|
+
ds: t,
|
|
657
|
+
ts: Date.now(),
|
|
658
|
+
subject: i.sid,
|
|
659
|
+
dna: o,
|
|
660
|
+
redirect: r
|
|
661
|
+
}, "sso"), a = await h(w(g.SIGN.VERIFY), { dsign: s, dna: o, redirect: r });
|
|
662
|
+
if (a.code !== u.OK || !a.data)
|
|
663
|
+
return !1;
|
|
664
|
+
const c = await h(w(g.SIGN.TOKEN), {
|
|
665
|
+
dsign: await x(N.TOKEN, {
|
|
666
|
+
origin: K(),
|
|
667
|
+
ts: Date.now(),
|
|
668
|
+
subject: a.data.code,
|
|
669
|
+
dna: o
|
|
670
|
+
}, "domain"),
|
|
671
|
+
pk: await ee(n)
|
|
672
|
+
});
|
|
673
|
+
return c.code !== u.OK || !c.data ? !1 : (i.access = c.data.token, c.data.ds && (i.ds = c.data.ds), i.exp = Date.now() + ue, W(), !0);
|
|
674
|
+
}
|
|
675
|
+
async function dt(e) {
|
|
676
|
+
return !e && i.access ? !0 : ft();
|
|
677
|
+
}
|
|
678
|
+
function de() {
|
|
679
|
+
return _ || (_ = (S() ? lt() : wt()).then((e) => (_ = null, e), (e) => {
|
|
680
|
+
throw _ = null, e;
|
|
681
|
+
})), _;
|
|
682
|
+
}
|
|
683
|
+
async function lt() {
|
|
684
|
+
const e = await h(w(g.COOKIE.REFRESH));
|
|
685
|
+
if (e.code === u.OK)
|
|
686
|
+
return !0;
|
|
687
|
+
if (e.code === u.AUTH_EXPIRED)
|
|
688
|
+
throw new O(e.message);
|
|
689
|
+
if (e.code === "500" && /重复/.test(e.message || ""))
|
|
690
|
+
return await U(800), !0;
|
|
691
|
+
throw new m(e.code, e.message);
|
|
692
|
+
}
|
|
693
|
+
async function wt() {
|
|
694
|
+
const e = i.access, t = i.ds;
|
|
695
|
+
if (!e || !t)
|
|
696
|
+
throw new O();
|
|
697
|
+
const n = await x(N.BIZ, {
|
|
698
|
+
method: "POST",
|
|
699
|
+
path: V(g.SIGN.REFRESH),
|
|
700
|
+
origin: K(),
|
|
701
|
+
ds: t,
|
|
702
|
+
ts: Date.now(),
|
|
703
|
+
subject: e
|
|
704
|
+
}, "domain"), o = await h(w(g.SIGN.REFRESH, "dsign=" + encodeURIComponent(n)));
|
|
705
|
+
if (o.code === u.OK && o.data)
|
|
706
|
+
return i.access = o.data.token, o.data.ds && (i.ds = o.data.ds), i.exp = Date.now() + ue, i.access;
|
|
707
|
+
const r = await gt(e, 1200);
|
|
708
|
+
if (r)
|
|
709
|
+
return r;
|
|
710
|
+
throw o.code === u.AUTH_EXPIRED ? new O(o.message) : new m(o.code, o.message);
|
|
711
|
+
}
|
|
712
|
+
function gt(e, t) {
|
|
713
|
+
const n = Date.now() + t;
|
|
714
|
+
return new Promise((o) => {
|
|
715
|
+
const r = () => {
|
|
716
|
+
const s = i.access;
|
|
717
|
+
if (s && s !== e) {
|
|
718
|
+
o(s);
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
if (Date.now() >= n) {
|
|
722
|
+
o(null);
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
setTimeout(r, 150);
|
|
726
|
+
};
|
|
727
|
+
r();
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
async function ht() {
|
|
731
|
+
try {
|
|
732
|
+
let t = await ge();
|
|
733
|
+
t.code === u.AUTH_EXPIRED && (await de(), t = await ge());
|
|
734
|
+
} catch (t) {
|
|
735
|
+
}
|
|
736
|
+
Pe();
|
|
737
|
+
const e = P();
|
|
738
|
+
typeof e.onLogout == "function" && e.onLogout("logout");
|
|
739
|
+
}
|
|
740
|
+
async function ge() {
|
|
741
|
+
if (S())
|
|
742
|
+
return h(w(g.LOGOUT));
|
|
743
|
+
const e = await ae(g.LOGOUT, i.access, i.ds);
|
|
744
|
+
return h(w(g.LOGOUT, e));
|
|
745
|
+
}
|
|
746
|
+
async function Et(e, t, n) {
|
|
747
|
+
const o = n || {};
|
|
748
|
+
return o.auth !== !1 && await De(), G(e, t || {}, o, !1);
|
|
749
|
+
}
|
|
750
|
+
async function De() {
|
|
751
|
+
if (S()) {
|
|
752
|
+
if (i.cookie || await M())
|
|
753
|
+
return;
|
|
754
|
+
throw R("expired"), new O();
|
|
755
|
+
}
|
|
756
|
+
if (!i.access && !await M())
|
|
757
|
+
throw R("expired"), new O();
|
|
758
|
+
}
|
|
759
|
+
async function G(e, t, n, o) {
|
|
760
|
+
const r = n.auth !== !1, s = r && !S();
|
|
761
|
+
let a = "", c = null;
|
|
762
|
+
if (s) {
|
|
763
|
+
c = i.access;
|
|
764
|
+
try {
|
|
765
|
+
a = await ae(e, c, i.ds);
|
|
766
|
+
} catch (d) {
|
|
767
|
+
if (!o && await M({ force: !0 }))
|
|
768
|
+
return G(e, t, n, !0);
|
|
769
|
+
throw R("expired"), new O();
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
const f = await h(w(e, a), t, n.timeout);
|
|
773
|
+
if (f.code === u.OK)
|
|
774
|
+
return f.data;
|
|
775
|
+
if (r && f.code === u.AUTH_EXPIRED && !o) {
|
|
776
|
+
if (s && c && i.access !== c)
|
|
777
|
+
return G(e, t, n, !0);
|
|
778
|
+
try {
|
|
779
|
+
await de();
|
|
780
|
+
} catch (d) {
|
|
781
|
+
throw d instanceof O && R("expired"), d;
|
|
782
|
+
}
|
|
783
|
+
return G(e, t, n, !0);
|
|
784
|
+
}
|
|
785
|
+
throw r && f.code === u.AUTH_EXPIRED ? (R("expired"), new O(f.message)) : Ne(f);
|
|
786
|
+
}
|
|
787
|
+
function Ne(e) {
|
|
788
|
+
const t = new m(e.code, e.message), n = P();
|
|
789
|
+
return n.onError && n.onError({ code: e.code, message: e.message, error: t }), t;
|
|
790
|
+
}
|
|
791
|
+
async function mt(e, t, n, o) {
|
|
792
|
+
return (S() ? !i.cookie : !i.access) && await De(), oe(e, t, n, o || {}, !1);
|
|
793
|
+
}
|
|
794
|
+
async function oe(e, t, n, o, r) {
|
|
795
|
+
let s = "";
|
|
796
|
+
if (!S())
|
|
797
|
+
try {
|
|
798
|
+
s = await ae(e, i.access, i.ds);
|
|
799
|
+
} catch (f) {
|
|
800
|
+
if (!r && await M({ force: !0 }))
|
|
801
|
+
return oe(e, t, n, o, !0);
|
|
802
|
+
throw R("expired"), new O();
|
|
803
|
+
}
|
|
804
|
+
const a = new FormData();
|
|
805
|
+
Object.keys(n || {}).forEach((f) => {
|
|
806
|
+
const d = n[f];
|
|
807
|
+
d != null && a.append(f, d);
|
|
808
|
+
}), a.append("file", t);
|
|
809
|
+
const c = await h(w(e, s), a, o.timeout);
|
|
810
|
+
if (c.code === u.OK)
|
|
811
|
+
return c.data;
|
|
812
|
+
if (c.code === u.AUTH_EXPIRED && !r) {
|
|
813
|
+
try {
|
|
814
|
+
await de();
|
|
815
|
+
} catch (f) {
|
|
816
|
+
throw f instanceof O && R("expired"), f;
|
|
817
|
+
}
|
|
818
|
+
return oe(e, t, n, o, !0);
|
|
819
|
+
}
|
|
820
|
+
throw Ne(c);
|
|
821
|
+
}
|
|
822
|
+
function Ot(e) {
|
|
823
|
+
const t = xe(e);
|
|
824
|
+
if (Ue(), t.nativeGuard !== "off") {
|
|
825
|
+
const n = Ce();
|
|
826
|
+
if (n.length) {
|
|
827
|
+
const o = "[dplugin/core] 这些原生能力在模块加载前已被替换:" + n.join(", ") + "(签名/请求可能被中间人截获,检查是否有脚本抢跑)";
|
|
828
|
+
if (t.nativeGuard === "error")
|
|
829
|
+
throw new Error(o);
|
|
830
|
+
typeof console != "undefined" && console.warn(o);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
const yt = {
|
|
835
|
+
login: ot,
|
|
836
|
+
logout: ht,
|
|
837
|
+
isLogin: fe,
|
|
838
|
+
getState: Z,
|
|
839
|
+
/** 页面加载时恢复登录态;返回是否已登录 */
|
|
840
|
+
restore: M
|
|
841
|
+
};
|
|
842
|
+
export {
|
|
843
|
+
O as AuthError,
|
|
844
|
+
m as BizError,
|
|
845
|
+
u as CODE,
|
|
846
|
+
yt as auth,
|
|
847
|
+
Pe as clear,
|
|
848
|
+
R as expire,
|
|
849
|
+
pe as getMode,
|
|
850
|
+
Z as getState,
|
|
851
|
+
pt as isAuthError,
|
|
852
|
+
fe as isLogin,
|
|
853
|
+
ot as login,
|
|
854
|
+
ht as logout,
|
|
855
|
+
Et as request,
|
|
856
|
+
Ot as setup,
|
|
857
|
+
i as store,
|
|
858
|
+
mt as upload,
|
|
859
|
+
M as verifyLogin
|
|
860
|
+
};
|
|
861
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
(function(d,I){typeof exports=="object"&&typeof module!="undefined"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(d=typeof globalThis!="undefined"?globalThis:d||self,I(d.DPluginCore={}))})(this,(function(d){"use strict";const I={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"warn"};function _e(e){return e&&Object.keys(I).forEach(t=>{e[t]!==void 0&&(I[t]=e[t])}),I}function P(){return I}const O=ke();function $(e,t){if(typeof e!="function")return!1;let n;try{n=Function.prototype.toString.call(e)}catch(r){return!1}const o=/^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(n);return o?o[1]===t:/\[native code\]/.test(n)&&!/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(n)}function xe(e){try{const t=e();return t&&typeof t.then=="function"&&Promise.resolve(t).catch(()=>{}),!1}catch(t){const n=String(t&&t.message||"");return!!t&&t.name==="TypeError"&&/argument|required|参数/i.test(n)}}function ke(){const e=globalThis,t=typeof e.window!="undefined"&&e.window&&typeof e.window.document!="undefined",n={fetch:null,idbOpen:null,subtle:null,pristine:{fetch:!1,idbOpen:!1,subtle:!1}};typeof e.fetch=="function"&&(n.pristine.fetch=$(e.fetch,"fetch"),n.fetch=e.fetch.bind(e)),typeof e.indexedDB!="undefined"&&e.indexedDB&&typeof e.indexedDB.open=="function"&&(n.pristine.idbOpen=$(e.indexedDB.open,"open")&&(!t||xe(()=>e.indexedDB.open())),n.idbOpen=e.indexedDB.open.bind(e.indexedDB));const o=e.crypto&&e.crypto.subtle?e.crypto.subtle:null;if(o){const r=["generateKey","exportKey","importKey","sign","verify","digest"],s={};let u=!0;r.forEach(c=>{typeof o[c]=="function"&&(s[c]=o[c].bind(o),u=u&&$(o[c],c))}),n.subtle=s,n.pristine.subtle=u}return n}function Me(){if(typeof window=="undefined"||typeof window.document=="undefined")return[];const e=[];return O.fetch&&!O.pristine.fetch&&e.push("fetch"),O.idbOpen&&!O.pristine.idbOpen&&e.push("indexedDB.open"),O.subtle&&!O.pristine.subtle&&e.push("crypto.subtle"),e}const a={OK:"200",PENDING:"202",AUTH_EXPIRED:"500900",FORBIDDEN:"401001",CORS_DENIED:"403",API_NOT_FOUND:"404",NETWORK:"NETWORK",TIMEOUT:"TIMEOUT"};class E extends Error{constructor(t,n){super(n||"请求失败"),this.name="BizError",this.code=t}}class y extends E{constructor(t){super(a.AUTH_EXPIRED,t||"登录已失效"),this.name="AuthError"}}function Ue(e){return e instanceof y||e&&e.code===a.AUTH_EXPIRED}const J="dplugin.access",Q="dplugin.sid",he="dplugin.ds",ee="dplugin.exp",te="dplugin.cookie";function ve(e){try{return localStorage.getItem(e)}catch(t){return null}}function pe(e,t){try{t==null||t===""?localStorage.removeItem(e):localStorage.setItem(e,t)}catch(n){}}const F={};function k(e){const t=ve(e);return t===null?F[e]===void 0?null:F[e]:t}function M(e,t){F[e]=t,pe(e,t)}const i={get access(){return k(J)},set access(e){M(J,e)},get sid(){return k(Q)},set sid(e){M(Q,e)},get ds(){return k(he)},set ds(e){M(he,e)},get exp(){const e=k(ee);return e?Number(e):0},set exp(e){M(ee,e?String(e):null)},get cookie(){return k(te)==="1"},set cookie(e){M(te,e?"1":null)},clear(){[J,Q,ee,te].forEach(e=>{F[e]=null,pe(e,null)})}};function w(e,t){const n=P().apiBase||"";let o=e;return/^https?:\/\//i.test(e)||(o=(n?n.replace(/\/+$/,""):"")+(e.startsWith("/")?e:"/"+e)),t&&(o+=(o.includes("?")?"&":"?")+t),o}function B(e){const t=w(e);if(/^https?:\/\//i.test(t))try{return new URL(t).pathname}catch(n){return t.split("?")[0]}try{return new URL(t,"http://localhost").pathname}catch(n){return t.split("?")[0]}}let T=null;function Le(e){try{const t=typeof location!="undefined"?location.href:void 0;return new URL(e||"",t).origin}catch(t){return null}}function Fe(){T=null}function ne(){if(T)return T;const e=P();if(e.mode==="sign"||e.mode==="cookie")return T=e.mode,T;const t=typeof location!="undefined"?location.origin:null;if(!t)return T="sign",T;const n=Le(e.apiBase||t);return T=n&&n===t?"cookie":"sign",T}function S(){return ne()==="cookie"}async function h(e,t,n){const o=P(),r=n||o.timeout,s=typeof AbortController!="undefined"?new AbortController:null,u=s?setTimeout(()=>s.abort(),r):null,c={method:"POST",credentials:S()?"same-origin":"omit",signal:s?s.signal:void 0};if(typeof FormData!="undefined"&&t instanceof FormData?c.body=t:t!=null&&(c.headers={"Content-Type":"text/plain"},c.body=JSON.stringify(t)),!O.fetch)throw new E(a.NETWORK,"当前环境不支持 fetch");let f;try{f=await O.fetch(e,c)}catch(b){throw u&&clearTimeout(u),b&&b.name==="AbortError"?new E(a.TIMEOUT,"请求超时,请重试"):new E(a.NETWORK,"网络异常,请检查网络后重试")}u&&clearTimeout(u);const l=await f.text();let g=null;if(l)try{g=JSON.parse(l)}catch(b){g=null}if(!f.ok)throw new E(String(f.status),g&&g.message||l||"请求失败");if(!g||g.code===null||g.code===void 0)throw new E("500","响应格式异常");return g.code=String(g.code),g}const Be="dplugin-auth",Ge=1,G="keys",oe="sso",re="domain",He={name:"ECDSA",namedCurve:"P-256"},Ye={name:"ECDSA",hash:"SHA-256"},H=new Map;let Y=null;function V(){if(!O.subtle){const e=typeof location!="undefined"&&location.origin?location.origin:"(未知 origin)";throw new Error("[dplugin/core] crypto.subtle 不存在:浏览器只在**安全上下文**里提供它(https、http://localhost、http://127.0.0.1、http://[::1]、file://);当前 origin="+e)}return O.subtle}function Ve(){return Y||(Y=new Promise(e=>{if(!O.idbOpen){e(null);return}let t;try{t=O.idbOpen(Be,Ge)}catch(n){e(null);return}t.onupgradeneeded=()=>{const n=t.result;n.objectStoreNames.contains(G)||n.createObjectStore(G)},t.onsuccess=()=>e(t.result),t.onerror=()=>e(null),t.onblocked=()=>e(null)}),Y)}function Ee(e,t){return Ve().then(n=>n?new Promise(o=>{let r;try{r=n.transaction(G,e)}catch(c){o(null);return}const s=r.objectStore(G);let u=null;try{const c=t(s);c&&(c.onsuccess=()=>{u=c.result},c.onerror=()=>{u=null})}catch(c){u=null}r.oncomplete=()=>o(u),r.onerror=()=>o(null),r.onabort=()=>o(null)}):null)}function me(e){return H.has(e)?Promise.resolve(H.get(e)):Ee("readonly",t=>t.get(e)).then(t=>(t&&H.set(e,t),t||null))}function Oe(e,t){return H.set(e,t),Ee("readwrite",n=>n.put(t,e)).then(()=>t)}async function ie(e){const n=await V().generateKey(He,!1,["sign","verify"]);return Oe(e,n)}async function ye(e){return me(e)}async function Se(e,t){return t?Oe(e,t):null}async function je(e){return await me(e)||ie(e)}function Xe(){return ie(oe)}function We(){return ie(re)}function se(){return ye(oe)}function be(){return ye(re)}async function ce(e){const n=await V().exportKey("spki",e.publicKey);let o="";const r=new Uint8Array(n);for(let s=0;s<r.length;s++)o+=String.fromCharCode(r[s]);return btoa(o)}function Ie(e){const t=new Uint8Array(e);let n="";for(let o=0;o<t.length;o++)n+=String.fromCharCode(t[o]);return btoa(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function qe(e,t){const o=await V().sign(Ye,e.privateKey,new TextEncoder().encode(t));return Ie(o)}async function Te(e){const t=V(),n=await t.exportKey("spki",e.publicKey),o=await t.digest("SHA-256",n);return Ie(o)}const ae={SSO:oe,DOMAIN:re},ue="-",ze="POST",K={BIZ:"BIZ",VERIFY:"VERIFY",VERIFY_SID:"VERIFY_SID",TOKEN:"TOKEN"};function m(e){return e==null||e===""?ue:String(e)}function Ze(e,t){const n=m(t.origin);return[m(e),m(t.method),m(t.path),n===ue?ue:n.toLowerCase(),m(t.ds),m(t.ts),m(t.subject),m(t.dna),m(t.redirect)].join(`
|
|
2
|
+
`)}function $e(e,t,n){return[m(e),m(t.subject),m(t.ds),m(t.ts),n].join(".")}async function A(e,t,n){const o=n==="sso"?await se():await be();if(!o)throw Pe(n==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");const r=await qe(o,Ze(e,t));return $e(e,t,r)}async function fe(e,t,n){if(!t||!n)throw Pe("缺少 access 或 ds,无法签名");const o=await A(K.BIZ,{method:ze,path:B(e),origin:C(),ds:n,ts:Date.now(),subject:t},"domain");return"dsign="+encodeURIComponent(o)}function C(){if(typeof location=="undefined")return"";if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(e){return""}}function Pe(e){const t=new Error(e);return t.code="KEY_MISSING",t}function Je(){const e=typeof navigator!="undefined"&&navigator.userAgent||"",t={device_brand:Qe(e),device_model:et(e),browser_name:tt(e),browser_version:nt(e),os_name:ot(e),os_version:rt(e)},n=i.ds;return n&&(t.device_id=n),t}function R(e,t){const n=e.match(t);return n?n[1]:""}function Qe(e){return/iPhone|iPad|iPod/.test(e)?"Apple":/Huawei|HONOR/i.test(e)?"Huawei":/Xiaomi|Redmi|POCO/i.test(e)?"Xiaomi":/OPPO|realme/i.test(e)?"OPPO":/vivo/i.test(e)?"vivo":/Samsung|SM-|SAMSUNG/i.test(e)?"Samsung":/Macintosh|Mac OS X/.test(e)?"Apple":""}function et(e){if(/iPhone/.test(e))return"iPhone";if(/iPad/.test(e))return"iPad";const t=e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return t?t[1].trim():/Macintosh/.test(e)?"Mac":""}function tt(e){return/MicroMessenger/i.test(e)?"WeChat":/Edg[A-Za-z]*\//.test(e)?"Edge":/OPR\/|Opera/.test(e)?"Opera":/Firefox\//.test(e)?"Firefox":/Chrome\/|CriOS\//.test(e)?"Chrome":/Safari\//.test(e)?"Safari":""}function nt(e){return/MicroMessenger\/([\d.]+)/.test(e)?R(e,/MicroMessenger\/([\d.]+)/):/Edg[A-Za-z]*\/([\d.]+)/.test(e)?R(e,/Edg[A-Za-z]*\/([\d.]+)/):/OPR\/([\d.]+)/.test(e)?R(e,/OPR\/([\d.]+)/):/Firefox\/([\d.]+)/.test(e)?R(e,/Firefox\/([\d.]+)/):/CriOS\/([\d.]+)/.test(e)?R(e,/CriOS\/([\d.]+)/):/Chrome\/([\d.]+)/.test(e)?R(e,/Chrome\/([\d.]+)/):/Version\/([\d.]+).*Safari/.test(e)?R(e,/Version\/([\d.]+)/):""}function ot(e){return/Windows NT/.test(e)?"Windows":/iPhone|iPad|iPod/.test(e)?"iOS":/Android/.test(e)?"Android":/Mac OS X|Macintosh/.test(e)?"macOS":/HarmonyOS/.test(e)?"HarmonyOS":/Linux/.test(e)?"Linux":""}function rt(e){if(/Windows NT 10\.0/.test(e))return"10/11";if(/Windows NT 6\.3/.test(e))return"8.1";if(/Windows NT 6\.1/.test(e))return"7";const t=e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(t)return t[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(e))return R(e,/Android[ _]([\d.]+)/);const n=e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return n?n[1].replace(/_/g,"."):""}const p={SIGN:{VERIFY:"/api/unsafe/dplugin/login/s/verify",TOKEN:"/api/unsafe/dplugin/login/s/token",REFRESH:"/api/unsafe/dplugin/login/s/refresh"},COOKIE:{VERIFY:"/api/unsafe/dplugin/login/c/verify",TOKEN:"/api/unsafe/dplugin/login/c/token",REFRESH:"/api/unsafe/dplugin/login/c/refresh"},LOGOUT:"/api/safe/dplugin/login/logout"},it="dp_dvt",de=1800*1e3;let U=null,j=null,_=null,X=!1;function W(){return typeof location!="undefined"?location.href:""}function v(e){return new Promise(t=>setTimeout(t,e))}function q(){const e=P();typeof e.onLogin=="function"&&e.onLogin(L())}function D(e,t){return new E(e.code,e.message||t)}function st(e,t,n){if(typeof document=="undefined")return;let o=e+"="+encodeURIComponent(t)+"; path=/; SameSite=Lax";o+="; max-age="+n,document.cookie=o}function L(){return{mode:ne(),loggedIn:z(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function z(){return S()?!!i.cookie:!!(i.access||i.sid)}function le(){i.clear()}function N(e){const t=z();le();const n=P();if(t&&typeof n.onLogout=="function"&&n.onLogout(e||"expired"),X||!n.enableAuth||!n.loginUrl||typeof location=="undefined")return;X=!0;const o=n.loginUrl.includes("?")?"&":"?";location.href=n.loginUrl+o+"referer="+encodeURIComponent(location.href)}function Re(e,t){return _||(_=ct(e,t).then(n=>(_=null,n),n=>{throw _=null,n}),_)}async function ct(e,t){const n=P(),o=Je();if(S())return ft(e,t,o,n);const r=await se(),s=await be();try{return await at(e,t,o,n)}catch(u){throw await Se(ae.SSO,r),await Se(ae.DOMAIN,s),u}}async function at(e,t,n,o){const r=await Xe(),s=await ce(r),u=await h(w(e),Object.assign({},t,{device:n,pk:s}));if(u.code!==a.OK)throw D(u,"登录失败");const c=u.data&&u.data.token;if(!c)throw D(u,"登录失败");const f=await We(),l=await Te(f),g=await ut(c,o,l),b=await h(w(p.SIGN.TOKEN),{dsign:await A(K.TOKEN,{origin:C(),ts:Date.now(),subject:g.code,dna:l},"domain"),pk:await ce(f)});if(b.code!==a.OK||!b.data)throw D(b,"登录失败");return i.sid=g.sid,i.ds=b.data.ds||g.ds,i.access=b.data.token,i.exp=Date.now()+de,X=!1,q(),L()}async function ut(e,t,n){const o=W(),r=Math.max(1,Number(t.pollMax)||5);for(let s=1;s<=r;s++){const u=i.ds||null,c=Date.now(),f=await A(K.VERIFY,{method:"POST",path:B(p.SIGN.VERIFY),origin:C(),ds:u,ts:c,subject:e,dna:n,redirect:o},"sso");let l;try{l=await h(w(p.SIGN.VERIFY),{dsign:f,dna:n,redirect:o})}catch(g){if((g.code===a.NETWORK||g.code===a.TIMEOUT)&&s<r){await v(t.pollInterval);continue}throw g}if(l.code===a.OK&&l.data)return l.data;if(l.code===a.PENDING){if(s<r){await v(t.pollInterval);continue}break}throw D(l,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function ft(e,t,n,o){const r=await h(w(e),Object.assign({},t,{device:n}));if(r.code!==a.OK)throw D(r,"登录失败");const s=r.data&&r.data.token;if(!s)throw D(r,"登录失败");return st(it,s,300),await dt(o),await De(),i.cookie=!0,X=!1,q(),L()}async function dt(e){const t=W(),n=Math.max(1,Number(e.pollMax)||5);for(let o=1;o<=n;o++){let r;try{r=await h(w(p.COOKIE.VERIFY),{redirect:t})}catch(s){if((s.code===a.NETWORK||s.code===a.TIMEOUT)&&o<n){await v(e.pollInterval);continue}throw s}if(r.code===a.OK)return r.data;if(r.code===a.PENDING){if(o<n){await v(e.pollInterval);continue}break}throw D(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function De(){const e=await h(w(p.COOKIE.TOKEN));if(e.code!==a.OK)throw D(e,"登录失败");return e}async function x(e){const t=!!(e&&e.force);return j||(j=(async()=>{try{return S()?await lt(t):await wt(t)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{j=null}})()),j}async function lt(e){return!e&&i.cookie?!0:(await h(w(p.COOKIE.VERIFY),{redirect:W()})).code!==a.OK||(await De()).code!==a.OK?(i.cookie=!1,!1):(i.cookie=!0,q(),!0)}async function gt(){if(!i.sid||!i.ds||!await se())return!1;const t=i.ds,n=await je(ae.DOMAIN),o=await Te(n),r=W(),s=await A(K.VERIFY_SID,{method:"POST",path:B(p.SIGN.VERIFY),origin:C(),ds:t,ts:Date.now(),subject:i.sid,dna:o,redirect:r},"sso"),u=await h(w(p.SIGN.VERIFY),{dsign:s,dna:o,redirect:r});if(u.code!==a.OK||!u.data)return!1;const c=await h(w(p.SIGN.TOKEN),{dsign:await A(K.TOKEN,{origin:C(),ts:Date.now(),subject:u.data.code,dna:o},"domain"),pk:await ce(n)});return c.code!==a.OK||!c.data?!1:(i.access=c.data.token,c.data.ds&&(i.ds=c.data.ds),i.exp=Date.now()+de,q(),!0)}async function wt(e){return!e&&i.access?!0:gt()}function ge(){return U||(U=(S()?ht():pt()).then(e=>(U=null,e),e=>{throw U=null,e})),U}async function ht(){const e=await h(w(p.COOKIE.REFRESH));if(e.code===a.OK)return!0;if(e.code===a.AUTH_EXPIRED)throw new y(e.message);if(e.code==="500"&&/重复/.test(e.message||""))return await v(800),!0;throw new E(e.code,e.message)}async function pt(){const e=i.access,t=i.ds;if(!e||!t)throw new y;const n=await A(K.BIZ,{method:"POST",path:B(p.SIGN.REFRESH),origin:C(),ds:t,ts:Date.now(),subject:e},"domain"),o=await h(w(p.SIGN.REFRESH,"dsign="+encodeURIComponent(n)));if(o.code===a.OK&&o.data)return i.access=o.data.token,o.data.ds&&(i.ds=o.data.ds),i.exp=Date.now()+de,i.access;const r=await Et(e,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new y(o.message):new E(o.code,o.message)}function Et(e,t){const n=Date.now()+t;return new Promise(o=>{const r=()=>{const s=i.access;if(s&&s!==e){o(s);return}if(Date.now()>=n){o(null);return}setTimeout(r,150)};r()})}async function Ne(){try{let t=await Ke();t.code===a.AUTH_EXPIRED&&(await ge(),t=await Ke())}catch(t){}le();const e=P();typeof e.onLogout=="function"&&e.onLogout("logout")}async function Ke(){if(S())return h(w(p.LOGOUT));const e=await fe(p.LOGOUT,i.access,i.ds);return h(w(p.LOGOUT,e))}async function mt(e,t,n){const o=n||{};return o.auth!==!1&&await Ae(),Z(e,t||{},o,!1)}async function Ae(){if(S()){if(i.cookie||await x())return;throw N("expired"),new y}if(!i.access&&!await x())throw N("expired"),new y}async function Z(e,t,n,o){const r=n.auth!==!1,s=r&&!S();let u="",c=null;if(s){c=i.access;try{u=await fe(e,c,i.ds)}catch(l){if(!o&&await x({force:!0}))return Z(e,t,n,!0);throw N("expired"),new y}}const f=await h(w(e,u),t,n.timeout);if(f.code===a.OK)return f.data;if(r&&f.code===a.AUTH_EXPIRED&&!o){if(s&&c&&i.access!==c)return Z(e,t,n,!0);try{await ge()}catch(l){throw l instanceof y&&N("expired"),l}return Z(e,t,n,!0)}throw r&&f.code===a.AUTH_EXPIRED?(N("expired"),new y(f.message)):Ce(f)}function Ce(e){const t=new E(e.code,e.message),n=P();return n.onError&&n.onError({code:e.code,message:e.message,error:t}),t}async function Ot(e,t,n,o){return(S()?!i.cookie:!i.access)&&await Ae(),we(e,t,n,o||{},!1)}async function we(e,t,n,o,r){let s="";if(!S())try{s=await fe(e,i.access,i.ds)}catch(f){if(!r&&await x({force:!0}))return we(e,t,n,o,!0);throw N("expired"),new y}const u=new FormData;Object.keys(n||{}).forEach(f=>{const l=n[f];l!=null&&u.append(f,l)}),u.append("file",t);const c=await h(w(e,s),u,o.timeout);if(c.code===a.OK)return c.data;if(c.code===a.AUTH_EXPIRED&&!r){try{await ge()}catch(f){throw f instanceof y&&N("expired"),f}return we(e,t,n,o,!0)}throw Ce(c)}function yt(e){const t=_e(e);if(Fe(),t.nativeGuard!=="off"){const n=Me();if(n.length){const o="[dplugin/core] 这些原生能力在模块加载前已被替换:"+n.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(t.nativeGuard==="error")throw new Error(o);typeof console!="undefined"&&console.warn(o)}}}const St={login:Re,logout:Ne,isLogin:z,getState:L,restore:x};d.AuthError=y,d.BizError=E,d.CODE=a,d.auth=St,d.clear=le,d.expire=N,d.getMode=ne,d.getState=L,d.isAuthError=Ue,d.isLogin=z,d.login=Re,d.logout=Ne,d.request=mt,d.setup=yt,d.store=i,d.upload=Ot,d.verifyLogin=x,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
|
|
3
|
+
//# sourceMappingURL=index.umd.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dplugin/core",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "DPlugin 前端登录与请求 SDK(同域 Cookie + 跨域 ECDSA 双模式)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"unpkg": "./dist/index.umd.js",
|
|
9
|
+
"jsdelivr": "./dist/index.umd.js",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.cjs",
|
|
14
|
+
"default": "./dist/index.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist/index.mjs",
|
|
19
|
+
"dist/index.cjs",
|
|
20
|
+
"dist/index.umd.js",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "vite build",
|
|
26
|
+
"test": "node test/flow.mjs && node test/cookie.mjs"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"dplugin",
|
|
30
|
+
"auth",
|
|
31
|
+
"request",
|
|
32
|
+
"ecdsa"
|
|
33
|
+
],
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/dplugin/dplugin-login.git"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/dplugin/dplugin-login/tree/main/dplugin-login-umdjs-core",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/dplugin/dplugin-login/issues"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"vite": "6.3.4"
|
|
51
|
+
}
|
|
52
|
+
}
|