@workopilot/webapp-sdk 0.1.2 → 0.1.4
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 +10 -1
- package/dist/http.d.ts +4 -0
- package/dist/http.js +24 -12
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/storage.d.ts +2 -0
- package/dist/storage.js +83 -2
- package/dist/types.d.ts +5 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -18,6 +18,7 @@ const sdk = createWiseAiFrontSdk({
|
|
|
18
18
|
env: 'prod',
|
|
19
19
|
javaBasePath: '/prod-api',
|
|
20
20
|
netBasePath: '/net-api',
|
|
21
|
+
secureStoreKey: 'please-replace-me-with-your-own-key',
|
|
21
22
|
});
|
|
22
23
|
|
|
23
24
|
await sdk.init();
|
|
@@ -34,6 +35,11 @@ await sdk.init();
|
|
|
34
35
|
| `enableEncrypt` | 否 | 读取环境变量 | 是否启用开发环境登录请求加密 |
|
|
35
36
|
| `clientId` | 否 | 读取环境变量 | 登录请求使用的客户端编号 |
|
|
36
37
|
| `rsaPublicKey` | 否 | 读取环境变量 | 启用加密时使用的 RSA 公钥 |
|
|
38
|
+
| `accessToken` | 否 | 空 | 调用方直接提供的 token |
|
|
39
|
+
| `tokenProvider` | 否 | 无 | 自定义同步或异步 token 获取函数 |
|
|
40
|
+
| `tokenQueryParam` | 否 | `runtimeToken` | iframe URL 中承载 token 的 Query 参数名 |
|
|
41
|
+
| `secureStoreKey` | 否 | 读取环境变量 | 解密 WiseAI 生产环境 SecureLS 登录态的密钥 |
|
|
42
|
+
| `secureStoreNamespace` | 否 | 自动扫描 | 明确指定宿主 Pinia 持久化命名空间 |
|
|
37
43
|
|
|
38
44
|
生产环境建议由宿主项目的 `.env.*` 文件管理环境参数,再传给 SDK:
|
|
39
45
|
|
|
@@ -45,13 +51,16 @@ VITE_WISEAI_NET_BASE_PATH=/net-api
|
|
|
45
51
|
VITE_GLOB_ENABLE_ENCRYPT=true
|
|
46
52
|
VITE_GLOB_RSA_PUBLIC_KEY=...
|
|
47
53
|
VITE_GLOB_APP_CLIENT_ID=...
|
|
54
|
+
VITE_APP_STORE_SECURE_KEY=please-replace-me-with-your-own-key
|
|
48
55
|
```
|
|
49
56
|
|
|
50
57
|
## 提供能力
|
|
51
58
|
|
|
52
59
|
### Token 和登录
|
|
53
60
|
|
|
54
|
-
-
|
|
61
|
+
- 按显式 token、`tokenProvider`、URL `runtimeToken`、明文存储、WiseAI SecureLS 存储的顺序获取 token。
|
|
62
|
+
- 兼容参考项目生产环境带版本号的 `*-prod-core-access` 加密 Pinia 登录态。
|
|
63
|
+
- 自动读取和保存开发环境的 `accessToken`。
|
|
55
64
|
- 自动检查 JWT 是否过期。
|
|
56
65
|
- 生产环境没有 token 时,通过 iframe `postMessage` 通知宿主重新登录。
|
|
57
66
|
- 开发环境没有 token 时,自动请求 `{javaBasePath}/auth/code` 获取验证码配置并弹出登录窗口。
|
package/dist/http.d.ts
CHANGED
|
@@ -13,9 +13,13 @@ export declare class WiseAiFrontSdk {
|
|
|
13
13
|
private readonly parentWindow?;
|
|
14
14
|
private readonly requestTimeoutMs;
|
|
15
15
|
private readonly rsaPublicKey;
|
|
16
|
+
private readonly secureStoreKey;
|
|
17
|
+
private readonly secureStoreNamespace?;
|
|
16
18
|
private readonly storage?;
|
|
17
19
|
private readonly tokenKey;
|
|
18
20
|
private readonly tokenStorageKey;
|
|
21
|
+
private readonly tokenProvider?;
|
|
22
|
+
private readonly tokenQueryParam;
|
|
19
23
|
private readonly uploadPath;
|
|
20
24
|
private readonly routePermissionPath;
|
|
21
25
|
private readonly loginRequest?;
|
package/dist/http.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { notifyReloginRequired, } from './bridge';
|
|
2
2
|
import { encryptRequestBody, } from './encryption';
|
|
3
3
|
import { isJwtExpired, } from './jwt';
|
|
4
|
-
import { clearToken, persistToken, resolveTokenFromStorage, } from './storage';
|
|
4
|
+
import { clearToken, persistToken, resolveTokenFromLocation, resolveTokenFromStorage, resolveTokensFromSecureStore, } from './storage';
|
|
5
5
|
import { openDevLoginDialog } from './dialog';
|
|
6
6
|
function inferEnvironment() {
|
|
7
7
|
if (typeof import.meta !== 'undefined') {
|
|
@@ -87,7 +87,7 @@ function unwrapEnvelope(body) {
|
|
|
87
87
|
}
|
|
88
88
|
export class WiseAiFrontSdk {
|
|
89
89
|
constructor(options) {
|
|
90
|
-
this.accessToken = '';
|
|
90
|
+
this.accessToken = options.accessToken?.trim() || '';
|
|
91
91
|
this.appCode = options.appCode;
|
|
92
92
|
this.captchaPath = options.captchaPath || '/auth/code';
|
|
93
93
|
this.clientId =
|
|
@@ -103,9 +103,15 @@ export class WiseAiFrontSdk {
|
|
|
103
103
|
this.requestTimeoutMs = options.requestTimeoutMs ?? 30000;
|
|
104
104
|
this.rsaPublicKey =
|
|
105
105
|
options.rsaPublicKey || readImportMetaEnv('VITE_GLOB_RSA_PUBLIC_KEY');
|
|
106
|
+
this.secureStoreKey =
|
|
107
|
+
options.secureStoreKey ||
|
|
108
|
+
readImportMetaEnv('VITE_APP_STORE_SECURE_KEY');
|
|
109
|
+
this.secureStoreNamespace = options.secureStoreNamespace;
|
|
106
110
|
this.storage = options.storage;
|
|
107
111
|
this.tokenKey = options.tokenKey || 'accessToken';
|
|
108
112
|
this.tokenStorageKey = options.tokenStorageKey || 'accessToken';
|
|
113
|
+
this.tokenProvider = options.tokenProvider;
|
|
114
|
+
this.tokenQueryParam = options.tokenQueryParam || 'runtimeToken';
|
|
109
115
|
this.uploadPath = options.uploadPath || '/api/file/upload';
|
|
110
116
|
this.routePermissionPath =
|
|
111
117
|
options.routePermissionPath || '/api/PluginApp/GetRouters';
|
|
@@ -120,19 +126,25 @@ export class WiseAiFrontSdk {
|
|
|
120
126
|
netBasePath: this.netBasePath,
|
|
121
127
|
};
|
|
122
128
|
}
|
|
123
|
-
getCurrentToken() {
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
async getCurrentToken() {
|
|
130
|
+
const providedToken = await this.tokenProvider?.();
|
|
131
|
+
const candidates = [
|
|
132
|
+
this.accessToken,
|
|
133
|
+
providedToken?.trim() || '',
|
|
134
|
+
resolveTokenFromLocation(this.tokenQueryParam),
|
|
135
|
+
resolveTokenFromStorage(this.storage, this.tokenKey, this.tokenStorageKey),
|
|
136
|
+
...resolveTokensFromSecureStore(this.storage, this.secureStoreKey, this.secureStoreNamespace),
|
|
137
|
+
];
|
|
138
|
+
for (const token of candidates) {
|
|
139
|
+
if (token && !isJwtExpired(token)) {
|
|
140
|
+
return token;
|
|
141
|
+
}
|
|
131
142
|
}
|
|
132
|
-
|
|
143
|
+
clearToken(this.storage, this.tokenKey, this.tokenStorageKey);
|
|
144
|
+
return '';
|
|
133
145
|
}
|
|
134
146
|
async ensureToken() {
|
|
135
|
-
const current = this.getCurrentToken();
|
|
147
|
+
const current = await this.getCurrentToken();
|
|
136
148
|
if (current) {
|
|
137
149
|
this.accessToken = current;
|
|
138
150
|
return current;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { decodeJwtPayload, isJwtExpired } from './jwt';
|
|
2
2
|
export { notifyReloginRequired } from './bridge';
|
|
3
3
|
import { WiseAiFrontSdk } from './http';
|
|
4
|
-
export { persistToken, resolveTokenFromStorage } from './storage';
|
|
4
|
+
export { persistToken, resolveTokenFromLocation, resolveTokenFromStorage, resolveTokensFromSecureStore, } from './storage';
|
|
5
5
|
export type { ApiBase, HttpMethod, JwtPayload, LoginCredentials, LoginModalOptions, LoginResult, MenuPermissionNode, MenuRouteMeta, RequestBodyType, RequestOptions, UploadOptions, WiseAiEnvironment, WiseAiFrontSdkOptions, WiseAiFrontSdkState, } from './types';
|
|
6
6
|
export { WiseAiFrontSdk } from './http';
|
|
7
7
|
export declare function createWiseAiFrontSdk(options: import('./types').WiseAiFrontSdkOptions): WiseAiFrontSdk;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { decodeJwtPayload, isJwtExpired } from './jwt';
|
|
2
2
|
export { notifyReloginRequired } from './bridge';
|
|
3
3
|
import { WiseAiFrontSdk } from './http';
|
|
4
|
-
export { persistToken, resolveTokenFromStorage } from './storage';
|
|
4
|
+
export { persistToken, resolveTokenFromLocation, resolveTokenFromStorage, resolveTokensFromSecureStore, } from './storage';
|
|
5
5
|
export { WiseAiFrontSdk } from './http';
|
|
6
6
|
export function createWiseAiFrontSdk(options) {
|
|
7
7
|
return new WiseAiFrontSdk(options);
|
package/dist/storage.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export declare function resolveTokenFromStorage(storage: Storage | undefined, tokenKey: string, tokenStorageKey: string): string;
|
|
2
|
+
export declare function resolveTokenFromLocation(queryParam?: string): string;
|
|
3
|
+
export declare function resolveTokensFromSecureStore(storage: Storage | undefined, encryptionSecret: string, namespace?: string): string[];
|
|
2
4
|
export declare function persistToken(storage: Storage | undefined, tokenKey: string, tokenStorageKey: string, token: string): void;
|
|
3
5
|
export declare function clearToken(storage: Storage | undefined, tokenKey: string, tokenStorageKey: string): void;
|
package/dist/storage.js
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
|
+
import SecureLS from 'secure-ls';
|
|
2
|
+
const SecureLSConstructor = (SecureLS.default || SecureLS);
|
|
1
3
|
const ACCESS_TOKEN_KEY = 'accessToken';
|
|
2
4
|
const REFRESH_TOKEN_KEY = 'refreshToken';
|
|
5
|
+
function normalizeToken(value) {
|
|
6
|
+
if (typeof value === 'string') {
|
|
7
|
+
const normalized = value.trim();
|
|
8
|
+
if (!normalized) {
|
|
9
|
+
return '';
|
|
10
|
+
}
|
|
11
|
+
if ((normalized.startsWith('{') && normalized.endsWith('}')) ||
|
|
12
|
+
(normalized.startsWith('"') && normalized.endsWith('"'))) {
|
|
13
|
+
try {
|
|
14
|
+
return normalizeToken(JSON.parse(normalized));
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return normalized;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return normalized;
|
|
21
|
+
}
|
|
22
|
+
if (value && typeof value === 'object') {
|
|
23
|
+
const source = value;
|
|
24
|
+
return normalizeToken(source.accessToken ||
|
|
25
|
+
source.access_token ||
|
|
26
|
+
source.token ||
|
|
27
|
+
source.state);
|
|
28
|
+
}
|
|
29
|
+
return '';
|
|
30
|
+
}
|
|
3
31
|
function getSafeStorage(storage) {
|
|
4
32
|
if (storage) {
|
|
5
33
|
return storage;
|
|
@@ -7,14 +35,19 @@ function getSafeStorage(storage) {
|
|
|
7
35
|
if (typeof window === 'undefined') {
|
|
8
36
|
return null;
|
|
9
37
|
}
|
|
10
|
-
|
|
38
|
+
try {
|
|
39
|
+
return window.localStorage;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
11
44
|
}
|
|
12
45
|
export function resolveTokenFromStorage(storage, tokenKey, tokenStorageKey) {
|
|
13
46
|
const safeStorage = getSafeStorage(storage);
|
|
14
47
|
if (!safeStorage) {
|
|
15
48
|
return '';
|
|
16
49
|
}
|
|
17
|
-
return (safeStorage.getItem(tokenStorageKey) ||
|
|
50
|
+
return normalizeToken(safeStorage.getItem(tokenStorageKey) ||
|
|
18
51
|
safeStorage.getItem(tokenKey) ||
|
|
19
52
|
safeStorage.getItem(`${tokenStorageKey}:${ACCESS_TOKEN_KEY}`) ||
|
|
20
53
|
safeStorage.getItem(`${tokenKey}:${ACCESS_TOKEN_KEY}`) ||
|
|
@@ -22,6 +55,54 @@ export function resolveTokenFromStorage(storage, tokenKey, tokenStorageKey) {
|
|
|
22
55
|
safeStorage.getItem(`${tokenKey}:${REFRESH_TOKEN_KEY}`) ||
|
|
23
56
|
'');
|
|
24
57
|
}
|
|
58
|
+
export function resolveTokenFromLocation(queryParam = 'runtimeToken') {
|
|
59
|
+
if (typeof window === 'undefined') {
|
|
60
|
+
return '';
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
return normalizeToken(new URL(window.location.href).searchParams.get(queryParam));
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export function resolveTokensFromSecureStore(storage, encryptionSecret, namespace) {
|
|
70
|
+
const safeStorage = getSafeStorage(storage);
|
|
71
|
+
if (!safeStorage || !encryptionSecret) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
const explicitKey = namespace ? `${namespace}-core-access` : '';
|
|
75
|
+
const storeKeys = explicitKey
|
|
76
|
+
? [explicitKey]
|
|
77
|
+
: Array.from({ length: safeStorage.length }, (_, index) => safeStorage.key(index))
|
|
78
|
+
.filter((key) => Boolean(key?.endsWith('-core-access')))
|
|
79
|
+
.sort((left, right) => {
|
|
80
|
+
const prodDifference = Number(right.includes('-prod-core-access')) -
|
|
81
|
+
Number(left.includes('-prod-core-access'));
|
|
82
|
+
return prodDifference || right.localeCompare(left);
|
|
83
|
+
});
|
|
84
|
+
const tokens = [];
|
|
85
|
+
for (const storeKey of storeKeys) {
|
|
86
|
+
const storeNamespace = storeKey.slice(0, -'-core-access'.length);
|
|
87
|
+
try {
|
|
88
|
+
const secureStorage = new SecureLSConstructor({
|
|
89
|
+
encodingType: 'aes',
|
|
90
|
+
encryptionSecret,
|
|
91
|
+
isCompression: true,
|
|
92
|
+
metaKey: `${storeNamespace}-secure-meta`,
|
|
93
|
+
storage: safeStorage,
|
|
94
|
+
});
|
|
95
|
+
const token = normalizeToken(secureStorage.get(storeKey));
|
|
96
|
+
if (token && !tokens.includes(token)) {
|
|
97
|
+
tokens.push(token);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// Ignore stores encrypted with another key or using another format.
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return tokens;
|
|
105
|
+
}
|
|
25
106
|
export function persistToken(storage, tokenKey, tokenStorageKey, token) {
|
|
26
107
|
const safeStorage = getSafeStorage(storage);
|
|
27
108
|
if (!safeStorage) {
|
package/dist/types.d.ts
CHANGED
|
@@ -74,6 +74,7 @@ export interface LoginModalOptions {
|
|
|
74
74
|
title?: string;
|
|
75
75
|
}
|
|
76
76
|
export interface WiseAiFrontSdkOptions {
|
|
77
|
+
accessToken?: string;
|
|
77
78
|
appCode: string;
|
|
78
79
|
captchaPath?: string;
|
|
79
80
|
clientId?: string;
|
|
@@ -91,7 +92,11 @@ export interface WiseAiFrontSdkOptions {
|
|
|
91
92
|
parentWindow?: Window;
|
|
92
93
|
requestTimeoutMs?: number;
|
|
93
94
|
rsaPublicKey?: string;
|
|
95
|
+
secureStoreKey?: string;
|
|
96
|
+
secureStoreNamespace?: string;
|
|
94
97
|
storage?: Storage;
|
|
98
|
+
tokenProvider?: () => null | string | undefined | Promise<null | string | undefined>;
|
|
99
|
+
tokenQueryParam?: string;
|
|
95
100
|
tokenKey?: string;
|
|
96
101
|
tokenStorageKey?: string;
|
|
97
102
|
uploadPath?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workopilot/webapp-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "WiseAI front-end SDK for iframe-hosted third-party apps",
|
|
5
5
|
"keywords": "wiseai,workopilot",
|
|
6
6
|
"author": "workopilot",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"crypto-js": "^4.2.0",
|
|
27
|
-
"jsencrypt": "^3.3.2"
|
|
27
|
+
"jsencrypt": "^3.3.2",
|
|
28
|
+
"secure-ls": "^2.0.0"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
30
31
|
"@types/crypto-js": "^4.2.2",
|