"use strict";(self.webpackChunkpasswords_webextension=self.webpackChunkpasswords_webextension||[]).push([[4150],{8962:(e,t,a)=>{a.d(t,{Z:()=>r});class r{async execute(e,t){console.trace("controller.execute() not implemented")}}},4695:(e,t,a)=>{a.r(t),a.d(t,{default:()=>o});var r=a(1842),s=a(7851),i=a(6567),n=a(8962),l=a(6420);class o extends n.Z{async execute(e,t){let a=e.getPayload(),r=new l.Z,n=await r.validate(a);if(!n.ok)return void t.setType("validation.error").setPayload(n);let o=n.server;await this._createServer(o,n)?(s.Z.addServer(o).catch(i.Z.catchEvt),t.setType("server.item").setPayload(o)):t.setType("validation.error").setPayload(n)}async _createServer(e,t){try{return e.setEnabled(!0).setTimeout(0),await r.Z.create(e),!0}catch(e){return i.Z.logError(e),t.ok=!1,t.message=e.message,!1}}}},6420:(e,t,a)=>{a.d(t,{Z:()=>c});var r=a(6306),s=a(6567),i=a(1515),n=a(9665),l=a(1842),o=a(5589),d=a(2492);class c{async validate(e){let t=this._checkFormData(e);if(!t.ok)return t;let a=this._createModel(e,t);return a&&await this._duplicateCheck(a,t)&&await this._checkConnection(a,t)&&await this._checkRequirements(a,t)&&(t.server=a),t}_checkFormData(e){let t={ok:!0,errors:{}};return this._validateLabel(e,t.errors)||(t.ok=!1),this._validateUser(e,t.errors)||(t.ok=!1),this._validateBaseUrl(e,t.errors)||(t.ok=!1),this._validateToken(e,t.errors)||(t.ok=!1),this._validateTimeout(e,t.errors)||(t.ok=!1),t.ok||(t.message=r.Z.translate("ValidationFailed")),t}_validateLabel(e,t){return this._validateEmpty("label",e,t)&&this._validateLength("label",1,48,e,t)}_validateUser(e,t){return this._validateEmpty("user",e,t)&&this._validateLength("user",1,48,e,t)}_validateBaseUrl(e,t){if(!this._validateEmpty("baseUrl",e,t)||!this._validateLength("baseUrl",12,1024,e,t))return!1;let a=e.baseUrl.indexOf("index.php");if(-1!==a)e.baseUrl=e.baseUrl.substr(0,a);else{let t=e.baseUrl.indexOf("apps/passwords");-1!==t&&(e.baseUrl=e.baseUrl.substr(0,t))}return"/"!==e.baseUrl.substr(-1)&&(e.baseUrl+="/"),!/^https:\/\/[\w.\/\-]+$/.test(t.baseUrl)||(t.baseUrl=r.Z.translate("ValidationNotAnUrl"),!1)}_validateToken(e,t){return!(!this._validateEmpty("token",e,t)||!this._validateLength("token",29,29,e,t))&&(!!/^([A-Za-z0-9]{5}-?){5}$/.test(e.token)||(t.token=r.Z.translate("ValidationNotAToken"),!1))}_validateTimeout(e,t){return e.hasOwnProperty("timeout")||(e.timeout=0),"string"==typeof e.timeout&&(e.timeout=parseInt(e.timeout)),-1!==[0,3e5,6e5,9e5,18e5,36e5].indexOf(e.timeout)||(t.timeout=r.Z.translate("ValidationInvalidTimeout"),!1)}_validateEmpty(e,t,a){if(!t.hasOwnProperty(e)||!t[e]||"string"!=typeof t[e])return a[e]=this._createErrorMessage(e,"ValidationNotEmpty"),!1;let r=t[e].toString().trim();return 0===r.length?(a[e]=this._createErrorMessage(e,"ValidationNotEmpty"),!1):(t[e]=r,!0)}_validateLength(e,t,a,r,s){return r[e].length<t?(s[e]=this._createErrorMessage(e,"ValidationMinLength",[a]),!1):!(r[e].length>a&&(s[e]=this._createErrorMessage(e,"ValidationMaxLength",[a]),1))}_createModel(e,t){try{return new o.Z(e)}catch(e){return s.Z.logError(e),t.ok=!1,t.message=e.message,!1}}async _checkConnection(e,t){try{let t=new i.V6(e,{}).getSessionAuthorization();return await t.load(),!0}catch(a){return s.Z.logError(a),t.ok=!1,"Failed to fetch"===a.message||"NetworkError"===a.message.substr(0,12)?t.message=r.Z.translate("ValidationNoConnection",e.getBaseUrl()):a instanceof n.yj?t.message=r.Z.translate("ValidationUnauthorizedError",e.getBaseUrl()):a instanceof n.oo?t.message=r.Z.translate("ValidationHttpError",e.getBaseUrl(),a.message):t.message=r.Z.translate("ValidationConnectionError",e.getBaseUrl(),a.message),!1}}async _checkRequirements(e,t){let a=new i.V6(e,{}),s=new d.Z(a);return!!await s.check()||(t.ok=!1,t.message=r.Z.translate("ValidationServerVersion"),!1)}async _duplicateCheck(e,t){let a=await l.Z.findAll();for(let s of a)if(e.getId()!==s.getId()&&e.getBaseUrl()===s.getBaseUrl()&&e.getUser()===s.getUser())return t.message=r.Z.translate("ValidationDuplicate"),t.ok=!1,!1;return!0}_createErrorMessage(e,t,a=[]){let s="Validation"+e[0].toUpperCase()+e.substr(1),i=r.Z.translate(s);return a.unshift(i),r.Z.translate(t,a)}}}}]);