@cloudbase/oauth 2.19.0 → 2.19.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/cjs/auth/apis.d.ts +4 -0
- package/dist/cjs/auth/apis.js +2 -1
- package/dist/cjs/index.js +3 -2
- package/dist/cjs/oauth2client/models.d.ts +2 -0
- package/dist/cjs/oauth2client/models.js +1 -1
- package/dist/cjs/oauth2client/oauth2client.d.ts +1 -0
- package/dist/cjs/oauth2client/oauth2client.js +66 -41
- package/dist/esm/auth/apis.d.ts +4 -0
- package/dist/esm/auth/apis.js +1 -0
- package/dist/esm/index.js +2 -1
- package/dist/esm/oauth2client/models.d.ts +2 -0
- package/dist/esm/oauth2client/oauth2client.d.ts +1 -0
- package/dist/esm/oauth2client/oauth2client.js +24 -7
- package/dist/miniprogram/index.js +1 -1
- package/package.json +3 -3
- package/src/auth/apis.ts +2 -0
- package/src/index.ts +2 -1
- package/src/oauth2client/models.ts +2 -0
- package/src/oauth2client/oauth2client.ts +26 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("tcboauth",[],t):"object"==typeof exports?exports.tcboauth=t():e.tcboauth=t()}("undefined"!=typeof window?window:this,()=>(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Auth:()=>G,CloudbaseOAuth:()=>M,authModels:()=>s});var r,n,i,o,a,s={};function c(){return"xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g,e=>{var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})}e.r(s),function(e){e.AUTH_SIGN_UP_URL="/auth/v1/signup",e.AUTH_TOKEN_URL="/auth/v1/token",e.AUTH_REVOKE_URL="/auth/v1/revoke",e.WEDA_USER_URL="/auth/v1/plugin/weda/userinfo",e.AUTH_RESET_PASSWORD="/auth/v1/reset",e.AUTH_GET_DEVICE_CODE="/auth/v1/device/code",e.CHECK_USERNAME="/auth/v1/checkUsername",e.CHECK_IF_USER_EXIST="/auth/v1/checkIfUserExist",e.GET_PROVIDER_TYPE="/auth/v1/mgr/provider/providerSubType",e.AUTH_SIGN_IN_URL="/auth/v1/signin",e.AUTH_SIGN_IN_ANONYMOUSLY_URL="/auth/v1/signin/anonymously",e.AUTH_SIGN_IN_WITH_PROVIDER_URL="/auth/v1/signin/with/provider",e.AUTH_SIGN_IN_WITH_WECHAT_URL="/auth/v1/signin/wechat/noauth",e.AUTH_SIGN_IN_CUSTOM="/auth/v1/signin/custom",e.PROVIDER_TOKEN_URL="/auth/v1/provider/token",e.PROVIDER_URI_URL="/auth/v1/provider/uri",e.USER_ME_URL="/auth/v1/user/me",e.AUTH_SIGNOUT_URL="/auth/v1/user/signout",e.USER_QUERY_URL="/auth/v1/user/query",e.USER_PRIFILE_URL="/auth/v1/user/profile",e.USER_BASIC_EDIT_URL="/auth/v1/user/basic/edit",e.USER_TRANS_BY_PROVIDER_URL="/auth/v1/user/trans/by/provider",e.PROVIDER_LIST="/auth/v1/user/provider",e.PROVIDER_BIND_URL="/auth/v1/user/provider/bind",e.PROVIDER_UNBIND_URL="/auth/v1/user/provider",e.CHECK_PWD_URL="/auth/v1/user/sudo",e.SUDO_URL="/auth/v1/user/sudo",e.BIND_CONTACT_URL="/auth/v1/user/contact",e.AUTH_SET_PASSWORD="/auth/v1/user/password",e.AUTHORIZE_DEVICE_URL="/auth/v1/user/device/authorize",e.AUTHORIZE_URL="/auth/v1/user/authorize",e.AUTHORIZE_INFO_URL="/auth/v1/user/authorize/info",e.AUTHORIZED_DEVICES_DELETE_URL="/auth/v1/user/authorized/devices/",e.AUTH_REVOKE_ALL_URL="/auth/v1/user/revoke/all",e.GET_USER_BEHAVIOR_LOG="/auth/v1/user/security/history",e.VERIFICATION_URL="/auth/v1/verification",e.VERIFY_URL="/auth/v1/verification/verify",e.CAPTCHA_DATA_URL="/auth/v1/captcha/data",e.VERIFY_CAPTCHA_DATA_URL="/auth/v1/captcha/data/verify",e.GET_CAPTCHA_URL="/auth/v1/captcha/init",e.GET_MINIPROGRAM_QRCODE="/auth/v1/qrcode/generate",e.GET_MINIPROGRAM_QRCODE_STATUS="/auth/v1/qrcode/get/status"}(r||(r={})),function(e){e.AUTH_SIGN_IN_URL="/auth/v2/signin/username",e.AUTH_TOKEN_URL="/auth/v2/token",e.USER_ME_URL="/auth/v2/user/me",e.VERIFY_URL="/auth/v2/signin/verificationcode",e.AUTH_SIGN_IN_WITH_PROVIDER_URL="/auth/v2/signin/with/provider",e.AUTH_PUBLIC_KEY="/auth/v2/signin/publichkey",e.AUTH_RESET_PASSWORD="/auth/v2/signin/password/update"}(n||(n={})),function(e){e.REGISTER="REGISTER",e.SIGN_IN="SIGN_IN",e.PASSWORD_RESET="PASSWORD_RESET",e.EMAIL_ADDRESS_CHANGE="EMAIL_ADDRESS_CHANGE",e.PHONE_NUMBER_CHANGE="PHONE_NUMBER_CHANGE"}(i||(i={})),function(e){e.UNREACHABLE="unreachable",e.LOCAL="local",e.CANCELLED="cancelled",e.UNKNOWN="unknown",e.UNAUTHENTICATED="unauthenticated",e.RESOURCE_EXHAUSTED="resource_exhausted",e.FAILED_PRECONDITION="failed_precondition",e.INVALID_ARGUMENT="invalid_argument",e.DEADLINE_EXCEEDED="deadline_exceeded",e.NOT_FOUND="not_found",e.ALREADY_EXISTS="already_exists",e.PERMISSION_DENIED="permission_denied",e.ABORTED="aborted",e.OUT_OF_RANGE="out_of_range",e.UNIMPLEMENTED="unimplemented",e.INTERNAL="internal",e.UNAVAILABLE="unavailable",e.DATA_LOSS="data_loss",e.INVALID_PASSWORD="invalid_password",e.PASSWORD_NOT_SET="password_not_set",e.INVALID_STATUS="invalid_status",e.USER_PENDING="user_pending",e.USER_BLOCKED="user_blocked",e.INVALID_VERIFICATION_CODE="invalid_verification_code",e.TWO_FACTOR_REQUIRED="two_factor_required",e.INVALID_TWO_FACTOR="invalid_two_factor",e.INVALID_TWO_FACTOR_RECOVERY="invalid_two_factor_recovery",e.UNDER_REVIEW="under_review",e.INVALID_REQUEST="invalid_request",e.UNAUTHORIZED_CLIENT="unauthorized_client",e.ACCESS_DENIED="access_denied",e.UNSUPPORTED_RESPONSE_TYPE="unsupported_response_type",e.INVALID_SCOPE="invalid_scope",e.INVALID_GRANT="invalid_grant",e.SERVER_ERROR="server_error",e.TEMPORARILY_UNAVAILABLE="temporarily_unavailable",e.INTERACTION_REQUIRED="interaction_required",e.LOGIN_REQUIRED="login_required",e.ACCOUNT_SELECTION_REQUIRED="account_selection_required",e.CONSENT_REQUIRED="consent_required",e.INVALID_REQUEST_URI="invalid_request_uri",e.INVALID_REQUEST_OBJECT="invalid_request_object",e.REQUEST_NOT_SUPPORTED="request_not_supported",e.REQUEST_URI_NOT_SUPPORTED="request_uri_not_supported",e.REGISTRATION_NOT_SUPPORTED="registration_not_supported",e.CAPTCHA_REQUIRED="captcha_required",e.CAPTCHA_INVALID="captcha_invalid"}(o||(o={})),function(e){e.CLIENT_ID="client_id",e.CLIENT_SECRET="client_secret",e.RESPONSE_TYPE="response_type",e.SCOPE="scope",e.STATE="state",e.REDIRECT_URI="redirect_uri",e.ERROR="error",e.ERROR_DESCRIPTION="error_description",e.ERROR_URI="error_uri",e.GRANT_TYPE="grant_type",e.CODE="code",e.ACCESS_TOKEN="access_token",e.TOKEN_TYPE="token_type",e.EXPIRES_IN="expires_in",e.USERNAME="username",e.PASSWORD="password",e.REFRESH_TOKEN="refresh_token"}(a||(a={}));var u=e=>{var t=t=>{for(var r in e)e.hasOwnProperty(r)&&(t[r]=u(e[r]));return t},r=null==e?"NullOrUndefined":Object.prototype.toString.call(e).slice(8,-1);if(["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"].includes(r))return e.slice();switch(r){case"Object":return t(Object.create(Object.getPrototypeOf(e)));case"Array":return t([]);case"Date":return new Date(e.valueOf());case"RegExp":return new RegExp(e.source,(e.global?"g":"")+(e.ignoreCase?"i":"")+(e.multiline?"m":"")+(e.sticky?"y":"")+(e.unicode?"u":""));default:return e}},d=e=>{var t=e.match(/^(?:http(s)?:\/\/[^\/]+)?(\/[^\?#]*)/);return t&&t[2]||""};function l(e,t,r,n,i,o,a){try{var s=e[o](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,i)}function h(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function a(e){l(o,n,i,a,s,"next",e)}function s(e){l(o,n,i,a,s,"throw",e)}a(void 0)}))}}var _=new Map;class v{constructor(e){this.clientId=(null==e?void 0:e.clientId)||"",_=_||new Map}run(e,t){var r=this;return h((function*(){e="".concat(r.clientId,"_").concat(e);var n=_.get(e);return n||(n=new Promise((n,i)=>{h((function*(){try{yield r.runIdlePromise();var o=t();n(yield o)}catch(e){i(e)}finally{_.delete(e)}}))()}),_.set(e,n)),n}))()}runIdlePromise(){return Promise.resolve()}}var f="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";function p(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function y(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?p(Object(r),!0).forEach((function(t){g(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):p(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function g(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function E(e,t,r,n,i,o,a){try{var s=e[o](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,i)}function m(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function a(e){E(o,n,i,a,s,"next",e)}function s(e){E(o,n,i,a,s,"throw",e)}a(void 0)}))}}var I=function(){var e=m((function*(e,t){var r=null,n=null;try{var i=Object.assign({},t);i.method||(i.method="GET"),i.body&&"string"!=typeof i.body&&(i.body=JSON.stringify(i.body));var a=yield fetch(e,i),s=yield a.json();null!=s&&s.error?(n=s).error_uri=new URL(e).pathname:r=s}catch(t){n={error:o.UNREACHABLE,error_description:t.message,error_uri:new URL(e).pathname}}if(n)throw n;return r}));return function(t,r){return e.apply(this,arguments)}}();var R=new class{constructor(e){this._env=(null==e?void 0:e.env)||""}getItem(e){var t=this;return m((function*(){return window.localStorage.getItem("".concat(e).concat(t._env))}))()}removeItem(e){var t=this;return m((function*(){window.localStorage.removeItem("".concat(e).concat(t._env))}))()}setItem(e,t){var r=this;return m((function*(){window.localStorage.setItem("".concat(e).concat(r._env),t)}))()}getItemSync(e){return window.localStorage.getItem("".concat(e).concat(this._env))}removeItemSync(e){window.localStorage.removeItem("".concat(e).concat(this._env))}setItemSync(e,t){window.localStorage.setItem("".concat(e).concat(this._env),t)}};function T(e){var t=!0;return null!=e&&e.expires_at&&null!=e&&e.access_token&&(t=e.expires_at<new Date),t}class S{constructor(e){this.credentials=null,this.singlePromise=null,this.tokenSectionName=e.tokenSectionName,this.storage=e.storage,this.clientId=e.clientId,this.singlePromise=new v({clientId:this.clientId})}getStorageCredentialsSync(){var e=null,t=this.storage.getItemSync(this.tokenSectionName);if(null!=t)try{var r;null!==(r=e=JSON.parse(t))&&void 0!==r&&r.expires_at&&(e.expires_at=new Date(e.expires_at))}catch(t){this.storage.removeItem(this.tokenSectionName),e=null}return e}setCredentials(e){var t=this;return m((function*(){if(null!=e&&e.expires_in){if(null!=e&&e.expires_at||(e.expires_at=new Date(Date.now()+1e3*(e.expires_in-30))),t.storage){var r=JSON.stringify(e);yield t.storage.setItem(t.tokenSectionName,r)}t.credentials=e}else t.storage&&(yield t.storage.removeItem(t.tokenSectionName)),t.credentials=null}))()}getCredentials(){var e=this;return m((function*(){return e.singlePromise.run("getCredentials",m((function*(){return T(e.credentials)&&(e.credentials=yield e.getStorageCredentials()),e.credentials})))}))()}getStorageCredentials(){var e=this;return m((function*(){return e.singlePromise.run("_getStorageCredentials",m((function*(){var t=null,r=yield e.storage.getItem(e.tokenSectionName);if(null!=r)try{var n;null!==(n=t=JSON.parse(r))&&void 0!==n&&n.expires_at&&(t.expires_at=new Date(t.expires_at))}catch(r){yield e.storage.removeItem(e.tokenSectionName),t=null}return t})))}))()}}class C{constructor(e){this.singlePromise=null,e.clientSecret||(e.clientSecret=""),!e.clientId&&e.env&&(e.clientId=e.env),this.apiOrigin=e.apiOrigin,this.clientId=e.clientId,this.singlePromise=new v({clientId:this.clientId}),this.retry=this.formatRetry(e.retry,C.defaultRetry),e.baseRequest?this.baseRequest=e.baseRequest:this.baseRequest=I,this.tokenInURL=e.tokenInURL,this.headers=e.headers,this.storage=e.storage||R,this.localCredentials=new S({tokenSectionName:"credentials_".concat(e.clientId),storage:this.storage,clientId:e.clientId}),this.clientSecret=e.clientSecret,e.clientId&&(this.basicAuth="Basic ".concat(function(e){for(var t,r,n,i,o="",a=0,s=(e=String(e)).length%3;a<e.length;){if((r=e.charCodeAt(a++))>255||(n=e.charCodeAt(a++))>255||(i=e.charCodeAt(a++))>255)throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");o+=f.charAt((t=r<<16|n<<8|i)>>18&63)+f.charAt(t>>12&63)+f.charAt(t>>6&63)+f.charAt(63&t)}return s?o.slice(0,s-3)+"===".substring(s):o}("".concat(e.clientId,":").concat(this.clientSecret)))),this.wxCloud=e.wxCloud;try{(function(){if("undefined"==typeof wx)return!1;if("undefined"==typeof Page)return!1;if(!wx.getSystemInfoSync)return!1;if(!wx.getStorageSync)return!1;if(!wx.setStorageSync)return!1;if(!wx.connectSocket)return!1;if(!wx.request)return!1;try{if(!wx.getSystemInfoSync())return!1;if("qq"===wx.getSystemInfoSync().AppPlatform)return!1}catch(e){return!1}return!0})()&&void 0===this.wxCloud&&e.env&&(wx.cloud.init({env:e.env}),this.wxCloud=wx.cloud)}catch(e){}this.refreshTokenFunc=e.refreshTokenFunc||this.defaultRefreshTokenFunc,this.anonymousSignInFunc=e.anonymousSignInFunc}setCredentials(e){return this.localCredentials.setCredentials(e)}getAccessToken(){var e=this;return m((function*(){var t=yield e.getCredentials();if(null!=t&&t.access_token)return Promise.resolve(t.access_token);var r={error:o.UNAUTHENTICATED};return Promise.reject(r)}))()}request(e,t){var r=this;return m((function*(){var n,i;t||(t={});var a=r.formatRetry(t.retry,r.retry);if(t.headers=t.headers||{},r.headers&&(t.headers=y(y({},r.headers),t.headers)),t.headers["x-request-id"]||(t.headers["x-request-id"]=c()),!t.headers["x-device-id"]){var s=yield r.getDeviceId();t.headers["x-device-id"]=s}if(null!==(n=t)&&void 0!==n&&n.withBasicAuth&&r.basicAuth&&(t.headers.Authorization=r.basicAuth),null!==(i=t)&&void 0!==i&&i.withCredentials){var u=yield r.getCredentials();u&&(r.tokenInURL?(e.indexOf("?")<0&&(e+="?"),e+="access_token=".concat(u.access_token)):t.headers.Authorization="".concat(u.token_type," ").concat(u.access_token))}else r.clientId&&e.indexOf("client_id")<0&&(e+=e.indexOf("?")<0?"?":"&",e+="client_id=".concat(r.clientId));e.startsWith("/")&&(e=r.apiOrigin+e);for(var d=null,l=a+1,h=0;h<l;h++){try{d=t.useWxCloud?yield r.wxCloudCallFunction(e,t):yield r.baseRequest(e,t);break}catch(e){if(t.withCredentials&&e&&e.error===o.UNAUTHENTICATED)return yield r.setCredentials(null),Promise.reject(e);if(h===a||!e||"unreachable"!==e.error)return Promise.reject(e)}yield r.sleep(C.retryInterval)}return d}))()}wxCloudCallFunction(e,t){var r=this;return m((function*(){var n=null,i=null;try{var a,s="";try{s=yield wx.getRendererUserAgent()}catch(e){}var{result:c}=yield r.wxCloud.callFunction({name:"httpOverCallFunction",data:{url:e,method:t.method,headers:y({origin:"https://servicewechat.com","User-Agent":s},t.headers),body:t.body}});null!=c&&null!==(a=c.body)&&void 0!==a&&a.error_code?(i=null==c?void 0:c.body).error_uri=d(e):n=null==c?void 0:c.body}catch(t){i={error:o.UNREACHABLE,error_description:t.message,error_uri:d(e)}}if(i)throw i;return n}))()}getCredentials(){var e=this;return m((function*(){var t=yield e.localCredentials.getCredentials();if(!t)return e.unAuthenticatedError("credentials not found");if(T(t))if(t.refresh_token)try{t=yield e.refreshToken(t)}catch(r){if("anonymous"!==t.scope)return Promise.reject(r);t=yield e.anonymousLogin(t)}else{if("anonymous"!==t.scope)return e.unAuthenticatedError("no refresh token found in credentials");t=yield e.anonymousLogin(t)}return t}))()}getCredentialsSync(){return this.localCredentials.getStorageCredentialsSync()}getCredentialsAsync(){return this.getCredentials()}getScope(){var e=this;return m((function*(){var t=yield e.localCredentials.getCredentials();return t?t.scope:e.unAuthenticatedError("credentials not found")}))()}getGroups(){var e=this;return m((function*(){var t=yield e.localCredentials.getCredentials();return t?t.groups:e.unAuthenticatedError("credentials not found")}))()}refreshToken(e){var t=this;return m((function*(){return t.singlePromise.run("_refreshToken",m((function*(){if(!e||!e.refresh_token)return t.unAuthenticatedError("no refresh token found in credentials");try{var r=yield t.refreshTokenFunc(e.refresh_token,e);return yield t.localCredentials.setCredentials(r),r}catch(e){return e.error===o.INVALID_GRANT?(yield t.localCredentials.setCredentials(null),t.unAuthenticatedError(e.error_description)):Promise.reject(e)}})))}))()}anonymousLogin(e){var t=this;return m((function*(){return t.singlePromise.run("_anonymousLogin",m((function*(){if(t.anonymousSignInFunc){var r=yield t.anonymousSignInFunc(e);e=r||(yield t.localCredentials.getCredentials())}else e=yield t.anonymousSignIn(e);return e})))}))()}checkRetry(e){var t=null;if(("number"!=typeof e||e<C.minRetry||e>C.maxRetry)&&(t={error:o.UNREACHABLE,error_description:"wrong options param: retry"}),t)throw t;return e}formatRetry(e,t){return void 0===e?t:this.checkRetry(e)}sleep(e){return m((function*(){return new Promise(t=>{setTimeout(()=>{t()},e)})}))()}anonymousSignIn(e){var t=this;return m((function*(){return t.singlePromise.run("_anonymous",m((function*(){if(!e||"anonymous"!==e.scope)return t.unAuthenticatedError("no anonymous in credentials");try{var n=yield t.request(r.AUTH_SIGN_IN_ANONYMOUSLY_URL,{method:"POST",withBasicAuth:!0,body:{}});return yield t.localCredentials.setCredentials(n),n}catch(e){return e.error===o.INVALID_GRANT?(yield t.localCredentials.setCredentials(null),t.unAuthenticatedError(e.error_description)):Promise.reject(e)}})))}))()}defaultRefreshTokenFunc(e,t){var i=this;return m((function*(){if(void 0===e||""===e)return i.unAuthenticatedError("refresh token not found");var o=r.AUTH_TOKEN_URL;return"v2"===(null==t?void 0:t.version)&&(o=n.AUTH_TOKEN_URL),y(y({},yield i.request(o,{method:"POST",body:{client_id:i.clientId,client_secret:i.clientSecret,grant_type:"refresh_token",refresh_token:e}})),{},{version:(null==t?void 0:t.version)||"v1"})}))()}getDeviceId(){var e=this;return m((function*(){if(e.deviceID)return e.deviceID;var t=yield e.storage.getItem("device_id");return"string"==typeof t&&t.length>=16&&t.length<=48||(t=c(),yield e.storage.setItem("device_id",t)),e.deviceID=t,t}))()}unAuthenticatedError(e){var t={error:o.UNAUTHENTICATED,error_description:e};return Promise.reject(t)}}function O(){if("undefined"==typeof globalThis)return!1;var{wx:e}=globalThis;if(void 0===e)return!1;if(void 0===globalThis.Page)return!1;if(!e.getSystemInfoSync)return!1;if(!e.getStorageSync)return!1;if(!e.setStorageSync)return!1;if(!e.connectSocket)return!1;if(!e.request)return!1;try{if(!e.getSystemInfoSync())return!1;if("qq"===e.getSystemInfoSync().AppPlatform)return!1}catch(e){return!1}return!0}C.defaultRetry=2,C.minRetry=0,C.maxRetry=5,C.retryInterval=1e3;var U=!1;function A(){U=U||void 0!==typeof window&&"miniprogram"===window.__wxjs_environment}try{O()||(U=U||!!navigator.userAgent.match(/miniprogram/i)||"miniprogram"===window.__wxjs_environment,window&&window.WeixinJSBridge&&window.WeixinJSBridge.invoke?A():"undefined"!=typeof document&&document.addEventListener("WeixinJSBridgeReady",A,!1))}catch(e){}function P(){return U}const w=class{constructor(e){if(this.params={},"string"==typeof e)this.parse(e);else if(e&&"object"==typeof e)for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&this.append(t,e[t])}parse(e){e.split("&").forEach(e=>{var[t,r]=e.split("=").map(decodeURIComponent);this.append(t,r)})}append(e,t){this.params[e]?this.params[e]=this.params[e].concat([t]):this.params[e]=[t]}get(e){return this.params[e]?this.params[e][0]:null}getAll(e){return this.params[e]||[]}delete(e){delete this.params[e]}has(e){return Object.prototype.hasOwnProperty.call(this.params,e)}set(e,t){this.params[e]=[t]}toString(){var e=this,t=[],r=function(r){Object.prototype.hasOwnProperty.call(e.params,r)&&e.params[r].forEach(e=>{t.push("".concat(encodeURIComponent(r),"=").concat(encodeURIComponent(e)))})};for(var n in this.params)r(n);return t.join("&")}};function b(e,t,r,n,i,o,a){try{var s=e[o](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,i)}function N(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function a(e){b(o,n,i,a,s,"next",e)}function s(e){b(o,n,i,a,s,"throw",e)}a(void 0)}))}}class D{constructor(e){e.openURIWithCallback||(e.openURIWithCallback=this.getDefaultOpenURIWithCallback()),e.storage||(e.storage=R),this.config=e,this.tokenSectionName="captcha_".concat(e.clientId)}isMatch(){var e,t;return(null===(e=this.config)||void 0===e||null===(e=e.adapter)||void 0===e||null===(t=e.isMatch)||void 0===t?void 0:t.call(e))||O()}request(e,t){var r=this;return N((function*(){t||(t={}),t.method||(t.method="GET");var n,i="".concat(t.method,":").concat(e),a=e;t.withCaptcha&&(a=yield r.appendCaptchaTokenToURL(e,i,!1));try{n=yield r.config.request(a,t)}catch(n){return n.error===o.CAPTCHA_REQUIRED||n.error===o.CAPTCHA_INVALID?(e=yield r.appendCaptchaTokenToURL(e,i,n.error===o.CAPTCHA_INVALID),r.config.request(e,t)):Promise.reject(n)}return n}))()}getDefaultOpenURIWithCallback(){if(!this.isMatch()&&!P()&&(window.location.search.indexOf("__captcha")>0&&(document.body.style.display="none"),null===document.getElementById("captcha_panel_wrap"))){var e=document.createElement("div");e.style.cssText="background-color: rgba(0, 0, 0, 0.7);position: fixed;left: 0px;right: 0px;top: 0px;bottom: 0px;padding: 9vw 0 0 0;display: none;z-index:100;",e.setAttribute("id","captcha_panel_wrap"),setTimeout(()=>{document.body.appendChild(e)},0)}return this.defaultOpenURIWithCallback}defaultOpenURIWithCallback(e,t){return N((function*(){var{width:r="355px",height:n="355px"}=t||{};if(e.match(/^(data:.*)$/))return Promise.reject({error:o.UNIMPLEMENTED,error_description:"need to impl captcha data"});var i=document.getElementById("captcha_panel_wrap"),a=document.createElement("iframe");return i.innerHTML="",a.setAttribute("src",e),a.setAttribute("id","review-panel-iframe"),a.style.cssText="min-width:".concat(r,";display:block;height:").concat(n,";margin:0 auto;background-color: rgb(255, 255, 255);border: none;"),i.appendChild(a),i.style.display="block",new Promise((e,t)=>{a.onload=function(){try{var r=window.location,n=a.contentWindow.location;if(n.host+n.pathname===r.host+r.pathname){i.style.display="none";var o=new w(n.search),s=o.get("captcha_token");return s?e({captcha_token:s,expires_in:Number(o.get("expires_in"))}):t({error:o.get("error"),error_description:o.get("error_description")})}i.style.display="block"}catch(e){i.style.display="block"}}})}))()}getCaptchaToken(e,t){var n=this;return N((function*(){if(!e){var i=yield n.findCaptchaToken();if(i)return i}var o;if(n.isMatch()||P()){var a=yield n.config.request(r.CAPTCHA_DATA_URL,{method:"POST",body:{state:t,redirect_uri:""},withCredentials:!1});o={url:"".concat(a.data,"?state=").concat(encodeURIComponent(t),"&token=").concat(encodeURIComponent(a.token))}}else{var s="".concat(window.location.origin+window.location.pathname,"?__captcha=on");if((o=yield n.config.request(r.GET_CAPTCHA_URL,{method:"POST",body:{client_id:n.config.clientId,redirect_uri:s,state:t},withCredentials:!1})).captcha_token){var c={captcha_token:o.captcha_token,expires_in:o.expires_in};return n.saveCaptchaToken(c),o.captcha_token}}var u=yield n.config.openURIWithCallback(o.url);return n.saveCaptchaToken(u),u.captcha_token}))()}appendCaptchaTokenToURL(e,t,r){var n=this;return N((function*(){var i=yield n.getCaptchaToken(r,t);return e.indexOf("?")>0?e+="&captcha_token=".concat(i):e+="?captcha_token=".concat(i),e}))()}saveCaptchaToken(e){var t=this;return N((function*(){e.expires_at=new Date(Date.now()+1e3*(e.expires_in-10));var r=JSON.stringify(e);yield t.config.storage.setItem(t.tokenSectionName,r)}))()}findCaptchaToken(){var e=this;return N((function*(){var t=yield e.config.storage.getItem(e.tokenSectionName);if(null!=t)try{var r=JSON.parse(t);return null!=r&&r.expires_at&&(r.expires_at=new Date(r.expires_at)),r.expires_at<new Date?null:r.captcha_token}catch(t){return yield e.config.storage.removeItem(e.tokenSectionName),null}return null}))()}}var L=["provider_redirect_uri","other_params"];function k(e,t,r,n,i,o,a){try{var s=e[o](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,i)}function x(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function a(e){k(o,n,i,a,s,"next",e)}function s(e){k(o,n,i,a,s,"throw",e)}a(void 0)}))}}function q(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function H(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?q(Object(r),!0).forEach((function(t){j(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):q(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function j(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function V(e,t){try{var r;if(((null==t||null===(r=t.root)||void 0===r?void 0:r.globalThis)||globalThis).IS_MP_BUILD)return}catch(e){}}class G{static parseParamsToSearch(e){return Object.keys(e).forEach(t=>{e[t]||delete e[t]}),new w(e).toString()}constructor(e){var{request:t}=e,r=e.credentialsClient;if(!r){var n={apiOrigin:e.apiOrigin,clientId:e.clientId,storage:e.storage,env:e.env,baseRequest:e.baseRequest,anonymousSignInFunc:e.anonymousSignInFunc,wxCloud:e.wxCloud};r=new C(n)}if(!t){var i=r.request.bind(r),o=new D(H({clientId:e.clientId,request:i,storage:e.storage,adapter:e.adapter},e.captchaOptions));t=o.request.bind(o)}this.config={env:e.env,apiOrigin:e.apiOrigin,clientId:e.clientId,request:t,credentialsClient:r,storage:e.storage||R,adapter:e.adapter}}getParamsByVersion(e,t){var i,o=u(e),a=(null===(i={v2:n}[null==o?void 0:o.version])||void 0===i?void 0:i[t])||r[t];return o&&delete o.version,{params:o,url:a}}signIn(e){var t=this;return x((function*(){var r=e.version||"v1",n=t.getParamsByVersion(e,"AUTH_SIGN_IN_URL");n.params.query&&delete n.params.query;var i=yield t.getEncryptParams(n.params),o=yield t.config.request(n.url,{method:"POST",body:i});return yield t.config.credentialsClient.setCredentials(H(H({},o),{},{version:r})),Promise.resolve(o)}))()}signInAnonymously(){var e=arguments,t=this;return x((function*(){var n=e.length>0&&void 0!==e[0]?e[0]:{},i=e.length>1&&void 0!==e[1]&&e[1],o=yield t.config.request(r.AUTH_SIGN_IN_ANONYMOUSLY_URL,{method:"POST",body:n,useWxCloud:i});return yield t.config.credentialsClient.setCredentials(o),Promise.resolve(o)}))()}signUp(e){var t=this;return x((function*(){var n=yield t.config.request(r.AUTH_SIGN_UP_URL,{method:"POST",body:e});return yield t.config.credentialsClient.setCredentials(n),Promise.resolve(n)}))()}signOut(e){var t=this;return x((function*(){var n={};if(e){try{n=yield t.config.request(r.AUTH_SIGNOUT_URL,{method:"POST",withCredentials:!0,body:e})}catch(e){e.error!==o.UNAUTHENTICATED&&console.log("sign_out_error",e)}return yield t.config.credentialsClient.setCredentials(),n}var i=yield t.config.credentialsClient.getAccessToken(),a=yield t.config.request(r.AUTH_REVOKE_URL,{method:"POST",body:{token:i}});return yield t.config.credentialsClient.setCredentials(),Promise.resolve(a)}))()}revokeAllDevices(){var e=this;return x((function*(){yield e.config.request(r.AUTH_REVOKE_ALL_URL,{method:"DELETE",withCredentials:!0})}))()}revokeDevice(e){var t=this;return x((function*(){yield t.config.request(r.AUTHORIZED_DEVICES_DELETE_URL+e.device_id,{method:"DELETE",withCredentials:!0})}))()}getVerification(e,t){var n=this;return x((function*(){var i=!1;"CUR_USER"===e.target?i=!0:(yield n.hasLoginState())&&(i=!0);return n.config.request(r.VERIFICATION_URL,{method:"POST",body:e,withCaptcha:(null==t?void 0:t.withCaptcha)||!1,withCredentials:i})}))()}verify(e){var t=this;return x((function*(){var r=t.getParamsByVersion(e,"VERIFY_URL"),n=yield t.config.request(r.url,{method:"POST",body:r.params});return"v2"===(null==e?void 0:e.version)&&(yield t.config.credentialsClient.setCredentials(H(H({},n),{},{version:"v2"}))),n}))()}genProviderRedirectUri(e){var t=this;return x((function*(){var{provider_redirect_uri:n,other_params:i={}}=e,o=function(e,t){if(null==e)return{};var r,n,i=function(e,t){if(null==e)return{};var r={};for(var n in e)if({}.hasOwnProperty.call(e,n)){if(t.includes(n))continue;r[n]=e[n]}return r}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(n=0;n<o.length;n++)r=o[n],t.includes(r)||{}.propertyIsEnumerable.call(e,r)&&(i[r]=e[r])}return i}(e,L);n&&!o.redirect_uri&&(o.redirect_uri=n);var a="".concat(r.PROVIDER_URI_URL,"?").concat(G.parseParamsToSearch(o));return Object.keys(i).forEach(e=>{var t=i[e];("sign_out_uri"!==e||(null==t?void 0:t.length)>0)&&(a+="&other_params[".concat(e,"]=").concat(encodeURIComponent(t)))}),t.config.request(a,{method:"GET"})}))()}grantProviderToken(e){var t=arguments,n=this;return x((function*(){var i=t.length>1&&void 0!==t[1]&&t[1];return n.config.request(r.PROVIDER_TOKEN_URL,{method:"POST",body:e,useWxCloud:i})}))()}patchProviderToken(e){var t=this;return x((function*(){return t.config.request(r.PROVIDER_TOKEN_URL,{method:"PATCH",body:e})}))()}signInWithProvider(e){var t=arguments,r=this;return x((function*(){var n=t.length>1&&void 0!==t[1]&&t[1],i=r.getParamsByVersion(e,"AUTH_SIGN_IN_WITH_PROVIDER_URL"),o=yield r.config.request(i.url,{method:"POST",body:i.params,useWxCloud:n});return yield r.config.credentialsClient.setCredentials(H(H({},o),{},{version:(null==e?void 0:e.version)||"v1"})),Promise.resolve(o)}))()}signInCustom(e){var t=this;return x((function*(){var n=yield t.config.request(r.AUTH_SIGN_IN_CUSTOM,{method:"POST",body:e});return yield t.config.credentialsClient.setCredentials(n),Promise.resolve(n)}))()}signInWithWechat(){var e=arguments,t=this;return x((function*(){var n=e.length>0&&void 0!==e[0]?e[0]:{},i=yield t.config.request(r.AUTH_SIGN_IN_WITH_WECHAT_URL,{method:"POST",body:n});return yield t.config.credentialsClient.setCredentials(i),Promise.resolve(i)}))()}bindWithProvider(e){var t=this;return x((function*(){return t.config.request(r.PROVIDER_BIND_URL,{method:"POST",body:e,withCredentials:!0})}))()}getUserProfile(e){var t=this;return x((function*(){return t.getUserInfo(e)}))()}getUserInfo(){var e=arguments,t=this;return x((function*(){var r,n=e.length>0&&void 0!==e[0]?e[0]:{},i=t.getParamsByVersion(n,"USER_ME_URL");if(null!==(r=i.params)&&void 0!==r&&r.query){var o=new w(i.params.query);i.url+="?".concat(o.toString())}var a=yield t.config.request(i.url,{method:"GET",withCredentials:!0});return a.sub&&(a.uid=a.sub),a}))()}getWedaUserInfo(){var e=this;return x((function*(){return yield e.config.request(r.WEDA_USER_URL,{method:"GET",withCredentials:!0})}))()}deleteMe(e){var t=this;return x((function*(){var r=t.getParamsByVersion(e,"USER_ME_URL"),n="".concat(r.url,"?").concat(G.parseParamsToSearch(r.params));return t.config.request(n,{method:"DELETE",withCredentials:!0})}))()}hasLoginState(){var e=this;return x((function*(){try{return yield e.config.credentialsClient.getAccessToken(),!0}catch(e){return!1}}))()}hasLoginStateSync(){return this.config.credentialsClient.getCredentialsSync()}getLoginState(){var e=this;return x((function*(){return e.config.credentialsClient.getCredentialsAsync()}))()}transByProvider(e){var t=this;return x((function*(){return t.config.request(r.USER_TRANS_BY_PROVIDER_URL,{method:"PATCH",body:e,withCredentials:!0})}))()}grantToken(e){var t=this;return x((function*(){var r=t.getParamsByVersion(e,"AUTH_TOKEN_URL");return t.config.request(r.url,{method:"POST",body:r.params})}))()}getProviders(){var e=this;return x((function*(){return e.config.request(r.PROVIDER_LIST,{method:"GET",withCredentials:!0})}))()}unbindProvider(e){var t=this;return x((function*(){return t.config.request("".concat(r.PROVIDER_UNBIND_URL,"/").concat(e.provider_id),{method:"DELETE",withCredentials:!0})}))()}checkPassword(e){var t=this;return x((function*(){return t.config.request("".concat(r.CHECK_PWD_URL),{method:"POST",withCredentials:!0,body:e})}))()}editContact(e){var t=this;return x((function*(){return t.config.request("".concat(r.BIND_CONTACT_URL),{method:"PATCH",withCredentials:!0,body:e})}))()}setPassword(e){var t=this;return x((function*(){return t.config.request("".concat(r.AUTH_SET_PASSWORD),{method:"PATCH",withCredentials:!0,body:e})}))()}updatePasswordByOld(e){var t=this;return x((function*(){var r=yield t.sudo({password:e.old_password});return t.setPassword({sudo_token:r.sudo_token,new_password:e.new_password})}))()}sudo(e){var t=this;return x((function*(){return t.config.request("".concat(r.SUDO_URL),{method:"POST",withCredentials:!0,body:e})}))()}sendVerificationCodeToCurrentUser(e){var t=this;return x((function*(){return e.target="CUR_USER",t.config.request(r.VERIFICATION_URL,{method:"POST",body:e,withCredentials:!0,withCaptcha:!0})}))()}changeBoundProvider(e){var t=this;return x((function*(){return t.config.request("".concat(r.PROVIDER_LIST,"/").concat(e.provider_id,"/trans"),{method:"POST",body:{provider_trans_token:e.trans_token},withCredentials:!0})}))()}setUserProfile(e){var t=this;return x((function*(){return t.config.request(r.USER_PRIFILE_URL,{method:"PATCH",body:e,withCredentials:!0})}))()}updateUserBasicInfo(e){var t=this;return x((function*(){return t.config.request(r.USER_BASIC_EDIT_URL,{method:"POST",withCredentials:!0,body:e})}))()}queryUserProfile(e){var t=this;return x((function*(){var n=new w(e);return t.config.request("".concat(r.USER_QUERY_URL,"?").concat(n.toString()),{method:"GET",withCredentials:!0})}))()}setCustomSignFunc(e){this.getCustomSignTicketFn=e}signInWithCustomTicket(){var e=this;return x((function*(){var t=e.getCustomSignTicketFn;if(!t)return Promise.reject({error:"failed_precondition",error_description:"please use setCustomSignFunc to set custom sign function"});var r=yield t();return e.signInCustom({provider_id:"custom",ticket:r})}))()}resetPassword(e){var t=this;return x((function*(){return t.config.request(r.AUTH_RESET_PASSWORD,{method:"POST",body:e})}))()}authorize(e){var t=this;return x((function*(){return t.config.request(r.AUTHORIZE_URL,{method:"POST",withCredentials:!0,body:e})}))()}authorizeDevice(e){var t=this;return x((function*(){return t.config.request(r.AUTHORIZE_DEVICE_URL,{method:"POST",withCredentials:!0,body:e})}))()}deviceAuthorize(e){var t=this;return x((function*(){return t.config.request(r.AUTH_GET_DEVICE_CODE,{method:"POST",body:e,withCredentials:!0})}))()}authorizeInfo(e){var t=this;return x((function*(){var n="".concat(r.AUTHORIZE_INFO_URL,"?").concat(G.parseParamsToSearch(e)),i=!0,o=!1;return(yield t.hasLoginState())&&(o=!0,i=!1),t.config.request(n,{method:"GET",withBasicAuth:i,withCredentials:o})}))()}checkUsername(e){var t=this;return x((function*(){return t.config.request(r.CHECK_USERNAME,{method:"GET",body:e,withCredentials:!0})}))()}checkIfUserExist(e){var t=this;return x((function*(){var n=new w(e);return t.config.request("".concat(r.CHECK_IF_USER_EXIST,"?").concat(n.toString()),{method:"GET"})}))()}loginScope(){var e=this;return x((function*(){return e.config.credentialsClient.getScope()}))()}loginGroups(){var e=this;return x((function*(){return e.config.credentialsClient.getGroups()}))()}refreshTokenForce(e){var t=this;return x((function*(){var r=yield t.config.credentialsClient.getCredentials();return yield t.config.credentialsClient.refreshToken(H(H({},r),{},{version:(null==e?void 0:e.version)||"v1"}))}))()}getCredentials(){var e=this;return x((function*(){return e.config.credentialsClient.getCredentials()}))()}getPublicKey(){var e=this;return x((function*(){return e.config.request(n.AUTH_PUBLIC_KEY,{method:"POST",body:{}})}))()}getEncryptParams(e){var t=this;return x((function*(){var{isEncrypt:r}=e;delete e.isEncrypt;var n=u(e),i=V(0,t.config.adapter);if(!i)return e;var o="",a="";try{var s=yield t.getPublicKey();if(o=s.public_key,a=s.public_key_thumbprint,!o||!a)throw s}catch(e){throw e}return{params:i.getEncryptInfo({publicKey:o,payload:n}),public_key_thumbprint:a}}))()}getProviderSubType(){var e=this;return x((function*(){return e.config.request(r.GET_PROVIDER_TYPE,{method:"POST",body:{provider_id:"weda"}})}))()}verifyCaptchaData(e){var t=this;return x((function*(){var{token:n,key:i}=e;return t.config.request(r.VERIFY_CAPTCHA_DATA_URL,{method:"POST",body:{token:n,key:i},withCredentials:!1})}))()}createCaptchaData(e){var t=this;return x((function*(){var{state:n,redirect_uri:i}=e;return t.config.request(r.CAPTCHA_DATA_URL,{method:"POST",body:{state:n,redirect_uri:i},withCredentials:!1})}))()}getMiniProgramCode(e){var t=this;return x((function*(){return t.config.request(r.GET_MINIPROGRAM_QRCODE,{method:"POST",body:e})}))()}getMiniProgramQrCodeStatus(e){var t=this;return x((function*(){return t.config.request(r.GET_MINIPROGRAM_QRCODE_STATUS,{method:"POST",body:e})}))()}getUserBehaviorLog(e){var t=this;return x((function*(){var n="".concat(r.GET_USER_BEHAVIOR_LOG,"?").concat({LOGIN:"query[action]=USER_LOGIN",MODIFY:"ne_query[action]=USER_LOGIN"}[e.type],"&limit=").concat(e.limit).concat(e.page_token?"&page_token=".concat(e.page_token):"");return t.config.request(n,{method:"GET",withCredentials:!0})}))()}modifyPassword(e){var t=this;return x((function*(){var n="",i="",o=V(0,t.config.adapter);if(!o)throw new Error("do not support encrypt, a encrypt util required.");try{var a=yield t.getPublicKey();if(n=a.public_key,i=a.public_key_thumbprint,!n||!i)throw a}catch(e){throw e}var s=e.password?o.getEncryptInfo({publicKey:n,payload:e.password}):"",c=o.getEncryptInfo({publicKey:n,payload:e.new_password});return t.config.request(r.USER_BASIC_EDIT_URL,{method:"POST",withCredentials:!0,body:{user_id:e.user_id,encrypt_password:s,encrypt_new_password:c,public_key_thumbprint:i}})}))()}modifyPasswordWithoutLogin(e){var t=this;return x((function*(){var r="",i="",o=V(0,t.config.adapter);if(!o)throw new Error("do not support encrypt, a encrypt util required.");try{var a=yield t.getPublicKey();if(r=a.public_key,i=a.public_key_thumbprint,!r||!i)throw a}catch(e){throw e}var s=e.password?o.getEncryptInfo({publicKey:r,payload:e.password}):"",c=o.getEncryptInfo({publicKey:r,payload:e.new_password});return t.config.request(n.AUTH_RESET_PASSWORD,{method:"POST",body:{username:e.username,password:s,new_password:c,public_key_thumbprint:i}})}))()}}function W(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function B(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?W(Object(r),!0).forEach((function(t){F(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):W(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function F(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}class M{constructor(e){var{apiOrigin:t,clientId:r,env:n,storage:i,request:o,baseRequest:a,anonymousSignInFunc:s,wxCloud:c,adapter:u}=e;this.oauth2client=new C({apiOrigin:t,clientId:r,env:n,storage:i,baseRequest:a||o,anonymousSignInFunc:s,wxCloud:c}),this.authApi=new G(B(B({credentialsClient:this.oauth2client},e),{},{request:o?this.oauth2client.request.bind(this.oauth2client):void 0,adapter:u}))}}return t})());
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("tcboauth",[],t):"object"==typeof exports?exports.tcboauth=t():e.tcboauth=t()}("undefined"!=typeof window?window:this,()=>(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Auth:()=>G,CloudbaseOAuth:()=>M,authModels:()=>s});var r,n,i,o,a,s={};function c(){return"xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g,e=>{var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})}e.r(s),function(e){e.AUTH_SIGN_UP_URL="/auth/v1/signup",e.AUTH_TOKEN_URL="/auth/v1/token",e.AUTH_REVOKE_URL="/auth/v1/revoke",e.WEDA_USER_URL="/auth/v1/plugin/weda/userinfo",e.AUTH_RESET_PASSWORD="/auth/v1/reset",e.AUTH_GET_DEVICE_CODE="/auth/v1/device/code",e.CHECK_USERNAME="/auth/v1/checkUsername",e.CHECK_IF_USER_EXIST="/auth/v1/checkIfUserExist",e.GET_PROVIDER_TYPE="/auth/v1/mgr/provider/providerSubType",e.AUTH_SIGN_IN_URL="/auth/v1/signin",e.AUTH_SIGN_IN_ANONYMOUSLY_URL="/auth/v1/signin/anonymously",e.AUTH_SIGN_IN_WITH_PROVIDER_URL="/auth/v1/signin/with/provider",e.AUTH_SIGN_IN_WITH_WECHAT_URL="/auth/v1/signin/wechat/noauth",e.AUTH_SIGN_IN_CUSTOM="/auth/v1/signin/custom",e.PROVIDER_TOKEN_URL="/auth/v1/provider/token",e.PROVIDER_URI_URL="/auth/v1/provider/uri",e.USER_ME_URL="/auth/v1/user/me",e.AUTH_SIGNOUT_URL="/auth/v1/user/signout",e.USER_QUERY_URL="/auth/v1/user/query",e.USER_PRIFILE_URL="/auth/v1/user/profile",e.USER_BASIC_EDIT_URL="/auth/v1/user/basic/edit",e.USER_TRANS_BY_PROVIDER_URL="/auth/v1/user/trans/by/provider",e.PROVIDER_LIST="/auth/v1/user/provider",e.PROVIDER_BIND_URL="/auth/v1/user/provider/bind",e.PROVIDER_UNBIND_URL="/auth/v1/user/provider",e.CHECK_PWD_URL="/auth/v1/user/sudo",e.SUDO_URL="/auth/v1/user/sudo",e.BIND_CONTACT_URL="/auth/v1/user/contact",e.AUTH_SET_PASSWORD="/auth/v1/user/password",e.AUTHORIZE_DEVICE_URL="/auth/v1/user/device/authorize",e.AUTHORIZE_URL="/auth/v1/user/authorize",e.AUTHORIZE_INFO_URL="/auth/v1/user/authorize/info",e.AUTHORIZED_DEVICES_DELETE_URL="/auth/v1/user/authorized/devices/",e.AUTH_REVOKE_ALL_URL="/auth/v1/user/revoke/all",e.GET_USER_BEHAVIOR_LOG="/auth/v1/user/security/history",e.VERIFICATION_URL="/auth/v1/verification",e.VERIFY_URL="/auth/v1/verification/verify",e.CAPTCHA_DATA_URL="/auth/v1/captcha/data",e.VERIFY_CAPTCHA_DATA_URL="/auth/v1/captcha/data/verify",e.GET_CAPTCHA_URL="/auth/v1/captcha/init",e.GET_MINIPROGRAM_QRCODE="/auth/v1/qrcode/generate",e.GET_MINIPROGRAM_QRCODE_STATUS="/auth/v1/qrcode/get/status"}(r||(r={})),function(e){e.AUTH_SIGN_IN_URL="/auth/v2/signin/username",e.AUTH_TOKEN_URL="/auth/v2/token",e.USER_ME_URL="/auth/v2/user/me",e.VERIFY_URL="/auth/v2/signin/verificationcode",e.AUTH_SIGN_IN_WITH_PROVIDER_URL="/auth/v2/signin/with/provider",e.AUTH_PUBLIC_KEY="/auth/v2/signin/publichkey",e.AUTH_RESET_PASSWORD="/auth/v2/signin/password/update"}(n||(n={})),function(e){e.REGISTER="REGISTER",e.SIGN_IN="SIGN_IN",e.PASSWORD_RESET="PASSWORD_RESET",e.EMAIL_ADDRESS_CHANGE="EMAIL_ADDRESS_CHANGE",e.PHONE_NUMBER_CHANGE="PHONE_NUMBER_CHANGE"}(i||(i={})),function(e){e.UNREACHABLE="unreachable",e.LOCAL="local",e.CANCELLED="cancelled",e.UNKNOWN="unknown",e.UNAUTHENTICATED="unauthenticated",e.RESOURCE_EXHAUSTED="resource_exhausted",e.FAILED_PRECONDITION="failed_precondition",e.INVALID_ARGUMENT="invalid_argument",e.DEADLINE_EXCEEDED="deadline_exceeded",e.NOT_FOUND="not_found",e.ALREADY_EXISTS="already_exists",e.PERMISSION_DENIED="permission_denied",e.ABORTED="aborted",e.OUT_OF_RANGE="out_of_range",e.UNIMPLEMENTED="unimplemented",e.INTERNAL="internal",e.UNAVAILABLE="unavailable",e.DATA_LOSS="data_loss",e.INVALID_PASSWORD="invalid_password",e.PASSWORD_NOT_SET="password_not_set",e.INVALID_STATUS="invalid_status",e.USER_PENDING="user_pending",e.USER_BLOCKED="user_blocked",e.INVALID_VERIFICATION_CODE="invalid_verification_code",e.TWO_FACTOR_REQUIRED="two_factor_required",e.INVALID_TWO_FACTOR="invalid_two_factor",e.INVALID_TWO_FACTOR_RECOVERY="invalid_two_factor_recovery",e.UNDER_REVIEW="under_review",e.INVALID_REQUEST="invalid_request",e.UNAUTHORIZED_CLIENT="unauthorized_client",e.ACCESS_DENIED="access_denied",e.UNSUPPORTED_RESPONSE_TYPE="unsupported_response_type",e.INVALID_SCOPE="invalid_scope",e.INVALID_GRANT="invalid_grant",e.SERVER_ERROR="server_error",e.TEMPORARILY_UNAVAILABLE="temporarily_unavailable",e.INTERACTION_REQUIRED="interaction_required",e.LOGIN_REQUIRED="login_required",e.ACCOUNT_SELECTION_REQUIRED="account_selection_required",e.CONSENT_REQUIRED="consent_required",e.INVALID_REQUEST_URI="invalid_request_uri",e.INVALID_REQUEST_OBJECT="invalid_request_object",e.REQUEST_NOT_SUPPORTED="request_not_supported",e.REQUEST_URI_NOT_SUPPORTED="request_uri_not_supported",e.REGISTRATION_NOT_SUPPORTED="registration_not_supported",e.CAPTCHA_REQUIRED="captcha_required",e.CAPTCHA_INVALID="captcha_invalid"}(o||(o={})),function(e){e.CLIENT_ID="client_id",e.CLIENT_SECRET="client_secret",e.RESPONSE_TYPE="response_type",e.SCOPE="scope",e.STATE="state",e.REDIRECT_URI="redirect_uri",e.ERROR="error",e.ERROR_DESCRIPTION="error_description",e.ERROR_URI="error_uri",e.GRANT_TYPE="grant_type",e.CODE="code",e.ACCESS_TOKEN="access_token",e.TOKEN_TYPE="token_type",e.EXPIRES_IN="expires_in",e.USERNAME="username",e.PASSWORD="password",e.REFRESH_TOKEN="refresh_token"}(a||(a={}));var u=e=>{var t=t=>{for(var r in e)e.hasOwnProperty(r)&&(t[r]=u(e[r]));return t},r=null==e?"NullOrUndefined":Object.prototype.toString.call(e).slice(8,-1);if(["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"].includes(r))return e.slice();switch(r){case"Object":return t(Object.create(Object.getPrototypeOf(e)));case"Array":return t([]);case"Date":return new Date(e.valueOf());case"RegExp":return new RegExp(e.source,(e.global?"g":"")+(e.ignoreCase?"i":"")+(e.multiline?"m":"")+(e.sticky?"y":"")+(e.unicode?"u":""));default:return e}},l=e=>{var t=e.match(/^(?:http(s)?:\/\/[^\/]+)?(\/[^\?#]*)/);return t&&t[2]||""};function d(e,t,r,n,i,o,a){try{var s=e[o](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,i)}function h(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function a(e){d(o,n,i,a,s,"next",e)}function s(e){d(o,n,i,a,s,"throw",e)}a(void 0)}))}}var _=new Map;class v{constructor(e){this.clientId=(null==e?void 0:e.clientId)||"",_=_||new Map}run(e,t){var r=this;return h((function*(){e="".concat(r.clientId,"_").concat(e);var n=_.get(e);return n||(n=new Promise((n,i)=>{h((function*(){try{yield r.runIdlePromise();var o=t();n(yield o)}catch(e){i(e)}finally{_.delete(e)}}))()}),_.set(e,n)),n}))()}runIdlePromise(){return Promise.resolve()}}var f="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";function p(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function y(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?p(Object(r),!0).forEach((function(t){g(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):p(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function g(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function E(e,t,r,n,i,o,a){try{var s=e[o](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,i)}function m(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function a(e){E(o,n,i,a,s,"next",e)}function s(e){E(o,n,i,a,s,"throw",e)}a(void 0)}))}}var I=function(){var e=m((function*(e,t){var r=null,n=null;try{var i=Object.assign({},t);i.method||(i.method="GET"),i.body&&"string"!=typeof i.body&&(i.body=JSON.stringify(i.body));var a=yield fetch(e,i),s=yield a.json();null!=s&&s.error?(n=s).error_uri=new URL(e).pathname:r=s}catch(t){n={error:o.UNREACHABLE,error_description:t.message,error_uri:new URL(e).pathname}}if(n)throw n;return r}));return function(t,r){return e.apply(this,arguments)}}();var R=new class{constructor(e){this._env=(null==e?void 0:e.env)||""}getItem(e){var t=this;return m((function*(){return window.localStorage.getItem("".concat(e).concat(t._env))}))()}removeItem(e){var t=this;return m((function*(){window.localStorage.removeItem("".concat(e).concat(t._env))}))()}setItem(e,t){var r=this;return m((function*(){window.localStorage.setItem("".concat(e).concat(r._env),t)}))()}getItemSync(e){return window.localStorage.getItem("".concat(e).concat(this._env))}removeItemSync(e){window.localStorage.removeItem("".concat(e).concat(this._env))}setItemSync(e,t){window.localStorage.setItem("".concat(e).concat(this._env),t)}};function T(e){var t=!0;return null!=e&&e.expires_at&&null!=e&&e.access_token&&(t=e.expires_at<new Date),t}class S{constructor(e){this.credentials=null,this.singlePromise=null,this.tokenSectionName=e.tokenSectionName,this.storage=e.storage,this.clientId=e.clientId,this.singlePromise=new v({clientId:this.clientId})}getStorageCredentialsSync(){var e=null,t=this.storage.getItemSync(this.tokenSectionName);if(null!=t)try{var r;null!==(r=e=JSON.parse(t))&&void 0!==r&&r.expires_at&&(e.expires_at=new Date(e.expires_at))}catch(t){this.storage.removeItem(this.tokenSectionName),e=null}return e}setCredentials(e){var t=this;return m((function*(){if(null!=e&&e.expires_in){if(null!=e&&e.expires_at||(e.expires_at=new Date(Date.now()+1e3*(e.expires_in-30))),t.storage){var r=JSON.stringify(e);yield t.storage.setItem(t.tokenSectionName,r)}t.credentials=e}else t.storage&&(yield t.storage.removeItem(t.tokenSectionName)),t.credentials=null}))()}getCredentials(){var e=this;return m((function*(){return e.singlePromise.run("getCredentials",m((function*(){return T(e.credentials)&&(e.credentials=yield e.getStorageCredentials()),e.credentials})))}))()}getStorageCredentials(){var e=this;return m((function*(){return e.singlePromise.run("_getStorageCredentials",m((function*(){var t=null,r=yield e.storage.getItem(e.tokenSectionName);if(null!=r)try{var n;null!==(n=t=JSON.parse(r))&&void 0!==n&&n.expires_at&&(t.expires_at=new Date(t.expires_at))}catch(r){yield e.storage.removeItem(e.tokenSectionName),t=null}return t})))}))()}}class C{constructor(e){this.singlePromise=null,e.clientSecret||(e.clientSecret=""),!e.clientId&&e.env&&(e.clientId=e.env),this.apiOrigin=e.apiOrigin,this.clientId=e.clientId,this.singlePromise=new v({clientId:this.clientId}),this.retry=this.formatRetry(e.retry,C.defaultRetry),e.baseRequest?this.baseRequest=e.baseRequest:this.baseRequest=I,this.tokenInURL=e.tokenInURL,this.headers=e.headers,this.storage=e.storage||R,this.localCredentials=new S({tokenSectionName:"credentials_".concat(e.clientId),storage:this.storage,clientId:e.clientId}),this.clientSecret=e.clientSecret,e.clientId&&(this.basicAuth="Basic ".concat(function(e){for(var t,r,n,i,o="",a=0,s=(e=String(e)).length%3;a<e.length;){if((r=e.charCodeAt(a++))>255||(n=e.charCodeAt(a++))>255||(i=e.charCodeAt(a++))>255)throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");o+=f.charAt((t=r<<16|n<<8|i)>>18&63)+f.charAt(t>>12&63)+f.charAt(t>>6&63)+f.charAt(63&t)}return s?o.slice(0,s-3)+"===".substring(s):o}("".concat(e.clientId,":").concat(this.clientSecret)))),this.wxCloud=e.wxCloud;try{(function(){if("undefined"==typeof wx)return!1;if("undefined"==typeof Page)return!1;if(!wx.getSystemInfoSync)return!1;if(!wx.getStorageSync)return!1;if(!wx.setStorageSync)return!1;if(!wx.connectSocket)return!1;if(!wx.request)return!1;try{if(!wx.getSystemInfoSync())return!1;if("qq"===wx.getSystemInfoSync().AppPlatform)return!1}catch(e){return!1}return!0})()&&void 0===this.wxCloud&&e.env&&(wx.cloud.init({env:e.env}),this.wxCloud=wx.cloud)}catch(e){}this.refreshTokenFunc=e.refreshTokenFunc||this.defaultRefreshTokenFunc,this.anonymousSignInFunc=e.anonymousSignInFunc,this.onCredentialsError=e.onCredentialsError}setCredentials(e){return this.localCredentials.setCredentials(e)}getAccessToken(){var e=this;return m((function*(){var t=yield e.getCredentials();if(null!=t&&t.access_token)return Promise.resolve(t.access_token);var r={error:o.UNAUTHENTICATED};return Promise.reject(r)}))()}request(e,t){var r=this;return m((function*(){var n,i;t||(t={});var a=r.formatRetry(t.retry,r.retry);if(t.headers=t.headers||{},r.headers&&(t.headers=y(y({},r.headers),t.headers)),t.headers["x-request-id"]||(t.headers["x-request-id"]=c()),!t.headers["x-device-id"]){var s=yield r.getDeviceId();t.headers["x-device-id"]=s}if(null!==(n=t)&&void 0!==n&&n.withBasicAuth&&r.basicAuth&&(t.headers.Authorization=r.basicAuth),null!==(i=t)&&void 0!==i&&i.withCredentials){var u=yield r.getCredentials();u&&(r.tokenInURL?(e.indexOf("?")<0&&(e+="?"),e+="access_token=".concat(u.access_token)):t.headers.Authorization="".concat(u.token_type," ").concat(u.access_token))}else r.clientId&&e.indexOf("client_id")<0&&(e+=e.indexOf("?")<0?"?":"&",e+="client_id=".concat(r.clientId));e.startsWith("/")&&(e=r.apiOrigin+e);for(var l=null,d=a+1,h=0;h<d;h++){try{l=t.useWxCloud?yield r.wxCloudCallFunction(e,t):yield r.baseRequest(e,t);break}catch(e){if(t.withCredentials&&e&&e.error===o.UNAUTHENTICATED)return yield r.setCredentials(null),Promise.reject(e);if(h===a||!e||"unreachable"!==e.error)return Promise.reject(e)}yield r.sleep(C.retryInterval)}return l}))()}wxCloudCallFunction(e,t){var r=this;return m((function*(){var n=null,i=null;try{var a,s="";try{s=yield wx.getRendererUserAgent()}catch(e){}var{result:c}=yield r.wxCloud.callFunction({name:"httpOverCallFunction",data:{url:e,method:t.method,headers:y({origin:"https://servicewechat.com","User-Agent":s},t.headers),body:t.body}});null!=c&&null!==(a=c.body)&&void 0!==a&&a.error_code?(i=null==c?void 0:c.body).error_uri=l(e):n=null==c?void 0:c.body}catch(t){i={error:o.UNREACHABLE,error_description:t.message,error_uri:l(e)}}if(i)throw i;return n}))()}getCredentials(){var e=this;return m((function*(){var t=yield e.localCredentials.getCredentials();if(!t){var r,n="credentials not found";return null===(r=e.onCredentialsError)||void 0===r||r.call(e,{msg:n}),e.unAuthenticatedError(n)}if(T(t))if(t.refresh_token)try{t=yield e.refreshToken(t)}catch(r){var i;if("anonymous"!==t.scope)return null===(i=e.onCredentialsError)||void 0===i||i.call(e,{msg:r.error_description}),Promise.reject(r);t=yield e.anonymousLogin(t)}else{if("anonymous"!==t.scope){var o,a="no refresh token found in credentials";return null===(o=e.onCredentialsError)||void 0===o||o.call(e,{msg:a}),e.unAuthenticatedError(a)}t=yield e.anonymousLogin(t)}return t}))()}getCredentialsSync(){return this.localCredentials.getStorageCredentialsSync()}getCredentialsAsync(){return this.getCredentials()}getScope(){var e=this;return m((function*(){var t=yield e.localCredentials.getCredentials();if(!t){var r,n="credentials not found";return null===(r=e.onCredentialsError)||void 0===r||r.call(e,{msg:n}),e.unAuthenticatedError(n)}return t.scope}))()}getGroups(){var e=this;return m((function*(){var t=yield e.localCredentials.getCredentials();if(!t){var r,n="credentials not found";return null===(r=e.onCredentialsError)||void 0===r||r.call(e,{msg:n}),e.unAuthenticatedError(n)}return t.groups}))()}refreshToken(e){var t=this;return m((function*(){return t.singlePromise.run("_refreshToken",m((function*(){if(!e||!e.refresh_token){var r,n="no refresh token found in credentials";return null===(r=t.onCredentialsError)||void 0===r||r.call(t,{msg:n}),t.unAuthenticatedError(n)}try{var i=yield t.refreshTokenFunc(e.refresh_token,e);return yield t.localCredentials.setCredentials(i),i}catch(e){var a;if(e.error===o.INVALID_GRANT){var s;yield t.localCredentials.setCredentials(null);var c=e.error_description;return null===(s=t.onCredentialsError)||void 0===s||s.call(t,{msg:c}),t.unAuthenticatedError(c)}return null===(a=t.onCredentialsError)||void 0===a||a.call(t,{msg:e.error_description}),Promise.reject(e)}})))}))()}anonymousLogin(e){var t=this;return m((function*(){return t.singlePromise.run("_anonymousLogin",m((function*(){if(t.anonymousSignInFunc){var r=yield t.anonymousSignInFunc(e);e=r||(yield t.localCredentials.getCredentials())}else e=yield t.anonymousSignIn(e);return e})))}))()}checkRetry(e){var t=null;if(("number"!=typeof e||e<C.minRetry||e>C.maxRetry)&&(t={error:o.UNREACHABLE,error_description:"wrong options param: retry"}),t)throw t;return e}formatRetry(e,t){return void 0===e?t:this.checkRetry(e)}sleep(e){return m((function*(){return new Promise(t=>{setTimeout(()=>{t()},e)})}))()}anonymousSignIn(e){var t=this;return m((function*(){return t.singlePromise.run("_anonymous",m((function*(){if(!e||"anonymous"!==e.scope)return t.unAuthenticatedError("no anonymous in credentials");try{var n=yield t.request(r.AUTH_SIGN_IN_ANONYMOUSLY_URL,{method:"POST",withBasicAuth:!0,body:{}});return yield t.localCredentials.setCredentials(n),n}catch(e){return e.error===o.INVALID_GRANT?(yield t.localCredentials.setCredentials(null),t.unAuthenticatedError(e.error_description)):Promise.reject(e)}})))}))()}defaultRefreshTokenFunc(e,t){var i=this;return m((function*(){if(void 0===e||""===e){var o,a="refresh token not found";return null===(o=i.onCredentialsError)||void 0===o||o.call(i,{msg:a}),i.unAuthenticatedError(a)}var s=r.AUTH_TOKEN_URL;return"v2"===(null==t?void 0:t.version)&&(s=n.AUTH_TOKEN_URL),y(y({},yield i.request(s,{method:"POST",body:{client_id:i.clientId,client_secret:i.clientSecret,grant_type:"refresh_token",refresh_token:e}})),{},{version:(null==t?void 0:t.version)||"v1"})}))()}getDeviceId(){var e=this;return m((function*(){if(e.deviceID)return e.deviceID;var t=yield e.storage.getItem("device_id");return"string"==typeof t&&t.length>=16&&t.length<=48||(t=c(),yield e.storage.setItem("device_id",t)),e.deviceID=t,t}))()}unAuthenticatedError(e){var t={error:o.UNAUTHENTICATED,error_description:e};return Promise.reject(t)}}function O(){if("undefined"==typeof globalThis)return!1;var{wx:e}=globalThis;if(void 0===e)return!1;if(void 0===globalThis.Page)return!1;if(!e.getSystemInfoSync)return!1;if(!e.getStorageSync)return!1;if(!e.setStorageSync)return!1;if(!e.connectSocket)return!1;if(!e.request)return!1;try{if(!e.getSystemInfoSync())return!1;if("qq"===e.getSystemInfoSync().AppPlatform)return!1}catch(e){return!1}return!0}C.defaultRetry=2,C.minRetry=0,C.maxRetry=5,C.retryInterval=1e3;var U=!1;function A(){U=U||void 0!==typeof window&&"miniprogram"===window.__wxjs_environment}try{O()||(U=U||!!navigator.userAgent.match(/miniprogram/i)||"miniprogram"===window.__wxjs_environment,window&&window.WeixinJSBridge&&window.WeixinJSBridge.invoke?A():"undefined"!=typeof document&&document.addEventListener("WeixinJSBridgeReady",A,!1))}catch(e){}function P(){return U}const w=class{constructor(e){if(this.params={},"string"==typeof e)this.parse(e);else if(e&&"object"==typeof e)for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&this.append(t,e[t])}parse(e){e.split("&").forEach(e=>{var[t,r]=e.split("=").map(decodeURIComponent);this.append(t,r)})}append(e,t){this.params[e]?this.params[e]=this.params[e].concat([t]):this.params[e]=[t]}get(e){return this.params[e]?this.params[e][0]:null}getAll(e){return this.params[e]||[]}delete(e){delete this.params[e]}has(e){return Object.prototype.hasOwnProperty.call(this.params,e)}set(e,t){this.params[e]=[t]}toString(){var e=this,t=[],r=function(r){Object.prototype.hasOwnProperty.call(e.params,r)&&e.params[r].forEach(e=>{t.push("".concat(encodeURIComponent(r),"=").concat(encodeURIComponent(e)))})};for(var n in this.params)r(n);return t.join("&")}};function b(e,t,r,n,i,o,a){try{var s=e[o](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,i)}function N(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function a(e){b(o,n,i,a,s,"next",e)}function s(e){b(o,n,i,a,s,"throw",e)}a(void 0)}))}}class D{constructor(e){e.openURIWithCallback||(e.openURIWithCallback=this.getDefaultOpenURIWithCallback()),e.storage||(e.storage=R),this.config=e,this.tokenSectionName="captcha_".concat(e.clientId)}isMatch(){var e,t;return(null===(e=this.config)||void 0===e||null===(e=e.adapter)||void 0===e||null===(t=e.isMatch)||void 0===t?void 0:t.call(e))||O()}request(e,t){var r=this;return N((function*(){t||(t={}),t.method||(t.method="GET");var n,i="".concat(t.method,":").concat(e),a=e;t.withCaptcha&&(a=yield r.appendCaptchaTokenToURL(e,i,!1));try{n=yield r.config.request(a,t)}catch(n){return n.error===o.CAPTCHA_REQUIRED||n.error===o.CAPTCHA_INVALID?(e=yield r.appendCaptchaTokenToURL(e,i,n.error===o.CAPTCHA_INVALID),r.config.request(e,t)):Promise.reject(n)}return n}))()}getDefaultOpenURIWithCallback(){if(!this.isMatch()&&!P()&&(window.location.search.indexOf("__captcha")>0&&(document.body.style.display="none"),null===document.getElementById("captcha_panel_wrap"))){var e=document.createElement("div");e.style.cssText="background-color: rgba(0, 0, 0, 0.7);position: fixed;left: 0px;right: 0px;top: 0px;bottom: 0px;padding: 9vw 0 0 0;display: none;z-index:100;",e.setAttribute("id","captcha_panel_wrap"),setTimeout(()=>{document.body.appendChild(e)},0)}return this.defaultOpenURIWithCallback}defaultOpenURIWithCallback(e,t){return N((function*(){var{width:r="355px",height:n="355px"}=t||{};if(e.match(/^(data:.*)$/))return Promise.reject({error:o.UNIMPLEMENTED,error_description:"need to impl captcha data"});var i=document.getElementById("captcha_panel_wrap"),a=document.createElement("iframe");return i.innerHTML="",a.setAttribute("src",e),a.setAttribute("id","review-panel-iframe"),a.style.cssText="min-width:".concat(r,";display:block;height:").concat(n,";margin:0 auto;background-color: rgb(255, 255, 255);border: none;"),i.appendChild(a),i.style.display="block",new Promise((e,t)=>{a.onload=function(){try{var r=window.location,n=a.contentWindow.location;if(n.host+n.pathname===r.host+r.pathname){i.style.display="none";var o=new w(n.search),s=o.get("captcha_token");return s?e({captcha_token:s,expires_in:Number(o.get("expires_in"))}):t({error:o.get("error"),error_description:o.get("error_description")})}i.style.display="block"}catch(e){i.style.display="block"}}})}))()}getCaptchaToken(e,t){var n=this;return N((function*(){if(!e){var i=yield n.findCaptchaToken();if(i)return i}var o;if(n.isMatch()||P()){var a=yield n.config.request(r.CAPTCHA_DATA_URL,{method:"POST",body:{state:t,redirect_uri:""},withCredentials:!1});o={url:"".concat(a.data,"?state=").concat(encodeURIComponent(t),"&token=").concat(encodeURIComponent(a.token))}}else{var s="".concat(window.location.origin+window.location.pathname,"?__captcha=on");if((o=yield n.config.request(r.GET_CAPTCHA_URL,{method:"POST",body:{client_id:n.config.clientId,redirect_uri:s,state:t},withCredentials:!1})).captcha_token){var c={captcha_token:o.captcha_token,expires_in:o.expires_in};return n.saveCaptchaToken(c),o.captcha_token}}var u=yield n.config.openURIWithCallback(o.url);return n.saveCaptchaToken(u),u.captcha_token}))()}appendCaptchaTokenToURL(e,t,r){var n=this;return N((function*(){var i=yield n.getCaptchaToken(r,t);return e.indexOf("?")>0?e+="&captcha_token=".concat(i):e+="?captcha_token=".concat(i),e}))()}saveCaptchaToken(e){var t=this;return N((function*(){e.expires_at=new Date(Date.now()+1e3*(e.expires_in-10));var r=JSON.stringify(e);yield t.config.storage.setItem(t.tokenSectionName,r)}))()}findCaptchaToken(){var e=this;return N((function*(){var t=yield e.config.storage.getItem(e.tokenSectionName);if(null!=t)try{var r=JSON.parse(t);return null!=r&&r.expires_at&&(r.expires_at=new Date(r.expires_at)),r.expires_at<new Date?null:r.captcha_token}catch(t){return yield e.config.storage.removeItem(e.tokenSectionName),null}return null}))()}}var L=["provider_redirect_uri","other_params"];function k(e,t,r,n,i,o,a){try{var s=e[o](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,i)}function x(e){return function(){var t=this,r=arguments;return new Promise((function(n,i){var o=e.apply(t,r);function a(e){k(o,n,i,a,s,"next",e)}function s(e){k(o,n,i,a,s,"throw",e)}a(void 0)}))}}function q(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function H(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?q(Object(r),!0).forEach((function(t){j(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):q(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function j(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function V(e,t){try{var r;if(((null==t||null===(r=t.root)||void 0===r?void 0:r.globalThis)||globalThis).IS_MP_BUILD)return}catch(e){}}class G{static parseParamsToSearch(e){return Object.keys(e).forEach(t=>{e[t]||delete e[t]}),new w(e).toString()}constructor(e){var{request:t}=e,r=e.credentialsClient;if(!r){var n={apiOrigin:e.apiOrigin,clientId:e.clientId,storage:e.storage,env:e.env,baseRequest:e.baseRequest,anonymousSignInFunc:e.anonymousSignInFunc,wxCloud:e.wxCloud,onCredentialsError:e.onCredentialsError};r=new C(n)}if(!t){var i=r.request.bind(r),o=new D(H({clientId:e.clientId,request:i,storage:e.storage,adapter:e.adapter},e.captchaOptions));t=o.request.bind(o)}this.config={env:e.env,apiOrigin:e.apiOrigin,clientId:e.clientId,request:t,credentialsClient:r,storage:e.storage||R,adapter:e.adapter}}getParamsByVersion(e,t){var i,o=u(e),a=(null===(i={v2:n}[null==o?void 0:o.version])||void 0===i?void 0:i[t])||r[t];return o&&delete o.version,{params:o,url:a}}signIn(e){var t=this;return x((function*(){var r=e.version||"v1",n=t.getParamsByVersion(e,"AUTH_SIGN_IN_URL");n.params.query&&delete n.params.query;var i=yield t.getEncryptParams(n.params),o=yield t.config.request(n.url,{method:"POST",body:i});return yield t.config.credentialsClient.setCredentials(H(H({},o),{},{version:r})),Promise.resolve(o)}))()}signInAnonymously(){var e=arguments,t=this;return x((function*(){var n=e.length>0&&void 0!==e[0]?e[0]:{},i=e.length>1&&void 0!==e[1]&&e[1],o=yield t.config.request(r.AUTH_SIGN_IN_ANONYMOUSLY_URL,{method:"POST",body:n,useWxCloud:i});return yield t.config.credentialsClient.setCredentials(o),Promise.resolve(o)}))()}signUp(e){var t=this;return x((function*(){var n=yield t.config.request(r.AUTH_SIGN_UP_URL,{method:"POST",body:e});return yield t.config.credentialsClient.setCredentials(n),Promise.resolve(n)}))()}signOut(e){var t=this;return x((function*(){var n={};if(e){try{n=yield t.config.request(r.AUTH_SIGNOUT_URL,{method:"POST",withCredentials:!0,body:e})}catch(e){e.error!==o.UNAUTHENTICATED&&console.log("sign_out_error",e)}return yield t.config.credentialsClient.setCredentials(),n}var i=yield t.config.credentialsClient.getAccessToken(),a=yield t.config.request(r.AUTH_REVOKE_URL,{method:"POST",body:{token:i}});return yield t.config.credentialsClient.setCredentials(),Promise.resolve(a)}))()}revokeAllDevices(){var e=this;return x((function*(){yield e.config.request(r.AUTH_REVOKE_ALL_URL,{method:"DELETE",withCredentials:!0})}))()}revokeDevice(e){var t=this;return x((function*(){yield t.config.request(r.AUTHORIZED_DEVICES_DELETE_URL+e.device_id,{method:"DELETE",withCredentials:!0})}))()}getVerification(e,t){var n=this;return x((function*(){var i=!1;"CUR_USER"===e.target?i=!0:(yield n.hasLoginState())&&(i=!0);return n.config.request(r.VERIFICATION_URL,{method:"POST",body:e,withCaptcha:(null==t?void 0:t.withCaptcha)||!1,withCredentials:i})}))()}verify(e){var t=this;return x((function*(){var r=t.getParamsByVersion(e,"VERIFY_URL"),n=yield t.config.request(r.url,{method:"POST",body:r.params});return"v2"===(null==e?void 0:e.version)&&(yield t.config.credentialsClient.setCredentials(H(H({},n),{},{version:"v2"}))),n}))()}genProviderRedirectUri(e){var t=this;return x((function*(){var{provider_redirect_uri:n,other_params:i={}}=e,o=function(e,t){if(null==e)return{};var r,n,i=function(e,t){if(null==e)return{};var r={};for(var n in e)if({}.hasOwnProperty.call(e,n)){if(t.includes(n))continue;r[n]=e[n]}return r}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(n=0;n<o.length;n++)r=o[n],t.includes(r)||{}.propertyIsEnumerable.call(e,r)&&(i[r]=e[r])}return i}(e,L);n&&!o.redirect_uri&&(o.redirect_uri=n);var a="".concat(r.PROVIDER_URI_URL,"?").concat(G.parseParamsToSearch(o));return Object.keys(i).forEach(e=>{var t=i[e];("sign_out_uri"!==e||(null==t?void 0:t.length)>0)&&(a+="&other_params[".concat(e,"]=").concat(encodeURIComponent(t)))}),t.config.request(a,{method:"GET"})}))()}grantProviderToken(e){var t=arguments,n=this;return x((function*(){var i=t.length>1&&void 0!==t[1]&&t[1];return n.config.request(r.PROVIDER_TOKEN_URL,{method:"POST",body:e,useWxCloud:i})}))()}patchProviderToken(e){var t=this;return x((function*(){return t.config.request(r.PROVIDER_TOKEN_URL,{method:"PATCH",body:e})}))()}signInWithProvider(e){var t=arguments,r=this;return x((function*(){var n=t.length>1&&void 0!==t[1]&&t[1],i=r.getParamsByVersion(e,"AUTH_SIGN_IN_WITH_PROVIDER_URL"),o=yield r.config.request(i.url,{method:"POST",body:i.params,useWxCloud:n});return yield r.config.credentialsClient.setCredentials(H(H({},o),{},{version:(null==e?void 0:e.version)||"v1"})),Promise.resolve(o)}))()}signInCustom(e){var t=this;return x((function*(){var n=yield t.config.request(r.AUTH_SIGN_IN_CUSTOM,{method:"POST",body:e});return yield t.config.credentialsClient.setCredentials(n),Promise.resolve(n)}))()}signInWithWechat(){var e=arguments,t=this;return x((function*(){var n=e.length>0&&void 0!==e[0]?e[0]:{},i=yield t.config.request(r.AUTH_SIGN_IN_WITH_WECHAT_URL,{method:"POST",body:n});return yield t.config.credentialsClient.setCredentials(i),Promise.resolve(i)}))()}bindWithProvider(e){var t=this;return x((function*(){return t.config.request(r.PROVIDER_BIND_URL,{method:"POST",body:e,withCredentials:!0})}))()}getUserProfile(e){var t=this;return x((function*(){return t.getUserInfo(e)}))()}getUserInfo(){var e=arguments,t=this;return x((function*(){var r,n=e.length>0&&void 0!==e[0]?e[0]:{},i=t.getParamsByVersion(n,"USER_ME_URL");if(null!==(r=i.params)&&void 0!==r&&r.query){var o=new w(i.params.query);i.url+="?".concat(o.toString())}var a=yield t.config.request(i.url,{method:"GET",withCredentials:!0});return a.sub&&(a.uid=a.sub),a}))()}getWedaUserInfo(){var e=this;return x((function*(){return yield e.config.request(r.WEDA_USER_URL,{method:"GET",withCredentials:!0})}))()}deleteMe(e){var t=this;return x((function*(){var r=t.getParamsByVersion(e,"USER_ME_URL"),n="".concat(r.url,"?").concat(G.parseParamsToSearch(r.params));return t.config.request(n,{method:"DELETE",withCredentials:!0})}))()}hasLoginState(){var e=this;return x((function*(){try{return yield e.config.credentialsClient.getAccessToken(),!0}catch(e){return!1}}))()}hasLoginStateSync(){return this.config.credentialsClient.getCredentialsSync()}getLoginState(){var e=this;return x((function*(){return e.config.credentialsClient.getCredentialsAsync()}))()}transByProvider(e){var t=this;return x((function*(){return t.config.request(r.USER_TRANS_BY_PROVIDER_URL,{method:"PATCH",body:e,withCredentials:!0})}))()}grantToken(e){var t=this;return x((function*(){var r=t.getParamsByVersion(e,"AUTH_TOKEN_URL");return t.config.request(r.url,{method:"POST",body:r.params})}))()}getProviders(){var e=this;return x((function*(){return e.config.request(r.PROVIDER_LIST,{method:"GET",withCredentials:!0})}))()}unbindProvider(e){var t=this;return x((function*(){return t.config.request("".concat(r.PROVIDER_UNBIND_URL,"/").concat(e.provider_id),{method:"DELETE",withCredentials:!0})}))()}checkPassword(e){var t=this;return x((function*(){return t.config.request("".concat(r.CHECK_PWD_URL),{method:"POST",withCredentials:!0,body:e})}))()}editContact(e){var t=this;return x((function*(){return t.config.request("".concat(r.BIND_CONTACT_URL),{method:"PATCH",withCredentials:!0,body:e})}))()}setPassword(e){var t=this;return x((function*(){return t.config.request("".concat(r.AUTH_SET_PASSWORD),{method:"PATCH",withCredentials:!0,body:e})}))()}updatePasswordByOld(e){var t=this;return x((function*(){var r=yield t.sudo({password:e.old_password});return t.setPassword({sudo_token:r.sudo_token,new_password:e.new_password})}))()}sudo(e){var t=this;return x((function*(){return t.config.request("".concat(r.SUDO_URL),{method:"POST",withCredentials:!0,body:e})}))()}sendVerificationCodeToCurrentUser(e){var t=this;return x((function*(){return e.target="CUR_USER",t.config.request(r.VERIFICATION_URL,{method:"POST",body:e,withCredentials:!0,withCaptcha:!0})}))()}changeBoundProvider(e){var t=this;return x((function*(){return t.config.request("".concat(r.PROVIDER_LIST,"/").concat(e.provider_id,"/trans"),{method:"POST",body:{provider_trans_token:e.trans_token},withCredentials:!0})}))()}setUserProfile(e){var t=this;return x((function*(){return t.config.request(r.USER_PRIFILE_URL,{method:"PATCH",body:e,withCredentials:!0})}))()}updateUserBasicInfo(e){var t=this;return x((function*(){return t.config.request(r.USER_BASIC_EDIT_URL,{method:"POST",withCredentials:!0,body:e})}))()}queryUserProfile(e){var t=this;return x((function*(){var n=new w(e);return t.config.request("".concat(r.USER_QUERY_URL,"?").concat(n.toString()),{method:"GET",withCredentials:!0})}))()}setCustomSignFunc(e){this.getCustomSignTicketFn=e}signInWithCustomTicket(){var e=this;return x((function*(){var t=e.getCustomSignTicketFn;if(!t)return Promise.reject({error:"failed_precondition",error_description:"please use setCustomSignFunc to set custom sign function"});var r=yield t();return e.signInCustom({provider_id:"custom",ticket:r})}))()}resetPassword(e){var t=this;return x((function*(){return t.config.request(r.AUTH_RESET_PASSWORD,{method:"POST",body:e})}))()}authorize(e){var t=this;return x((function*(){return t.config.request(r.AUTHORIZE_URL,{method:"POST",withCredentials:!0,body:e})}))()}authorizeDevice(e){var t=this;return x((function*(){return t.config.request(r.AUTHORIZE_DEVICE_URL,{method:"POST",withCredentials:!0,body:e})}))()}deviceAuthorize(e){var t=this;return x((function*(){return t.config.request(r.AUTH_GET_DEVICE_CODE,{method:"POST",body:e,withCredentials:!0})}))()}authorizeInfo(e){var t=this;return x((function*(){var n="".concat(r.AUTHORIZE_INFO_URL,"?").concat(G.parseParamsToSearch(e)),i=!0,o=!1;return(yield t.hasLoginState())&&(o=!0,i=!1),t.config.request(n,{method:"GET",withBasicAuth:i,withCredentials:o})}))()}checkUsername(e){var t=this;return x((function*(){return t.config.request(r.CHECK_USERNAME,{method:"GET",body:e,withCredentials:!0})}))()}checkIfUserExist(e){var t=this;return x((function*(){var n=new w(e);return t.config.request("".concat(r.CHECK_IF_USER_EXIST,"?").concat(n.toString()),{method:"GET"})}))()}loginScope(){var e=this;return x((function*(){return e.config.credentialsClient.getScope()}))()}loginGroups(){var e=this;return x((function*(){return e.config.credentialsClient.getGroups()}))()}refreshTokenForce(e){var t=this;return x((function*(){var r=yield t.config.credentialsClient.getCredentials();return yield t.config.credentialsClient.refreshToken(H(H({},r),{},{version:(null==e?void 0:e.version)||"v1"}))}))()}getCredentials(){var e=this;return x((function*(){return e.config.credentialsClient.getCredentials()}))()}getPublicKey(){var e=this;return x((function*(){return e.config.request(n.AUTH_PUBLIC_KEY,{method:"POST",body:{}})}))()}getEncryptParams(e){var t=this;return x((function*(){var{isEncrypt:r}=e;delete e.isEncrypt;var n=u(e),i=V(0,t.config.adapter);if(!i)return e;var o="",a="";try{var s=yield t.getPublicKey();if(o=s.public_key,a=s.public_key_thumbprint,!o||!a)throw s}catch(e){throw e}return{params:i.getEncryptInfo({publicKey:o,payload:n}),public_key_thumbprint:a}}))()}getProviderSubType(){var e=this;return x((function*(){return e.config.request(r.GET_PROVIDER_TYPE,{method:"POST",body:{provider_id:"weda"}})}))()}verifyCaptchaData(e){var t=this;return x((function*(){var{token:n,key:i}=e;return t.config.request(r.VERIFY_CAPTCHA_DATA_URL,{method:"POST",body:{token:n,key:i},withCredentials:!1})}))()}createCaptchaData(e){var t=this;return x((function*(){var{state:n,redirect_uri:i}=e;return t.config.request(r.CAPTCHA_DATA_URL,{method:"POST",body:{state:n,redirect_uri:i},withCredentials:!1})}))()}getMiniProgramCode(e){var t=this;return x((function*(){return t.config.request(r.GET_MINIPROGRAM_QRCODE,{method:"POST",body:e})}))()}getMiniProgramQrCodeStatus(e){var t=this;return x((function*(){return t.config.request(r.GET_MINIPROGRAM_QRCODE_STATUS,{method:"POST",body:e})}))()}getUserBehaviorLog(e){var t=this;return x((function*(){var n="".concat(r.GET_USER_BEHAVIOR_LOG,"?").concat({LOGIN:"query[action]=USER_LOGIN",MODIFY:"ne_query[action]=USER_LOGIN"}[e.type],"&limit=").concat(e.limit).concat(e.page_token?"&page_token=".concat(e.page_token):"");return t.config.request(n,{method:"GET",withCredentials:!0})}))()}modifyPassword(e){var t=this;return x((function*(){var n="",i="",o=V(0,t.config.adapter);if(!o)throw new Error("do not support encrypt, a encrypt util required.");try{var a=yield t.getPublicKey();if(n=a.public_key,i=a.public_key_thumbprint,!n||!i)throw a}catch(e){throw e}var s=e.password?o.getEncryptInfo({publicKey:n,payload:e.password}):"",c=o.getEncryptInfo({publicKey:n,payload:e.new_password});return t.config.request(r.USER_BASIC_EDIT_URL,{method:"POST",withCredentials:!0,body:{user_id:e.user_id,encrypt_password:s,encrypt_new_password:c,public_key_thumbprint:i}})}))()}modifyPasswordWithoutLogin(e){var t=this;return x((function*(){var r="",i="",o=V(0,t.config.adapter);if(!o)throw new Error("do not support encrypt, a encrypt util required.");try{var a=yield t.getPublicKey();if(r=a.public_key,i=a.public_key_thumbprint,!r||!i)throw a}catch(e){throw e}var s=e.password?o.getEncryptInfo({publicKey:r,payload:e.password}):"",c=o.getEncryptInfo({publicKey:r,payload:e.new_password});return t.config.request(n.AUTH_RESET_PASSWORD,{method:"POST",body:{username:e.username,password:s,new_password:c,public_key_thumbprint:i}})}))()}}function W(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function B(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?W(Object(r),!0).forEach((function(t){F(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):W(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function F(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}class M{constructor(e){var{apiOrigin:t,clientId:r,env:n,storage:i,request:o,baseRequest:a,anonymousSignInFunc:s,wxCloud:c,adapter:u,onCredentialsError:l}=e;this.oauth2client=new C({apiOrigin:t,clientId:r,env:n,storage:i,baseRequest:a||o,anonymousSignInFunc:s,wxCloud:c,onCredentialsError:l}),this.authApi=new G(B(B({credentialsClient:this.oauth2client},e),{},{request:o?this.oauth2client.request.bind(this.oauth2client):void 0,adapter:u}))}}return t})());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudbase/oauth",
|
|
3
|
-
"version": "2.19.
|
|
3
|
+
"version": "2.19.2",
|
|
4
4
|
"description": "cloudbase javascript sdk auth componets",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -20,12 +20,12 @@
|
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@babel/preset-env": "^7.25.3",
|
|
23
|
-
"@cloudbase/types": "2.19.
|
|
23
|
+
"@cloudbase/types": "2.19.2",
|
|
24
24
|
"@types/node": "^22.5.4",
|
|
25
25
|
"terser-webpack-plugin": "^3.0.2",
|
|
26
26
|
"ts-loader": "^9.5.1",
|
|
27
27
|
"webpack-bundle-analyzer": "^4.9.1"
|
|
28
28
|
},
|
|
29
29
|
"license": "Apache-2.0",
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "9fcfb7f266cc039eb434045ee4f07c316e0aaf23"
|
|
31
31
|
}
|
package/src/auth/apis.ts
CHANGED
|
@@ -101,6 +101,7 @@ export interface AuthOptions {
|
|
|
101
101
|
env?: string
|
|
102
102
|
wxCloud?: any
|
|
103
103
|
adapter?: SDKAdapterInterface
|
|
104
|
+
onCredentialsError?: (data: { msg: string, eventType?: 'credentials_error' }) => void
|
|
104
105
|
}
|
|
105
106
|
|
|
106
107
|
/**
|
|
@@ -136,6 +137,7 @@ export class Auth {
|
|
|
136
137
|
baseRequest: opts.baseRequest /* || opts.request */, // opts.request 废弃不用来表示 oauth rquest
|
|
137
138
|
anonymousSignInFunc: opts.anonymousSignInFunc,
|
|
138
139
|
wxCloud: opts.wxCloud,
|
|
140
|
+
onCredentialsError: opts.onCredentialsError,
|
|
139
141
|
}
|
|
140
142
|
oAuth2Client = new OAuth2Client(initOptions)
|
|
141
143
|
}
|
package/src/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ export class CloudbaseOAuth {
|
|
|
11
11
|
public authApi: Auth
|
|
12
12
|
|
|
13
13
|
constructor(authOptions: AuthOptions) {
|
|
14
|
-
const { apiOrigin, clientId, env, storage, request, baseRequest, anonymousSignInFunc, wxCloud, adapter } = authOptions
|
|
14
|
+
const { apiOrigin, clientId, env, storage, request, baseRequest, anonymousSignInFunc, wxCloud, adapter, onCredentialsError } = authOptions
|
|
15
15
|
this.oauth2client = new OAuth2Client({
|
|
16
16
|
apiOrigin,
|
|
17
17
|
clientId,
|
|
@@ -20,6 +20,7 @@ export class CloudbaseOAuth {
|
|
|
20
20
|
baseRequest: baseRequest || request,
|
|
21
21
|
anonymousSignInFunc,
|
|
22
22
|
wxCloud,
|
|
23
|
+
onCredentialsError,
|
|
23
24
|
})
|
|
24
25
|
|
|
25
26
|
this.authApi = new Auth({
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SimpleStorage } from './interface'
|
|
2
2
|
import { ErrorType } from './consts'
|
|
3
|
+
import { AuthOptions } from '../auth/apis'
|
|
3
4
|
|
|
4
5
|
/** Credentials **/
|
|
5
6
|
export interface Credentials {
|
|
@@ -66,4 +67,5 @@ export interface OAuth2ClientOptions {
|
|
|
66
67
|
// hook
|
|
67
68
|
anonymousSignInFunc?: (Credentials) => Promise<Credentials | void>
|
|
68
69
|
wxCloud?: any;
|
|
70
|
+
onCredentialsError?: AuthOptions['onCredentialsError']
|
|
69
71
|
}
|
|
@@ -18,6 +18,7 @@ import { getPathName } from '../utils/index'
|
|
|
18
18
|
import { SinglePromise } from '../utils/function/single-promise'
|
|
19
19
|
import { weBtoa } from '../utils/base64'
|
|
20
20
|
import { isMatch } from '../utils/cloudbase-adapter-wx_mp'
|
|
21
|
+
import { AuthOptions } from '../auth/apis'
|
|
21
22
|
|
|
22
23
|
const RequestIdHeaderName = 'x-request-id'
|
|
23
24
|
const DeviceIdHeaderName = 'x-device-id'
|
|
@@ -306,6 +307,7 @@ export class OAuth2Client implements AuthClient {
|
|
|
306
307
|
private anonymousSignInFunc: (Credentials) => Promise<Credentials | void>
|
|
307
308
|
private wxCloud: any
|
|
308
309
|
private basicAuth: string
|
|
310
|
+
private onCredentialsError: AuthOptions['onCredentialsError'] | undefined
|
|
309
311
|
|
|
310
312
|
/**
|
|
311
313
|
* constructor
|
|
@@ -354,6 +356,7 @@ export class OAuth2Client implements AuthClient {
|
|
|
354
356
|
}
|
|
355
357
|
this.refreshTokenFunc = options.refreshTokenFunc || this.defaultRefreshTokenFunc
|
|
356
358
|
this.anonymousSignInFunc = options.anonymousSignInFunc
|
|
359
|
+
this.onCredentialsError = options.onCredentialsError
|
|
357
360
|
}
|
|
358
361
|
|
|
359
362
|
/**
|
|
@@ -499,7 +502,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
499
502
|
public async getCredentials(): Promise<Credentials | null> {
|
|
500
503
|
let credentials: Credentials = await this.localCredentials.getCredentials()
|
|
501
504
|
if (!credentials) {
|
|
502
|
-
|
|
505
|
+
const msg = 'credentials not found'
|
|
506
|
+
this.onCredentialsError?.({ msg })
|
|
507
|
+
return this.unAuthenticatedError(msg)
|
|
503
508
|
}
|
|
504
509
|
if (isCredentialsExpired(credentials)) {
|
|
505
510
|
if (credentials.refresh_token) {
|
|
@@ -509,13 +514,16 @@ export class OAuth2Client implements AuthClient {
|
|
|
509
514
|
if (credentials.scope === 'anonymous') {
|
|
510
515
|
credentials = await this.anonymousLogin(credentials)
|
|
511
516
|
} else {
|
|
517
|
+
this.onCredentialsError?.({ msg: error.error_description })
|
|
512
518
|
return Promise.reject(error)
|
|
513
519
|
}
|
|
514
520
|
}
|
|
515
521
|
} else if (credentials.scope === 'anonymous') {
|
|
516
522
|
credentials = await this.anonymousLogin(credentials)
|
|
517
523
|
} else {
|
|
518
|
-
|
|
524
|
+
const msg = 'no refresh token found in credentials'
|
|
525
|
+
this.onCredentialsError?.({ msg })
|
|
526
|
+
return this.unAuthenticatedError(msg)
|
|
519
527
|
}
|
|
520
528
|
}
|
|
521
529
|
return credentials
|
|
@@ -536,7 +544,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
536
544
|
public async getScope(): Promise<string> {
|
|
537
545
|
const credentials: Credentials = await this.localCredentials.getCredentials()
|
|
538
546
|
if (!credentials) {
|
|
539
|
-
|
|
547
|
+
const msg = 'credentials not found'
|
|
548
|
+
this.onCredentialsError?.({ msg })
|
|
549
|
+
return this.unAuthenticatedError(msg)
|
|
540
550
|
}
|
|
541
551
|
return credentials.scope
|
|
542
552
|
}
|
|
@@ -544,7 +554,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
544
554
|
public async getGroups(): Promise<string[]> {
|
|
545
555
|
const credentials: Credentials = await this.localCredentials.getCredentials()
|
|
546
556
|
if (!credentials) {
|
|
547
|
-
|
|
557
|
+
const msg = 'credentials not found'
|
|
558
|
+
this.onCredentialsError?.({ msg })
|
|
559
|
+
return this.unAuthenticatedError(msg)
|
|
548
560
|
}
|
|
549
561
|
return credentials.groups
|
|
550
562
|
}
|
|
@@ -557,7 +569,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
557
569
|
public async refreshToken(credentials: Credentials): Promise<Credentials> {
|
|
558
570
|
return this.singlePromise.run('_refreshToken', async () => {
|
|
559
571
|
if (!credentials || !credentials.refresh_token) {
|
|
560
|
-
|
|
572
|
+
const msg = 'no refresh token found in credentials'
|
|
573
|
+
this.onCredentialsError?.({ msg })
|
|
574
|
+
return this.unAuthenticatedError(msg)
|
|
561
575
|
}
|
|
562
576
|
try {
|
|
563
577
|
const newCredentials: Credentials = await this.refreshTokenFunc(credentials.refresh_token, credentials)
|
|
@@ -566,8 +580,11 @@ export class OAuth2Client implements AuthClient {
|
|
|
566
580
|
} catch (error) {
|
|
567
581
|
if (error.error === ErrorType.INVALID_GRANT) {
|
|
568
582
|
await this.localCredentials.setCredentials(null)
|
|
569
|
-
|
|
583
|
+
const msg = error.error_description
|
|
584
|
+
this.onCredentialsError?.({ msg })
|
|
585
|
+
return this.unAuthenticatedError(msg)
|
|
570
586
|
}
|
|
587
|
+
this.onCredentialsError?.({ msg: error.error_description })
|
|
571
588
|
return Promise.reject(error)
|
|
572
589
|
}
|
|
573
590
|
})
|
|
@@ -666,7 +683,9 @@ export class OAuth2Client implements AuthClient {
|
|
|
666
683
|
*/
|
|
667
684
|
private async defaultRefreshTokenFunc(refreshToken?: string, credentials?: Credentials): Promise<Credentials> {
|
|
668
685
|
if (refreshToken === undefined || refreshToken === '') {
|
|
669
|
-
|
|
686
|
+
const msg = 'refresh token not found'
|
|
687
|
+
this.onCredentialsError?.({ msg })
|
|
688
|
+
return this.unAuthenticatedError(msg)
|
|
670
689
|
}
|
|
671
690
|
|
|
672
691
|
let url: string = ApiUrls.AUTH_TOKEN_URL
|