UNPKG

@web3auth/auth-adapter

Version:
414 lines (386 loc) 19.5 kB
/******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ var __webpack_modules__ = ({ /***/ 439: /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { // EXPORTS __webpack_require__.d(__webpack_exports__, { M: () => (/* binding */ AuthAdapter) }); ;// external "@babel/runtime/helpers/objectSpread2" const objectSpread2_namespaceObject = require("@babel/runtime/helpers/objectSpread2"); var objectSpread2_default = /*#__PURE__*/__webpack_require__.n(objectSpread2_namespaceObject); ;// external "@babel/runtime/helpers/defineProperty" const defineProperty_namespaceObject = require("@babel/runtime/helpers/defineProperty"); var defineProperty_default = /*#__PURE__*/__webpack_require__.n(defineProperty_namespaceObject); // EXTERNAL MODULE: external "@web3auth/auth" var auth_ = __webpack_require__(894); ;// external "@web3auth/base" const base_namespaceObject = require("@web3auth/base"); ;// external "deepmerge" const external_deepmerge_namespaceObject = require("deepmerge"); var external_deepmerge_default = /*#__PURE__*/__webpack_require__.n(external_deepmerge_namespaceObject); // EXTERNAL MODULE: ./src/config.ts var config = __webpack_require__(998); ;// ./src/authAdapter.ts class AuthAdapter extends base_namespaceObject.BaseAdapter { constructor(params = {}) { super(params); defineProperty_default()(this, "name", base_namespaceObject.WALLET_ADAPTERS.AUTH); defineProperty_default()(this, "adapterNamespace", base_namespaceObject.ADAPTER_NAMESPACES.MULTICHAIN); defineProperty_default()(this, "type", base_namespaceObject.ADAPTER_CATEGORY.IN_APP); defineProperty_default()(this, "authInstance", null); defineProperty_default()(this, "status", base_namespaceObject.ADAPTER_STATUS.NOT_READY); defineProperty_default()(this, "currentChainNamespace", base_namespaceObject.CHAIN_NAMESPACES.EIP155); defineProperty_default()(this, "privateKeyProvider", null); defineProperty_default()(this, "authOptions", void 0); defineProperty_default()(this, "loginSettings", { loginProvider: "" }); this.setAdapterSettings(objectSpread2_default()(objectSpread2_default()({}, params.adapterSettings), {}, { chainConfig: params.chainConfig, clientId: params.clientId || "", sessionTime: params.sessionTime, web3AuthNetwork: params.web3AuthNetwork, useCoreKitKey: params.useCoreKitKey, privateKeyProvider: params.privateKeyProvider })); this.loginSettings = params.loginSettings || { loginProvider: "" }; this.privateKeyProvider = params.privateKeyProvider || null; } get chainConfigProxy() { return this.chainConfig ? objectSpread2_default()({}, this.chainConfig) : null; } get provider() { if (this.status !== base_namespaceObject.ADAPTER_STATUS.NOT_READY && this.privateKeyProvider) { return this.privateKeyProvider; } return null; } set provider(_) { throw new Error("Not implemented"); } async init(options) { super.checkInitializationRequirements(); if (!this.clientId) throw base_namespaceObject.WalletInitializationError.invalidParams("clientId is required before auth's initialization"); if (!this.authOptions) throw base_namespaceObject.WalletInitializationError.invalidParams("authOptions is required before auth's initialization"); const isRedirectResult = this.authOptions.uxMode === auth_.UX_MODE.REDIRECT; this.authOptions = objectSpread2_default()(objectSpread2_default()({}, this.authOptions), {}, { replaceUrlOnRedirect: isRedirectResult, useCoreKitKey: this.useCoreKitKey }); this.authInstance = new auth_.Auth(objectSpread2_default()(objectSpread2_default()({}, this.authOptions), {}, { clientId: this.clientId, network: this.authOptions.network || this.web3AuthNetwork || auth_.WEB3AUTH_NETWORK.SAPPHIRE_MAINNET })); base_namespaceObject.log.debug("initializing auth adapter init"); await this.authInstance.init(); if (!this.chainConfig) throw base_namespaceObject.WalletInitializationError.invalidParams("chainConfig is required before initialization"); this.status = base_namespaceObject.ADAPTER_STATUS.READY; this.emit(base_namespaceObject.ADAPTER_EVENTS.READY, base_namespaceObject.WALLET_ADAPTERS.AUTH); try { base_namespaceObject.log.debug("initializing auth adapter"); const finalPrivKey = this._getFinalPrivKey(); // connect only if it is redirect result or if connect (adapter is cached/already connected in same session) is true if (finalPrivKey && (options.autoConnect || isRedirectResult)) { this.rehydrated = true; await this.connect(); } } catch (error) { base_namespaceObject.log.error("Failed to connect with cached auth provider", error); this.emit(base_namespaceObject.ADAPTER_EVENTS.ERRORED, error); } } async connect(params = { loginProvider: "" }) { super.checkConnectionRequirements(); this.status = base_namespaceObject.ADAPTER_STATUS.CONNECTING; this.emit(base_namespaceObject.ADAPTER_EVENTS.CONNECTING, objectSpread2_default()(objectSpread2_default()({}, params), {}, { adapter: base_namespaceObject.WALLET_ADAPTERS.AUTH })); try { await this.connectWithProvider(params); return this.provider; } catch (error) { base_namespaceObject.log.error("Failed to connect with auth provider", error); // ready again to be connected this.status = base_namespaceObject.ADAPTER_STATUS.READY; this.emit(base_namespaceObject.ADAPTER_EVENTS.ERRORED, error); if (error !== null && error !== void 0 && error.message.includes("user closed popup")) { throw base_namespaceObject.WalletLoginError.popupClosed(); } else if (error instanceof base_namespaceObject.Web3AuthError) { throw error; } throw base_namespaceObject.WalletLoginError.connectionError("Failed to login with auth", error); } } async enableMFA(params = { loginProvider: "" }) { if (this.status !== base_namespaceObject.ADAPTER_STATUS.CONNECTED) throw base_namespaceObject.WalletLoginError.notConnectedError("Not connected with wallet"); if (!this.authInstance) throw base_namespaceObject.WalletInitializationError.notReady("authInstance is not ready"); try { await this.authInstance.enableMFA(params); } catch (error) { base_namespaceObject.log.error("Failed to enable MFA with auth provider", error); if (error instanceof base_namespaceObject.Web3AuthError) { throw error; } throw base_namespaceObject.WalletLoginError.connectionError("Failed to enable MFA with auth", error); } } async manageMFA(params = { loginProvider: "" }) { if (this.status !== base_namespaceObject.ADAPTER_STATUS.CONNECTED) throw base_namespaceObject.WalletLoginError.notConnectedError("Not connected with wallet"); if (!this.authInstance) throw base_namespaceObject.WalletInitializationError.notReady("authInstance is not ready"); try { await this.authInstance.manageMFA(params); } catch (error) { base_namespaceObject.log.error("Failed to manage MFA with auth provider", error); if (error instanceof base_namespaceObject.Web3AuthError) { throw error; } throw base_namespaceObject.WalletLoginError.connectionError("Failed to manage MFA with auth", error); } } async disconnect(options = { cleanup: false }) { if (this.status !== base_namespaceObject.ADAPTER_STATUS.CONNECTED) throw base_namespaceObject.WalletLoginError.notConnectedError("Not connected with wallet"); if (!this.authInstance) throw base_namespaceObject.WalletInitializationError.notReady("authInstance is not ready"); await this.authInstance.logout(); if (options.cleanup) { this.status = base_namespaceObject.ADAPTER_STATUS.NOT_READY; this.authInstance = null; this.privateKeyProvider = null; } else { // ready to be connected again this.status = base_namespaceObject.ADAPTER_STATUS.READY; } this.rehydrated = false; this.emit(base_namespaceObject.ADAPTER_EVENTS.DISCONNECTED); } async authenticateUser() { if (this.status !== base_namespaceObject.ADAPTER_STATUS.CONNECTED) throw base_namespaceObject.WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first"); const userInfo = await this.getUserInfo(); return { idToken: userInfo.idToken }; } async getUserInfo() { if (this.status !== base_namespaceObject.ADAPTER_STATUS.CONNECTED) throw base_namespaceObject.WalletLoginError.notConnectedError("Not connected with wallet"); if (!this.authInstance) throw base_namespaceObject.WalletInitializationError.notReady("authInstance is not ready"); const userInfo = this.authInstance.getUserInfo(); return userInfo; } // should be called only before initialization. setAdapterSettings(adapterSettings) { super.setAdapterSettings(adapterSettings); const defaultOptions = (0,config/* getAuthDefaultOptions */.M)(); base_namespaceObject.log.info("setting adapter settings", adapterSettings); this.authOptions = external_deepmerge_default().all([defaultOptions.adapterSettings, this.authOptions || {}, adapterSettings || {}]); if (adapterSettings.web3AuthNetwork) { this.authOptions.network = adapterSettings.web3AuthNetwork; } if (adapterSettings.privateKeyProvider) { this.privateKeyProvider = adapterSettings.privateKeyProvider; } } async addChain(chainConfig, init = false) { var _this$privateKeyProvi; super.checkAddChainRequirements(chainConfig, init); (_this$privateKeyProvi = this.privateKeyProvider) === null || _this$privateKeyProvi === void 0 || _this$privateKeyProvi.addChain(chainConfig); this.addChainConfig(chainConfig); } async switchChain(params, init = false) { var _this$privateKeyProvi2; super.checkSwitchChainRequirements(params, init); await ((_this$privateKeyProvi2 = this.privateKeyProvider) === null || _this$privateKeyProvi2 === void 0 ? void 0 : _this$privateKeyProvi2.switchChain(params)); this.setAdapterSettings({ chainConfig: this.getChainConfig(params.chainId) }); } _getFinalPrivKey() { if (!this.authInstance) return ""; let finalPrivKey = this.authInstance.privKey; // coreKitKey is available only for custom verifiers by default if (this.useCoreKitKey) { // this is to check if the user has already logged in but coreKitKey is not available. // when useCoreKitKey is set to true. // This is to ensure that when there is no user session active, we don't throw an exception. if (this.authInstance.privKey && !this.authInstance.coreKitKey) { throw base_namespaceObject.WalletLoginError.coreKitKeyNotFound(); } finalPrivKey = this.authInstance.coreKitKey; } return finalPrivKey; } _getFinalEd25519PrivKey() { if (!this.authInstance) return ""; let finalPrivKey = this.authInstance.ed25519PrivKey; // coreKitKey is available only for custom verifiers by default if (this.useCoreKitKey) { // this is to check if the user has already logged in but coreKitKey is not available. // when useCoreKitKey is set to true. // This is to ensure that when there is no user session active, we don't throw an exception. if (this.authInstance.ed25519PrivKey && !this.authInstance.coreKitEd25519Key) { throw base_namespaceObject.WalletLoginError.coreKitKeyNotFound(); } finalPrivKey = this.authInstance.coreKitEd25519Key; } return finalPrivKey; } async connectWithProvider(params = { loginProvider: "" }) { var _params$extraLoginOpt; if (!this.privateKeyProvider) throw base_namespaceObject.WalletInitializationError.invalidParams("PrivateKey Provider is required before initialization"); if (!this.authInstance) throw base_namespaceObject.WalletInitializationError.notReady("authInstance is not ready"); const keyAvailable = this._getFinalPrivKey(); // if not logged in then login if (!keyAvailable || (_params$extraLoginOpt = params.extraLoginOptions) !== null && _params$extraLoginOpt !== void 0 && _params$extraLoginOpt.id_token) { var _params$extraLoginOpt2; // always use "other" curve to return token with all keys encoded so wallet service can switch between evm and solana namespace this.loginSettings.curve = auth_.SUPPORTED_KEY_CURVES.OTHER; if (!params.loginProvider && !this.loginSettings.loginProvider) throw base_namespaceObject.WalletInitializationError.invalidParams("loginProvider is required for login"); await this.authInstance.login(external_deepmerge_default().all([this.loginSettings, params, { extraLoginOptions: objectSpread2_default()(objectSpread2_default()({}, params.extraLoginOptions || {}), {}, { login_hint: params.login_hint || ((_params$extraLoginOpt2 = params.extraLoginOptions) === null || _params$extraLoginOpt2 === void 0 ? void 0 : _params$extraLoginOpt2.login_hint) }) }])); } let finalPrivKey = this._getFinalPrivKey(); if (finalPrivKey) { if (this.currentChainNamespace === base_namespaceObject.CHAIN_NAMESPACES.SOLANA) { finalPrivKey = this._getFinalEd25519PrivKey(); } await this.privateKeyProvider.setupProvider(finalPrivKey); this.status = base_namespaceObject.ADAPTER_STATUS.CONNECTED; this.emit(base_namespaceObject.ADAPTER_EVENTS.CONNECTED, { adapter: base_namespaceObject.WALLET_ADAPTERS.AUTH, reconnected: this.rehydrated, provider: this.provider }); } } } /***/ }), /***/ 998: /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ M: () => (/* binding */ getAuthDefaultOptions) /* harmony export */ }); /* harmony import */ var _web3auth_auth__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(894); /* harmony import */ var _web3auth_auth__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_web3auth_auth__WEBPACK_IMPORTED_MODULE_0__); const getAuthDefaultOptions = () => { return { adapterSettings: { network: _web3auth_auth__WEBPACK_IMPORTED_MODULE_0__.WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, clientId: "", uxMode: _web3auth_auth__WEBPACK_IMPORTED_MODULE_0__.UX_MODE.POPUP }, loginSettings: {}, privateKeyProvider: undefined }; }; /***/ }), /***/ 17: /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony import */ var _web3auth_auth__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(894); /* harmony import */ var _web3auth_auth__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_web3auth_auth__WEBPACK_IMPORTED_MODULE_0__); /* harmony reexport (unknown) */ var __WEBPACK_REEXPORT_OBJECT__ = {}; /* harmony reexport (unknown) */ for(const __WEBPACK_IMPORT_KEY__ in _web3auth_auth__WEBPACK_IMPORTED_MODULE_0__) if(__WEBPACK_IMPORT_KEY__ !== "default") __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = () => _web3auth_auth__WEBPACK_IMPORTED_MODULE_0__[__WEBPACK_IMPORT_KEY__] /* harmony reexport (unknown) */ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__); /***/ }), /***/ 894: /***/ ((module) => { module.exports = require("@web3auth/auth"); /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /* webpack/runtime/compat get default export */ /******/ (() => { /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = (module) => { /******/ var getter = module && module.__esModule ? /******/ () => (module['default']) : /******/ () => (module); /******/ __webpack_require__.d(getter, { a: getter }); /******/ return getter; /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ AuthAdapter: () => (/* reexport safe */ _authAdapter__WEBPACK_IMPORTED_MODULE_0__.M), /* harmony export */ getAuthDefaultOptions: () => (/* reexport safe */ _config__WEBPACK_IMPORTED_MODULE_1__.M) /* harmony export */ }); /* harmony import */ var _authAdapter__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(439); /* harmony import */ var _config__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(998); /* harmony import */ var _interface__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(17); /* harmony reexport (unknown) */ var __WEBPACK_REEXPORT_OBJECT__ = {}; /* harmony reexport (unknown) */ for(const __WEBPACK_IMPORT_KEY__ in _interface__WEBPACK_IMPORTED_MODULE_2__) if(["default","AuthAdapter","getAuthDefaultOptions"].indexOf(__WEBPACK_IMPORT_KEY__) < 0) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = () => _interface__WEBPACK_IMPORTED_MODULE_2__[__WEBPACK_IMPORT_KEY__] /* harmony reexport (unknown) */ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__); module.exports = __webpack_exports__; /******/ })() ;