@dplugin/core 2.1.0 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2 -2
- package/dist/index.mjs +162 -150
- package/dist/index.umd.js +2 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"error"};function
|
|
2
|
-
`)}function Te(e,n,t){return[e,
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"error"};function oe(e){return e&&Object.keys(I).forEach(n=>{e[n]!==void 0&&(I[n]=e[n])}),I}function O(){return I}const a=ce();function B(e,n){if(typeof e!="function")return!1;let t;try{t=Function.prototype.toString.call(e)}catch(o){return!1}const r=/^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(t);return r?r[1]===n:/\[native code\]/.test(t)&&!/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(t)}function se(e){try{const n=e();return n&&typeof n.then=="function"&&Promise.resolve(n).catch(()=>{}),!1}catch(n){const t=String(n&&n.message||"");return!!n&&n.name==="TypeError"&&/argument|required|参数/i.test(t)}}function ce(){const e=globalThis,n=typeof e.window!="undefined"&&e.window&&typeof e.window.document!="undefined",t={fetch:null,idbOpen:null,subtle:null,pristine:{fetch:!1,idbOpen:!1,subtle:!1}};typeof e.fetch=="function"&&(t.pristine.fetch=B(e.fetch,"fetch"),t.fetch=e.fetch.bind(e)),typeof e.indexedDB!="undefined"&&e.indexedDB&&typeof e.indexedDB.open=="function"&&(t.pristine.idbOpen=B(e.indexedDB.open,"open")&&(!n||se(()=>e.indexedDB.open())),t.idbOpen=e.indexedDB.open.bind(e.indexedDB));const r=e.crypto&&e.crypto.subtle?e.crypto.subtle:null;if(r){const o=["generateKey","exportKey","importKey","sign","verify","digest"],i={};let s=!0;o.forEach(c=>{typeof r[c]=="function"&&(i[c]=r[c].bind(r),s=s&&B(r[c],c))}),t.subtle=i,t.pristine.subtle=s}return t}function ue(){if(typeof window=="undefined"||typeof window.document=="undefined")return[];const e=[];return a.fetch&&!a.pristine.fetch&&e.push("fetch"),a.idbOpen&&!a.pristine.idbOpen&&e.push("indexedDB.open"),a.subtle&&!a.pristine.subtle&&e.push("crypto.subtle"),e}const f={OK:"200",LOGIN_PENDING:"202",LOGIN_TIMEOUT:"g.login.timeout",NETWORK:"NETWORK",TIMEOUT:"TIMEOUT"};class h extends Error{constructor(n,t){super(t||"请求失败"),this.name="BizError",this.code=n}}class ae extends h{constructor(n){super(f.LOGIN_TIMEOUT,n||"登录已失效"),this.name="AuthError"}}const V="dp.token",U="dp.sid",z="dp.ds";function fe(e){try{return localStorage.getItem(e)}catch(n){return null}}function J(e,n){try{n==null||n===""?localStorage.removeItem(e):localStorage.setItem(e,n)}catch(t){}}const M={};function F(e){const n=fe(e);return n===null?M[e]===void 0?null:M[e]:n}function j(e,n){M[e]=n,J(e,n)}const u={get token(){return F(V)},set token(e){j(V,e)},get sid(){return F(U)},set sid(e){j(U,e)},get ds(){return F(z)},set ds(e){j(z,e)},clear(){[V,U].forEach(e=>{M[e]=null,J(e,null)})}},le="dplugin",de=1,x="keys",ge={name:"ECDSA",namedCurve:"P-256"},he={name:"ECDSA",hash:"SHA-256"},D=new Map;let K=null;function v(){if(!a.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 a.subtle}function pe(){return K||(K=new Promise(e=>{if(!a.idbOpen){e(null);return}let n;try{n=a.idbOpen(le,de)}catch(t){e(null);return}n.onupgradeneeded=()=>{const t=n.result;t.objectStoreNames.contains(x)||t.createObjectStore(x)},n.onsuccess=()=>e(n.result),n.onerror=()=>e(null),n.onblocked=()=>e(null)}),K)}function Q(e,n){return pe().then(t=>t?new Promise(r=>{let o;try{o=t.transaction(x,e)}catch(c){r(null);return}const i=o.objectStore(x);let s=null;try{const c=n(i);c&&(c.onsuccess=()=>{s=c.result},c.onerror=()=>{s=null})}catch(c){s=null}o.oncomplete=()=>r(s),o.onerror=()=>r(null),o.onabort=()=>r(null)}):null)}async function P(e){return D.has(e)?Promise.resolve(D.get(e)):Q("readonly",n=>n.get(e)).then(n=>(n&&D.set(e,n),n||null))}function ee(e){const n=new Uint8Array(e);let t="";for(let r=0;r<n.length;r++)t+=String.fromCharCode(n[r]);return btoa(t).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function ne(e){let n=await P(e);return n||(n=await v().generateKey(ge,!1,["sign","verify"]),D.set(e,n),Q("readwrite",r=>r.put(n,e)).then(()=>n))}function we(){return ne("sso")}function me(){return ne("domain")}async function ye(){return await P("sso")}async function Oe(){return await P("domain")}async function te(){let e=await P("domain");const n=v(),t=await n.exportKey("spki",e.publicKey),r=await n.digest("SHA-256",t);return ee(r)}async function W(e){let n=await P(e);const r=await v().exportKey("spki",n.publicKey);let o="";const i=new Uint8Array(r);for(let s=0;s<i.length;s++)o+=String.fromCharCode(i[s]);return btoa(o)}async function Ee(e,n){const r=await v().sign(he,e.privateKey,new TextEncoder().encode(n));return ee(r)}const Se="-",_={BIZ:"BIZ",VTT:"VTT",VSD:"VSD",TOK:"TOK"};function y(e){return e==null||e===""?Se:String(e)}function be(e,n){const t=y(n.origin);return[e,n.method,n.path,t.toLowerCase(),y(n.ds),n.ts,y(n.token),y(n.dna),y(n.redirect)].join(`
|
|
2
|
+
`)}function Te(e,n,t){return[e,y(n.token),y(n.ds),n.ts,t].join(".")}function ke(e){const n=new Error(e);return n.code="KEY_MISSING",n}async function A(e,n,t){const r=t==="sso"?await ye():await Oe();if(!r)throw ke(t==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");let o=n.path.indexOf("?");o>0&&(n.path=n.path.substring(0,o));const i=await Ee(r,be(e,n));return Te(e,n,i)}let g=null;function Ie(e){try{const n=typeof location!="undefined"?location.href:void 0;return new URL(e||"",n).origin}catch(n){return null}}function Pe(){g=null}function Ne(){if(g)return g;const e=O();if(e.mode==="sign"||e.mode==="cookie")return g=e.mode,g;const n=typeof location!="undefined"?location.origin:null;if(!n)return g="sign",g;const t=Ie(e.apiBase||n);return g=t&&t===n?"cookie":"sign",g}function E(){return Ne()==="cookie"}function Re(e,n){let t=O();throw t.onError&&n.showError&&t.onError({code:e.code,message:e.message}),new h(e.code,e.message)}let C,Y=null;function Ke(){return C||(C=Promise.resolve().then(()=>Y()).finally(()=>{C=null})),C}async function Ce(e,n,t,r,o){const i=O(),s=o||i.timeout,c=typeof AbortController!="undefined"?new AbortController:null,N=c?setTimeout(()=>c.abort(),s):null,m={method:n};if(e?m.credentials="same-origin":m.credentials="omit",m.signal=c?c.signal:void 0,typeof FormData!="undefined"&&r instanceof FormData?m.body=r:r!=null&&(m.headers={"Content-Type":"text/plain"},m.body=JSON.stringify(r)),!a.fetch)throw new h(f.NETWORK,"当前环境不支持 fetch");let R;try{R=await a.fetch(t,m)}catch(G){throw N&&clearTimeout(N),G&&G.name==="AbortError"?new h(f.TIMEOUT,"请求超时,请重试"):new h(f.NETWORK,"网络异常,请检查网络后重试")}N&&clearTimeout(N);const L=await R.text();let d=null;if(L)try{d=JSON.parse(L)}catch(G){d=null}if(!R.ok)throw new h(String(R.status),d&&d.message||L||"请求失败");if(!d||d.code===null||d.code===void 0)throw new h("500","响应格式异常");return d.code=String(d.code),d}async function l(e,n=null,t=null){const r=t||{};r.method||(r.method="POST"),r.showError=r.showError!==!1;let o=!0;/^https?:\/\/.+$/.test(e)?e.indexOf(O().apiBase)===-1&&(o=!1):e=O().apiBase+e,o&&!E()&&(o=!1);let i=e;if(!o&&u.ds&&u.token){let c=await A(_.BIZ,{method:r.method,path:new URL(e,location.href).pathname,ds:u.ds,ts:Date.now(),token:u.token},"domain");i.indexOf("?")>0?i=i+"&dsign="+c:i=i+"?dsign="+c}const s=await Ce(o,r.method,i,n,r.timeout);return s.code===f.LOGIN_TIMEOUT&&!r.skipRefresh&&Y&&await Ke()?l(e,n,Object.assign({},r,{skipRefresh:!0})):(/^2.+/.test(s.code)||Re(s,r),s)}async function De(e,n,t,r){const o=new FormData;return Object.keys(t||{}).forEach(i=>{const s=t[i];s!=null&&o.append(i,s)}),o.append("file",n),l(e,o,r)}function Me(e){Y=e}function xe(){const e=typeof navigator!="undefined"&&navigator.userAgent||"",n={device_brand:_e(e),device_model:Ae(e),browser_name:ve(e),browser_version:Le(e),os_name:Ge(e),os_version:Be(e)},t=u.ds;return t&&(n.device_id=t),n}function w(e,n){const t=e.match(n);return t?t[1]:""}function _e(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 Ae(e){if(/iPhone/.test(e))return"iPhone";if(/iPad/.test(e))return"iPad";const n=e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return n?n[1].trim():/Macintosh/.test(e)?"Mac":""}function ve(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 Le(e){return/MicroMessenger\/([\d.]+)/.test(e)?w(e,/MicroMessenger\/([\d.]+)/):/Edg[A-Za-z]*\/([\d.]+)/.test(e)?w(e,/Edg[A-Za-z]*\/([\d.]+)/):/OPR\/([\d.]+)/.test(e)?w(e,/OPR\/([\d.]+)/):/Firefox\/([\d.]+)/.test(e)?w(e,/Firefox\/([\d.]+)/):/CriOS\/([\d.]+)/.test(e)?w(e,/CriOS\/([\d.]+)/):/Chrome\/([\d.]+)/.test(e)?w(e,/Chrome\/([\d.]+)/):/Version\/([\d.]+).*Safari/.test(e)?w(e,/Version\/([\d.]+)/):""}function Ge(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 Be(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 n=e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(n)return n[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(e))return w(e,/Android[ _]([\d.]+)/);const t=e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return t?t[1].replace(/_/g,"."):""}function re(){if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(e){return""}}let H,Z;function ie(e){const n=typeof location!="undefined"&&location.search||"";return(!H||Z!==n)&&(Z=n,H=new URLSearchParams(n)),H.get(e)}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"};Me(Ze);function Ve(e){return new Promise(n=>setTimeout(n,e))}let S=null;async function Ue(e,n){if(S)return S;const t=xe();return S=(E()?Fe(e,n,t):je(e,n,t)).finally(()=>{S=null}),S}async function Fe(e,n,t){let r=await l(e,Object.assign({},n,{device:t}));return await $(r.data,!0,!0)}async function je(e,n,t){await we();const r=await W("sso"),o=await l(e,Object.assign({},n,{device:t,pk:r}));return await $(o.data,!0,!0)}let b;async function $(e,n=!1,t=!1){return b||(b=He(e,n,t).finally(()=>{b=null}),b)}async function He(e,n,t){e||(e={});let r;if(t){r=ie("referer");let o=O(),i;for(let s=0;s<o.pollMax;s++){if(i=E()?await q(e,r,n):await X(e,r,n),i.code!==f.LOGIN_PENDING)return i;await Ve(o.pollInterval)}return i}else return r=window.location.href,E()?await q(e,r,n):await X(e,r,n)}async function q(e,n,t){return await l(p.COOKIE.VERIFY,{redirect:n},{skipRefresh:!0,showError:t})}async function X(e,n,t){let r={method:"POST",path:p.SIGN.VERIFY,origin:re(),ts:Date.now(),ds:u.ds,dna:ie("dna"),redirect:n},o;if(e&&e.token)r.token=e.token,o=await A(_.VTT,r,"sso");else{if(!u.sid||!u.ds)throw new h(f.LOGIN_TIMEOUT,"未登录");r.token=u.sid,o=await A(_.VSD,r,"sso")}const i=await l(p.SIGN.VERIFY,{dsign:o,dna:r.dna,redirect:r.redirect},{skipRefresh:!0,showError:t});return i.code===f.OK&&(u.sid=i.data.sid,i.data.ds&&(u.ds=i.data.ds)),i}let T;async function We(e,n=!0){return T||(T=Ye(e,n).finally(()=>{T=null}),T)}async function Ye(e,n){return E()?await $e(e,n):await ze(e,n)}async function $e(e,n){return await l(p.COOKIE.TOKEN,null,{skipRefresh:!0,showError:n})}async function ze(e,n){let t={method:"POST",path:p.SIGN.TOKEN,origin:re(),ts:Date.now(),ds:u.ds},r;if(e&&e.code)t.token=e.code,r=await A(_.TOK,t,"domain");else throw new h(f.LOGIN_TIMEOUT,"未登录");let o=await W("domain");const i=await l(p.SIGN.TOKEN,{dsign:r,pk:o},{skipRefresh:!0,showError:n});return i.code===f.OK&&(u.token=i.data.token,i.data.ds&&(u.ds=i.data.ds)),i}let k;async function Ze(){return k||(k=qe().finally(()=>{k=null}),k)}async function qe(){return E()?await Xe():await Je()}async function Xe(){return await l(p.COOKIE.REFRESH,null,{skipRefresh:!0,showError:!1})}async function Je(){const e=u.token,n=u.ds;if(!e||!n)throw new ae;const t=await l(p.SIGN.REFRESH,null,{skipRefresh:!0,showError:!1});return t.code===f.OK&&(t.data.ds&&(u.ds=t.data.ds),u.token=t.data.token),t}async function Qe(){(await l(p.LOGOUT)).code===f.OK&&u.clear()}async function en(){let e=O(),n=encodeURIComponent(window.location.href),t=e.loginUrl+"?referer="+n;if(!E()){await me();let r=await te();t+="&dna="+r}(window.top||window).location.href=t}function nn(e){const n=oe(e);if(Pe(),n.nativeGuard!=="off"){const t=ue();if(t.length){const r="[dplugin/core] 这些原生能力在模块加载前已被替换:"+t.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(n.nativeGuard==="error")throw new Error(r);typeof console!="undefined"&&console.warn(r)}}}exports.getDna=te;exports.getPublicKey=W;exports.gotoLoginPage=en;exports.login=Ue;exports.logout=Qe;exports.request=l;exports.setup=nn;exports.token=We;exports.upload=De;exports.verify=$;
|
|
3
3
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.mjs
CHANGED
|
@@ -37,7 +37,7 @@ function B(e, n) {
|
|
|
37
37
|
let t;
|
|
38
38
|
try {
|
|
39
39
|
t = Function.prototype.toString.call(e);
|
|
40
|
-
} catch (
|
|
40
|
+
} catch (o) {
|
|
41
41
|
return !1;
|
|
42
42
|
}
|
|
43
43
|
const r = /^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(t);
|
|
@@ -63,11 +63,11 @@ function se() {
|
|
|
63
63
|
typeof e.fetch == "function" && (t.pristine.fetch = B(e.fetch, "fetch"), t.fetch = e.fetch.bind(e)), typeof e.indexedDB != "undefined" && e.indexedDB && typeof e.indexedDB.open == "function" && (t.pristine.idbOpen = B(e.indexedDB.open, "open") && (!n || oe(() => e.indexedDB.open())), t.idbOpen = e.indexedDB.open.bind(e.indexedDB));
|
|
64
64
|
const r = e.crypto && e.crypto.subtle ? e.crypto.subtle : null;
|
|
65
65
|
if (r) {
|
|
66
|
-
const
|
|
66
|
+
const o = ["generateKey", "exportKey", "importKey", "sign", "verify", "digest"], i = {};
|
|
67
67
|
let s = !0;
|
|
68
|
-
|
|
69
|
-
typeof r[c] == "function" && (
|
|
70
|
-
}), t.subtle =
|
|
68
|
+
o.forEach((c) => {
|
|
69
|
+
typeof r[c] == "function" && (i[c] = r[c].bind(r), s = s && B(r[c], c));
|
|
70
|
+
}), t.subtle = i, t.pristine.subtle = s;
|
|
71
71
|
}
|
|
72
72
|
return t;
|
|
73
73
|
}
|
|
@@ -94,7 +94,7 @@ class ue extends h {
|
|
|
94
94
|
super(f.LOGIN_TIMEOUT, n || "登录已失效"), this.name = "AuthError";
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
const V = "dp.token", U = "dp.sid",
|
|
97
|
+
const V = "dp.token", U = "dp.sid", Y = "dp.ds";
|
|
98
98
|
function ae(e) {
|
|
99
99
|
try {
|
|
100
100
|
return localStorage.getItem(e);
|
|
@@ -102,7 +102,7 @@ function ae(e) {
|
|
|
102
102
|
return null;
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
function
|
|
105
|
+
function X(e, n) {
|
|
106
106
|
try {
|
|
107
107
|
n == null || n === "" ? localStorage.removeItem(e) : localStorage.setItem(e, n);
|
|
108
108
|
} catch (t) {
|
|
@@ -114,7 +114,7 @@ function F(e) {
|
|
|
114
114
|
return n === null ? D[e] === void 0 ? null : D[e] : n;
|
|
115
115
|
}
|
|
116
116
|
function H(e, n) {
|
|
117
|
-
D[e] = n,
|
|
117
|
+
D[e] = n, X(e, n);
|
|
118
118
|
}
|
|
119
119
|
const u = {
|
|
120
120
|
get token() {
|
|
@@ -130,10 +130,10 @@ const u = {
|
|
|
130
130
|
H(U, e);
|
|
131
131
|
},
|
|
132
132
|
get ds() {
|
|
133
|
-
return F(
|
|
133
|
+
return F(Y);
|
|
134
134
|
},
|
|
135
135
|
set ds(e) {
|
|
136
|
-
H(
|
|
136
|
+
H(Y, e);
|
|
137
137
|
},
|
|
138
138
|
/**
|
|
139
139
|
* 清登录态。
|
|
@@ -143,7 +143,7 @@ const u = {
|
|
|
143
143
|
*/
|
|
144
144
|
clear() {
|
|
145
145
|
[V, U].forEach((e) => {
|
|
146
|
-
D[e] = null,
|
|
146
|
+
D[e] = null, X(e, null);
|
|
147
147
|
});
|
|
148
148
|
}
|
|
149
149
|
}, fe = "dplugin", le = 1, M = "keys", de = { name: "ECDSA", namedCurve: "P-256" }, ge = { name: "ECDSA", hash: "SHA-256" }, x = /* @__PURE__ */ new Map();
|
|
@@ -174,19 +174,19 @@ function he() {
|
|
|
174
174
|
}, n.onsuccess = () => e(n.result), n.onerror = () => e(null), n.onblocked = () => e(null);
|
|
175
175
|
}), K);
|
|
176
176
|
}
|
|
177
|
-
function
|
|
177
|
+
function q(e, n) {
|
|
178
178
|
return he().then((t) => t ? new Promise((r) => {
|
|
179
|
-
let
|
|
179
|
+
let o;
|
|
180
180
|
try {
|
|
181
|
-
|
|
181
|
+
o = t.transaction(M, e);
|
|
182
182
|
} catch (c) {
|
|
183
183
|
r(null);
|
|
184
184
|
return;
|
|
185
185
|
}
|
|
186
|
-
const
|
|
186
|
+
const i = o.objectStore(M);
|
|
187
187
|
let s = null;
|
|
188
188
|
try {
|
|
189
|
-
const c = n(
|
|
189
|
+
const c = n(i);
|
|
190
190
|
c && (c.onsuccess = () => {
|
|
191
191
|
s = c.result;
|
|
192
192
|
}, c.onerror = () => {
|
|
@@ -195,63 +195,63 @@ function J(e, n) {
|
|
|
195
195
|
} catch (c) {
|
|
196
196
|
s = null;
|
|
197
197
|
}
|
|
198
|
-
|
|
198
|
+
o.oncomplete = () => r(s), o.onerror = () => r(null), o.onabort = () => r(null);
|
|
199
199
|
}) : null);
|
|
200
200
|
}
|
|
201
|
-
async function
|
|
202
|
-
return x.has(e) ? Promise.resolve(x.get(e)) :
|
|
201
|
+
async function P(e) {
|
|
202
|
+
return x.has(e) ? Promise.resolve(x.get(e)) : q("readonly", (n) => n.get(e)).then((n) => (n && x.set(e, n), n || null));
|
|
203
203
|
}
|
|
204
|
-
function
|
|
204
|
+
function J(e) {
|
|
205
205
|
const n = new Uint8Array(e);
|
|
206
206
|
let t = "";
|
|
207
207
|
for (let r = 0; r < n.length; r++)
|
|
208
208
|
t += String.fromCharCode(n[r]);
|
|
209
209
|
return btoa(t).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
210
210
|
}
|
|
211
|
-
async function
|
|
212
|
-
let n = await
|
|
213
|
-
return n || (n = await v().generateKey(de, !1, ["sign", "verify"]), x.set(e, n),
|
|
211
|
+
async function Q(e) {
|
|
212
|
+
let n = await P(e);
|
|
213
|
+
return n || (n = await v().generateKey(de, !1, ["sign", "verify"]), x.set(e, n), q("readwrite", (r) => r.put(n, e)).then(() => n));
|
|
214
214
|
}
|
|
215
215
|
function pe() {
|
|
216
|
-
return
|
|
216
|
+
return Q("sso");
|
|
217
217
|
}
|
|
218
|
-
function
|
|
219
|
-
return
|
|
220
|
-
}
|
|
221
|
-
async function we() {
|
|
222
|
-
return await N("sso");
|
|
218
|
+
function we() {
|
|
219
|
+
return Q("domain");
|
|
223
220
|
}
|
|
224
221
|
async function me() {
|
|
225
|
-
return await
|
|
222
|
+
return await P("sso");
|
|
226
223
|
}
|
|
227
|
-
async function
|
|
228
|
-
|
|
224
|
+
async function Oe() {
|
|
225
|
+
return await P("domain");
|
|
226
|
+
}
|
|
227
|
+
async function ye() {
|
|
228
|
+
let e = await P("domain");
|
|
229
229
|
const n = v(), t = await n.exportKey("spki", e.publicKey), r = await n.digest("SHA-256", t);
|
|
230
|
-
return
|
|
230
|
+
return J(r);
|
|
231
231
|
}
|
|
232
|
-
async function
|
|
233
|
-
let n = await
|
|
232
|
+
async function ee(e) {
|
|
233
|
+
let n = await P(e);
|
|
234
234
|
const r = await v().exportKey("spki", n.publicKey);
|
|
235
|
-
let
|
|
236
|
-
const
|
|
237
|
-
for (let s = 0; s <
|
|
238
|
-
|
|
239
|
-
return btoa(
|
|
235
|
+
let o = "";
|
|
236
|
+
const i = new Uint8Array(r);
|
|
237
|
+
for (let s = 0; s < i.length; s++)
|
|
238
|
+
o += String.fromCharCode(i[s]);
|
|
239
|
+
return btoa(o);
|
|
240
240
|
}
|
|
241
|
-
async function
|
|
241
|
+
async function Ee(e, n) {
|
|
242
242
|
const r = await v().sign(ge, e.privateKey, new TextEncoder().encode(n));
|
|
243
|
-
return
|
|
243
|
+
return J(r);
|
|
244
244
|
}
|
|
245
|
-
const
|
|
245
|
+
const Se = "-", _ = {
|
|
246
246
|
BIZ: "BIZ",
|
|
247
247
|
VTT: "VTT",
|
|
248
248
|
VSD: "VSD",
|
|
249
249
|
TOK: "TOK"
|
|
250
250
|
};
|
|
251
251
|
function O(e) {
|
|
252
|
-
return e == null || e === "" ?
|
|
252
|
+
return e == null || e === "" ? Se : String(e);
|
|
253
253
|
}
|
|
254
|
-
function
|
|
254
|
+
function be(e, n) {
|
|
255
255
|
const t = O(n.origin);
|
|
256
256
|
return [
|
|
257
257
|
e,
|
|
@@ -266,24 +266,24 @@ function Ee(e, n) {
|
|
|
266
266
|
].join(`
|
|
267
267
|
`);
|
|
268
268
|
}
|
|
269
|
-
function
|
|
269
|
+
function Te(e, n, t) {
|
|
270
270
|
return [e, O(n.token), O(n.ds), n.ts, t].join(".");
|
|
271
271
|
}
|
|
272
|
-
function
|
|
272
|
+
function ke(e) {
|
|
273
273
|
const n = new Error(e);
|
|
274
274
|
return n.code = "KEY_MISSING", n;
|
|
275
275
|
}
|
|
276
276
|
async function A(e, n, t) {
|
|
277
|
-
const r = t === "sso" ? await
|
|
277
|
+
const r = t === "sso" ? await me() : await Oe();
|
|
278
278
|
if (!r)
|
|
279
|
-
throw
|
|
280
|
-
let
|
|
281
|
-
|
|
282
|
-
const
|
|
283
|
-
return
|
|
279
|
+
throw ke(t === "sso" ? "缺少登录签名密钥" : "缺少业务签名密钥");
|
|
280
|
+
let o = n.path.indexOf("?");
|
|
281
|
+
o > 0 && (n.path = n.path.substring(0, o));
|
|
282
|
+
const i = await Ee(r, be(e, n));
|
|
283
|
+
return Te(e, n, i);
|
|
284
284
|
}
|
|
285
285
|
let g = null;
|
|
286
|
-
function
|
|
286
|
+
function Ie(e) {
|
|
287
287
|
try {
|
|
288
288
|
const n = typeof location != "undefined" ? location.href : void 0;
|
|
289
289
|
return new URL(e || "", n).origin;
|
|
@@ -291,10 +291,10 @@ function Te(e) {
|
|
|
291
291
|
return null;
|
|
292
292
|
}
|
|
293
293
|
}
|
|
294
|
-
function
|
|
294
|
+
function Pe() {
|
|
295
295
|
g = null;
|
|
296
296
|
}
|
|
297
|
-
function
|
|
297
|
+
function Ne() {
|
|
298
298
|
if (g)
|
|
299
299
|
return g;
|
|
300
300
|
const e = y();
|
|
@@ -303,24 +303,24 @@ function Ie() {
|
|
|
303
303
|
const n = typeof location != "undefined" ? location.origin : null;
|
|
304
304
|
if (!n)
|
|
305
305
|
return g = "sign", g;
|
|
306
|
-
const t =
|
|
306
|
+
const t = Ie(e.apiBase || n);
|
|
307
307
|
return g = t && t === n ? "cookie" : "sign", g;
|
|
308
308
|
}
|
|
309
309
|
function E() {
|
|
310
|
-
return
|
|
310
|
+
return Ne() === "cookie";
|
|
311
311
|
}
|
|
312
|
-
function
|
|
312
|
+
function Re(e, n) {
|
|
313
313
|
let t = y();
|
|
314
314
|
throw t.onError && n.showError && t.onError({ code: e.code, message: e.message }), new h(e.code, e.message);
|
|
315
315
|
}
|
|
316
|
-
let C,
|
|
317
|
-
function
|
|
318
|
-
return C || (C = Promise.resolve().then(() =>
|
|
316
|
+
let C, W = null;
|
|
317
|
+
function Ke() {
|
|
318
|
+
return C || (C = Promise.resolve().then(() => W()).finally(() => {
|
|
319
319
|
C = null;
|
|
320
320
|
})), C;
|
|
321
321
|
}
|
|
322
|
-
async function
|
|
323
|
-
const
|
|
322
|
+
async function Ce(e, n, t, r, o) {
|
|
323
|
+
const i = y(), s = o || i.timeout, c = typeof AbortController != "undefined" ? new AbortController() : null, N = c ? setTimeout(() => c.abort(), s) : null, m = {
|
|
324
324
|
method: n
|
|
325
325
|
};
|
|
326
326
|
if (e ? m.credentials = "same-origin" : m.credentials = "omit", m.signal = c ? c.signal : void 0, typeof FormData != "undefined" && r instanceof FormData ? m.body = r : r != null && (m.headers = { "Content-Type": "text/plain" }, m.body = JSON.stringify(r)), !a.fetch)
|
|
@@ -329,9 +329,9 @@ async function Re(e, n, t, r, i) {
|
|
|
329
329
|
try {
|
|
330
330
|
R = await a.fetch(t, m);
|
|
331
331
|
} catch (L) {
|
|
332
|
-
throw
|
|
332
|
+
throw N && clearTimeout(N), L && L.name === "AbortError" ? new h(f.TIMEOUT, "请求超时,请重试") : new h(f.NETWORK, "网络异常,请检查网络后重试");
|
|
333
333
|
}
|
|
334
|
-
|
|
334
|
+
N && clearTimeout(N);
|
|
335
335
|
const G = await R.text();
|
|
336
336
|
let d = null;
|
|
337
337
|
if (G)
|
|
@@ -349,10 +349,10 @@ async function Re(e, n, t, r, i) {
|
|
|
349
349
|
async function l(e, n = null, t = null) {
|
|
350
350
|
const r = t || {};
|
|
351
351
|
r.method || (r.method = "POST"), r.showError = r.showError !== !1;
|
|
352
|
-
let
|
|
353
|
-
/^https?:\/\/.+$/.test(e) ? e.indexOf(y().apiBase) === -1 && (
|
|
354
|
-
let
|
|
355
|
-
if (!
|
|
352
|
+
let o = !0;
|
|
353
|
+
/^https?:\/\/.+$/.test(e) ? e.indexOf(y().apiBase) === -1 && (o = !1) : e = y().apiBase + e, o && !E() && (o = !1);
|
|
354
|
+
let i = e;
|
|
355
|
+
if (!o && u.ds && u.token) {
|
|
356
356
|
let c = await A(_.BIZ, {
|
|
357
357
|
method: r.method,
|
|
358
358
|
path: new URL(e, location.href).pathname,
|
|
@@ -360,29 +360,29 @@ async function l(e, n = null, t = null) {
|
|
|
360
360
|
ts: Date.now(),
|
|
361
361
|
token: u.token
|
|
362
362
|
}, "domain");
|
|
363
|
-
|
|
363
|
+
i.indexOf("?") > 0 ? i = i + "&dsign=" + c : i = i + "?dsign=" + c;
|
|
364
364
|
}
|
|
365
|
-
const s = await
|
|
366
|
-
return s.code === f.LOGIN_TIMEOUT && !r.skipRefresh &&
|
|
365
|
+
const s = await Ce(o, r.method, i, n, r.timeout);
|
|
366
|
+
return s.code === f.LOGIN_TIMEOUT && !r.skipRefresh && W && await Ke() ? l(e, n, Object.assign({}, r, { skipRefresh: !0 })) : (/^2.+/.test(s.code) || Re(s, r), s);
|
|
367
367
|
}
|
|
368
368
|
async function Xe(e, n, t, r) {
|
|
369
|
-
const
|
|
370
|
-
return Object.keys(t || {}).forEach((
|
|
371
|
-
const s = t[
|
|
372
|
-
s != null &&
|
|
373
|
-
}),
|
|
369
|
+
const o = new FormData();
|
|
370
|
+
return Object.keys(t || {}).forEach((i) => {
|
|
371
|
+
const s = t[i];
|
|
372
|
+
s != null && o.append(i, s);
|
|
373
|
+
}), o.append("file", n), l(e, o, r);
|
|
374
374
|
}
|
|
375
|
-
function
|
|
376
|
-
|
|
375
|
+
function xe(e) {
|
|
376
|
+
W = e;
|
|
377
377
|
}
|
|
378
|
-
function
|
|
378
|
+
function De() {
|
|
379
379
|
const e = typeof navigator != "undefined" && navigator.userAgent || "", n = {
|
|
380
|
-
device_brand:
|
|
381
|
-
device_model:
|
|
382
|
-
browser_name:
|
|
383
|
-
browser_version:
|
|
384
|
-
os_name:
|
|
385
|
-
os_version:
|
|
380
|
+
device_brand: Me(e),
|
|
381
|
+
device_model: _e(e),
|
|
382
|
+
browser_name: Ae(e),
|
|
383
|
+
browser_version: ve(e),
|
|
384
|
+
os_name: Ge(e),
|
|
385
|
+
os_version: Le(e)
|
|
386
386
|
}, t = u.ds;
|
|
387
387
|
return t && (n.device_id = t), n;
|
|
388
388
|
}
|
|
@@ -390,25 +390,25 @@ function w(e, n) {
|
|
|
390
390
|
const t = e.match(n);
|
|
391
391
|
return t ? t[1] : "";
|
|
392
392
|
}
|
|
393
|
-
function
|
|
393
|
+
function Me(e) {
|
|
394
394
|
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" : "";
|
|
395
395
|
}
|
|
396
|
-
function
|
|
396
|
+
function _e(e) {
|
|
397
397
|
if (/iPhone/.test(e)) return "iPhone";
|
|
398
398
|
if (/iPad/.test(e)) return "iPad";
|
|
399
399
|
const n = e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);
|
|
400
400
|
return n ? n[1].trim() : /Macintosh/.test(e) ? "Mac" : "";
|
|
401
401
|
}
|
|
402
|
-
function
|
|
402
|
+
function Ae(e) {
|
|
403
403
|
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" : "";
|
|
404
404
|
}
|
|
405
|
-
function
|
|
405
|
+
function ve(e) {
|
|
406
406
|
return /MicroMessenger\/([\d.]+)/.test(e) ? w(e, /MicroMessenger\/([\d.]+)/) : /Edg[A-Za-z]*\/([\d.]+)/.test(e) ? w(e, /Edg[A-Za-z]*\/([\d.]+)/) : /OPR\/([\d.]+)/.test(e) ? w(e, /OPR\/([\d.]+)/) : /Firefox\/([\d.]+)/.test(e) ? w(e, /Firefox\/([\d.]+)/) : /CriOS\/([\d.]+)/.test(e) ? w(e, /CriOS\/([\d.]+)/) : /Chrome\/([\d.]+)/.test(e) ? w(e, /Chrome\/([\d.]+)/) : /Version\/([\d.]+).*Safari/.test(e) ? w(e, /Version\/([\d.]+)/) : "";
|
|
407
407
|
}
|
|
408
|
-
function
|
|
408
|
+
function Ge(e) {
|
|
409
409
|
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" : "";
|
|
410
410
|
}
|
|
411
|
-
function
|
|
411
|
+
function Le(e) {
|
|
412
412
|
if (/Windows NT 10\.0/.test(e)) return "10/11";
|
|
413
413
|
if (/Windows NT 6\.3/.test(e)) return "8.1";
|
|
414
414
|
if (/Windows NT 6\.1/.test(e)) return "7";
|
|
@@ -418,7 +418,7 @@ function ve(e) {
|
|
|
418
418
|
const t = e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);
|
|
419
419
|
return t ? t[1].replace(/_/g, ".") : "";
|
|
420
420
|
}
|
|
421
|
-
function
|
|
421
|
+
function ne() {
|
|
422
422
|
if (location.origin)
|
|
423
423
|
return location.origin.toLowerCase();
|
|
424
424
|
try {
|
|
@@ -427,10 +427,10 @@ function te() {
|
|
|
427
427
|
return "";
|
|
428
428
|
}
|
|
429
429
|
}
|
|
430
|
-
let j,
|
|
431
|
-
function
|
|
430
|
+
let j, $;
|
|
431
|
+
function te(e) {
|
|
432
432
|
const n = typeof location != "undefined" && location.search || "";
|
|
433
|
-
return (!j ||
|
|
433
|
+
return (!j || $ !== n) && ($ = n, j = new URLSearchParams(n)), j.get(e);
|
|
434
434
|
}
|
|
435
435
|
const p = {
|
|
436
436
|
SIGN: {
|
|
@@ -445,110 +445,113 @@ const p = {
|
|
|
445
445
|
},
|
|
446
446
|
LOGOUT: "/api/safe/dplugin/login/logout"
|
|
447
447
|
};
|
|
448
|
-
|
|
449
|
-
function
|
|
448
|
+
xe(Ye);
|
|
449
|
+
function Be(e) {
|
|
450
450
|
return new Promise((n) => setTimeout(n, e));
|
|
451
451
|
}
|
|
452
452
|
let S = null;
|
|
453
453
|
async function qe(e, n) {
|
|
454
454
|
if (S)
|
|
455
455
|
return S;
|
|
456
|
-
const t =
|
|
457
|
-
return S = (E() ?
|
|
456
|
+
const t = De();
|
|
457
|
+
return S = (E() ? Ve(e, n, t) : Ue(e, n, t)).finally(() => {
|
|
458
458
|
S = null;
|
|
459
459
|
}), S;
|
|
460
460
|
}
|
|
461
|
-
async function
|
|
461
|
+
async function Ve(e, n, t) {
|
|
462
462
|
let r = await l(e, Object.assign({}, n, { device: t }));
|
|
463
463
|
return await re(r.data, !0, !0);
|
|
464
464
|
}
|
|
465
|
-
async function
|
|
465
|
+
async function Ue(e, n, t) {
|
|
466
466
|
await pe();
|
|
467
|
-
const r = await
|
|
468
|
-
return await re(
|
|
467
|
+
const r = await ee("sso"), o = await l(e, Object.assign({}, n, { device: t, pk: r }));
|
|
468
|
+
return await re(o.data, !0, !0);
|
|
469
469
|
}
|
|
470
470
|
let b;
|
|
471
471
|
async function re(e, n = !1, t = !1) {
|
|
472
|
-
return b || (b =
|
|
472
|
+
return b || (b = Fe(e, n, t).finally(() => {
|
|
473
473
|
b = null;
|
|
474
474
|
}), b);
|
|
475
475
|
}
|
|
476
|
-
async function
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
476
|
+
async function Fe(e, n, t) {
|
|
477
|
+
e || (e = {});
|
|
478
|
+
let r;
|
|
479
|
+
if (t) {
|
|
480
|
+
r = te("referer");
|
|
481
|
+
let o = y(), i;
|
|
482
|
+
for (let s = 0; s < o.pollMax; s++) {
|
|
483
|
+
if (i = E() ? await z(e, r, n) : await Z(e, r, n), i.code !== f.LOGIN_PENDING)
|
|
481
484
|
return i;
|
|
482
|
-
await
|
|
485
|
+
await Be(o.pollInterval);
|
|
483
486
|
}
|
|
484
487
|
return i;
|
|
485
488
|
} else
|
|
486
|
-
return E() ? await
|
|
489
|
+
return r = window.location.href, E() ? await z(e, r, n) : await Z(e, r, n);
|
|
487
490
|
}
|
|
488
|
-
async function
|
|
489
|
-
return await l(p.COOKIE.VERIFY, { redirect:
|
|
491
|
+
async function z(e, n, t) {
|
|
492
|
+
return await l(p.COOKIE.VERIFY, { redirect: n }, { skipRefresh: !0, showError: t });
|
|
490
493
|
}
|
|
491
|
-
async function
|
|
492
|
-
let
|
|
494
|
+
async function Z(e, n, t) {
|
|
495
|
+
let r = {
|
|
493
496
|
method: "POST",
|
|
494
497
|
path: p.SIGN.VERIFY,
|
|
495
|
-
origin:
|
|
498
|
+
origin: ne(),
|
|
496
499
|
ts: Date.now(),
|
|
497
500
|
ds: u.ds,
|
|
498
|
-
dna:
|
|
499
|
-
redirect:
|
|
500
|
-
},
|
|
501
|
+
dna: te("dna"),
|
|
502
|
+
redirect: n
|
|
503
|
+
}, o;
|
|
501
504
|
if (e && e.token)
|
|
502
|
-
|
|
505
|
+
r.token = e.token, o = await A(_.VTT, r, "sso");
|
|
503
506
|
else {
|
|
504
507
|
if (!u.sid || !u.ds)
|
|
505
508
|
throw new h(f.LOGIN_TIMEOUT, "未登录");
|
|
506
|
-
|
|
509
|
+
r.token = u.sid, o = await A(_.VSD, r, "sso");
|
|
507
510
|
}
|
|
508
|
-
const i = await l(p.SIGN.VERIFY, { dsign:
|
|
509
|
-
return i.code === f.OK && (u.
|
|
511
|
+
const i = await l(p.SIGN.VERIFY, { dsign: o, dna: r.dna, redirect: r.redirect }, { skipRefresh: !0, showError: t });
|
|
512
|
+
return i.code === f.OK && (u.sid = i.data.sid, i.data.ds && (u.ds = i.data.ds)), i;
|
|
510
513
|
}
|
|
511
514
|
let T;
|
|
512
|
-
async function Je(e) {
|
|
513
|
-
return T || (T =
|
|
515
|
+
async function Je(e, n = !0) {
|
|
516
|
+
return T || (T = He(e, n).finally(() => {
|
|
514
517
|
T = null;
|
|
515
518
|
}), T);
|
|
516
519
|
}
|
|
517
|
-
async function
|
|
518
|
-
return E() ? await
|
|
520
|
+
async function He(e, n) {
|
|
521
|
+
return E() ? await je(e, n) : await We(e, n);
|
|
519
522
|
}
|
|
520
|
-
async function
|
|
521
|
-
return await l(p.COOKIE.TOKEN, null, { skipRefresh: !0 });
|
|
523
|
+
async function je(e, n) {
|
|
524
|
+
return await l(p.COOKIE.TOKEN, null, { skipRefresh: !0, showError: n });
|
|
522
525
|
}
|
|
523
|
-
async function
|
|
524
|
-
let
|
|
526
|
+
async function We(e, n) {
|
|
527
|
+
let t = {
|
|
525
528
|
method: "POST",
|
|
526
529
|
path: p.SIGN.TOKEN,
|
|
527
|
-
origin:
|
|
530
|
+
origin: ne(),
|
|
528
531
|
ts: Date.now(),
|
|
529
532
|
ds: u.ds
|
|
530
|
-
},
|
|
533
|
+
}, r;
|
|
531
534
|
if (e && e.code)
|
|
532
|
-
|
|
535
|
+
t.token = e.code, r = await A(_.TOK, t, "domain");
|
|
533
536
|
else
|
|
534
537
|
throw new h(f.LOGIN_TIMEOUT, "未登录");
|
|
535
|
-
let
|
|
536
|
-
const i = await l(p.SIGN.TOKEN, { dsign:
|
|
538
|
+
let o = await ee("domain");
|
|
539
|
+
const i = await l(p.SIGN.TOKEN, { dsign: r, pk: o }, { skipRefresh: !0, showError: n });
|
|
537
540
|
return i.code === f.OK && (u.token = i.data.token, i.data.ds && (u.ds = i.data.ds)), i;
|
|
538
541
|
}
|
|
539
542
|
let k;
|
|
540
|
-
async function
|
|
541
|
-
return k || (k =
|
|
543
|
+
async function Ye() {
|
|
544
|
+
return k || (k = $e().finally(() => {
|
|
542
545
|
k = null;
|
|
543
546
|
}), k);
|
|
544
547
|
}
|
|
545
|
-
async function
|
|
546
|
-
return E() ? await
|
|
548
|
+
async function $e() {
|
|
549
|
+
return E() ? await ze() : await Ze();
|
|
547
550
|
}
|
|
548
|
-
async function
|
|
551
|
+
async function ze() {
|
|
549
552
|
return await l(p.COOKIE.REFRESH, null, { skipRefresh: !0, showError: !1 });
|
|
550
553
|
}
|
|
551
|
-
async function
|
|
554
|
+
async function Ze() {
|
|
552
555
|
const e = u.token, n = u.ds;
|
|
553
556
|
if (!e || !n)
|
|
554
557
|
throw new ue();
|
|
@@ -558,9 +561,18 @@ async function $e() {
|
|
|
558
561
|
async function Qe() {
|
|
559
562
|
(await l(p.LOGOUT)).code === f.OK && u.clear();
|
|
560
563
|
}
|
|
561
|
-
function en(
|
|
564
|
+
async function en() {
|
|
565
|
+
let e = y(), n = encodeURIComponent(window.location.href), t = e.loginUrl + "?referer=" + n;
|
|
566
|
+
if (!E()) {
|
|
567
|
+
await we();
|
|
568
|
+
let r = await ye();
|
|
569
|
+
t += "&dna=" + r;
|
|
570
|
+
}
|
|
571
|
+
(window.top || window).location.href = t;
|
|
572
|
+
}
|
|
573
|
+
function nn(e) {
|
|
562
574
|
const n = ie(e);
|
|
563
|
-
if (
|
|
575
|
+
if (Pe(), n.nativeGuard !== "off") {
|
|
564
576
|
const t = ce();
|
|
565
577
|
if (t.length) {
|
|
566
578
|
const r = "[dplugin/core] 这些原生能力在模块加载前已被替换:" + t.join(", ") + "(签名/请求可能被中间人截获,检查是否有脚本抢跑)";
|
|
@@ -571,13 +583,13 @@ function en(e) {
|
|
|
571
583
|
}
|
|
572
584
|
}
|
|
573
585
|
export {
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
586
|
+
ye as getDna,
|
|
587
|
+
ee as getPublicKey,
|
|
588
|
+
en as gotoLoginPage,
|
|
577
589
|
qe as login,
|
|
578
590
|
Qe as logout,
|
|
579
591
|
l as request,
|
|
580
|
-
|
|
592
|
+
nn as setup,
|
|
581
593
|
Je as token,
|
|
582
594
|
Xe as upload,
|
|
583
595
|
re as verify
|
package/dist/index.umd.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(function(a,h){typeof exports=="object"&&typeof module!="undefined"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(a=typeof globalThis!="undefined"?globalThis:a||self,h(a.DPluginCore={}))})(this,(function(a){"use strict";const h={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"error"};function
|
|
2
|
-
`)}function ke(e,n,t){return[e,
|
|
1
|
+
(function(a,h){typeof exports=="object"&&typeof module!="undefined"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(a=typeof globalThis!="undefined"?globalThis:a||self,h(a.DPluginCore={}))})(this,(function(a){"use strict";const h={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"error"};function se(e){return e&&Object.keys(h).forEach(n=>{e[n]!==void 0&&(h[n]=e[n])}),h}function O(){return h}const f=ue();function x(e,n){if(typeof e!="function")return!1;let t;try{t=Function.prototype.toString.call(e)}catch(o){return!1}const r=/^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(t);return r?r[1]===n:/\[native code\]/.test(t)&&!/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(t)}function ce(e){try{const n=e();return n&&typeof n.then=="function"&&Promise.resolve(n).catch(()=>{}),!1}catch(n){const t=String(n&&n.message||"");return!!n&&n.name==="TypeError"&&/argument|required|参数/i.test(t)}}function ue(){const e=globalThis,n=typeof e.window!="undefined"&&e.window&&typeof e.window.document!="undefined",t={fetch:null,idbOpen:null,subtle:null,pristine:{fetch:!1,idbOpen:!1,subtle:!1}};typeof e.fetch=="function"&&(t.pristine.fetch=x(e.fetch,"fetch"),t.fetch=e.fetch.bind(e)),typeof e.indexedDB!="undefined"&&e.indexedDB&&typeof e.indexedDB.open=="function"&&(t.pristine.idbOpen=x(e.indexedDB.open,"open")&&(!n||ce(()=>e.indexedDB.open())),t.idbOpen=e.indexedDB.open.bind(e.indexedDB));const r=e.crypto&&e.crypto.subtle?e.crypto.subtle:null;if(r){const o=["generateKey","exportKey","importKey","sign","verify","digest"],i={};let s=!0;o.forEach(c=>{typeof r[c]=="function"&&(i[c]=r[c].bind(r),s=s&&x(r[c],c))}),t.subtle=i,t.pristine.subtle=s}return t}function ae(){if(typeof window=="undefined"||typeof window.document=="undefined")return[];const e=[];return f.fetch&&!f.pristine.fetch&&e.push("fetch"),f.idbOpen&&!f.pristine.idbOpen&&e.push("indexedDB.open"),f.subtle&&!f.pristine.subtle&&e.push("crypto.subtle"),e}const l={OK:"200",LOGIN_PENDING:"202",LOGIN_TIMEOUT:"g.login.timeout",NETWORK:"NETWORK",TIMEOUT:"TIMEOUT"};class p extends Error{constructor(n,t){super(t||"请求失败"),this.name="BizError",this.code=n}}class fe extends p{constructor(n){super(l.LOGIN_TIMEOUT,n||"登录已失效"),this.name="AuthError"}}const B="dp.token",V="dp.sid",Z="dp.ds";function le(e){try{return localStorage.getItem(e)}catch(n){return null}}function q(e,n){try{n==null||n===""?localStorage.removeItem(e):localStorage.setItem(e,n)}catch(t){}}const R={};function U(e){const n=le(e);return n===null?R[e]===void 0?null:R[e]:n}function F(e,n){R[e]=n,q(e,n)}const u={get token(){return U(B)},set token(e){F(B,e)},get sid(){return U(V)},set sid(e){F(V,e)},get ds(){return U(Z)},set ds(e){F(Z,e)},clear(){[B,V].forEach(e=>{R[e]=null,q(e,null)})}},de="dplugin",ge=1,K="keys",he={name:"ECDSA",namedCurve:"P-256"},pe={name:"ECDSA",hash:"SHA-256"},C=new Map;let D=null;function M(){if(!f.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 f.subtle}function we(){return D||(D=new Promise(e=>{if(!f.idbOpen){e(null);return}let n;try{n=f.idbOpen(de,ge)}catch(t){e(null);return}n.onupgradeneeded=()=>{const t=n.result;t.objectStoreNames.contains(K)||t.createObjectStore(K)},n.onsuccess=()=>e(n.result),n.onerror=()=>e(null),n.onblocked=()=>e(null)}),D)}function X(e,n){return we().then(t=>t?new Promise(r=>{let o;try{o=t.transaction(K,e)}catch(c){r(null);return}const i=o.objectStore(K);let s=null;try{const c=n(i);c&&(c.onsuccess=()=>{s=c.result},c.onerror=()=>{s=null})}catch(c){s=null}o.oncomplete=()=>r(s),o.onerror=()=>r(null),o.onabort=()=>r(null)}):null)}async function T(e){return C.has(e)?Promise.resolve(C.get(e)):X("readonly",n=>n.get(e)).then(n=>(n&&C.set(e,n),n||null))}function J(e){const n=new Uint8Array(e);let t="";for(let r=0;r<n.length;r++)t+=String.fromCharCode(n[r]);return btoa(t).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function Q(e){let n=await T(e);return n||(n=await M().generateKey(he,!1,["sign","verify"]),C.set(e,n),X("readwrite",r=>r.put(n,e)).then(()=>n))}function me(){return Q("sso")}function ye(){return Q("domain")}async function Oe(){return await T("sso")}async function Ee(){return await T("domain")}async function ee(){let e=await T("domain");const n=M(),t=await n.exportKey("spki",e.publicKey),r=await n.digest("SHA-256",t);return J(r)}async function j(e){let n=await T(e);const r=await M().exportKey("spki",n.publicKey);let o="";const i=new Uint8Array(r);for(let s=0;s<i.length;s++)o+=String.fromCharCode(i[s]);return btoa(o)}async function Se(e,n){const r=await M().sign(pe,e.privateKey,new TextEncoder().encode(n));return J(r)}const be="-",_={BIZ:"BIZ",VTT:"VTT",VSD:"VSD",TOK:"TOK"};function E(e){return e==null||e===""?be:String(e)}function Te(e,n){const t=E(n.origin);return[e,n.method,n.path,t.toLowerCase(),E(n.ds),n.ts,E(n.token),E(n.dna),E(n.redirect)].join(`
|
|
2
|
+
`)}function ke(e,n,t){return[e,E(n.token),E(n.ds),n.ts,t].join(".")}function Ie(e){const n=new Error(e);return n.code="KEY_MISSING",n}async function A(e,n,t){const r=t==="sso"?await Oe():await Ee();if(!r)throw Ie(t==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");let o=n.path.indexOf("?");o>0&&(n.path=n.path.substring(0,o));const i=await Se(r,Te(e,n));return ke(e,n,i)}let w=null;function Pe(e){try{const n=typeof location!="undefined"?location.href:void 0;return new URL(e||"",n).origin}catch(n){return null}}function Ne(){w=null}function Re(){if(w)return w;const e=O();if(e.mode==="sign"||e.mode==="cookie")return w=e.mode,w;const n=typeof location!="undefined"?location.origin:null;if(!n)return w="sign",w;const t=Pe(e.apiBase||n);return w=t&&t===n?"cookie":"sign",w}function S(){return Re()==="cookie"}function Ke(e,n){let t=O();throw t.onError&&n.showError&&t.onError({code:e.code,message:e.message}),new p(e.code,e.message)}let v,H=null;function Ce(){return v||(v=Promise.resolve().then(()=>H()).finally(()=>{v=null})),v}async function De(e,n,t,r,o){const i=O(),s=o||i.timeout,c=typeof AbortController!="undefined"?new AbortController:null,L=c?setTimeout(()=>c.abort(),s):null,b={method:n};if(e?b.credentials="same-origin":b.credentials="omit",b.signal=c?c.signal:void 0,typeof FormData!="undefined"&&r instanceof FormData?b.body=r:r!=null&&(b.headers={"Content-Type":"text/plain"},b.body=JSON.stringify(r)),!f.fetch)throw new p(l.NETWORK,"当前环境不支持 fetch");let G;try{G=await f.fetch(t,b)}catch(z){throw L&&clearTimeout(L),z&&z.name==="AbortError"?new p(l.TIMEOUT,"请求超时,请重试"):new p(l.NETWORK,"网络异常,请检查网络后重试")}L&&clearTimeout(L);const $=await G.text();let g=null;if($)try{g=JSON.parse($)}catch(z){g=null}if(!G.ok)throw new p(String(G.status),g&&g.message||$||"请求失败");if(!g||g.code===null||g.code===void 0)throw new p("500","响应格式异常");return g.code=String(g.code),g}async function d(e,n=null,t=null){const r=t||{};r.method||(r.method="POST"),r.showError=r.showError!==!1;let o=!0;/^https?:\/\/.+$/.test(e)?e.indexOf(O().apiBase)===-1&&(o=!1):e=O().apiBase+e,o&&!S()&&(o=!1);let i=e;if(!o&&u.ds&&u.token){let c=await A(_.BIZ,{method:r.method,path:new URL(e,location.href).pathname,ds:u.ds,ts:Date.now(),token:u.token},"domain");i.indexOf("?")>0?i=i+"&dsign="+c:i=i+"?dsign="+c}const s=await De(o,r.method,i,n,r.timeout);return s.code===l.LOGIN_TIMEOUT&&!r.skipRefresh&&H&&await Ce()?d(e,n,Object.assign({},r,{skipRefresh:!0})):(/^2.+/.test(s.code)||Ke(s,r),s)}async function Me(e,n,t,r){const o=new FormData;return Object.keys(t||{}).forEach(i=>{const s=t[i];s!=null&&o.append(i,s)}),o.append("file",n),d(e,o,r)}function _e(e){H=e}function Ae(){const e=typeof navigator!="undefined"&&navigator.userAgent||"",n={device_brand:ve(e),device_model:Le(e),browser_name:Ge(e),browser_version:xe(e),os_name:Be(e),os_version:Ve(e)},t=u.ds;return t&&(n.device_id=t),n}function y(e,n){const t=e.match(n);return t?t[1]:""}function ve(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 Le(e){if(/iPhone/.test(e))return"iPhone";if(/iPad/.test(e))return"iPad";const n=e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return n?n[1].trim():/Macintosh/.test(e)?"Mac":""}function Ge(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 xe(e){return/MicroMessenger\/([\d.]+)/.test(e)?y(e,/MicroMessenger\/([\d.]+)/):/Edg[A-Za-z]*\/([\d.]+)/.test(e)?y(e,/Edg[A-Za-z]*\/([\d.]+)/):/OPR\/([\d.]+)/.test(e)?y(e,/OPR\/([\d.]+)/):/Firefox\/([\d.]+)/.test(e)?y(e,/Firefox\/([\d.]+)/):/CriOS\/([\d.]+)/.test(e)?y(e,/CriOS\/([\d.]+)/):/Chrome\/([\d.]+)/.test(e)?y(e,/Chrome\/([\d.]+)/):/Version\/([\d.]+).*Safari/.test(e)?y(e,/Version\/([\d.]+)/):""}function Be(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 Ve(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 n=e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(n)return n[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(e))return y(e,/Android[ _]([\d.]+)/);const t=e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return t?t[1].replace(/_/g,"."):""}function ne(){if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(e){return""}}let W,te;function re(e){const n=typeof location!="undefined"&&location.search||"";return(!W||te!==n)&&(te=n,W=new URLSearchParams(n)),W.get(e)}const m={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"};_e(qe);function Ue(e){return new Promise(n=>setTimeout(n,e))}let k=null;async function Fe(e,n){if(k)return k;const t=Ae();return k=(S()?je(e,n,t):He(e,n,t)).finally(()=>{k=null}),k}async function je(e,n,t){let r=await d(e,Object.assign({},n,{device:t}));return await Y(r.data,!0,!0)}async function He(e,n,t){await me();const r=await j("sso"),o=await d(e,Object.assign({},n,{device:t,pk:r}));return await Y(o.data,!0,!0)}let I;async function Y(e,n=!1,t=!1){return I||(I=We(e,n,t).finally(()=>{I=null}),I)}async function We(e,n,t){e||(e={});let r;if(t){r=re("referer");let o=O(),i;for(let s=0;s<o.pollMax;s++){if(i=S()?await ie(e,r,n):await oe(e,r,n),i.code!==l.LOGIN_PENDING)return i;await Ue(o.pollInterval)}return i}else return r=window.location.href,S()?await ie(e,r,n):await oe(e,r,n)}async function ie(e,n,t){return await d(m.COOKIE.VERIFY,{redirect:n},{skipRefresh:!0,showError:t})}async function oe(e,n,t){let r={method:"POST",path:m.SIGN.VERIFY,origin:ne(),ts:Date.now(),ds:u.ds,dna:re("dna"),redirect:n},o;if(e&&e.token)r.token=e.token,o=await A(_.VTT,r,"sso");else{if(!u.sid||!u.ds)throw new p(l.LOGIN_TIMEOUT,"未登录");r.token=u.sid,o=await A(_.VSD,r,"sso")}const i=await d(m.SIGN.VERIFY,{dsign:o,dna:r.dna,redirect:r.redirect},{skipRefresh:!0,showError:t});return i.code===l.OK&&(u.sid=i.data.sid,i.data.ds&&(u.ds=i.data.ds)),i}let P;async function Ye(e,n=!0){return P||(P=$e(e,n).finally(()=>{P=null}),P)}async function $e(e,n){return S()?await ze(e,n):await Ze(e,n)}async function ze(e,n){return await d(m.COOKIE.TOKEN,null,{skipRefresh:!0,showError:n})}async function Ze(e,n){let t={method:"POST",path:m.SIGN.TOKEN,origin:ne(),ts:Date.now(),ds:u.ds},r;if(e&&e.code)t.token=e.code,r=await A(_.TOK,t,"domain");else throw new p(l.LOGIN_TIMEOUT,"未登录");let o=await j("domain");const i=await d(m.SIGN.TOKEN,{dsign:r,pk:o},{skipRefresh:!0,showError:n});return i.code===l.OK&&(u.token=i.data.token,i.data.ds&&(u.ds=i.data.ds)),i}let N;async function qe(){return N||(N=Xe().finally(()=>{N=null}),N)}async function Xe(){return S()?await Je():await Qe()}async function Je(){return await d(m.COOKIE.REFRESH,null,{skipRefresh:!0,showError:!1})}async function Qe(){const e=u.token,n=u.ds;if(!e||!n)throw new fe;const t=await d(m.SIGN.REFRESH,null,{skipRefresh:!0,showError:!1});return t.code===l.OK&&(t.data.ds&&(u.ds=t.data.ds),u.token=t.data.token),t}async function en(){(await d(m.LOGOUT)).code===l.OK&&u.clear()}async function nn(){let e=O(),n=encodeURIComponent(window.location.href),t=e.loginUrl+"?referer="+n;if(!S()){await ye();let r=await ee();t+="&dna="+r}(window.top||window).location.href=t}function tn(e){const n=se(e);if(Ne(),n.nativeGuard!=="off"){const t=ae();if(t.length){const r="[dplugin/core] 这些原生能力在模块加载前已被替换:"+t.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(n.nativeGuard==="error")throw new Error(r);typeof console!="undefined"&&console.warn(r)}}}a.getDna=ee,a.getPublicKey=j,a.gotoLoginPage=nn,a.login=Fe,a.logout=en,a.request=d,a.setup=tn,a.token=Ye,a.upload=Me,a.verify=Y,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
|
|
3
3
|
//# sourceMappingURL=index.umd.js.map
|