// All material copyright ESRI, All Rights Reserved, unless otherwise specified. // See https://js.arcgis.com/4.7/esri/copyright.txt for details. //>>built define("../core/declare dojo/_base/config dojo/_base/lang dojo/_base/array dojo/Deferred dojo/_base/url dojo/sniff dojo/cookie dojo/io-query dojo/regexp ../kernel ../config ../core/lang ./ServerInfo ../core/urlUtils ../core/deferredUtils ../core/Accessor ../request ../core/Error ../core/Evented ./OAuthCredential ./OAuthInfo".split(" "),function(z,N,q,h,O,w,G,C,P,Q,p,D,v,E,r,H,R,y,t,I,J,K){var A={},L=function(a){var b=(new w(a.owningSystemUrl)).host;a=(new w(a.server)).host;var c=/.+\.arcgis\.com$/i; return c.test(b)&&c.test(a)},F=function(a,b){return!!(L(a)&&b&&h.some(b,function(b){return b.test(a.server)}))},u;z=z(I,{declaredClass:"esri.identity.IdentityManagerBase",constructor:function(){this._portalConfig=q.getObject("esriGeowConfig");this.serverInfos=[];this.oAuthInfos=[];this.credentials=[];this._soReqs=[];this._xoReqs=[];this._portals=[];this._getOAuthHash();window.addEventListener("pageshow",function(a){this._pageShowHandler(a)}.bind(this))},defaultOAuthInfo:null,defaultTokenValidity:60, tokenValidity:null,signInPage:null,useSignInPage:!0,normalizeWebTierAuth:!1,_busy:null,_rejectOnPersistedPageShow:!1,_oAuthHash:null,_gwTokenUrl:"/sharing/generateToken",_agsRest:"/rest/services",_agsPortal:/\/sharing(\/|$)/i,_agsAdmin:/https?:\/\/[^\/]+\/[^\/]+\/admin\/?(\/.*)?$/i,_adminSvcs:/\/admin\/services(\/|$)/i,_agolSuffix:".arcgis.com",_gwDomains:[{regex:/https?:\/\/www\.arcgis\.com/i,tokenServiceUrl:"https://www.arcgis.com/sharing/generateToken"},{regex:/https?:\/\/dev\.arcgis\.com/i,tokenServiceUrl:"https://dev.arcgis.com/sharing/generateToken"}, {regex:/https?:\/\/.*dev[^.]*\.arcgis\.com/i,tokenServiceUrl:"https://devext.arcgis.com/sharing/generateToken"},{regex:/https?:\/\/.*qa[^.]*\.arcgis\.com/i,tokenServiceUrl:"https://qaext.arcgis.com/sharing/generateToken"},{regex:/https?:\/\/.*.arcgis\.com/i,tokenServiceUrl:"https://www.arcgis.com/sharing/generateToken"}],_legacyFed:[],_regexSDirUrl:/http.+\/rest\/services\/?/ig,_regexServerType:/(\/(MapServer|GeocodeServer|GPServer|GeometryServer|ImageServer|NAServer|FeatureServer|GeoDataServer|GlobeServer|MobileServer|GeoenrichmentServer|VectorTileServer|SceneServer)).*/ig, _gwUser:/http.+\/users\/([^\/]+)\/?.*/i,_gwItem:/http.+\/items\/([^\/]+)\/?.*/i,_gwGroup:/http.+\/groups\/([^\/]+)\/?.*/i,_errorCodes:[499,498,403,401],_rePortalTokenSvc:/\/sharing(\/rest)?\/generatetoken/i,_publicUrls:[/\/arcgis\/tokens/i,/\/sharing(\/rest)?\/generatetoken/i,/\/rest\/info/i],_createDefaultOAuthInfo:!0,_hasTestedIfAppIsOnPortal:!1,registerServers:function(a){var b=this.serverInfos;b?(a=h.filter(a,function(a){return!this.findServerInfo(a.server)},this),this.serverInfos=b.concat(a)): this.serverInfos=a;h.forEach(a,function(a){a.owningSystemUrl&&this._portals.push(a.owningSystemUrl);if(a.hasPortal){this._portals.push(a.server);var b=D.request.corsEnabledServers,c=this._getOrigin(a.tokenServiceUrl);r.canUseXhr(a.server)||b.push(a.server.replace(/^https?:\/\//i,""));r.canUseXhr(c)||b.push(c.replace(/^https?:\/\//i,""))}},this)},registerOAuthInfos:function(a){var b=this.oAuthInfos;b?(a=h.filter(a,function(a){return!this.findOAuthInfo(a.portalUrl)},this),this.oAuthInfos=b.concat(a)): this.oAuthInfos=a},registerToken:function(a){a=q.mixin({},a);var b=this._sanitizeUrl(a.server),c=this.findServerInfo(b),d=this._isServerRsrc(b),e=!0,k;c||(c=new E,c.server=this._getServerInstanceRoot(b),d?c.hasServer=!0:(c.tokenServiceUrl=this._getTokenSvcUrl(b),c.hasPortal=!0),this.registerServers([c]));(k=this._findCredential(b))?(delete a.server,q.mixin(k,a),e=!1):(k=new u({userId:a.userId,server:c.server,token:a.token,expires:a.expires,ssl:a.ssl,scope:d?"server":"portal"}),k.resources=[b],this.credentials.push(k)); k.emitTokenChange(!1);e||k.refreshServerTokens()},toJSON:function(){return v.fixJson({serverInfos:h.map(this.serverInfos,function(a){return a.toJSON()}),oAuthInfos:h.map(this.oAuthInfos,function(a){return a.toJSON()}),credentials:h.map(this.credentials,function(a){return a.toJSON()})})},initialize:function(a){if(a){q.isString(a)&&(a=JSON.parse(a));var b=a.serverInfos,c=a.oAuthInfos;a=a.credentials;if(b){var d=[];h.forEach(b,function(a){a.server&&a.tokenServiceUrl&&d.push(a.declaredClass?a:new E(a))}); d.length&&this.registerServers(d)}if(c){var e=[];h.forEach(c,function(a){a.appId&&e.push(a.declaredClass?a:new K(a))});e.length&&this.registerOAuthInfos(e)}a&&h.forEach(a,function(a){a.server&&a.token&&a.expires&&a.expires>(new Date).getTime()&&(a=a.declaredClass?a:new u(a),a.emitTokenChange(),this.credentials.push(a))},this)}},findServerInfo:function(a){var b;a=this._sanitizeUrl(a);h.some(this.serverInfos,function(c){this._hasSameServerInstance(c.server,a)&&(b=c);return!!b},this);return b},findOAuthInfo:function(a){var b; a=this._sanitizeUrl(a);h.some(this.oAuthInfos,function(c){this._hasSameServerInstance(c.portalUrl,a)&&(b=c);return!!b},this);return b},findCredential:function(a,b){var c,d;a=this._sanitizeUrl(a);d=this._isServerRsrc(a)?"server":"portal";b?h.some(this.credentials,function(e){this._hasSameServerInstance(e.server,a)&&b===e.userId&&e.scope===d&&(c=e);return!!c},this):h.some(this.credentials,function(b){this._hasSameServerInstance(b.server,a)&&-1!==this._getIdenticalSvcIdx(a,b)&&b.scope===d&&(c=b);return!!c}, this);return c},getCredential:function(a,b){var c,d,e=!0;v.isDefined(b)&&(q.isObject(b)?(c=!!b.token,d=b.error,e=!1!==b.prompt):c=b);a=this._sanitizeUrl(a);var k=H.makeDeferredCancellingPending(),f=this._isAdminResource(a),l=c&&this._doPortalSignIn(a)?this._getEsriAuthCookie():null;if((c=c?this.findCredential(a):null)&&d&&498===d.code)c.destroy(),l&&l.token===b.token&&C("esri_auth",null,{expires:-1,path:"/",domain:document.domain});else if(l||c)return a=new t("identity-manager:not-authorized","You are currently signed in as: '"+ (l&&l.email||c&&c.userId)+"'. You do not have access to this resource: "+a,{error:d}),k.reject(a),k.promise;if(d=this._findCredential(a,b))return k.resolve(d),k.promise;d=this.findServerInfo(a);if(d)!d.hasServer&&this._isServerRsrc(a)&&(d._restInfoPms=this._getTokenSvcUrl(a),d.hasServer=!0);else{l=this._getTokenSvcUrl(a);if(!l)return a=new t("identity-manager:unknown-resource","Unknown resource - could not find token service endpoint."),k.reject(a),k.promise;d=new E;d.server=this._getServerInstanceRoot(a); q.isString(l)?(d.tokenServiceUrl=l,d.hasPortal=!0):(d._restInfoPms=l,d.hasServer=!0);this.registerServers([d])}e&&d.hasPortal&&void 0===d._selfReq&&!this._findOAuthInfo(a)&&(d._selfReq={owningTenant:b&&b.owningTenant,selfDfd:this._getPortalSelf(d.tokenServiceUrl.replace(this._rePortalTokenSvc,"/sharing/rest/portals/self"),a)});return this._enqueue(a,d,b,k,f)},getResourceName:function(a){return this._isRESTService(a)?a.replace(this._regexSDirUrl,"").replace(this._regexServerType,"")||"":this._gwUser.test(a)&& a.replace(this._gwUser,"$1")||this._gwItem.test(a)&&a.replace(this._gwItem,"$1")||this._gwGroup.test(a)&&a.replace(this._gwGroup,"$1")||""},generateToken:function(a,b,c){var d,e,k,f,l,h,g=this._rePortalTokenSvc.test(a.tokenServiceUrl),x=new w(window.location.href.toLowerCase()),M=this._getEsriAuthCookie(),B,m=!b;f=a.shortLivedTokenValidity;var n;b&&(n=p.id.tokenValidity||f||p.id.defaultTokenValidity,n>f&&0f.expires?(g=e,f.destroy()):(g=f,e.destroy()):g=e.isValid()?e:f,h._oAuthCred= g),g.isValid()?c(new u({userId:g.userId,server:m.server,token:g.token,expires:g.expires,ssl:g.ssl,_oAuthCred:g})):b._oAuthHash&&b._oAuthHash.state.portalUrl===h.portalUrl?(n=b._oAuthHash,m=new u({userId:n.username,server:m.server,token:n.access_token,expires:(new Date).getTime()+1E3*Number(n.expires_in),ssl:"true"===n.ssl,oAuthState:n.state,_oAuthCred:g}),g.storage=n.persist?window.localStorage:window.sessionStorage,g.token=m.token,g.expires=m.expires,g.userId=m.userId,g.ssl=m.ssl,g.save(),b._oAuthHash= null,c(m)):n?a._pendingDfd=b.oAuthSignIn(a.resUrl_,m,h,a.options_).then(c,d):(m=new t("identity-manager:not-authenticated","User is not signed in."),d(m))):n?b._checkProtocol(a.resUrl_,m,d,a.admin_)&&(n=a.options_,a.admin_&&(n=n||{},n.isAdmin=!0),a._pendingDfd=b.signIn(a.resUrl_,m,n).then(c,d)):(m=new t("identity-manager:not-authenticated","User is not signed in."),d(m))},k=function(){var g=a.sinfo_,e=g.owningSystemUrl,f=a.options_,k,m,n,l;f&&(k=f.token,m=f.error,n=f.prompt);(l=b._findCredential(e, {token:k,resource:a.resUrl_}))||h.some(b.credentials,function(a){this._isIdProvider(e,a.server)&&(l=a);return!!l},b);l?(f=b.findCredential(a.resUrl_,l.userId))?c(f):F(g,b._legacyFed)?(f=l.toJSON(),f.server=g.server,f.resources=null,c(new u(f))):(a._pendingDfd=b.generateToken(b.findServerInfo(l.server),null,{serverUrl:a.resUrl_,token:l.token,ssl:l.ssl})).then(function(b){c(new u({userId:l.userId,server:g.server,token:b.token,expires:v.isDefined(b.expires)?Number(b.expires):null,ssl:!!b.ssl,isAdmin:a.admin_, validity:b.validity}))},d):(b._busy=null,k&&(a.options_.token=null),(a._pendingDfd=b.getCredential(e.replace(/\/?$/,"/sharing"),{resource:a.resUrl_,owningTenant:g.owningTenant,token:k,error:m,prompt:n})).then(function(c){b._enqueue(a.resUrl_,a.sinfo_,a.options_,a,a.admin_)},function(a){d(a)}))};this._errbackFunc=d;var f=a.sinfo_.owningSystemUrl,l=this._isServerRsrc(a.resUrl_),p=a.sinfo_._restInfoPms;p?p.promise.then(function(c){var d=a.sinfo_;d.adminTokenServiceUrl=d._restInfoPms.adminUrl;d._restInfoPms= null;d.tokenServiceUrl=q.getObject("authInfo.tokenServicesUrl",!1,c)||q.getObject("authInfo.tokenServiceUrl",!1,c)||q.getObject("tokenServiceUrl",!1,c);d.shortLivedTokenValidity=q.getObject("authInfo.shortLivedTokenValidity",!1,c);d.currentVersion=c.currentVersion;d.owningTenant=c.owningTenant;(c=d.owningSystemUrl=c.owningSystemUrl)&&b._portals.push(c);l&&c?k():e()},function(){a.sinfo_._restInfoPms=null;var b=new t("identity-manager:server-identification-failed","Unknown resource - could not find token service endpoint."); d(b)}):l&&f?k():a.sinfo_._selfReq?a.sinfo_._selfReq.selfDfd.then(function(c){var d={},e,g,f,h;c&&(e=c.user&&c.user.username,d.username=e,d.allSSL=c.allSSL,g=c.supportsOAuth,f=c.currentVersion,"multitenant"===c.portalMode&&(h=c.customBaseUrl));a.sinfo_.webTierAuth=!!e;return e&&b.normalizeWebTierAuth?b.generateToken(a.sinfo_,null,{ssl:d.allSSL}).always(function(a){d.portalToken=a&&a.token;d.tokenExpiration=a&&a.expires;return d}):!e&&g&&4.4<=parseFloat(f)&&!b._doPortalSignIn(a.resUrl_)?b._generateOAuthInfo({portalUrl:a.sinfo_.server, customBaseUrl:h,owningTenant:a.sinfo_._selfReq.owningTenant}).always(function(){return d}):d}).always(function(b){a.sinfo_._selfReq=null;b?e(b.username,b.allSSL,b.portalToken,b.tokenExpiration):e()}):e()},_generateOAuthInfo:function(a){var b=this,c,d=a.portalUrl,e=a.customBaseUrl,h=a.owningTenant;if(a=!this.defaultOAuthInfo&&this._createDefaultOAuthInfo&&!this._hasTestedIfAppIsOnPortal){c=window.location.href;var f=c.indexOf("?");-1b&&(b=0);this._refreshTimer=setTimeout(q.hitch(this,this.refreshToken),b>a?b-a:b)}});z.Credential=u;return z});