@esri/arcgis-rest-auth
Advanced tools
Comparing version 3.4.2 to 3.4.3
@@ -390,3 +390,3 @@ /* Copyright (c) 2017-2019 Environmental Systems Research Institute, Inc. | ||
var ssl = typeof credential.ssl !== "undefined" ? credential.ssl : true; | ||
var expires = credential.expires || Date.now() + 7200000 /* 2 hours */; | ||
var expires = credential.expires || Date.now() + 7200000; /* 2 hours */ | ||
return new UserSession({ | ||
@@ -668,11 +668,24 @@ portal: credential.server.includes("sharing/rest") | ||
var isValidType = event.data.type === "arcgis:auth:requestCredential"; | ||
var isTokenValid = _this.tokenExpires.getTime() > Date.now(); | ||
if (isValidOrigin && isValidType) { | ||
var credential = _this.toCredential(); | ||
// the following line allows us to conform to our spec without changing other depended-on functionality | ||
// https://github.com/Esri/arcgis-rest-js/blob/master/packages/arcgis-rest-auth/post-message-auth-spec.md#arcgisauthcredential | ||
credential.server = credential.server.replace("/sharing/rest", ""); | ||
event.source.postMessage({ | ||
type: "arcgis:auth:credential", | ||
credential: credential, | ||
}, event.origin); | ||
var msg = {}; | ||
if (isTokenValid) { | ||
var credential = _this.toCredential(); | ||
// arcgis:auth:error with {name: "", message: ""} | ||
// the following line allows us to conform to our spec without changing other depended-on functionality | ||
// https://github.com/Esri/arcgis-rest-js/blob/master/packages/arcgis-rest-auth/post-message-auth-spec.md#arcgisauthcredential | ||
credential.server = credential.server.replace("/sharing/rest", ""); | ||
msg = { type: "arcgis:auth:credential", credential: credential }; | ||
} | ||
else { | ||
// Return an error | ||
msg = { | ||
type: "arcgis:auth:error", | ||
error: { | ||
name: "tokenExpiredError", | ||
message: "Session token was expired, and not returned to the child application", | ||
}, | ||
}; | ||
} | ||
event.source.postMessage(msg, event.origin); | ||
} | ||
@@ -679,0 +692,0 @@ }; |
@@ -393,3 +393,3 @@ "use strict"; | ||
var ssl = typeof credential.ssl !== "undefined" ? credential.ssl : true; | ||
var expires = credential.expires || Date.now() + 7200000 /* 2 hours */; | ||
var expires = credential.expires || Date.now() + 7200000; /* 2 hours */ | ||
return new UserSession({ | ||
@@ -671,11 +671,24 @@ portal: credential.server.includes("sharing/rest") | ||
var isValidType = event.data.type === "arcgis:auth:requestCredential"; | ||
var isTokenValid = _this.tokenExpires.getTime() > Date.now(); | ||
if (isValidOrigin && isValidType) { | ||
var credential = _this.toCredential(); | ||
// the following line allows us to conform to our spec without changing other depended-on functionality | ||
// https://github.com/Esri/arcgis-rest-js/blob/master/packages/arcgis-rest-auth/post-message-auth-spec.md#arcgisauthcredential | ||
credential.server = credential.server.replace("/sharing/rest", ""); | ||
event.source.postMessage({ | ||
type: "arcgis:auth:credential", | ||
credential: credential, | ||
}, event.origin); | ||
var msg = {}; | ||
if (isTokenValid) { | ||
var credential = _this.toCredential(); | ||
// arcgis:auth:error with {name: "", message: ""} | ||
// the following line allows us to conform to our spec without changing other depended-on functionality | ||
// https://github.com/Esri/arcgis-rest-js/blob/master/packages/arcgis-rest-auth/post-message-auth-spec.md#arcgisauthcredential | ||
credential.server = credential.server.replace("/sharing/rest", ""); | ||
msg = { type: "arcgis:auth:credential", credential: credential }; | ||
} | ||
else { | ||
// Return an error | ||
msg = { | ||
type: "arcgis:auth:error", | ||
error: { | ||
name: "tokenExpiredError", | ||
message: "Session token was expired, and not returned to the child application", | ||
}, | ||
}; | ||
} | ||
event.source.postMessage(msg, event.origin); | ||
} | ||
@@ -682,0 +695,0 @@ }; |
/* @preserve | ||
* @esri/arcgis-rest-auth - v3.4.2 - Apache-2.0 | ||
* @esri/arcgis-rest-auth - v3.4.3 - Apache-2.0 | ||
* Copyright (c) 2017-2021 Esri, Inc. | ||
* Sat Oct 23 2021 08:28:16 GMT-0600 (Mountain Daylight Time) | ||
* Fri Oct 29 2021 14:49:25 GMT-0600 (Mountain Daylight Time) | ||
*/ | ||
@@ -638,3 +638,3 @@ (function (global, factory) { | ||
var ssl = typeof credential.ssl !== "undefined" ? credential.ssl : true; | ||
var expires = credential.expires || Date.now() + 7200000 /* 2 hours */; | ||
var expires = credential.expires || Date.now() + 7200000; /* 2 hours */ | ||
return new UserSession({ | ||
@@ -916,11 +916,24 @@ portal: credential.server.includes("sharing/rest") | ||
var isValidType = event.data.type === "arcgis:auth:requestCredential"; | ||
var isTokenValid = _this.tokenExpires.getTime() > Date.now(); | ||
if (isValidOrigin && isValidType) { | ||
var credential = _this.toCredential(); | ||
// the following line allows us to conform to our spec without changing other depended-on functionality | ||
// https://github.com/Esri/arcgis-rest-js/blob/master/packages/arcgis-rest-auth/post-message-auth-spec.md#arcgisauthcredential | ||
credential.server = credential.server.replace("/sharing/rest", ""); | ||
event.source.postMessage({ | ||
type: "arcgis:auth:credential", | ||
credential: credential, | ||
}, event.origin); | ||
var msg = {}; | ||
if (isTokenValid) { | ||
var credential = _this.toCredential(); | ||
// arcgis:auth:error with {name: "", message: ""} | ||
// the following line allows us to conform to our spec without changing other depended-on functionality | ||
// https://github.com/Esri/arcgis-rest-js/blob/master/packages/arcgis-rest-auth/post-message-auth-spec.md#arcgisauthcredential | ||
credential.server = credential.server.replace("/sharing/rest", ""); | ||
msg = { type: "arcgis:auth:credential", credential: credential }; | ||
} | ||
else { | ||
// Return an error | ||
msg = { | ||
type: "arcgis:auth:error", | ||
error: { | ||
name: "tokenExpiredError", | ||
message: "Session token was expired, and not returned to the child application", | ||
}, | ||
}; | ||
} | ||
event.source.postMessage(msg, event.origin); | ||
} | ||
@@ -927,0 +940,0 @@ }; |
/* @preserve | ||
* @esri/arcgis-rest-auth - v3.4.2 - Apache-2.0 | ||
* @esri/arcgis-rest-auth - v3.4.3 - Apache-2.0 | ||
* Copyright (c) 2017-2021 Esri, Inc. | ||
* Sat Oct 23 2021 08:28:20 GMT-0600 (Mountain Daylight Time) | ||
* Fri Oct 29 2021 14:49:28 GMT-0600 (Mountain Daylight Time) | ||
*/ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@esri/arcgis-rest-request")):"function"==typeof define&&define.amd?define(["exports","@esri/arcgis-rest-request"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).arcgisRest=e.arcgisRest||{},e.arcgisRest)}(this,function(e,f){"use strict";var k=function(){return(k=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var s in t=arguments[r])Object.prototype.hasOwnProperty.call(t,s)&&(e[s]=t[s]);return e}).apply(this,arguments)};function i(e,t){return t.rawResponse=!1,f.request(e,t).then(function(e){var t={token:e.access_token,username:e.username,expires:new Date(Date.now()+(1e3*e.expires_in-1e3)),ssl:!0===e.ssl};return e.refresh_token&&(t.refreshToken=e.refresh_token),t})}var t=(r.prototype.getToken=function(e,t){return this.token&&this.expires&&this.expires.getTime()>Date.now()?Promise.resolve(this.token):this._pendingTokenRequest||(this._pendingTokenRequest=this.refreshToken(t),this._pendingTokenRequest)},r.prototype.refreshToken=function(e){var t=this,e=k({params:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials",expiration:this.duration}},e);return i(this.portal+"/oauth2/token/",e).then(function(e){return t._pendingTokenRequest=null,t.token=e.token,t.expires=e.expires,e.token})},r.prototype.refreshSession=function(){var e=this;return this.refreshToken().then(function(){return e})},r);function r(e){this.clientId=e.clientId,this.clientSecret=e.clientSecret,this.token=e.token,this.expires=e.expires,this.portal=e.portal||"https://www.arcgis.com/sharing/rest",this.duration=e.duration||7200}var n=(s.prototype.getToken=function(e){return Promise.resolve(this.key)},s);function s(e){this.key=e.key}function o(e,t){return"undefined"!=typeof window&&window.location&&window.location.host?t.params.referer=window.location.host:t.params.referer=f.NODEJS_DEFAULT_REFERER_HEADER,f.request(e,t)}var a=/^https?:\/\/(\S+)\.arcgis\.com.+/;function h(e){return a.test(e)}function u(e){if(!a.test(e))return null;e=e.match(a)[1].split(".").pop();return e.includes("dev")?"dev":e.includes("qa")?"qa":"production"}function p(e,t){t=f.cleanUrl(function(e){if(!a.test(e))return e;switch(u(e)){case"dev":return"https://devext.arcgis.com/sharing/rest";case"qa":return"https://qaext.arcgis.com/sharing/rest";default:return"https://www.arcgis.com/sharing/rest"}}(t)).replace(/https?:\/\//,""),e=f.cleanUrl(e).replace(/https?:\/\//,"");return new RegExp(e,"i").test(t)}function c(e,t,r){void 0===r&&(r="https://www.arcgis.com/sharing/rest");r+="/oauth2/validateAppAccess",e={method:"POST",params:{f:"json",client_id:t,token:e}};return f.request(r,e)}var l=(Object.defineProperty(g.prototype,"token",{get:function(){return this._token},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"tokenExpires",{get:function(){return this._tokenExpires},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"refreshToken",{get:function(){return this._refreshToken},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"refreshTokenExpires",{get:function(){return this._refreshTokenExpires},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"trustedServers",{get:function(){return console.log("DEPRECATED: use federatedServers instead"),this.federatedServers},enumerable:!1,configurable:!0}),g.beginOAuth2=function(e,t){void 0===t&&(t=window),e.duration&&console.log("DEPRECATED: 'duration' is deprecated - use 'expiration' instead");var r=k({portal:"https://www.arcgis.com/sharing/rest",provider:"arcgis",expiration:20160,popup:!0,popupWindowFeatures:"height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes",state:e.clientId,locale:""},e),n=r.portal,s=r.provider,o=r.clientId,i=r.expiration,a=r.redirectUri,h=r.popup,u=r.popupWindowFeatures,p=r.state,c=r.locale,r=r.params,c="arcgis"===s?n+"/oauth2/authorize?client_id="+o+"&response_type=token&expiration="+(e.duration||i)+"&redirect_uri="+encodeURIComponent(a)+"&state="+p+"&locale="+c:n+"/oauth2/social/authorize?client_id="+o+"&socialLoginProviderName="+s+"&autoAccountCreateForSocial=true&response_type=token&expiration="+(e.duration||i)+"&redirect_uri="+encodeURIComponent(a)+"&state="+p+"&locale="+c;if(r&&(c=c+"&"+f.encodeQueryString(r)),h){var l,d=((l={promise:null,resolve:null,reject:null}).promise=new Promise(function(e,t){l.resolve=e,l.reject=t}),l);return t["__ESRI_REST_AUTH_HANDLER_"+o]=function(e,t){e?(e=JSON.parse(e),d.reject(new f.ArcGISAuthError(e.errorMessage,e.error))):t&&(t=JSON.parse(t),d.resolve(new g({clientId:o,portal:n,ssl:t.ssl,token:t.token,tokenExpires:new Date(t.expires),username:t.username})))},t.open(c,"oauth-window",u),d.promise}t.location.href=c},g.completeOAuth2=function(e,s){void 0===s&&(s=window);var t=k({portal:"https://www.arcgis.com/sharing/rest",popup:!0},e),o=t.portal,i=t.clientId,a=t.popup;function r(e,t){try{var r=void 0,n="__ESRI_REST_AUTH_HANDLER_"+i;if(a&&(s.opener?s.opener.parent&&s.opener.parent[n]?r=s.opener.parent[n]:s.opener&&s.opener[n]&&(r=s.opener[n]):s!==s.parent&&s.parent&&s.parent[n]&&(r=s.parent[n]),r))return r(e?JSON.stringify(e):void 0,JSON.stringify(t)),void s.close()}catch(e){throw new f.ArcGISAuthError('Unable to complete authentication. It\'s possible you specified popup based oAuth2 but no handler from "beginOAuth2()" present. This generally happens because the "popup" option differs between "beginOAuth2()" and "completeOAuth2()".')}if(e)throw new f.ArcGISAuthError(e.errorMessage,e.error);return new g({clientId:i,portal:o,ssl:t.ssl,token:t.token,tokenExpires:t.expires,username:t.username})}e=f.decodeQueryString(s.location.hash);if(!e.access_token){var n=void 0,h="Unknown error";return e.error&&(n=e.error,h=e.error_description),r({error:n,errorMessage:h})}t=e.access_token,n=new Date(Date.now()+1e3*parseInt(e.expires_in,10)-6e4),h=e.username;return r(void 0,{token:t,expires:n,ssl:"true"===e.ssl,username:h})},g.fromParent=function(e,n){var s;return!n&&window&&(n=window),new Promise(function(t,r){s=function(e){if(e.source===n.parent&&e.data)try{return t(g.parentMessageHandler(e))}catch(e){return r(e)}},n.addEventListener("message",s,!1),n.parent.postMessage({type:"arcgis:auth:requestCredential"},e)}).then(function(e){return n.removeEventListener("message",s,!1),e})},g.authorize=function(e,t){e.duration&&console.log("DEPRECATED: 'duration' is deprecated - use 'expiration' instead");var r=k({portal:"https://arcgis.com/sharing/rest",expiration:20160},e),n=r.portal,s=r.clientId,o=r.expiration,r=r.redirectUri;t.writeHead(301,{Location:n+"/oauth2/authorize?client_id="+s+"&expiration="+(e.duration||o)+"&response_type=code&redirect_uri="+encodeURIComponent(r)}),t.end()},g.exchangeAuthorizationCode=function(e,t){var e=k({portal:"https://www.arcgis.com/sharing/rest",refreshTokenTTL:20160},e),r=e.portal,n=e.clientId,s=e.redirectUri,o=e.refreshTokenTTL;return i(r+"/oauth2/token",{params:{grant_type:"authorization_code",client_id:n,redirect_uri:s,code:t}}).then(function(e){return new g({clientId:n,portal:r,ssl:e.ssl,redirectUri:s,refreshToken:e.refreshToken,refreshTokenTTL:o,refreshTokenExpires:new Date(Date.now()+60*(o-1)*1e3),token:e.token,tokenExpires:e.expires,username:e.username})})},g.deserialize=function(e){e=JSON.parse(e);return new g({clientId:e.clientId,refreshToken:e.refreshToken,refreshTokenExpires:new Date(e.refreshTokenExpires),username:e.username,password:e.password,token:e.token,tokenExpires:new Date(e.tokenExpires),portal:e.portal,ssl:e.ssl,tokenDuration:e.tokenDuration,redirectUri:e.redirectUri,refreshTokenTTL:e.refreshTokenTTL})},g.fromCredential=function(e){var t=void 0===e.ssl||e.ssl,r=e.expires||Date.now()+72e5;return new g({portal:e.server.includes("sharing/rest")?e.server:e.server+"/sharing/rest",ssl:t,token:e.token,username:e.userId,tokenExpires:new Date(r)})},g.parentMessageHandler=function(e){if("arcgis:auth:credential"===e.data.type)return g.fromCredential(e.data.credential);if("arcgis:auth:error"!==e.data.type)throw new Error("Unknown message type.");var t=new Error(e.data.error.message);throw t.name=e.data.error.name,t},g.prototype.toCredential=function(){return{expires:this.tokenExpires.getTime(),server:this.portal,ssl:this.ssl,token:this.token,userId:this.username}},g.prototype.getUser=function(e){var t=this;if(this._pendingUserRequest)return this._pendingUserRequest;if(this._user)return Promise.resolve(this._user);var r=this.portal+"/community/self",e=k(k({httpMethod:"GET",authentication:this},e),{rawResponse:!1});return this._pendingUserRequest=f.request(r,e).then(function(e){return t._user=e,t._pendingUserRequest=null,e}),this._pendingUserRequest},g.prototype.getPortal=function(e){var t=this;if(this._pendingPortalRequest)return this._pendingPortalRequest;if(this._portalInfo)return Promise.resolve(this._portalInfo);var r=this.portal+"/portals/self",e=k(k({httpMethod:"GET",authentication:this},e),{rawResponse:!1});return this._pendingPortalRequest=f.request(r,e).then(function(e){return t._portalInfo=e,t._pendingPortalRequest=null,e}),this._pendingPortalRequest},g.prototype.getUsername=function(){return this.username?Promise.resolve(this.username):this._user?Promise.resolve(this._user.username):this.getUser().then(function(e){return e.username})},g.prototype.getToken=function(e,t){return r=this.portal,n=e,s=h(r),o=h(n),r=u(r),n=u(n),s&&o&&r===n||new RegExp(this.portal,"i").test(e)?this.getFreshToken(t):this.getTokenForServer(e,t);var r,n,s,o},g.prototype.validateAppAccess=function(t){return this.getToken(this.portal).then(function(e){return c(e,t)})},g.prototype.toJSON=function(){return{clientId:this.clientId,refreshToken:this.refreshToken,refreshTokenExpires:this.refreshTokenExpires,username:this.username,password:this.password,token:this.token,tokenExpires:this.tokenExpires,portal:this.portal,ssl:this.ssl,tokenDuration:this.tokenDuration,redirectUri:this.redirectUri,refreshTokenTTL:this.refreshTokenTTL}},g.prototype.serialize=function(){return JSON.stringify(this)},g.prototype.enablePostMessageAuth=function(e,t){!t&&window&&(t=window),this._hostHandler=this.createPostMessageHandler(e),t.addEventListener("message",this._hostHandler,!1)},g.prototype.disablePostMessageAuth=function(e){!e&&window&&(e=window),e.removeEventListener("message",this._hostHandler,!1)},g.prototype.refreshSession=function(e){return this._user=null,this.username&&this.password?this.refreshWithUsernameAndPassword(e):this.clientId&&this.refreshToken?this.refreshWithRefreshToken():Promise.reject(new f.ArcGISAuthError("Unable to refresh token."))},g.prototype.getServerRootUrl=function(e){var t=f.cleanUrl(e).split(/\/rest(\/admin)?\/services(?:\/|#|\?|$)/)[0].match(/(https?:\/\/)(.+)/),r=(t[0],t[1]),e=t[2].split("/"),t=e[0],e=e.slice(1);return""+r+t.toLowerCase()+"/"+e.join("/")},g.prototype.getDomainCredentials=function(t){return this.trustedDomains&&this.trustedDomains.length&&this.trustedDomains.some(function(e){return t.startsWith(e)})?"include":"same-origin"},g.prototype.createPostMessageHandler=function(n){var s=this;return function(e){var t=-1<n.indexOf(e.origin),r="arcgis:auth:requestCredential"===e.data.type;t&&r&&((r=s.toCredential()).server=r.server.replace("/sharing/rest",""),e.source.postMessage({type:"arcgis:auth:credential",credential:r},e.origin))}},g.prototype.getTokenForServer=function(t,r){var n=this,s=this.getServerRootUrl(t),e=this.federatedServers[s];return e&&e.expires&&e.expires.getTime()>Date.now()?Promise.resolve(e.token):this._pendingTokenRequests[s]||(this._pendingTokenRequests[s]=this.fetchAuthorizedDomains().then(function(){return f.request(s+"/rest/info",{credentials:n.getDomainCredentials(t)}).then(function(e){if(e.owningSystemUrl){if(p(e.owningSystemUrl,n.portal))return f.request(e.owningSystemUrl+"/sharing/rest/info",r);throw new f.ArcGISAuthError(t+" is not federated with "+n.portal+".","NOT_FEDERATED")}if(e.authInfo&&void 0!==n.federatedServers[s])return Promise.resolve({authInfo:e.authInfo});throw new f.ArcGISAuthError(t+" is not federated with any portal and is not explicitly trusted.","NOT_FEDERATED")}).then(function(e){return e.authInfo.tokenServicesUrl}).then(function(e){return n.token&&n.tokenExpires.getTime()>Date.now()?o(e,{params:{token:n.token,serverUrl:t,expiration:n.tokenDuration,client:"referer"}}):o(e,{params:{username:n.username,password:n.password,expiration:n.tokenDuration,client:"referer"}}).then(function(e){return n._token=e.token,n._tokenExpires=new Date(e.expires),e})}).then(function(e){return n.federatedServers[s]={expires:new Date(e.expires),token:e.token},delete n._pendingTokenRequests[s],e.token})}),this._pendingTokenRequests[s])},g.prototype.getFreshToken=function(e){var t=this;return this.token&&!this.tokenExpires||this.token&&this.tokenExpires&&this.tokenExpires.getTime()>Date.now()?Promise.resolve(this.token):(this._pendingTokenRequests[this.portal]||(this._pendingTokenRequests[this.portal]=this.refreshSession(e).then(function(e){return t._pendingTokenRequests[t.portal]=null,e.token})),this._pendingTokenRequests[this.portal])},g.prototype.refreshWithUsernameAndPassword=function(e){var t=this,e=k({params:{username:this.username,password:this.password,expiration:this.tokenDuration}},e);return o(this.portal+"/generateToken",e).then(function(e){return t._token=e.token,t._tokenExpires=new Date(e.expires),t})},g.prototype.refreshWithRefreshToken=function(e){var t=this;if(this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now())return this.refreshRefreshToken(e);e=k({params:{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}},e);return i(this.portal+"/oauth2/token",e).then(function(e){return t._token=e.token,t._tokenExpires=e.expires,t})},g.prototype.refreshRefreshToken=function(e){var t=this,e=k({params:{client_id:this.clientId,refresh_token:this.refreshToken,redirect_uri:this.redirectUri,grant_type:"exchange_refresh_token"}},e);return i(this.portal+"/oauth2/token",e).then(function(e){return t._token=e.token,t._tokenExpires=e.expires,t._refreshToken=e.refreshToken,t._refreshTokenExpires=new Date(Date.now()+60*(t.refreshTokenTTL-1)*1e3),t})},g.prototype.fetchAuthorizedDomains=function(){var t=this;return this.server||!this.portal?Promise.resolve(this):this.getPortal().then(function(e){return e.authorizedCrossOriginDomains&&e.authorizedCrossOriginDomains.length&&(t.trustedDomains=e.authorizedCrossOriginDomains.filter(function(e){return!e.startsWith("http://")}).map(function(e){return e.startsWith("https://")?e:"https://"+e})),t})},g);function g(e){var t;this.clientId=e.clientId,this._refreshToken=e.refreshToken,this._refreshTokenExpires=e.refreshTokenExpires,this.username=e.username,this.password=e.password,this._token=e.token,this._tokenExpires=e.tokenExpires,this.portal=e.portal?f.cleanUrl(e.portal):"https://www.arcgis.com/sharing/rest",this.ssl=e.ssl,this.provider=e.provider||"arcgis",this.tokenDuration=e.tokenDuration||20160,this.redirectUri=e.redirectUri,this.refreshTokenTTL=e.refreshTokenTTL||20160,this.server=e.server,this.federatedServers={},this.trustedDomains=[],e.server&&(t=this.getServerRootUrl(e.server),this.federatedServers[t]={token:e.token,expires:e.tokenExpires}),this._pendingTokenRequests={}}e.ApiKey=n,e.ApplicationSession=t,e.UserSession=l,e.exchangeToken=function(e,t,r){return void 0===r&&(r="https://www.arcgis.com/sharing/rest"),r+="/oauth2/exchangeToken",e={method:"POST",params:{f:"json",client_id:t,token:e}},f.request(r,e).then(function(e){return e.token})},e.fetchToken=i,e.generateToken=o,e.platformSelf=function(e,t,r){return void 0===r&&(r="https://www.arcgis.com/sharing/rest"),r+="/oauth2/platformSelf?f=json",t={method:"POST",headers:{"X-Esri-Auth-Client-Id":e,"X-Esri-Auth-Redirect-Uri":t},params:{f:"json"}},f.request(r,t)},e.validateAppAccess=c,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@esri/arcgis-rest-request")):"function"==typeof define&&define.amd?define(["exports","@esri/arcgis-rest-request"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).arcgisRest=e.arcgisRest||{},e.arcgisRest)}(this,function(e,f){"use strict";var k=function(){return(k=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var s in t=arguments[r])Object.prototype.hasOwnProperty.call(t,s)&&(e[s]=t[s]);return e}).apply(this,arguments)};function i(e,t){return t.rawResponse=!1,f.request(e,t).then(function(e){var t={token:e.access_token,username:e.username,expires:new Date(Date.now()+(1e3*e.expires_in-1e3)),ssl:!0===e.ssl};return e.refresh_token&&(t.refreshToken=e.refresh_token),t})}var t=(r.prototype.getToken=function(e,t){return this.token&&this.expires&&this.expires.getTime()>Date.now()?Promise.resolve(this.token):this._pendingTokenRequest||(this._pendingTokenRequest=this.refreshToken(t),this._pendingTokenRequest)},r.prototype.refreshToken=function(e){var t=this,e=k({params:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials",expiration:this.duration}},e);return i(this.portal+"/oauth2/token/",e).then(function(e){return t._pendingTokenRequest=null,t.token=e.token,t.expires=e.expires,e.token})},r.prototype.refreshSession=function(){var e=this;return this.refreshToken().then(function(){return e})},r);function r(e){this.clientId=e.clientId,this.clientSecret=e.clientSecret,this.token=e.token,this.expires=e.expires,this.portal=e.portal||"https://www.arcgis.com/sharing/rest",this.duration=e.duration||7200}var n=(s.prototype.getToken=function(e){return Promise.resolve(this.key)},s);function s(e){this.key=e.key}function o(e,t){return"undefined"!=typeof window&&window.location&&window.location.host?t.params.referer=window.location.host:t.params.referer=f.NODEJS_DEFAULT_REFERER_HEADER,f.request(e,t)}var a=/^https?:\/\/(\S+)\.arcgis\.com.+/;function h(e){return a.test(e)}function u(e){if(!a.test(e))return null;e=e.match(a)[1].split(".").pop();return e.includes("dev")?"dev":e.includes("qa")?"qa":"production"}function p(e,t){t=f.cleanUrl(function(e){if(!a.test(e))return e;switch(u(e)){case"dev":return"https://devext.arcgis.com/sharing/rest";case"qa":return"https://qaext.arcgis.com/sharing/rest";default:return"https://www.arcgis.com/sharing/rest"}}(t)).replace(/https?:\/\//,""),e=f.cleanUrl(e).replace(/https?:\/\//,"");return new RegExp(e,"i").test(t)}function c(e,t,r){void 0===r&&(r="https://www.arcgis.com/sharing/rest");r+="/oauth2/validateAppAccess",e={method:"POST",params:{f:"json",client_id:t,token:e}};return f.request(r,e)}var l=(Object.defineProperty(g.prototype,"token",{get:function(){return this._token},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"tokenExpires",{get:function(){return this._tokenExpires},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"refreshToken",{get:function(){return this._refreshToken},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"refreshTokenExpires",{get:function(){return this._refreshTokenExpires},enumerable:!1,configurable:!0}),Object.defineProperty(g.prototype,"trustedServers",{get:function(){return console.log("DEPRECATED: use federatedServers instead"),this.federatedServers},enumerable:!1,configurable:!0}),g.beginOAuth2=function(e,t){void 0===t&&(t=window),e.duration&&console.log("DEPRECATED: 'duration' is deprecated - use 'expiration' instead");var r=k({portal:"https://www.arcgis.com/sharing/rest",provider:"arcgis",expiration:20160,popup:!0,popupWindowFeatures:"height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes",state:e.clientId,locale:""},e),n=r.portal,s=r.provider,o=r.clientId,i=r.expiration,a=r.redirectUri,h=r.popup,u=r.popupWindowFeatures,p=r.state,c=r.locale,r=r.params,c="arcgis"===s?n+"/oauth2/authorize?client_id="+o+"&response_type=token&expiration="+(e.duration||i)+"&redirect_uri="+encodeURIComponent(a)+"&state="+p+"&locale="+c:n+"/oauth2/social/authorize?client_id="+o+"&socialLoginProviderName="+s+"&autoAccountCreateForSocial=true&response_type=token&expiration="+(e.duration||i)+"&redirect_uri="+encodeURIComponent(a)+"&state="+p+"&locale="+c;if(r&&(c=c+"&"+f.encodeQueryString(r)),h){var l,d=((l={promise:null,resolve:null,reject:null}).promise=new Promise(function(e,t){l.resolve=e,l.reject=t}),l);return t["__ESRI_REST_AUTH_HANDLER_"+o]=function(e,t){e?(e=JSON.parse(e),d.reject(new f.ArcGISAuthError(e.errorMessage,e.error))):t&&(t=JSON.parse(t),d.resolve(new g({clientId:o,portal:n,ssl:t.ssl,token:t.token,tokenExpires:new Date(t.expires),username:t.username})))},t.open(c,"oauth-window",u),d.promise}t.location.href=c},g.completeOAuth2=function(e,s){void 0===s&&(s=window);var t=k({portal:"https://www.arcgis.com/sharing/rest",popup:!0},e),o=t.portal,i=t.clientId,a=t.popup;function r(e,t){try{var r=void 0,n="__ESRI_REST_AUTH_HANDLER_"+i;if(a&&(s.opener?s.opener.parent&&s.opener.parent[n]?r=s.opener.parent[n]:s.opener&&s.opener[n]&&(r=s.opener[n]):s!==s.parent&&s.parent&&s.parent[n]&&(r=s.parent[n]),r))return r(e?JSON.stringify(e):void 0,JSON.stringify(t)),void s.close()}catch(e){throw new f.ArcGISAuthError('Unable to complete authentication. It\'s possible you specified popup based oAuth2 but no handler from "beginOAuth2()" present. This generally happens because the "popup" option differs between "beginOAuth2()" and "completeOAuth2()".')}if(e)throw new f.ArcGISAuthError(e.errorMessage,e.error);return new g({clientId:i,portal:o,ssl:t.ssl,token:t.token,tokenExpires:t.expires,username:t.username})}e=f.decodeQueryString(s.location.hash);if(!e.access_token){var n=void 0,h="Unknown error";return e.error&&(n=e.error,h=e.error_description),r({error:n,errorMessage:h})}t=e.access_token,n=new Date(Date.now()+1e3*parseInt(e.expires_in,10)-6e4),h=e.username;return r(void 0,{token:t,expires:n,ssl:"true"===e.ssl,username:h})},g.fromParent=function(e,n){var s;return!n&&window&&(n=window),new Promise(function(t,r){s=function(e){if(e.source===n.parent&&e.data)try{return t(g.parentMessageHandler(e))}catch(e){return r(e)}},n.addEventListener("message",s,!1),n.parent.postMessage({type:"arcgis:auth:requestCredential"},e)}).then(function(e){return n.removeEventListener("message",s,!1),e})},g.authorize=function(e,t){e.duration&&console.log("DEPRECATED: 'duration' is deprecated - use 'expiration' instead");var r=k({portal:"https://arcgis.com/sharing/rest",expiration:20160},e),n=r.portal,s=r.clientId,o=r.expiration,r=r.redirectUri;t.writeHead(301,{Location:n+"/oauth2/authorize?client_id="+s+"&expiration="+(e.duration||o)+"&response_type=code&redirect_uri="+encodeURIComponent(r)}),t.end()},g.exchangeAuthorizationCode=function(e,t){var e=k({portal:"https://www.arcgis.com/sharing/rest",refreshTokenTTL:20160},e),r=e.portal,n=e.clientId,s=e.redirectUri,o=e.refreshTokenTTL;return i(r+"/oauth2/token",{params:{grant_type:"authorization_code",client_id:n,redirect_uri:s,code:t}}).then(function(e){return new g({clientId:n,portal:r,ssl:e.ssl,redirectUri:s,refreshToken:e.refreshToken,refreshTokenTTL:o,refreshTokenExpires:new Date(Date.now()+60*(o-1)*1e3),token:e.token,tokenExpires:e.expires,username:e.username})})},g.deserialize=function(e){e=JSON.parse(e);return new g({clientId:e.clientId,refreshToken:e.refreshToken,refreshTokenExpires:new Date(e.refreshTokenExpires),username:e.username,password:e.password,token:e.token,tokenExpires:new Date(e.tokenExpires),portal:e.portal,ssl:e.ssl,tokenDuration:e.tokenDuration,redirectUri:e.redirectUri,refreshTokenTTL:e.refreshTokenTTL})},g.fromCredential=function(e){var t=void 0===e.ssl||e.ssl,r=e.expires||Date.now()+72e5;return new g({portal:e.server.includes("sharing/rest")?e.server:e.server+"/sharing/rest",ssl:t,token:e.token,username:e.userId,tokenExpires:new Date(r)})},g.parentMessageHandler=function(e){if("arcgis:auth:credential"===e.data.type)return g.fromCredential(e.data.credential);if("arcgis:auth:error"!==e.data.type)throw new Error("Unknown message type.");var t=new Error(e.data.error.message);throw t.name=e.data.error.name,t},g.prototype.toCredential=function(){return{expires:this.tokenExpires.getTime(),server:this.portal,ssl:this.ssl,token:this.token,userId:this.username}},g.prototype.getUser=function(e){var t=this;if(this._pendingUserRequest)return this._pendingUserRequest;if(this._user)return Promise.resolve(this._user);var r=this.portal+"/community/self",e=k(k({httpMethod:"GET",authentication:this},e),{rawResponse:!1});return this._pendingUserRequest=f.request(r,e).then(function(e){return t._user=e,t._pendingUserRequest=null,e}),this._pendingUserRequest},g.prototype.getPortal=function(e){var t=this;if(this._pendingPortalRequest)return this._pendingPortalRequest;if(this._portalInfo)return Promise.resolve(this._portalInfo);var r=this.portal+"/portals/self",e=k(k({httpMethod:"GET",authentication:this},e),{rawResponse:!1});return this._pendingPortalRequest=f.request(r,e).then(function(e){return t._portalInfo=e,t._pendingPortalRequest=null,e}),this._pendingPortalRequest},g.prototype.getUsername=function(){return this.username?Promise.resolve(this.username):this._user?Promise.resolve(this._user.username):this.getUser().then(function(e){return e.username})},g.prototype.getToken=function(e,t){return r=this.portal,n=e,s=h(r),o=h(n),r=u(r),n=u(n),s&&o&&r===n||new RegExp(this.portal,"i").test(e)?this.getFreshToken(t):this.getTokenForServer(e,t);var r,n,s,o},g.prototype.validateAppAccess=function(t){return this.getToken(this.portal).then(function(e){return c(e,t)})},g.prototype.toJSON=function(){return{clientId:this.clientId,refreshToken:this.refreshToken,refreshTokenExpires:this.refreshTokenExpires,username:this.username,password:this.password,token:this.token,tokenExpires:this.tokenExpires,portal:this.portal,ssl:this.ssl,tokenDuration:this.tokenDuration,redirectUri:this.redirectUri,refreshTokenTTL:this.refreshTokenTTL}},g.prototype.serialize=function(){return JSON.stringify(this)},g.prototype.enablePostMessageAuth=function(e,t){!t&&window&&(t=window),this._hostHandler=this.createPostMessageHandler(e),t.addEventListener("message",this._hostHandler,!1)},g.prototype.disablePostMessageAuth=function(e){!e&&window&&(e=window),e.removeEventListener("message",this._hostHandler,!1)},g.prototype.refreshSession=function(e){return this._user=null,this.username&&this.password?this.refreshWithUsernameAndPassword(e):this.clientId&&this.refreshToken?this.refreshWithRefreshToken():Promise.reject(new f.ArcGISAuthError("Unable to refresh token."))},g.prototype.getServerRootUrl=function(e){var t=f.cleanUrl(e).split(/\/rest(\/admin)?\/services(?:\/|#|\?|$)/)[0].match(/(https?:\/\/)(.+)/),r=(t[0],t[1]),e=t[2].split("/"),t=e[0],e=e.slice(1);return""+r+t.toLowerCase()+"/"+e.join("/")},g.prototype.getDomainCredentials=function(t){return this.trustedDomains&&this.trustedDomains.length&&this.trustedDomains.some(function(e){return t.startsWith(e)})?"include":"same-origin"},g.prototype.createPostMessageHandler=function(s){var o=this;return function(e){var t=-1<s.indexOf(e.origin),r="arcgis:auth:requestCredential"===e.data.type,n=o.tokenExpires.getTime()>Date.now();t&&r&&(r={},r=n?((n=o.toCredential()).server=n.server.replace("/sharing/rest",""),{type:"arcgis:auth:credential",credential:n}):{type:"arcgis:auth:error",error:{name:"tokenExpiredError",message:"Session token was expired, and not returned to the child application"}},e.source.postMessage(r,e.origin))}},g.prototype.getTokenForServer=function(t,r){var n=this,s=this.getServerRootUrl(t),e=this.federatedServers[s];return e&&e.expires&&e.expires.getTime()>Date.now()?Promise.resolve(e.token):this._pendingTokenRequests[s]||(this._pendingTokenRequests[s]=this.fetchAuthorizedDomains().then(function(){return f.request(s+"/rest/info",{credentials:n.getDomainCredentials(t)}).then(function(e){if(e.owningSystemUrl){if(p(e.owningSystemUrl,n.portal))return f.request(e.owningSystemUrl+"/sharing/rest/info",r);throw new f.ArcGISAuthError(t+" is not federated with "+n.portal+".","NOT_FEDERATED")}if(e.authInfo&&void 0!==n.federatedServers[s])return Promise.resolve({authInfo:e.authInfo});throw new f.ArcGISAuthError(t+" is not federated with any portal and is not explicitly trusted.","NOT_FEDERATED")}).then(function(e){return e.authInfo.tokenServicesUrl}).then(function(e){return n.token&&n.tokenExpires.getTime()>Date.now()?o(e,{params:{token:n.token,serverUrl:t,expiration:n.tokenDuration,client:"referer"}}):o(e,{params:{username:n.username,password:n.password,expiration:n.tokenDuration,client:"referer"}}).then(function(e){return n._token=e.token,n._tokenExpires=new Date(e.expires),e})}).then(function(e){return n.federatedServers[s]={expires:new Date(e.expires),token:e.token},delete n._pendingTokenRequests[s],e.token})}),this._pendingTokenRequests[s])},g.prototype.getFreshToken=function(e){var t=this;return this.token&&!this.tokenExpires||this.token&&this.tokenExpires&&this.tokenExpires.getTime()>Date.now()?Promise.resolve(this.token):(this._pendingTokenRequests[this.portal]||(this._pendingTokenRequests[this.portal]=this.refreshSession(e).then(function(e){return t._pendingTokenRequests[t.portal]=null,e.token})),this._pendingTokenRequests[this.portal])},g.prototype.refreshWithUsernameAndPassword=function(e){var t=this,e=k({params:{username:this.username,password:this.password,expiration:this.tokenDuration}},e);return o(this.portal+"/generateToken",e).then(function(e){return t._token=e.token,t._tokenExpires=new Date(e.expires),t})},g.prototype.refreshWithRefreshToken=function(e){var t=this;if(this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now())return this.refreshRefreshToken(e);e=k({params:{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}},e);return i(this.portal+"/oauth2/token",e).then(function(e){return t._token=e.token,t._tokenExpires=e.expires,t})},g.prototype.refreshRefreshToken=function(e){var t=this,e=k({params:{client_id:this.clientId,refresh_token:this.refreshToken,redirect_uri:this.redirectUri,grant_type:"exchange_refresh_token"}},e);return i(this.portal+"/oauth2/token",e).then(function(e){return t._token=e.token,t._tokenExpires=e.expires,t._refreshToken=e.refreshToken,t._refreshTokenExpires=new Date(Date.now()+60*(t.refreshTokenTTL-1)*1e3),t})},g.prototype.fetchAuthorizedDomains=function(){var t=this;return this.server||!this.portal?Promise.resolve(this):this.getPortal().then(function(e){return e.authorizedCrossOriginDomains&&e.authorizedCrossOriginDomains.length&&(t.trustedDomains=e.authorizedCrossOriginDomains.filter(function(e){return!e.startsWith("http://")}).map(function(e){return e.startsWith("https://")?e:"https://"+e})),t})},g);function g(e){var t;this.clientId=e.clientId,this._refreshToken=e.refreshToken,this._refreshTokenExpires=e.refreshTokenExpires,this.username=e.username,this.password=e.password,this._token=e.token,this._tokenExpires=e.tokenExpires,this.portal=e.portal?f.cleanUrl(e.portal):"https://www.arcgis.com/sharing/rest",this.ssl=e.ssl,this.provider=e.provider||"arcgis",this.tokenDuration=e.tokenDuration||20160,this.redirectUri=e.redirectUri,this.refreshTokenTTL=e.refreshTokenTTL||20160,this.server=e.server,this.federatedServers={},this.trustedDomains=[],e.server&&(t=this.getServerRootUrl(e.server),this.federatedServers[t]={token:e.token,expires:e.tokenExpires}),this._pendingTokenRequests={}}e.ApiKey=n,e.ApplicationSession=t,e.UserSession=l,e.exchangeToken=function(e,t,r){return void 0===r&&(r="https://www.arcgis.com/sharing/rest"),r+="/oauth2/exchangeToken",e={method:"POST",params:{f:"json",client_id:t,token:e}},f.request(r,e).then(function(e){return e.token})},e.fetchToken=i,e.generateToken=o,e.platformSelf=function(e,t,r){return void 0===r&&(r="https://www.arcgis.com/sharing/rest"),r+="/oauth2/platformSelf?f=json",t={method:"POST",headers:{"X-Esri-Auth-Client-Id":e,"X-Esri-Auth-Redirect-Uri":t},params:{f:"json"}},f.request(r,t)},e.validateAppAccess=c,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=auth.umd.min.js.map |
{ | ||
"name": "@esri/arcgis-rest-auth", | ||
"version": "3.4.2", | ||
"version": "3.4.3", | ||
"description": "Authentication helpers for @esri/arcgis-rest-js.", | ||
@@ -16,7 +16,7 @@ "main": "dist/node/index.js", | ||
"dependencies": { | ||
"@esri/arcgis-rest-types": "^3.4.2", | ||
"@esri/arcgis-rest-types": "^3.4.3", | ||
"tslib": "^1.13.0" | ||
}, | ||
"devDependencies": { | ||
"@esri/arcgis-rest-request": "^3.4.2" | ||
"@esri/arcgis-rest-request": "^3.4.3" | ||
}, | ||
@@ -23,0 +23,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
433938
4619