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
7
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.1.0 to 2.1.1

dist/esm/federation-utils.d.ts

1

dist/esm/UserSession.d.ts

@@ -78,2 +78,3 @@ /// <reference types="node" />

state?: string;
[key: string]: any;
}

@@ -80,0 +81,0 @@ /**

15

dist/esm/UserSession.js
/* Copyright (c) 2017-2019 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import * as tslib_1 from "tslib";
import { request, ArcGISAuthError, cleanUrl } from "@esri/arcgis-rest-request";
import { request, ArcGISAuthError, cleanUrl, encodeQueryString } from "@esri/arcgis-rest-request";
import { generateToken } from "./generate-token";
import { fetchToken } from "./fetch-token";
import { canUseOnlineToken, isFederated } from "./federation-utils";
function defer() {

@@ -96,3 +97,3 @@ var deferred = {

locale: ""
}, options), portal = _a.portal, provider = _a.provider, clientId = _a.clientId, duration = _a.duration, redirectUri = _a.redirectUri, popup = _a.popup, state = _a.state, locale = _a.locale;
}, options), portal = _a.portal, provider = _a.provider, clientId = _a.clientId, duration = _a.duration, redirectUri = _a.redirectUri, popup = _a.popup, state = _a.state, locale = _a.locale, params = _a.params;
var url;

@@ -105,2 +106,6 @@ if (provider === "arcgis") {

}
// append additional params
if (params) {
url = url + "&" + encodeQueryString(params);
}
if (!popup) {

@@ -364,5 +369,3 @@ win.location.href = url;

UserSession.prototype.getToken = function (url, requestOptions) {
if ((arcgisOnlinePortalRegex.test(this.portal) ||
arcgisOnlineOrgPortalRegex.test(this.portal)) &&
arcgisOnlineUrlRegex.test(url)) {
if (canUseOnlineToken(this.portal, url)) {
return this.getFreshToken(requestOptions);

@@ -449,3 +452,3 @@ }

*/
if (!new RegExp(response.owningSystemUrl, "i").test(_this.portal)) {
if (!isFederated(response.owningSystemUrl, _this.portal)) {
throw new ArcGISAuthError(url + " is not federated with " + _this.portal + ".", "NOT_FEDERATED");

@@ -452,0 +455,0 @@ }

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

var fetch_token_1 = require("./fetch-token");
var federation_utils_1 = require("./federation-utils");
function defer() {

@@ -99,3 +100,3 @@ var deferred = {

locale: ""
}, options), portal = _a.portal, provider = _a.provider, clientId = _a.clientId, duration = _a.duration, redirectUri = _a.redirectUri, popup = _a.popup, state = _a.state, locale = _a.locale;
}, options), portal = _a.portal, provider = _a.provider, clientId = _a.clientId, duration = _a.duration, redirectUri = _a.redirectUri, popup = _a.popup, state = _a.state, locale = _a.locale, params = _a.params;
var url;

@@ -108,2 +109,6 @@ if (provider === "arcgis") {

}
// append additional params
if (params) {
url = url + "&" + arcgis_rest_request_1.encodeQueryString(params);
}
if (!popup) {

@@ -367,5 +372,3 @@ win.location.href = url;

UserSession.prototype.getToken = function (url, requestOptions) {
if ((arcgisOnlinePortalRegex.test(this.portal) ||
arcgisOnlineOrgPortalRegex.test(this.portal)) &&
arcgisOnlineUrlRegex.test(url)) {
if (federation_utils_1.canUseOnlineToken(this.portal, url)) {
return this.getFreshToken(requestOptions);

@@ -452,3 +455,3 @@ }

*/
if (!new RegExp(response.owningSystemUrl, "i").test(_this.portal)) {
if (!federation_utils_1.isFederated(response.owningSystemUrl, _this.portal)) {
throw new arcgis_rest_request_1.ArcGISAuthError(url + " is not federated with " + _this.portal + ".", "NOT_FEDERATED");

@@ -455,0 +458,0 @@ }

/* @preserve
* @esri/arcgis-rest-auth - v2.1.0 - Apache-2.0
* @esri/arcgis-rest-auth - v2.1.1 - Apache-2.0
* Copyright (c) 2017-2019 Esri, Inc.
* Tue Jun 18 2019 11:19:39 GMT-0700 (PDT)
* Mon Jul 15 2019 11:50:03 GMT-0700 (PDT)
*/

@@ -132,2 +132,52 @@ (function (global, factory) {

/**
* Used to test if a URL is an ArcGIS Online URL
*/
var arcgisOnlineUrlRegex = /^https?:\/\/(\S+)\.arcgis\.com.+/;
function isOnline(url) {
return arcgisOnlineUrlRegex.test(url);
}
function normalizeOnlinePortalUrl(portalUrl) {
if (!arcgisOnlineUrlRegex.test(portalUrl)) {
return portalUrl;
}
switch (getOnlineEnvironment(portalUrl)) {
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";
}
}
function getOnlineEnvironment(url) {
if (!arcgisOnlineUrlRegex.test(url)) {
return null;
}
var match = url.match(arcgisOnlineUrlRegex);
var subdomain = match[1].split(".").pop();
if (subdomain.includes("dev")) {
return "dev";
}
if (subdomain.includes("qa")) {
return "qa";
}
return "production";
}
function isFederated(owningSystemUrl, portalUrl) {
var normalizedPortalUrl = arcgisRestRequest.cleanUrl(normalizeOnlinePortalUrl(portalUrl)).replace(/https?:\/\//, "");
var normalizedOwningSystemUrl = arcgisRestRequest.cleanUrl(owningSystemUrl).replace(/https?:\/\//, "");
return new RegExp(normalizedOwningSystemUrl, "i").test(normalizedPortalUrl);
}
function canUseOnlineToken(portalUrl, requestUrl) {
var portalIsOnline = isOnline(portalUrl);
var requestIsOnline = isOnline(requestUrl);
var portalEnv = getOnlineEnvironment(portalUrl);
var requestEnv = getOnlineEnvironment(requestUrl);
if (portalIsOnline && requestIsOnline && portalEnv === requestEnv) {
return true;
}
return false;
}
/* Copyright (c) 2017-2019 Environmental Systems Research Institute, Inc.

@@ -148,14 +198,2 @@ * Apache-2.0 */

/**
* Used to test if a URL is an ArcGIS Online URL
*/
var arcgisOnlineUrlRegex = /^https?:\/\/\S+\.arcgis\.com.+/;
/**
* Used to test if a URL is production ArcGIS Online Portal
*/
var arcgisOnlinePortalRegex = /^https?:\/\/www\.arcgis\.com\/sharing\/rest+/;
/**
* Used to test if a URL is an ArcGIS Online Organization Portal
*/
var arcgisOnlineOrgPortalRegex = /^https?:\/\/(?:[a-z0-9-]+\.maps)?.\arcgis\.com\/sharing\/rest/;
/**
* ```js

@@ -225,3 +263,3 @@ * import { UserSession } from '@esri/arcgis-rest-auth';

locale: ""
}, options), portal = _a.portal, provider = _a.provider, clientId = _a.clientId, duration = _a.duration, redirectUri = _a.redirectUri, popup = _a.popup, state = _a.state, locale = _a.locale;
}, options), portal = _a.portal, provider = _a.provider, clientId = _a.clientId, duration = _a.duration, redirectUri = _a.redirectUri, popup = _a.popup, state = _a.state, locale = _a.locale, params = _a.params;
var url;

@@ -234,2 +272,6 @@ if (provider === "arcgis") {

}
// append additional params
if (params) {
url = url + "&" + arcgisRestRequest.encodeQueryString(params);
}
if (!popup) {

@@ -493,5 +535,3 @@ win.location.href = url;

UserSession.prototype.getToken = function (url, requestOptions) {
if ((arcgisOnlinePortalRegex.test(this.portal) ||
arcgisOnlineOrgPortalRegex.test(this.portal)) &&
arcgisOnlineUrlRegex.test(url)) {
if (canUseOnlineToken(this.portal, url)) {
return this.getFreshToken(requestOptions);

@@ -578,3 +618,3 @@ }

*/
if (!new RegExp(response.owningSystemUrl, "i").test(_this.portal)) {
if (!isFederated(response.owningSystemUrl, _this.portal)) {
throw new arcgisRestRequest.ArcGISAuthError(url + " is not federated with " + _this.portal + ".", "NOT_FEDERATED");

@@ -581,0 +621,0 @@ }

/* @preserve
* @esri/arcgis-rest-auth - v2.1.0 - Apache-2.0
* @esri/arcgis-rest-auth - v2.1.1 - Apache-2.0
* Copyright (c) 2017-2019 Esri, Inc.
* Tue Jun 18 2019 11:19:42 GMT-0700 (PDT)
* Mon Jul 15 2019 11:50:08 GMT-0700 (PDT)
*/
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@esri/arcgis-rest-request")):"function"==typeof define&&define.amd?define(["exports","@esri/arcgis-rest-request"],r):r((e=e||self).arcgisRest=e.arcgisRest||{},e.arcgisRest)}(this,function(e,k){"use strict";var d=function(){return(d=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var s in r=arguments[t])Object.prototype.hasOwnProperty.call(r,s)&&(e[s]=r[s]);return e}).apply(this,arguments)};function a(e,r){var t=r;return t.rawResponse=!1,k.request(e,t).then(function(e){var r={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&&(r.refreshToken=e.refresh_token),r})}var r=function(){function e(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}return e.prototype.getToken=function(e,r){return this.token&&this.expires&&this.expires.getTime()>Date.now()?Promise.resolve(this.token):(this._pendingTokenRequest||(this._pendingTokenRequest=this.refreshToken(r)),this._pendingTokenRequest)},e.prototype.refreshToken=function(e){var r=this,t=d({params:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials",expiration:this.duration}},e);return a(this.portal+"/oauth2/token/",t).then(function(e){return r._pendingTokenRequest=null,r.token=e.token,r.expires=e.expires,e.token})},e.prototype.refreshSession=function(){var e=this;return this.refreshToken().then(function(){return e})},e}();function o(e,r){var t=r;return"undefined"!=typeof window&&window.location&&window.location.host?t.params.referer=window.location.host:t.params.referer=k.NODEJS_DEFAULT_REFERER_HEADER,k.request(e,t)}var t=/^https?:\/\/\S+\.arcgis\.com.+/,n=/^https?:\/\/www\.arcgis\.com\/sharing\/rest+/,s=/^https?:\/\/(?:[a-z0-9-]+\.maps)?.\arcgis\.com\/sharing\/rest/,i=function(){function f(e){if(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?k.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){var r=this.getServerRootUrl(e.server);this.trustedServers[r]={token:e.token,expires:e.tokenExpires}}this._pendingTokenRequests={}}return f.beginOAuth2=function(e,r){void 0===r&&(r=window);var t,n=d({portal:"https://www.arcgis.com/sharing/rest",provider:"arcgis",duration:20160,popup:!0,state:e.clientId,locale:""},e),s=n.portal,o=n.provider,i=n.clientId,a=n.duration,h=n.redirectUri,p=n.popup,u=n.state,c=n.locale;if(t="arcgis"===o?s+"/oauth2/authorize?client_id="+i+"&response_type=token&expiration="+a+"&redirect_uri="+encodeURIComponent(h)+"&state="+u+"&locale="+c:s+"/oauth2/social/authorize?client_id="+i+"&socialLoginProviderName="+o+"&autoAccountCreateForSocial=true&response_type=token&expiration="+a+"&redirect_uri="+encodeURIComponent(h)+"&state="+u+"&locale="+c,p){var l=function(){var t={promise:null,resolve:null,reject:null};return t.promise=new Promise(function(e,r){t.resolve=e,t.reject=r}),t}();return r["__ESRI_REST_AUTH_HANDLER_"+i]=function(e,r){if(e){var t=JSON.parse(e);l.reject(new k.ArcGISAuthError(t.errorMessage,t.error))}else if(r){var n=JSON.parse(r);l.resolve(new f({clientId:i,portal:s,ssl:n.ssl,token:n.token,tokenExpires:new Date(n.expires),username:n.username}))}},r.open(t,"oauth-window","height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"),l.promise}r.location.href=t},f.completeOAuth2=function(e,t){void 0===t&&(t=window);var r=d({portal:"https://www.arcgis.com/sharing/rest"},e),n=r.portal,s=r.clientId;function o(e,r){if(t.opener&&t.opener.parent)return t.opener.parent["__ESRI_REST_AUTH_HANDLER_"+s](e?JSON.stringify(e):void 0,JSON.stringify(r)),void t.close();if(t!==t.parent)return t.parent["__ESRI_REST_AUTH_HANDLER_"+s](e?JSON.stringify(e):void 0,JSON.stringify(r)),void t.close();if(e)throw new k.ArcGISAuthError(e.errorMessage,e.error);return new f({clientId:s,portal:n,ssl:r.ssl,token:r.token,tokenExpires:r.expires,username:r.username})}var i=t.location.href.match(/access_token=(.+)&expires_in=(.+)&username=([^&]+)/);if(!i){var a=t.location.href.match(/error=(.+)&error_description=(.+)/);return o({error:a[1],errorMessage:decodeURIComponent(a[2])})}var h=i[1],p=new Date(Date.now()+1e3*parseInt(i[2],10)-6e4),u=decodeURIComponent(i[3]);return o(void 0,{token:h,expires:p,ssl:-1<t.location.href.indexOf("&ssl=true")||-1<t.location.href.indexOf("#ssl=true"),username:u})},f.authorize=function(e,r){var t=d({portal:"https://arcgis.com/sharing/rest",duration:20160},e),n=t.portal,s=t.clientId,o=t.duration,i=t.redirectUri;r.writeHead(301,{Location:n+"/oauth2/authorize?client_id="+s+"&duration="+o+"&response_type=code&redirect_uri="+encodeURIComponent(i)}),r.end()},f.exchangeAuthorizationCode=function(e,r){var t=d({portal:"https://www.arcgis.com/sharing/rest",refreshTokenTTL:1440},e),n=t.portal,s=t.clientId,o=t.redirectUri,i=t.refreshTokenTTL;return a(n+"/oauth2/token",{params:{grant_type:"authorization_code",client_id:s,redirect_uri:o,code:r}}).then(function(e){return new f({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})})},f.deserialize=function(e){var r=JSON.parse(e);return new f({clientId:r.clientId,refreshToken:r.refreshToken,refreshTokenExpires:new Date(r.refreshTokenExpires),username:r.username,password:r.password,token:r.token,tokenExpires:new Date(r.tokenExpires),portal:r.portal,ssl:r.ssl,tokenDuration:r.tokenDuration,redirectUri:r.redirectUri,refreshTokenTTL:r.refreshTokenTTL})},f.fromCredential=function(e){return new f({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)})},Object.defineProperty(f.prototype,"token",{get:function(){return this._token},enumerable:!0,configurable:!0}),Object.defineProperty(f.prototype,"tokenExpires",{get:function(){return this._tokenExpires},enumerable:!0,configurable:!0}),Object.defineProperty(f.prototype,"refreshToken",{get:function(){return this._refreshToken},enumerable:!0,configurable:!0}),Object.defineProperty(f.prototype,"refreshTokenExpires",{get:function(){return this._refreshTokenExpires},enumerable:!0,configurable:!0}),f.prototype.toCredential=function(){return{expires:this.tokenExpires.getTime(),server:this.portal,ssl:this.ssl,token:this.token,userId:this.username}},f.prototype.getUser=function(e){var r=this;if(this._user&&this._user.username===this.username)return Promise.resolve(this._user);var t=this.portal+"/community/users/"+encodeURIComponent(this.username),n=d({httpMethod:"GET",authentication:this},e,{rawResponse:!1});return k.request(t,n).then(function(e){return r._user=e})},f.prototype.getToken=function(e,r){return(n.test(this.portal)||s.test(this.portal))&&t.test(e)?this.getFreshToken(r):new RegExp(this.portal,"i").test(e)?this.getFreshToken(r):this.getTokenForServer(e,r)},f.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}},f.prototype.serialize=function(){return JSON.stringify(this)},f.prototype.refreshSession=function(e){return this._user=null,this.username&&this.password?this.refreshWithUsernameAndPassword(e):this.clientId&&this.refreshToken?this.refreshWithRefreshToken():Promise.reject(new k.ArcGISAuthError("Unable to refresh token."))},f.prototype.getServerRootUrl=function(e){var r=e.split(/\/rest(\/admin)?\/services(?:\/|#|\?|$)/)[0].match(/(https?:\/\/)(.+)/),t=(r[0],r[1]),n=r[2].split("/"),s=n[0],o=n.slice(1);return""+t+s.toLowerCase()+"/"+o.join("/")},f.prototype.getTokenForServer=function(r,t){var n=this,s=this.getServerRootUrl(r),e=this.trustedServers[s];return e&&e.expires&&e.expires.getTime()>Date.now()?Promise.resolve(e.token):(this._pendingTokenRequests[s]||(this._pendingTokenRequests[s]=k.request(s+"/rest/info").then(function(e){if(e.owningSystemUrl){if(new RegExp(e.owningSystemUrl,"i").test(n.portal))return k.request(e.owningSystemUrl+"/sharing/rest/info",t);throw new k.ArcGISAuthError(r+" is not federated with "+n.portal+".","NOT_FEDERATED")}if(e.authInfo&&void 0!==n.trustedServers[s])return Promise.resolve({authInfo:e.authInfo});throw new k.ArcGISAuthError(r+" 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:r,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},e.token})),this._pendingTokenRequests[s])},f.prototype.getFreshToken=function(e){var r=this;return 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 r._pendingTokenRequests[r.portal]=null,e.token})),this._pendingTokenRequests[this.portal])},f.prototype.refreshWithUsernameAndPassword=function(e){var r=this,t=d({params:{username:this.username,password:this.password,expiration:this.tokenDuration}},e);return o(this.portal+"/generateToken",t).then(function(e){return r._token=e.token,r._tokenExpires=new Date(e.expires),r})},f.prototype.refreshWithRefreshToken=function(e){var r=this;if(this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now())return this.refreshRefreshToken(e);var t=d({params:{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}},e);return a(this.portal+"/oauth2/token",t).then(function(e){return r._token=e.token,r._tokenExpires=e.expires,r})},f.prototype.refreshRefreshToken=function(e){var r=this,t=d({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",t).then(function(e){return r._token=e.token,r._tokenExpires=e.expires,r._refreshToken=e.refreshToken,r._refreshTokenExpires=new Date(Date.now()+60*(r.refreshTokenTTL-1)*1e3),r})},f}();e.ApplicationSession=r,e.UserSession=i,e.fetchToken=a,e.generateToken=o,Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@esri/arcgis-rest-request")):"function"==typeof define&&define.amd?define(["exports","@esri/arcgis-rest-request"],r):r((e=e||self).arcgisRest=e.arcgisRest||{},e.arcgisRest)}(this,function(e,d){"use strict";var T=function(){return(T=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var s in r=arguments[t])Object.prototype.hasOwnProperty.call(r,s)&&(e[s]=r[s]);return e}).apply(this,arguments)};function a(e,r){var t=r;return t.rawResponse=!1,d.request(e,t).then(function(e){var r={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&&(r.refreshToken=e.refresh_token),r})}var r=function(){function e(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}return e.prototype.getToken=function(e,r){return this.token&&this.expires&&this.expires.getTime()>Date.now()?Promise.resolve(this.token):(this._pendingTokenRequest||(this._pendingTokenRequest=this.refreshToken(r)),this._pendingTokenRequest)},e.prototype.refreshToken=function(e){var r=this,t=T({params:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials",expiration:this.duration}},e);return a(this.portal+"/oauth2/token/",t).then(function(e){return r._pendingTokenRequest=null,r.token=e.token,r.expires=e.expires,e.token})},e.prototype.refreshSession=function(){var e=this;return this.refreshToken().then(function(){return e})},e}();function o(e,r){var t=r;return"undefined"!=typeof window&&window.location&&window.location.host?t.params.referer=window.location.host:t.params.referer=d.NODEJS_DEFAULT_REFERER_HEADER,d.request(e,t)}var s=/^https?:\/\/(\S+)\.arcgis\.com.+/;function i(e){return s.test(e)}function h(e){if(!s.test(e))return null;var r=e.match(s)[1].split(".").pop();return r.includes("dev")?"dev":r.includes("qa")?"qa":"production"}function u(e,r){var t=d.cleanUrl(function(e){if(!s.test(e))return e;switch(h(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"}}(r)).replace(/https?:\/\//,""),n=d.cleanUrl(e).replace(/https?:\/\//,"");return new RegExp(n,"i").test(t)}var t=function(){function k(e){if(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?d.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){var r=this.getServerRootUrl(e.server);this.trustedServers[r]={token:e.token,expires:e.tokenExpires}}this._pendingTokenRequests={}}return k.beginOAuth2=function(e,r){void 0===r&&(r=window);var t,n=T({portal:"https://www.arcgis.com/sharing/rest",provider:"arcgis",duration:20160,popup:!0,state:e.clientId,locale:""},e),s=n.portal,o=n.provider,i=n.clientId,a=n.duration,h=n.redirectUri,u=n.popup,p=n.state,c=n.locale,l=n.params;if(t="arcgis"===o?s+"/oauth2/authorize?client_id="+i+"&response_type=token&expiration="+a+"&redirect_uri="+encodeURIComponent(h)+"&state="+p+"&locale="+c:s+"/oauth2/social/authorize?client_id="+i+"&socialLoginProviderName="+o+"&autoAccountCreateForSocial=true&response_type=token&expiration="+a+"&redirect_uri="+encodeURIComponent(h)+"&state="+p+"&locale="+c,l&&(t=t+"&"+d.encodeQueryString(l)),u){var f=function(){var t={promise:null,resolve:null,reject:null};return t.promise=new Promise(function(e,r){t.resolve=e,t.reject=r}),t}();return r["__ESRI_REST_AUTH_HANDLER_"+i]=function(e,r){if(e){var t=JSON.parse(e);f.reject(new d.ArcGISAuthError(t.errorMessage,t.error))}else if(r){var n=JSON.parse(r);f.resolve(new k({clientId:i,portal:s,ssl:n.ssl,token:n.token,tokenExpires:new Date(n.expires),username:n.username}))}},r.open(t,"oauth-window","height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"),f.promise}r.location.href=t},k.completeOAuth2=function(e,t){void 0===t&&(t=window);var r=T({portal:"https://www.arcgis.com/sharing/rest"},e),n=r.portal,s=r.clientId;function o(e,r){if(t.opener&&t.opener.parent)return t.opener.parent["__ESRI_REST_AUTH_HANDLER_"+s](e?JSON.stringify(e):void 0,JSON.stringify(r)),void t.close();if(t!==t.parent)return t.parent["__ESRI_REST_AUTH_HANDLER_"+s](e?JSON.stringify(e):void 0,JSON.stringify(r)),void t.close();if(e)throw new d.ArcGISAuthError(e.errorMessage,e.error);return new k({clientId:s,portal:n,ssl:r.ssl,token:r.token,tokenExpires:r.expires,username:r.username})}var i=t.location.href.match(/access_token=(.+)&expires_in=(.+)&username=([^&]+)/);if(!i){var a=t.location.href.match(/error=(.+)&error_description=(.+)/);return o({error:a[1],errorMessage:decodeURIComponent(a[2])})}var h=i[1],u=new Date(Date.now()+1e3*parseInt(i[2],10)-6e4),p=decodeURIComponent(i[3]);return o(void 0,{token:h,expires:u,ssl:-1<t.location.href.indexOf("&ssl=true")||-1<t.location.href.indexOf("#ssl=true"),username:p})},k.authorize=function(e,r){var t=T({portal:"https://arcgis.com/sharing/rest",duration:20160},e),n=t.portal,s=t.clientId,o=t.duration,i=t.redirectUri;r.writeHead(301,{Location:n+"/oauth2/authorize?client_id="+s+"&duration="+o+"&response_type=code&redirect_uri="+encodeURIComponent(i)}),r.end()},k.exchangeAuthorizationCode=function(e,r){var t=T({portal:"https://www.arcgis.com/sharing/rest",refreshTokenTTL:1440},e),n=t.portal,s=t.clientId,o=t.redirectUri,i=t.refreshTokenTTL;return a(n+"/oauth2/token",{params:{grant_type:"authorization_code",client_id:s,redirect_uri:o,code:r}}).then(function(e){return new k({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})})},k.deserialize=function(e){var r=JSON.parse(e);return new k({clientId:r.clientId,refreshToken:r.refreshToken,refreshTokenExpires:new Date(r.refreshTokenExpires),username:r.username,password:r.password,token:r.token,tokenExpires:new Date(r.tokenExpires),portal:r.portal,ssl:r.ssl,tokenDuration:r.tokenDuration,redirectUri:r.redirectUri,refreshTokenTTL:r.refreshTokenTTL})},k.fromCredential=function(e){return new k({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)})},Object.defineProperty(k.prototype,"token",{get:function(){return this._token},enumerable:!0,configurable:!0}),Object.defineProperty(k.prototype,"tokenExpires",{get:function(){return this._tokenExpires},enumerable:!0,configurable:!0}),Object.defineProperty(k.prototype,"refreshToken",{get:function(){return this._refreshToken},enumerable:!0,configurable:!0}),Object.defineProperty(k.prototype,"refreshTokenExpires",{get:function(){return this._refreshTokenExpires},enumerable:!0,configurable:!0}),k.prototype.toCredential=function(){return{expires:this.tokenExpires.getTime(),server:this.portal,ssl:this.ssl,token:this.token,userId:this.username}},k.prototype.getUser=function(e){var r=this;if(this._user&&this._user.username===this.username)return Promise.resolve(this._user);var t=this.portal+"/community/users/"+encodeURIComponent(this.username),n=T({httpMethod:"GET",authentication:this},e,{rawResponse:!1});return d.request(t,n).then(function(e){return r._user=e})},k.prototype.getToken=function(e,r){return function(e,r){var t=i(e),n=i(r),s=h(e),o=h(r);return!(!t||!n||s!==o)}(this.portal,e)?this.getFreshToken(r):new RegExp(this.portal,"i").test(e)?this.getFreshToken(r):this.getTokenForServer(e,r)},k.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}},k.prototype.serialize=function(){return JSON.stringify(this)},k.prototype.refreshSession=function(e){return this._user=null,this.username&&this.password?this.refreshWithUsernameAndPassword(e):this.clientId&&this.refreshToken?this.refreshWithRefreshToken():Promise.reject(new d.ArcGISAuthError("Unable to refresh token."))},k.prototype.getServerRootUrl=function(e){var r=e.split(/\/rest(\/admin)?\/services(?:\/|#|\?|$)/)[0].match(/(https?:\/\/)(.+)/),t=(r[0],r[1]),n=r[2].split("/"),s=n[0],o=n.slice(1);return""+t+s.toLowerCase()+"/"+o.join("/")},k.prototype.getTokenForServer=function(r,t){var n=this,s=this.getServerRootUrl(r),e=this.trustedServers[s];return e&&e.expires&&e.expires.getTime()>Date.now()?Promise.resolve(e.token):(this._pendingTokenRequests[s]||(this._pendingTokenRequests[s]=d.request(s+"/rest/info").then(function(e){if(e.owningSystemUrl){if(u(e.owningSystemUrl,n.portal))return d.request(e.owningSystemUrl+"/sharing/rest/info",t);throw new d.ArcGISAuthError(r+" is not federated with "+n.portal+".","NOT_FEDERATED")}if(e.authInfo&&void 0!==n.trustedServers[s])return Promise.resolve({authInfo:e.authInfo});throw new d.ArcGISAuthError(r+" 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:r,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},e.token})),this._pendingTokenRequests[s])},k.prototype.getFreshToken=function(e){var r=this;return 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 r._pendingTokenRequests[r.portal]=null,e.token})),this._pendingTokenRequests[this.portal])},k.prototype.refreshWithUsernameAndPassword=function(e){var r=this,t=T({params:{username:this.username,password:this.password,expiration:this.tokenDuration}},e);return o(this.portal+"/generateToken",t).then(function(e){return r._token=e.token,r._tokenExpires=new Date(e.expires),r})},k.prototype.refreshWithRefreshToken=function(e){var r=this;if(this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now())return this.refreshRefreshToken(e);var t=T({params:{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}},e);return a(this.portal+"/oauth2/token",t).then(function(e){return r._token=e.token,r._tokenExpires=e.expires,r})},k.prototype.refreshRefreshToken=function(e){var r=this,t=T({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",t).then(function(e){return r._token=e.token,r._tokenExpires=e.expires,r._refreshToken=e.refreshToken,r._refreshTokenExpires=new Date(Date.now()+60*(r.refreshTokenTTL-1)*1e3),r})},k}();e.ApplicationSession=r,e.UserSession=t,e.fetchToken=a,e.generateToken=o,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=auth.umd.min.js.map
{
"name": "@esri/arcgis-rest-auth",
"version": "2.1.0",
"version": "2.1.1",
"description": "Authentication helpers for @esri/arcgis-rest-js.",

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

"dependencies": {
"@esri/arcgis-rest-types": "^2.1.0",
"@esri/arcgis-rest-types": "^2.1.1",
"tslib": "^1.9.3"
},
"devDependencies": {
"@esri/arcgis-rest-request": "^2.1.0"
"@esri/arcgis-rest-request": "^2.1.1"
},

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

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