/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */

window[esri._dojoScopeName||"dojo"]._xdResourceLoaded(function(_1,_2,_3){return {depends:[["provide","esri.IdentityManagerBase"],["require","dojo.cookie"],["require","esri.utils"]],defineResource:function(_4,_5,_6){if(!_4._hasResource["esri.IdentityManagerBase"]){_4._hasResource["esri.IdentityManagerBase"]=true;_4.provide("esri.IdentityManagerBase");_4.require("dojo.cookie");_4.require("esri.utils");(function(){var _7={};_4.declare("esri.IdentityManagerBase",null,{constructor:function(){this._portalConfig=_4.getObject("esriGeowConfig");var _8=_4.getObject("esriGeowConfig.restBaseUrl");if(_8){this._portalDomains.push((new _4._Url(_8)).authority);}},serverInfos:[],credentials:[],tokenValidity:60,signInPage:null,_busy:null,_soReqs:[],_xoReqs:[],_gwTokenUrl:"/sharing/generateToken",_agsRest:"/rest/services",_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"}],_portalDomains:["arcgis.com"],_regexSDirUrl:/http.+\/rest\/services\/?/ig,_regexServerType:/(\/(MapServer|GeocodeServer|GPServer|GeometryServer|ImageServer|NAServer|FeatureServer|GeoDataServer|GlobeServer|MobileServer)).*/ig,_gwUser:/http.+\/users\/([^\/]+)\/?.*/i,_gwItem:/http.+\/items\/([^\/]+)\/?.*/i,_gwGroup:/http.+\/groups\/([^\/]+)\/?.*/i,_errorCodes:[499,498,403,401],_publicUrls:[/\/arcgis\/tokens/i,/\/sharing\/generatetoken/i,/\/rest\/info/i],registerServers:function(_9){var _a=this.serverInfos;if(_a){_9=_4.filter(_9,function(_b){return !this.findServerInfo(_b.server);},this);this.serverInfos=_a.concat(_9);}else{this.serverInfos=_9;}},toJson:function(){return esri._sanitize({"serverInfos":_4.map(this.serverInfos,function(_c){return _c.toJson();}),"credentials":_4.map(this.credentials,function(_d){return _d.toJson();})});},initialize:function(_e){if(!_e){return;}if(_4.isString(_e)){_e=_4.fromJson(_e);}var _f=_e.serverInfos,_10=_e.credentials;if(_f){var _11=[];_4.forEach(_f,function(_12){if(_12.server&&_12.tokenServiceUrl){_11.push(_12.declaredClass?_12:new esri.ServerInfo(_12));}});if(_11.length){this.registerServers(_11);}}if(_10){_4.forEach(_10,function(crd){if(crd.userId&&crd.server&&crd.token&&crd.expires&&(crd.expires>(new Date()).getTime())){crd=crd.declaredClass?crd:new esri.Credential(crd);crd.onTokenChange();this.credentials.push(crd);}},this);}},findServerInfo:function(_13){var _14;_13=this._sanitizeUrl(_13);_4.some(this.serverInfos,function(_15){if(esri._hasSameOrigin(_15.server,_13,true)){_14=_15;return true;}return false;});return _14;},findCredential:function(_16,_17){var _18;_16=this._sanitizeUrl(_16);if(_17){_4.some(this.credentials,function(crd){if(esri._hasSameOrigin(_16,crd.server,true)&&_17===crd.userId){_18=crd;return true;}return false;},this);}else{_4.some(this.credentials,function(crd){if(esri._hasSameOrigin(_16,crd.server,true)){_18=crd;if(this._getIdenticalSvcIdx(_16,crd)!==-1){return true;}}return false;},this);}return _18;},getCredential:function(_19,_1a){_19=this._sanitizeUrl(_19);var dfd=new _4.Deferred(esri._dfdCanceller),err,_1b=(_1a&&this._doPortalSignIn(_19))?_4.cookie("esri_auth"):null;if(_1b){_1b=_4.fromJson(_1b);err=new Error("You are currently signed in as: '"+_1b.email+"'. You do not have access to this resource: "+_19);err.code="IdentityManagerBase."+1;err.log=_4.config.isDebug;dfd.errback(err);return dfd;}var _1c=this._findCredential(_19,_1a);if(_1c){dfd.callback(_1c);return dfd;}var _1d=this.findServerInfo(_19);if(!_1d){var _1e=this._getTokenSvcUrl(_19);if(!_1e){err=new Error("Unknown resource - could not find token service endpoint.");err.code="IdentityManagerBase."+2;err.log=_4.config.isDebug;dfd.errback(err);return dfd;}_1d=new esri.ServerInfo();_1d.server=this._getOrigin(_19);if(_4.isString(_1e)){_1d.tokenServiceUrl=_1e;}else{_1d._restInfoDfd=_1e;}this.registerServers([_1d]);}return this._enqueue(_19,_1d,dfd);},getResourceName:function(_1f){if(_1f.toLowerCase().indexOf(this._agsRest)!==-1){return _1f.replace(this._regexSDirUrl,"").replace(this._regexServerType,"")||"";}else{return (this._gwUser.test(_1f)&&_1f.replace(this._gwUser,"$1"))||(this._gwItem.test(_1f)&&_1f.replace(this._gwItem,"$1"))||(this._gwGroup.test(_1f)&&_1f.replace(this._gwGroup,"$1"))||"";}},generateToken:function(_20,_21){var _22=esri.request({url:_20.tokenServiceUrl,content:{request:"getToken",username:_21.username,password:_21.password,expiration:esri.id.tokenValidity,referer:_20.tokenServiceUrl.toLowerCase().indexOf("/sharing/generatetoken")!==-1?window.location.href:null,f:"json"},handleAs:"json"},{usePost:true,disableIdentityLookup:true});_22.addCallback(function(_23){if(!_23||!_23.token){var err=new Error("Unable to generate token");err.code="IdentityManagerBase."+3;err.log=_4.config.isDebug;return err;}var _24=_20.server;if(!_7[_24]){_7[_24]={};}_7[_24][_21.username]=_21.password;return _23;});_22.addErrback(function(_25){});return _22;},isBusy:function(){return !!this._busy;},setRedirectionHandler:function(_26){this._redirectFunc=_26;},setProtocolErrorHandler:function(_27){this._protocolFunc=_27;},signIn:function(){},_findCredential:function(_28,_29){var _2a=_4.filter(this.credentials,function(crd){return esri._hasSameOrigin(crd.server,_28,true);});var idx=-1;if(_2a.length){if(_2a.length===1){if(_29){idx=this._getIdenticalSvcIdx(_28,_2a[0]);if(idx!==-1){_2a[0].resources.splice(idx,1);if(!_2a[0].resources.length){this.credentials.splice(_4.indexOf(this.credentials,_2a[0]),1);}}}else{if(this._getIdenticalSvcIdx(_28,_2a[0])===-1){_2a[0].resources.push(_28);}return _2a[0];}}else{var _2b;_4.some(_2a,function(crd){var i=this._getIdenticalSvcIdx(_28,crd);if(i!==-1){_2b=crd;idx=i;return true;}return false;},this);if(_29){if(_2b){_2b.resources.splice(idx,1);if(!_2b.resources.length){this.credentials.splice(_4.indexOf(this.credentials,_2b),1);}}}else{if(_2b){return _2b;}}}}},_getOrigin:function(_2c){var uri=new _4._Url(_2c);return uri.scheme+"://"+uri.host+(esri._isDefined(uri.port)?(":"+uri.port):"");},_sanitizeUrl:function(url){url=_4.trim(url);var _2d=(esri.config.defaults.io.proxyUrl||"").toLowerCase(),_2e=_2d?url.toLowerCase().indexOf(_2d+"?"):-1;if(_2e!==-1){url=url.substring(_2e+_2d.length+1);}return esri.urlToObject(url).path;},_isIdenticalService:function(_2f,_30){if(this._isPortalDomain(_2f)){return true;}var _31=_2f.replace(this._regexSDirUrl,"").replace(this._regexServerType,"$1"),_32=_30.replace(this._regexSDirUrl,"").replace(this._regexServerType,"$1");return _31===_32;},_isPortalDomain:function(_33){var _34=false;if(this._portalDomains.length){_34=_4.some(this._portalDomains,function(_35){return (_33.toLowerCase().indexOf(_35.toLowerCase())!==-1)?true:false;});}if(!_34){if(!this._arcgisUrl){var _36=_4.getObject("esri.arcgis.utils.arcgisUrl");if(_36){this._arcgisUrl=(new _4._Url(_36)).authority;}}if(this._arcgisUrl){_34=_33.toLowerCase().indexOf(this._arcgisUrl.toLowerCase())!==-1;}}return _34;},_isIdProvider:function(_37,_38){var i=-1,j=-1;_4.forEach(this._gwDomains,function(_39,idx){if(i===-1&&_39.regex.test(_37)){i=idx;}if(j===-1&&_39.regex.test(_38)){j=idx;}});var _3a=false;if(i>-1&&j>-1){if(i===0||i===4){if(j===0||j===4){_3a=true;}}else{if(i===1){if(j===1||j===2){_3a=true;}}else{if(i===2){if(j===2){_3a=true;}}else{if(i===3){if(j===3){_3a=true;}}}}}}return _3a;},_isPublic:function(_3b){_3b=this._sanitizeUrl(_3b);return _4.some(this._publicUrls,function(_3c){return _3c.test(_3b);});},_getIdenticalSvcIdx:function(_3d,_3e){var idx=-1;_4.some(_3e.resources,function(_3f,i){if(this._isIdenticalService(_3d,_3f)){idx=i;return true;}return false;},this);return idx;},_getSuffix:function(_40){return _40.replace(this._regexSDirUrl,"").replace(this._regexServerType,"$1");},_getTokenSvcUrl:function(_41){if(_41.toLowerCase().indexOf(this._agsRest)!==-1){_41=_41.substring(0,_41.toLowerCase().indexOf("/rest/")+"/rest/".length)+"info";if(this._isPortalDomain(_41)){_41=_41.replace(/http:/i,"https:");}return esri.request({url:_41,content:{f:"json"},handleAs:"json",callbackParamName:"callback"});}else{if(this._isPortalDomain(_41)){var url="";_4.some(this._gwDomains,function(_42){if(_42.regex.test(_41)){url=_42.tokenServiceUrl;return true;}return false;});if(!url){var _43=this._getOrigin(_41);url=_43.replace(/http:/i,"https:")+this._gwTokenUrl;}return url;}else{if(_41.toLowerCase().indexOf("premium.arcgisonline.com")!==-1){return "https://premium.arcgisonline.com/server/tokens";}}}},_doPortalSignIn:function(_44){if(_4.cookie.isSupported()){var _45=_4.cookie("esri_auth"),_46=this._portalConfig,_47=window.location.href;if((_46||this._isPortalDomain(_47)||_45)&&this._isPortalDomain(_44)&&(this._isIdProvider(_47,_44)||(_46&&(esri._hasSameOrigin(_46.restBaseUrl,_44,true)||this._isIdProvider(_46.restBaseUrl,_44))))){return true;}}return false;},_checkProtocol:function(_48,_49,_4a){var _4b=true,_4c=_49.tokenServiceUrl;if(_4.trim(_4c).toLowerCase().indexOf("https:")===0&&window.location.href.toLowerCase().indexOf("https:")!==0&&!esri._canDoXOXHR(_4c)&&!esri._canDoXOXHR(esri._getProxyUrl(true).path)){_4b=this._protocolFunc?!!this._protocolFunc({resourceUrl:_48,serverInfo:_49}):false;if(!_4b){var err=new Error("Aborted the Sign-In process to avoid sending password over insecure connection.");err.code="IdentityManagerBase."+4;err.log=_4.config.isDebug;_4a(err);}}return _4b;},_enqueue:function(_4d,_4e,dfd){if(!dfd){dfd=new _4.Deferred(esri._dfdCanceller);}dfd.resUrl_=_4d;dfd.sinfo_=_4e;if(this._busy){if(esri._hasSameOrigin(_4d,this._busy.resUrl_,true)){this._soReqs.push(dfd);}else{this._xoReqs.push(dfd);}}else{this._doSignIn(dfd);}return dfd;},_doSignIn:function(dfd){this._busy=dfd;var _4f=this;var _50=function(_51){if(!_51.resources){_51.resources=[];}_51.resources.push(dfd.resUrl_);_51.onTokenChange();if(_4.indexOf(_4f.credentials,_51)===-1){_4f.credentials.push(_51);}var _52=_4f._soReqs,_53={};_4f._soReqs=[];if(!_4f._isPortalDomain(dfd.resUrl_)){_4.forEach(_52,function(_54){if(!this._isIdenticalService(dfd.resUrl_,_54.resUrl_)){var _55=this._getSuffix(_54.resUrl_);if(!_53[_55]){_53[_55]=true;_51.resources.push(_54.resUrl_);}}},_4f);}dfd.callback(_51);_4.forEach(_52,function(_56){_56.callback(_51);});_4f._busy=dfd.resUrl_=dfd.sinfo_=null;if(_4f._xoReqs.length){_4f._doSignIn(_4f._xoReqs.shift());}},_57=function(_58){dfd.errback(_58);_4f._busy=dfd.resUrl_=dfd.sinfo_=null;if(_4f._soReqs.length){_4f._doSignIn(_4f._soReqs.shift());}if(_4f._xoReqs.length){_4f._doSignIn(_4f._xoReqs.shift());}};if(this._doPortalSignIn(dfd.resUrl_)){var _59=_4.cookie("esri_auth"),_5a=this._portalConfig;if(_59){_59=_4.fromJson(_59);_50(new esri.Credential({userId:_59.email,server:dfd.sinfo_.server,token:_59.token,expires:null}));return;}else{var _5b="",_5c=window.location.href;if(this.signInPage){_5b=this.signInPage;}else{if(_5a){_5b=_5a.baseUrl+_5a.signin;}else{if(this._isIdProvider(_5c,dfd.resUrl_)){_5b=this._getOrigin(_5c)+"/home/signin.html";}else{_5b=dfd.sinfo_.server+"/home/signin.html";}}}_5b=_5b.replace(/http:/i,"https:");if(_5a&&_5a.useSSL===false){_5b=_5b.replace(/https:/i,"http:");}if(_5c.toLowerCase().replace("https","http").indexOf(_5b.toLowerCase().replace("https","http"))===0){var err=new Error("Cannot redirect to Sign-In page from within Sign-In page. URL of the resource that triggered this workflow: "+dfd.resUrl_);err.code="IdentityManagerBase."+5;err.log=_4.config.isDebug;_57(err);}else{if(this._redirectFunc){this._redirectFunc({signInPage:_5b,returnUrlParamName:"returnUrl",returnUrl:_5c,resourceUrl:dfd.resUrl_,serverInfo:dfd.sinfo_});}else{window.location=_5b+"?returnUrl="+window.escape(_5c);}}return;}}var _5d=dfd.sinfo_.tokenServiceUrl;if(_5d){if(this._checkProtocol(dfd.resUrl_,dfd.sinfo_,_57)){dfd._pendingDfd=this.signIn(dfd.resUrl_,dfd.sinfo_).addCallbacks(_50,_57);}}else{dfd.sinfo_._restInfoDfd.addCallbacks(function(_5e){var _5f=dfd.sinfo_;_5f._restInfoDfd=null;_5f.tokenServiceUrl=_4.getObject("authInfo.tokenServicesUrl",false,_5e)||_4.getObject("authInfo.tokenServiceUrl",false,_5e)||_4.getObject("tokenServiceUrl",false,_5e);_5f.shortLivedTokenValidity=_4.getObject("authInfo.shortLivedTokenValidity",false,_5e);var _60=_4.getObject("owningSystemUrl",false,_5e);if(_60){var _61=_4f.findCredential(_60);if(_61){_61=_61.toJson();_61.resources=null;_61.server=_5f.server;_50(new esri.Credential(_61));return;}}if(_4f._checkProtocol(dfd.resUrl_,_5f,_57)){dfd._pendingDfd=_4f.signIn(dfd.resUrl_,_5f).addCallbacks(_50,_57);}},function(){dfd.sinfo_._restInfoDfd=null;var err=new Error("Unknown resource - could not find token service endpoint.");err.code="IdentityManagerBase."+2;err.log=_4.config.isDebug;_57(err);});}}});_4.declare("esri.ServerInfo",null,{constructor:function(_62){_4.mixin(this,_62);},toJson:function(){return esri._sanitize({server:this.server,tokenServiceUrl:this.tokenServiceUrl});}});_4.declare("esri.Credential",null,{tokenRefreshBuffer:2,constructor:function(_63){_4.mixin(this,_63);this.resources=this.resources||[];if(!esri._isDefined(this.creationTime)){this.creationTime=(new Date()).getTime();}},refreshToken:function(){var _64=this,_65=esri.id.findServerInfo(this.server),_66=_7[this.server],_67=_66&&_66[this.userId];if(!_67){var dfd,_68=this.resources&&this.resources[0];if(_68){_68=esri.id._sanitizeUrl(_68);this._enqueued=1;dfd=esri.id._enqueue(_68,_65);dfd.addBoth(function(){_64._enqueued=0;});}return dfd;}return esri.id.generateToken(_65,{username:_64.userId,password:_67}).addCallback(function(_69){_64.token=_69.token;_64.expires=esri._isDefined(_69.expires)?Number(_69.expires):null;_64.creationTime=(new Date()).getTime();_64.onTokenChange();}).addErrback(function(){});},onTokenChange:function(){clearTimeout(this._refreshTimer);if(esri._isDefined(this.expires)||esri._isDefined(this.validity)){this._startRefreshTimer();}},onDestroy:function(){},destroy:function(){this.userId=this.server=this.token=this.expires=this.validity=this.resources=this.creationTime=null;var _6a=_4.indexOf(esri.id.credentials,this);if(_6a>-1){esri.id.credentials.splice(_6a,1);}this.onTokenChange();this.onDestroy();},toJson:function(){return this._toJson();},_toJson:function(){var _6b=esri._sanitize({userId:this.userId,server:this.server,token:this.token,expires:this.expires,validity:this.validity,creationTime:this.creationTime});var _6c=this.resources;if(_6c&&_6c.length>0){_6b.resources=_6c;}return _6b;},_startRefreshTimer:function(){clearTimeout(this._refreshTimer);var _6d=this.tokenRefreshBuffer*60000,_6e=this.validity?(this.creationTime+(this.validity*60000)):this.expires,_6f=(_6e-(new Date()).getTime());if(_6f<0){_6f=0;}this._refreshTimer=setTimeout(_4.hitch(this,this.refreshToken),(_6f>_6d)?(_6f-_6d):_6f);}});}());}}};});