Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@esri/arcgis-rest-auth

Package Overview
Dependencies
Maintainers
8
Versions
118
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@esri/arcgis-rest-auth - npm Package Compare versions

Comparing version 2.24.0 to 2.25.0

41

dist/esm/app-tokens.d.ts

@@ -21,5 +21,19 @@ /**

export declare function exchangeToken(token: string, clientId: string, portal?: string): Promise<string>;
/**
* Response from the `platformSelf(...)` function.
*/
export interface IPlatformSelfResponse {
/**
* Username of the user the encrypted cookie was issued for
*/
username: string;
/**
* Token the consuming application can use, It is tied to the
* clientId used in the `platformSelf` call
*/
token: string;
/**
* Token expiration, in seconds-from-now
*/
expires_in: number;
}

@@ -43,2 +57,29 @@ /**

* an ArcGIS Enterprise installation. Custom applications can not use this.
*
* ```js
* // convert the encrypted platform cookie into a UserSession
* import { platformSelf, UserSession } from '@esri/arcgis-rest-auth';
*
* const portal = 'https://www.arcgis.com/sharing/rest';
* const clientId = 'YOURAPPCLIENID';
*
* // exchange esri_aopc cookie
* return platformSelf(clientId, 'https://your-app-redirect-uri', portal)
* .then((response) => {
* const currentTimestamp = new Date().getTime();
* const tokenExpiresTimestamp = currentTimestamp + (response.expires_in * 1000);
* // Construct the session and return it
* return new UserSession({
* portal,
* clientId,
* username: response.username,
* token: response.token,
* tokenExpires: new Date(tokenExiresTimestamp),
* ssl: true
* });
* })
*
* ```
*
*
* @param clientId

@@ -45,0 +86,0 @@ * @param redirectUri

@@ -54,2 +54,29 @@ /* Copyright (c) 2018-2020 Environmental Systems Research Institute, Inc.

* an ArcGIS Enterprise installation. Custom applications can not use this.
*
* ```js
* // convert the encrypted platform cookie into a UserSession
* import { platformSelf, UserSession } from '@esri/arcgis-rest-auth';
*
* const portal = 'https://www.arcgis.com/sharing/rest';
* const clientId = 'YOURAPPCLIENID';
*
* // exchange esri_aopc cookie
* return platformSelf(clientId, 'https://your-app-redirect-uri', portal)
* .then((response) => {
* const currentTimestamp = new Date().getTime();
* const tokenExpiresTimestamp = currentTimestamp + (response.expires_in * 1000);
* // Construct the session and return it
* return new UserSession({
* portal,
* clientId,
* username: response.username,
* token: response.token,
* tokenExpires: new Date(tokenExiresTimestamp),
* ssl: true
* });
* })
*
* ```
*
*
* @param clientId

@@ -56,0 +83,0 @@ * @param redirectUri

1

dist/esm/index.d.ts

@@ -8,1 +8,2 @@ export * from "./ApplicationSession";

export * from "./app-tokens";
export * from "./validate-app-access";

@@ -9,2 +9,3 @@ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.

export * from "./app-tokens";
export * from "./validate-app-access";
//# sourceMappingURL=index.js.map

@@ -58,2 +58,29 @@ "use strict";

* an ArcGIS Enterprise installation. Custom applications can not use this.
*
* ```js
* // convert the encrypted platform cookie into a UserSession
* import { platformSelf, UserSession } from '@esri/arcgis-rest-auth';
*
* const portal = 'https://www.arcgis.com/sharing/rest';
* const clientId = 'YOURAPPCLIENID';
*
* // exchange esri_aopc cookie
* return platformSelf(clientId, 'https://your-app-redirect-uri', portal)
* .then((response) => {
* const currentTimestamp = new Date().getTime();
* const tokenExpiresTimestamp = currentTimestamp + (response.expires_in * 1000);
* // Construct the session and return it
* return new UserSession({
* portal,
* clientId,
* username: response.username,
* token: response.token,
* tokenExpires: new Date(tokenExiresTimestamp),
* ssl: true
* });
* })
*
* ```
*
*
* @param clientId

@@ -60,0 +87,0 @@ * @param redirectUri

@@ -12,2 +12,3 @@ "use strict";

tslib_1.__exportStar(require("./app-tokens"), exports);
tslib_1.__exportStar(require("./validate-app-access"), exports);
//# sourceMappingURL=index.js.map
/* @preserve
* @esri/arcgis-rest-auth - v2.24.0 - Apache-2.0
* Copyright (c) 2017-2020 Esri, Inc.
* Fri Dec 18 2020 15:28:35 GMT-0800 (Pacific Standard Time)
* @esri/arcgis-rest-auth - v2.25.0 - Apache-2.0
* Copyright (c) 2017-2021 Esri, Inc.
* Mon Jan 11 2021 11:45:51 GMT-0700 (Mountain Standard Time)
*/

@@ -1035,2 +1035,29 @@ (function (global, factory) {

* an ArcGIS Enterprise installation. Custom applications can not use this.
*
* ```js
* // convert the encrypted platform cookie into a UserSession
* import { platformSelf, UserSession } from '@esri/arcgis-rest-auth';
*
* const portal = 'https://www.arcgis.com/sharing/rest';
* const clientId = 'YOURAPPCLIENID';
*
* // exchange esri_aopc cookie
* return platformSelf(clientId, 'https://your-app-redirect-uri', portal)
* .then((response) => {
* const currentTimestamp = new Date().getTime();
* const tokenExpiresTimestamp = currentTimestamp + (response.expires_in * 1000);
* // Construct the session and return it
* return new UserSession({
* portal,
* clientId,
* username: response.username,
* token: response.token,
* tokenExpires: new Date(tokenExiresTimestamp),
* ssl: true
* });
* })
*
* ```
*
*
* @param clientId

@@ -1066,2 +1093,3 @@ * @param redirectUri

exports.platformSelf = platformSelf;
exports.validateAppAccess = validateAppAccess;

@@ -1068,0 +1096,0 @@ Object.defineProperty(exports, '__esModule', { value: true });

8

dist/umd/auth.umd.min.js
/* @preserve
* @esri/arcgis-rest-auth - v2.24.0 - Apache-2.0
* Copyright (c) 2017-2020 Esri, Inc.
* Fri Dec 18 2020 15:28:38 GMT-0800 (Pacific Standard Time)
* @esri/arcgis-rest-auth - v2.25.0 - Apache-2.0
* Copyright (c) 2017-2021 Esri, Inc.
* Mon Jan 11 2021 11:45:55 GMT-0700 (Mountain Standard 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,g){"use strict";var w=function(){return(w=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 a(e,t){var r=t;return r.rawResponse=!1,g.request(e,r).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,r=w({params:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials",expiration:this.duration}},e);return a(this.portal+"/oauth2/token/",r).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}function o(e,t){var r=t;return"undefined"!=typeof window&&window.location&&window.location.host?r.params.referer=window.location.host:r.params.referer=g.NODEJS_DEFAULT_REFERER_HEADER,g.request(e,r)}var s=/^https?:\/\/(\S+)\.arcgis\.com.+/;function h(e){return s.test(e)}function u(e){if(!s.test(e))return null;var t=e.match(s)[1].split(".").pop();return t.includes("dev")?"dev":t.includes("qa")?"qa":"production"}function i(e,t){var r=g.cleanUrl(function(e){if(!s.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?:\/\//,""),n=g.cleanUrl(e).replace(/https?:\/\//,"");return new RegExp(n,"i").test(r)}var n=(Object.defineProperty(m.prototype,"token",{get:function(){return this._token},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"tokenExpires",{get:function(){return this._tokenExpires},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"refreshToken",{get:function(){return this._refreshToken},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"refreshTokenExpires",{get:function(){return this._refreshTokenExpires},enumerable:!1,configurable:!0}),m.beginOAuth2=function(e,t){void 0===t&&(t=window);var r=w({portal:"https://www.arcgis.com/sharing/rest",provider:"arcgis",duration:20160,popup:!0,popupWindowFeatures:"height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes",state:e.clientId,locale:""},e),s=r.portal,n=r.provider,o=r.clientId,i=r.duration,a=r.redirectUri,h=r.popup,u=r.popupWindowFeatures,p=r.state,c=r.locale,l=r.params,d="arcgis"===n?s+"/oauth2/authorize?client_id="+o+"&response_type=token&expiration="+i+"&redirect_uri="+encodeURIComponent(a)+"&state="+p+"&locale="+c:s+"/oauth2/social/authorize?client_id="+o+"&socialLoginProviderName="+n+"&autoAccountCreateForSocial=true&response_type=token&expiration="+i+"&redirect_uri="+encodeURIComponent(a)+"&state="+p+"&locale="+c;if(l&&(d=d+"&"+g.encodeQueryString(l)),h){var f,k=((f={promise:null,resolve:null,reject:null}).promise=new Promise(function(e,t){f.resolve=e,f.reject=t}),f);return t["__ESRI_REST_AUTH_HANDLER_"+o]=function(e,t){var r,n;e?(r=JSON.parse(e),k.reject(new g.ArcGISAuthError(r.errorMessage,r.error))):t&&(n=JSON.parse(t),k.resolve(new m({clientId:o,portal:s,ssl:n.ssl,token:n.token,tokenExpires:new Date(n.expires),username:n.username})))},t.open(d,"oauth-window",u),k.promise}t.location.href=d},m.completeOAuth2=function(e,s){void 0===s&&(s=window);var t=w({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 g.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 g.ArcGISAuthError(e.errorMessage,e.error);return new m({clientId:i,portal:o,ssl:t.ssl,token:t.token,tokenExpires:t.expires,username:t.username})}var n=g.decodeQueryString(s.location.hash);if(!n.access_token){var h=void 0,u="Unknown error";return n.error&&(h=n.error,u=n.error_description),r({error:h,errorMessage:u})}var p=n.access_token,c=new Date(Date.now()+1e3*parseInt(n.expires_in,10)-6e4),l=n.username;return r(void 0,{token:p,expires:c,ssl:"true"===n.ssl,username:l})},m.fromParent=function(n,s){var o;return!s&&window&&(s=window),new Promise(function(t,r){o=function(e){if(e.origin===n)try{return t(m.parentMessageHandler(e))}catch(e){return r(e)}},s.addEventListener("message",o,!1),s.parent.postMessage({type:"arcgis:auth:requestCredential"},n)}).then(function(e){return s.removeEventListener("message",o,!1),e})},m.authorize=function(e,t){var r=w({portal:"https://arcgis.com/sharing/rest",duration:20160},e),n=r.portal,s=r.clientId,o=r.duration,i=r.redirectUri;t.writeHead(301,{Location:n+"/oauth2/authorize?client_id="+s+"&duration="+o+"&response_type=code&redirect_uri="+encodeURIComponent(i)}),t.end()},m.exchangeAuthorizationCode=function(e,t){var r=w({portal:"https://www.arcgis.com/sharing/rest",refreshTokenTTL:1440},e),n=r.portal,s=r.clientId,o=r.redirectUri,i=r.refreshTokenTTL;return a(n+"/oauth2/token",{params:{grant_type:"authorization_code",client_id:s,redirect_uri:o,code:t}}).then(function(e){return new m({clientId:s,portal:n,ssl:e.ssl,redirectUri:o,refreshToken:e.refreshToken,refreshTokenTTL:i,refreshTokenExpires:new Date(Date.now()+1e3*(i-1)),token:e.token,tokenExpires:e.expires,username:e.username})})},m.deserialize=function(e){var t=JSON.parse(e);return new m({clientId:t.clientId,refreshToken:t.refreshToken,refreshTokenExpires:new Date(t.refreshTokenExpires),username:t.username,password:t.password,token:t.token,tokenExpires:new Date(t.tokenExpires),portal:t.portal,ssl:t.ssl,tokenDuration:t.tokenDuration,redirectUri:t.redirectUri,refreshTokenTTL:t.refreshTokenTTL})},m.fromCredential=function(e){return new m({portal:e.server.includes("sharing/rest")?e.server:e.server+"/sharing/rest",ssl:e.ssl,token:e.token,username:e.userId,tokenExpires:new Date(e.expires)})},m.parentMessageHandler=function(e){if("arcgis:auth:credential"===e.data.type)return m.fromCredential(e.data.credential);throw"arcgis:auth:rejected"===e.data.type?new Error(e.data.message):new Error("Unknown message type.")},m.prototype.toCredential=function(){return{expires:this.tokenExpires.getTime(),server:this.portal,ssl:this.ssl,token:this.token,userId:this.username}},m.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",n=w(w({httpMethod:"GET",authentication:this},e),{rawResponse:!1});return this._pendingUserRequest=g.request(r,n).then(function(e){return t._user=e,t._pendingUserRequest=null,e}),this._pendingUserRequest},m.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})},m.prototype.getToken=function(e,t){return r=this.portal,n=e,s=h(r),o=h(n),i=u(r),a=u(n),s&&o&&i===a||new RegExp(this.portal,"i").test(e)?this.getFreshToken(t):this.getTokenForServer(e,t);var r,n,s,o,i,a},m.prototype.validateAppAccess=function(t){return this.getToken(this.portal).then(function(e){return function(e,t,r){void 0===r&&(r="https://www.arcgis.com/sharing/rest");var n=r+"/oauth2/validateAppAccess",s={method:"POST",params:{f:"json",client_id:t,token:e}};return g.request(n,s)}(e,t)})},m.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}},m.prototype.serialize=function(){return JSON.stringify(this)},m.prototype.enablePostMessageAuth=function(e,t){!t&&window&&(t=window),this._hostHandler=this.createPostMessageHandler(e),t.addEventListener("message",this._hostHandler,!1)},m.prototype.disablePostMessageAuth=function(e){!e&&window&&(e=window),e.removeEventListener("message",this._hostHandler,!1)},m.prototype.refreshSession=function(e){return this._user=null,this.username&&this.password?this.refreshWithUsernameAndPassword(e):this.clientId&&this.refreshToken?this.refreshWithRefreshToken():Promise.reject(new g.ArcGISAuthError("Unable to refresh token."))},m.prototype.getServerRootUrl=function(e){var t=g.cleanUrl(e).split(/\/rest(\/admin)?\/services(?:\/|#|\?|$)/)[0].match(/(https?:\/\/)(.+)/),r=(t[0],t[1]),n=t[2].split("/"),s=n[0],o=n.slice(1);return""+r+s.toLowerCase()+"/"+o.join("/")},m.prototype.createPostMessageHandler=function(r){var n=this;return function(e){var t;-1<r.indexOf(e.origin)?(t=n.toCredential(),e.source.postMessage({type:"arcgis:auth:credential",credential:t},e.origin)):e.source.postMessage({type:"arcgis:auth:rejected",message:"Rejected authentication request."},e.origin)}},m.prototype.getTokenForServer=function(t,r){var n=this,s=this.getServerRootUrl(t),e=this.trustedServers[s];return e&&e.expires&&e.expires.getTime()>Date.now()?Promise.resolve(e.token):(this._pendingTokenRequests[s]||(this._pendingTokenRequests[s]=g.request(s+"/rest/info").then(function(e){if(e.owningSystemUrl){if(i(e.owningSystemUrl,n.portal))return g.request(e.owningSystemUrl+"/sharing/rest/info",r);throw new g.ArcGISAuthError(t+" is not federated with "+n.portal+".","NOT_FEDERATED")}if(e.authInfo&&void 0!==n.trustedServers[s])return Promise.resolve({authInfo:e.authInfo});throw new g.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.trustedServers[s]={expires:new Date(e.expires),token:e.token},delete n._pendingTokenRequests[s],e.token})),this._pendingTokenRequests[s])},m.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])},m.prototype.refreshWithUsernameAndPassword=function(e){var t=this,r=w({params:{username:this.username,password:this.password,expiration:this.tokenDuration}},e);return o(this.portal+"/generateToken",r).then(function(e){return t._token=e.token,t._tokenExpires=new Date(e.expires),t})},m.prototype.refreshWithRefreshToken=function(e){var t=this;if(this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now())return this.refreshRefreshToken(e);var r=w({params:{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}},e);return a(this.portal+"/oauth2/token",r).then(function(e){return t._token=e.token,t._tokenExpires=e.expires,t})},m.prototype.refreshRefreshToken=function(e){var t=this,r=w({params:{client_id:this.clientId,refresh_token:this.refreshToken,redirect_uri:this.redirectUri,grant_type:"exchange_refresh_token"}},e);return a(this.portal+"/oauth2/token",r).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})},m);function m(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?g.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||1440,this.trustedServers={},e.server&&(t=this.getServerRootUrl(e.server),this.trustedServers[t]={token:e.token,expires:e.tokenExpires}),this._pendingTokenRequests={}}e.ApplicationSession=t,e.UserSession=n,e.exchangeToken=function(e,t,r){void 0===r&&(r="https://www.arcgis.com/sharing/rest");var n=r+"/oauth2/exchangeToken",s={method:"POST",params:{f:"json",client_id:t,token:e}};return g.request(n,s).then(function(e){return e.token})},e.fetchToken=a,e.generateToken=o,e.platformSelf=function(e,t,r){void 0===r&&(r="https://www.arcgis.com/sharing/rest");var n=r+"/oauth2/platformSelf?f=json",s={method:"POST",headers:{"X-Esri-Auth-Client-Id":e,"X-Esri-Auth-Redirect-Uri":t},params:{f:"json"}};return g.request(n,s)},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,g){"use strict";var w=function(){return(w=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 a(e,t){var r=t;return r.rawResponse=!1,g.request(e,r).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,r=w({params:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials",expiration:this.duration}},e);return a(this.portal+"/oauth2/token/",r).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}function o(e,t){var r=t;return"undefined"!=typeof window&&window.location&&window.location.host?r.params.referer=window.location.host:r.params.referer=g.NODEJS_DEFAULT_REFERER_HEADER,g.request(e,r)}var s=/^https?:\/\/(\S+)\.arcgis\.com.+/;function h(e){return s.test(e)}function p(e){if(!s.test(e))return null;var t=e.match(s)[1].split(".").pop();return t.includes("dev")?"dev":t.includes("qa")?"qa":"production"}function i(e,t){var r=g.cleanUrl(function(e){if(!s.test(e))return e;switch(p(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?:\/\//,""),n=g.cleanUrl(e).replace(/https?:\/\//,"");return new RegExp(n,"i").test(r)}function n(e,t,r){void 0===r&&(r="https://www.arcgis.com/sharing/rest");var n=r+"/oauth2/validateAppAccess",s={method:"POST",params:{f:"json",client_id:t,token:e}};return g.request(n,s)}var u=(Object.defineProperty(m.prototype,"token",{get:function(){return this._token},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"tokenExpires",{get:function(){return this._tokenExpires},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"refreshToken",{get:function(){return this._refreshToken},enumerable:!1,configurable:!0}),Object.defineProperty(m.prototype,"refreshTokenExpires",{get:function(){return this._refreshTokenExpires},enumerable:!1,configurable:!0}),m.beginOAuth2=function(e,t){void 0===t&&(t=window);var r=w({portal:"https://www.arcgis.com/sharing/rest",provider:"arcgis",duration:20160,popup:!0,popupWindowFeatures:"height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes",state:e.clientId,locale:""},e),s=r.portal,n=r.provider,o=r.clientId,i=r.duration,a=r.redirectUri,h=r.popup,p=r.popupWindowFeatures,u=r.state,c=r.locale,l=r.params,d="arcgis"===n?s+"/oauth2/authorize?client_id="+o+"&response_type=token&expiration="+i+"&redirect_uri="+encodeURIComponent(a)+"&state="+u+"&locale="+c:s+"/oauth2/social/authorize?client_id="+o+"&socialLoginProviderName="+n+"&autoAccountCreateForSocial=true&response_type=token&expiration="+i+"&redirect_uri="+encodeURIComponent(a)+"&state="+u+"&locale="+c;if(l&&(d=d+"&"+g.encodeQueryString(l)),h){var f,k=((f={promise:null,resolve:null,reject:null}).promise=new Promise(function(e,t){f.resolve=e,f.reject=t}),f);return t["__ESRI_REST_AUTH_HANDLER_"+o]=function(e,t){var r,n;e?(r=JSON.parse(e),k.reject(new g.ArcGISAuthError(r.errorMessage,r.error))):t&&(n=JSON.parse(t),k.resolve(new m({clientId:o,portal:s,ssl:n.ssl,token:n.token,tokenExpires:new Date(n.expires),username:n.username})))},t.open(d,"oauth-window",p),k.promise}t.location.href=d},m.completeOAuth2=function(e,s){void 0===s&&(s=window);var t=w({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 g.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 g.ArcGISAuthError(e.errorMessage,e.error);return new m({clientId:i,portal:o,ssl:t.ssl,token:t.token,tokenExpires:t.expires,username:t.username})}var n=g.decodeQueryString(s.location.hash);if(!n.access_token){var h=void 0,p="Unknown error";return n.error&&(h=n.error,p=n.error_description),r({error:h,errorMessage:p})}var u=n.access_token,c=new Date(Date.now()+1e3*parseInt(n.expires_in,10)-6e4),l=n.username;return r(void 0,{token:u,expires:c,ssl:"true"===n.ssl,username:l})},m.fromParent=function(n,s){var o;return!s&&window&&(s=window),new Promise(function(t,r){o=function(e){if(e.origin===n)try{return t(m.parentMessageHandler(e))}catch(e){return r(e)}},s.addEventListener("message",o,!1),s.parent.postMessage({type:"arcgis:auth:requestCredential"},n)}).then(function(e){return s.removeEventListener("message",o,!1),e})},m.authorize=function(e,t){var r=w({portal:"https://arcgis.com/sharing/rest",duration:20160},e),n=r.portal,s=r.clientId,o=r.duration,i=r.redirectUri;t.writeHead(301,{Location:n+"/oauth2/authorize?client_id="+s+"&duration="+o+"&response_type=code&redirect_uri="+encodeURIComponent(i)}),t.end()},m.exchangeAuthorizationCode=function(e,t){var r=w({portal:"https://www.arcgis.com/sharing/rest",refreshTokenTTL:1440},e),n=r.portal,s=r.clientId,o=r.redirectUri,i=r.refreshTokenTTL;return a(n+"/oauth2/token",{params:{grant_type:"authorization_code",client_id:s,redirect_uri:o,code:t}}).then(function(e){return new m({clientId:s,portal:n,ssl:e.ssl,redirectUri:o,refreshToken:e.refreshToken,refreshTokenTTL:i,refreshTokenExpires:new Date(Date.now()+1e3*(i-1)),token:e.token,tokenExpires:e.expires,username:e.username})})},m.deserialize=function(e){var t=JSON.parse(e);return new m({clientId:t.clientId,refreshToken:t.refreshToken,refreshTokenExpires:new Date(t.refreshTokenExpires),username:t.username,password:t.password,token:t.token,tokenExpires:new Date(t.tokenExpires),portal:t.portal,ssl:t.ssl,tokenDuration:t.tokenDuration,redirectUri:t.redirectUri,refreshTokenTTL:t.refreshTokenTTL})},m.fromCredential=function(e){return new m({portal:e.server.includes("sharing/rest")?e.server:e.server+"/sharing/rest",ssl:e.ssl,token:e.token,username:e.userId,tokenExpires:new Date(e.expires)})},m.parentMessageHandler=function(e){if("arcgis:auth:credential"===e.data.type)return m.fromCredential(e.data.credential);throw"arcgis:auth:rejected"===e.data.type?new Error(e.data.message):new Error("Unknown message type.")},m.prototype.toCredential=function(){return{expires:this.tokenExpires.getTime(),server:this.portal,ssl:this.ssl,token:this.token,userId:this.username}},m.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",n=w(w({httpMethod:"GET",authentication:this},e),{rawResponse:!1});return this._pendingUserRequest=g.request(r,n).then(function(e){return t._user=e,t._pendingUserRequest=null,e}),this._pendingUserRequest},m.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})},m.prototype.getToken=function(e,t){return r=this.portal,n=e,s=h(r),o=h(n),i=p(r),a=p(n),s&&o&&i===a||new RegExp(this.portal,"i").test(e)?this.getFreshToken(t):this.getTokenForServer(e,t);var r,n,s,o,i,a},m.prototype.validateAppAccess=function(t){return this.getToken(this.portal).then(function(e){return n(e,t)})},m.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}},m.prototype.serialize=function(){return JSON.stringify(this)},m.prototype.enablePostMessageAuth=function(e,t){!t&&window&&(t=window),this._hostHandler=this.createPostMessageHandler(e),t.addEventListener("message",this._hostHandler,!1)},m.prototype.disablePostMessageAuth=function(e){!e&&window&&(e=window),e.removeEventListener("message",this._hostHandler,!1)},m.prototype.refreshSession=function(e){return this._user=null,this.username&&this.password?this.refreshWithUsernameAndPassword(e):this.clientId&&this.refreshToken?this.refreshWithRefreshToken():Promise.reject(new g.ArcGISAuthError("Unable to refresh token."))},m.prototype.getServerRootUrl=function(e){var t=g.cleanUrl(e).split(/\/rest(\/admin)?\/services(?:\/|#|\?|$)/)[0].match(/(https?:\/\/)(.+)/),r=(t[0],t[1]),n=t[2].split("/"),s=n[0],o=n.slice(1);return""+r+s.toLowerCase()+"/"+o.join("/")},m.prototype.createPostMessageHandler=function(r){var n=this;return function(e){var t;-1<r.indexOf(e.origin)?(t=n.toCredential(),e.source.postMessage({type:"arcgis:auth:credential",credential:t},e.origin)):e.source.postMessage({type:"arcgis:auth:rejected",message:"Rejected authentication request."},e.origin)}},m.prototype.getTokenForServer=function(t,r){var n=this,s=this.getServerRootUrl(t),e=this.trustedServers[s];return e&&e.expires&&e.expires.getTime()>Date.now()?Promise.resolve(e.token):(this._pendingTokenRequests[s]||(this._pendingTokenRequests[s]=g.request(s+"/rest/info").then(function(e){if(e.owningSystemUrl){if(i(e.owningSystemUrl,n.portal))return g.request(e.owningSystemUrl+"/sharing/rest/info",r);throw new g.ArcGISAuthError(t+" is not federated with "+n.portal+".","NOT_FEDERATED")}if(e.authInfo&&void 0!==n.trustedServers[s])return Promise.resolve({authInfo:e.authInfo});throw new g.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.trustedServers[s]={expires:new Date(e.expires),token:e.token},delete n._pendingTokenRequests[s],e.token})),this._pendingTokenRequests[s])},m.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])},m.prototype.refreshWithUsernameAndPassword=function(e){var t=this,r=w({params:{username:this.username,password:this.password,expiration:this.tokenDuration}},e);return o(this.portal+"/generateToken",r).then(function(e){return t._token=e.token,t._tokenExpires=new Date(e.expires),t})},m.prototype.refreshWithRefreshToken=function(e){var t=this;if(this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now())return this.refreshRefreshToken(e);var r=w({params:{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}},e);return a(this.portal+"/oauth2/token",r).then(function(e){return t._token=e.token,t._tokenExpires=e.expires,t})},m.prototype.refreshRefreshToken=function(e){var t=this,r=w({params:{client_id:this.clientId,refresh_token:this.refreshToken,redirect_uri:this.redirectUri,grant_type:"exchange_refresh_token"}},e);return a(this.portal+"/oauth2/token",r).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})},m);function m(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?g.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||1440,this.trustedServers={},e.server&&(t=this.getServerRootUrl(e.server),this.trustedServers[t]={token:e.token,expires:e.tokenExpires}),this._pendingTokenRequests={}}e.ApplicationSession=t,e.UserSession=u,e.exchangeToken=function(e,t,r){void 0===r&&(r="https://www.arcgis.com/sharing/rest");var n=r+"/oauth2/exchangeToken",s={method:"POST",params:{f:"json",client_id:t,token:e}};return g.request(n,s).then(function(e){return e.token})},e.fetchToken=a,e.generateToken=o,e.platformSelf=function(e,t,r){void 0===r&&(r="https://www.arcgis.com/sharing/rest");var n=r+"/oauth2/platformSelf?f=json",s={method:"POST",headers:{"X-Esri-Auth-Client-Id":e,"X-Esri-Auth-Redirect-Uri":t},params:{f:"json"}};return g.request(n,s)},e.validateAppAccess=n,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=auth.umd.min.js.map
{
"name": "@esri/arcgis-rest-auth",
"version": "2.24.0",
"version": "2.25.0",
"description": "Authentication helpers for @esri/arcgis-rest-js.",

@@ -16,7 +16,7 @@ "main": "dist/node/index.js",

"dependencies": {
"@esri/arcgis-rest-types": "^2.24.0",
"@esri/arcgis-rest-types": "^2.25.0",
"tslib": "^1.13.0"
},
"devDependencies": {
"@esri/arcgis-rest-request": "^2.24.0"
"@esri/arcgis-rest-request": "^2.25.0"
},

@@ -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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc