@esri/arcgis-rest-auth
Advanced tools
Comparing version 1.4.0 to 1.4.1
@@ -6,2 +6,12 @@ /// <reference types="node" /> | ||
/** | ||
* Represents a [credential]((https://developers.arcgis.com/javascript/latest/api-reference/esri-identity-Credential.html)) object used to access a secure ArcGIS resource. | ||
*/ | ||
export interface ICredential { | ||
expires: number; | ||
server: string; | ||
ssl: boolean; | ||
token: string; | ||
userId: string; | ||
} | ||
/** | ||
* Options for static OAuth 2.0 helper methods on `UserSession`. | ||
@@ -215,2 +225,25 @@ */ | ||
/** | ||
* Translates authentication from the format used in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/). | ||
* | ||
* ```js | ||
* UserSession.fromCredential({ | ||
* userId: "jsmith", | ||
* token: "secret" | ||
* }); | ||
* ``` | ||
* | ||
* @returns UserSession | ||
*/ | ||
static fromCredential(credential: ICredential): UserSession; | ||
/** | ||
* Returns authentication in a format useable in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/). | ||
* | ||
* ```js | ||
* esriId.registerToken(session.toCredential()); | ||
* ``` | ||
* | ||
* @returns ICredential | ||
*/ | ||
toCredential(): ICredential; | ||
/** | ||
* Returns information about the currently logged in [user](https://developers.arcgis.com/rest/users-groups-and-items/user.htm). Subsequent calls will *not* result in additional web traffic. | ||
@@ -217,0 +250,0 @@ * |
@@ -99,3 +99,3 @@ /* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
var _a = tslib_1.__assign({ | ||
portal: "https://arcgis.com/sharing/rest", | ||
portal: "https://www.arcgis.com/sharing/rest", | ||
duration: 20160, | ||
@@ -142,3 +142,3 @@ popup: true, | ||
if (win === void 0) { win = window; } | ||
var _a = tslib_1.__assign({ portal: "https://arcgis.com/sharing/rest" }, options), portal = _a.portal, clientId = _a.clientId; | ||
var _a = tslib_1.__assign({ portal: "https://www.arcgis.com/sharing/rest" }, options), portal = _a.portal, clientId = _a.clientId; | ||
function completeSignIn(error, oauthInfo) { | ||
@@ -243,2 +243,40 @@ if (win.opener && win.opener.parent) { | ||
/** | ||
* Translates authentication from the format used in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/). | ||
* | ||
* ```js | ||
* UserSession.fromCredential({ | ||
* userId: "jsmith", | ||
* token: "secret" | ||
* }); | ||
* ``` | ||
* | ||
* @returns UserSession | ||
*/ | ||
UserSession.fromCredential = function (credential) { | ||
return new UserSession({ | ||
portal: credential.server + "/sharing/rest", | ||
token: credential.token, | ||
username: credential.userId, | ||
tokenExpires: new Date(credential.expires) | ||
}); | ||
}; | ||
/** | ||
* Returns authentication in a format useable in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/). | ||
* | ||
* ```js | ||
* esriId.registerToken(session.toCredential()); | ||
* ``` | ||
* | ||
* @returns ICredential | ||
*/ | ||
UserSession.prototype.toCredential = function () { | ||
return { | ||
expires: this.tokenExpires.getTime(), | ||
server: this.portal, | ||
ssl: true, | ||
token: this.token, | ||
userId: this.username | ||
}; | ||
}; | ||
/** | ||
* Returns information about the currently logged in [user](https://developers.arcgis.com/rest/users-groups-and-items/user.htm). Subsequent calls will *not* result in additional web traffic. | ||
@@ -340,6 +378,9 @@ * | ||
/** | ||
* if this server is not owned by this portal bail out with an error | ||
* since we know we wont be able to generate a token | ||
* if this server is not owned by this portal or the stand-alone | ||
* instance of ArcGIS Server doesn't advertise federation, | ||
* bail out with an error since we know we wont | ||
* be able to generate a token | ||
*/ | ||
if (!new RegExp(owningSystemUrl).test(_this.portal)) { | ||
if (!owningSystemUrl || | ||
!new RegExp(owningSystemUrl).test(_this.portal)) { | ||
throw new ArcGISAuthError(url + " is not federated with " + _this.portal + ".", "NOT_FEDERATED"); | ||
@@ -346,0 +387,0 @@ } |
@@ -101,3 +101,3 @@ "use strict"; | ||
var _a = tslib_1.__assign({ | ||
portal: "https://arcgis.com/sharing/rest", | ||
portal: "https://www.arcgis.com/sharing/rest", | ||
duration: 20160, | ||
@@ -144,3 +144,3 @@ popup: true, | ||
if (win === void 0) { win = window; } | ||
var _a = tslib_1.__assign({ portal: "https://arcgis.com/sharing/rest" }, options), portal = _a.portal, clientId = _a.clientId; | ||
var _a = tslib_1.__assign({ portal: "https://www.arcgis.com/sharing/rest" }, options), portal = _a.portal, clientId = _a.clientId; | ||
function completeSignIn(error, oauthInfo) { | ||
@@ -245,2 +245,40 @@ if (win.opener && win.opener.parent) { | ||
/** | ||
* Translates authentication from the format used in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/). | ||
* | ||
* ```js | ||
* UserSession.fromCredential({ | ||
* userId: "jsmith", | ||
* token: "secret" | ||
* }); | ||
* ``` | ||
* | ||
* @returns UserSession | ||
*/ | ||
UserSession.fromCredential = function (credential) { | ||
return new UserSession({ | ||
portal: credential.server + "/sharing/rest", | ||
token: credential.token, | ||
username: credential.userId, | ||
tokenExpires: new Date(credential.expires) | ||
}); | ||
}; | ||
/** | ||
* Returns authentication in a format useable in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/). | ||
* | ||
* ```js | ||
* esriId.registerToken(session.toCredential()); | ||
* ``` | ||
* | ||
* @returns ICredential | ||
*/ | ||
UserSession.prototype.toCredential = function () { | ||
return { | ||
expires: this.tokenExpires.getTime(), | ||
server: this.portal, | ||
ssl: true, | ||
token: this.token, | ||
userId: this.username | ||
}; | ||
}; | ||
/** | ||
* Returns information about the currently logged in [user](https://developers.arcgis.com/rest/users-groups-and-items/user.htm). Subsequent calls will *not* result in additional web traffic. | ||
@@ -342,6 +380,9 @@ * | ||
/** | ||
* if this server is not owned by this portal bail out with an error | ||
* since we know we wont be able to generate a token | ||
* if this server is not owned by this portal or the stand-alone | ||
* instance of ArcGIS Server doesn't advertise federation, | ||
* bail out with an error since we know we wont | ||
* be able to generate a token | ||
*/ | ||
if (!new RegExp(owningSystemUrl).test(_this.portal)) { | ||
if (!owningSystemUrl || | ||
!new RegExp(owningSystemUrl).test(_this.portal)) { | ||
throw new arcgis_rest_request_1.ArcGISAuthError(url + " is not federated with " + _this.portal + ".", "NOT_FEDERATED"); | ||
@@ -348,0 +389,0 @@ } |
/* @preserve | ||
* @esri/arcgis-rest-auth - v1.4.0 - Wed Jun 06 2018 09:52:03 GMT-0700 (PDT) | ||
* @esri/arcgis-rest-auth - v1.4.1 - Wed Jun 20 2018 09:27:01 GMT-0700 (PDT) | ||
* Copyright (c) 2017 - 2018 Environmental Systems Research Institute, Inc. | ||
@@ -206,3 +206,3 @@ * Apache-2.0 | ||
var _a = __assign({ | ||
portal: "https://arcgis.com/sharing/rest", | ||
portal: "https://www.arcgis.com/sharing/rest", | ||
duration: 20160, | ||
@@ -249,3 +249,3 @@ popup: true, | ||
if (win === void 0) { win = window; } | ||
var _a = __assign({ portal: "https://arcgis.com/sharing/rest" }, options), portal = _a.portal, clientId = _a.clientId; | ||
var _a = __assign({ portal: "https://www.arcgis.com/sharing/rest" }, options), portal = _a.portal, clientId = _a.clientId; | ||
function completeSignIn(error, oauthInfo) { | ||
@@ -350,2 +350,40 @@ if (win.opener && win.opener.parent) { | ||
/** | ||
* Translates authentication from the format used in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/). | ||
* | ||
* ```js | ||
* UserSession.fromCredential({ | ||
* userId: "jsmith", | ||
* token: "secret" | ||
* }); | ||
* ``` | ||
* | ||
* @returns UserSession | ||
*/ | ||
UserSession.fromCredential = function (credential) { | ||
return new UserSession({ | ||
portal: credential.server + "/sharing/rest", | ||
token: credential.token, | ||
username: credential.userId, | ||
tokenExpires: new Date(credential.expires) | ||
}); | ||
}; | ||
/** | ||
* Returns authentication in a format useable in the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/). | ||
* | ||
* ```js | ||
* esriId.registerToken(session.toCredential()); | ||
* ``` | ||
* | ||
* @returns ICredential | ||
*/ | ||
UserSession.prototype.toCredential = function () { | ||
return { | ||
expires: this.tokenExpires.getTime(), | ||
server: this.portal, | ||
ssl: true, | ||
token: this.token, | ||
userId: this.username | ||
}; | ||
}; | ||
/** | ||
* Returns information about the currently logged in [user](https://developers.arcgis.com/rest/users-groups-and-items/user.htm). Subsequent calls will *not* result in additional web traffic. | ||
@@ -446,6 +484,9 @@ * | ||
/** | ||
* if this server is not owned by this portal bail out with an error | ||
* since we know we wont be able to generate a token | ||
* if this server is not owned by this portal or the stand-alone | ||
* instance of ArcGIS Server doesn't advertise federation, | ||
* bail out with an error since we know we wont | ||
* be able to generate a token | ||
*/ | ||
if (!new RegExp(owningSystemUrl).test(_this.portal)) { | ||
if (!owningSystemUrl || | ||
!new RegExp(owningSystemUrl).test(_this.portal)) { | ||
throw new arcgisRestRequest.ArcGISAuthError(url + " is not federated with " + _this.portal + ".", "NOT_FEDERATED"); | ||
@@ -452,0 +493,0 @@ } |
/* @preserve | ||
* @esri/arcgis-rest-auth - v1.4.0 - Wed Jun 06 2018 09:52:05 GMT-0700 (PDT) | ||
* @esri/arcgis-rest-auth - v1.4.1 - Wed Jun 20 2018 09:27:06 GMT-0700 (PDT) | ||
* Copyright (c) 2017 - 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.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.arcgisRest=e.arcgisRest||{},e.arcgisRest)}(this,function(e,t){"use strict";function r(e,r){return t.request(e,{params:r}).then(function(e){var t={token:e.access_token,username:e.username,expires:new Date(Date.now()+(60*e.expires_in*1e3-6e4))};return e.refresh_token&&(t.refreshToken=e.refresh_token),t})}var n=function(){function e(e){this.clientId=e.clientId,this.clientSecret=e.clientSecret,this.token=e.token,this.expires=e.expires,this.portal="https://www.arcgis.com/sharing/rest",this.duration=e.duration||20160}return e.prototype.getToken=function(e){return this.token&&this.expires&&this.expires.getTime()>Date.now()?Promise.resolve(this.token):this._pendingTokenRequest?this._pendingTokenRequest:(this._pendingTokenRequest=this.refreshToken(),this._pendingTokenRequest)},e.prototype.refreshToken=function(){var e=this;return r(this.portal+"/oauth2/token/",{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}).then(function(t){return e._pendingTokenRequest=null,e.token=t.token,e.expires=t.expires,t.token})},e.prototype.refreshSession=function(){var e=this;return this.refreshToken().then(function(){return e})},e}(),o=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e};function s(e,r){return"undefined"!=typeof window&&window.location&&window.location.host?r.referer=window.location.host:r.referer="@esri.arcgis-rest-auth",t.request(e,{params:r})}var i=function(){function e(e){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||"https://www.arcgis.com/sharing/rest",this.tokenDuration=e.tokenDuration||20160,this.redirectUri=e.redirectUri,this.refreshTokenTTL=e.refreshTokenTTL||1440,this.trustedServers={},this._pendingTokenRequests={}}return Object.defineProperty(e.prototype,"token",{get:function(){return this._token},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"tokenExpires",{get:function(){return this._tokenExpires},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"refreshToken",{get:function(){return this._refreshToken},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"refreshTokenExpires",{get:function(){return this._refreshTokenExpires},enumerable:!0,configurable:!0}),e.beginOAuth2=function(r,n){void 0===n&&(n=window);var s=o({portal:"https://arcgis.com/sharing/rest",duration:20160,popup:!0,state:r.clientId,locale:""},r),i=s.portal,h=s.clientId,a=s.duration,u=s.redirectUri,p=s.popup,c=s.state,f=s.locale,k=i+"/oauth2/authorize?client_id="+h+"&response_type=token&expiration="+a+"&redirect_uri="+encodeURIComponent(u)+"&state="+c+"&locale="+f;if(p){var d,l=((d={promise:null,resolve:null,reject:null}).promise=new Promise(function(e,t){d.resolve=e,d.reject=t}),d);return n["__ESRI_REST_AUTH_HANDLER_"+h]=function(r,n){if(r){var o=JSON.parse(r);l.reject(new t.ArcGISAuthError(o.errorMessage,o.error))}else if(n){var s=JSON.parse(n);l.resolve(new e({clientId:h,portal:i,token:s.token,tokenExpires:new Date(s.expires),username:s.username}))}},n.open(k,"oauth-window","height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"),l.promise}n.location.href=k},e.completeOAuth2=function(r,n){void 0===n&&(n=window);var s=o({portal:"https://arcgis.com/sharing/rest"},r),i=s.portal,h=s.clientId;function a(r,o){if(n.opener&&n.opener.parent)return n.opener.parent["__ESRI_REST_AUTH_HANDLER_"+h](r?JSON.stringify(r):void 0,JSON.stringify(o)),void n.close();if(n!==n.parent)return n.parent["__ESRI_REST_AUTH_HANDLER_"+h](r?JSON.stringify(r):void 0,JSON.stringify(o)),void n.close();if(r)throw new t.ArcGISAuthError(r.errorMessage,r.error);return new e({clientId:h,portal:i,token:o.token,tokenExpires:o.expires,username:o.username})}var u=n.location.href.match(/access_token=(.+)&expires_in=(.+)&username=([^&]+)/);if(!u){var p=n.location.href.match(/error=(.+)&error_description=(.+)/);return a({error:p[1],errorMessage:decodeURIComponent(p[2])})}return a(void 0,{token:u[1],expires:new Date(Date.now()+1e3*parseInt(u[2],10)-6e4),username:decodeURIComponent(u[3])})},e.authorize=function(e,t){var r=o({portal:"https://arcgis.com/sharing/rest",duration:20160},e),n=r.portal,s=r.clientId,i=r.duration,h=r.redirectUri;t.writeHead(301,{Location:n+"/oauth2/authorize?client_id="+s+"&duration="+i+"&response_type=code&redirect_uri="+encodeURIComponent(h)}),t.end()},e.exchangeAuthorizationCode=function(t,n){var s=o({portal:"https://www.arcgis.com/sharing/rest",duration:20160,refreshTokenTTL:1440},t),i=s.portal,h=s.clientId,a=(s.duration,s.redirectUri),u=s.refreshTokenTTL;return r(i+"/oauth2/token",{grant_type:"authorization_code",client_id:h,redirect_uri:a,code:n}).then(function(t){return new e({clientId:h,portal:i,redirectUri:a,refreshToken:t.refreshToken,refreshTokenTTL:u,refreshTokenExpires:new Date(Date.now()+1e3*(u-1)),token:t.token,tokenExpires:t.expires,username:t.username})})},e.deserialize=function(t){var r=JSON.parse(t);return new e({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,tokenDuration:r.tokenDuration,redirectUri:r.redirectUri,refreshTokenTTL:r.refreshTokenTTL})},e.prototype.getUser=function(){var e=this;if(this._user&&this._user.username===this.username)return new Promise(function(t){return t(e._user)});var r=this.portal+"/community/users/"+encodeURIComponent(this.username);return t.request(r,{httpMethod:"GET",authentication:this}).then(function(t){return e._user=t,t})},e.prototype.getToken=function(e){return/^https?:\/\/www\.arcgis\.com\/sharing\/rest\/?/.test(this.portal),/^https?:\/\/\S+\.arcgis\.com.+/.test(e)?this.getFreshToken():new RegExp(this.portal).test(e)?this.getFreshToken():this.getTokenForServer(e)},e.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,tokenDuration:this.tokenDuration,redirectUri:this.redirectUri,refreshTokenTTL:this.refreshTokenTTL}},e.prototype.serialize=function(){return JSON.stringify(this)},e.prototype.refreshSession=function(){return this.username&&this.password?this.refreshWithUsernameAndPassword():this.clientId&&this.refreshToken?this.refreshWithRefreshToken():Promise.reject(new t.ArcGISAuthError("Unable to refresh token."))},e.prototype.getTokenForServer=function(e){var r=this,n=e.split("/rest/services/")[0],o=this.trustedServers[n];return o&&o.expires.getTime()>Date.now()?Promise.resolve(o.token):this._pendingTokenRequests[n]?this._pendingTokenRequests[n]:(this._pendingTokenRequests[n]=t.request(n+"/rest/info").then(function(e){return e.owningSystemUrl}).then(function(n){if(!new RegExp(n).test(r.portal))throw new t.ArcGISAuthError(e+" is not federated with "+r.portal+".","NOT_FEDERATED");return t.request(n+"/sharing/rest/info")}).then(function(e){return e.authInfo.tokenServicesUrl}).then(function(t){return s(t,{token:r.token,serverUrl:e,expiration:r.tokenDuration})}).then(function(e){return r.trustedServers[n]={expires:new Date(e.expires),token:e.token},e.token}),this._pendingTokenRequests[n])},e.prototype.getFreshToken=function(){var e=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().then(function(t){return e._pendingTokenRequests[e.portal]=null,t.token})),this._pendingTokenRequests[this.portal])},e.prototype.refreshWithUsernameAndPassword=function(){var e=this;return s(this.portal+"/generateToken",{username:this.username,password:this.password,expiration:this.tokenDuration}).then(function(t){return e._token=t.token,e._tokenExpires=new Date(t.expires),e})},e.prototype.refreshWithRefreshToken=function(){var e=this;return this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now()?this.refreshRefreshToken():r(this.portal+"/oauth2/token",{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}).then(function(t){return e._token=t.token,e._tokenExpires=t.expires,e})},e.prototype.refreshRefreshToken=function(){var e=this;return r(this.portal+"/oauth2/token",{client_id:this.clientId,refresh_token:this.refreshToken,redirect_uri:this.redirectUri,grant_type:"exchange_refresh_token"}).then(function(t){return e._token=t.token,e._tokenExpires=t.expires,e._refreshToken=t.refreshToken,e._refreshTokenExpires=new Date(Date.now()+60*(e.refreshTokenTTL-1)*1e3),e})},e}();e.ApplicationSession=n,e.UserSession=i,e.fetchToken=r,e.generateToken=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.arcgisRest=e.arcgisRest||{},e.arcgisRest)}(this,function(e,t){"use strict";function r(e,r){return t.request(e,{params:r}).then(function(e){var t={token:e.access_token,username:e.username,expires:new Date(Date.now()+(60*e.expires_in*1e3-6e4))};return e.refresh_token&&(t.refreshToken=e.refresh_token),t})}var n=function(){function e(e){this.clientId=e.clientId,this.clientSecret=e.clientSecret,this.token=e.token,this.expires=e.expires,this.portal="https://www.arcgis.com/sharing/rest",this.duration=e.duration||20160}return e.prototype.getToken=function(e){return this.token&&this.expires&&this.expires.getTime()>Date.now()?Promise.resolve(this.token):this._pendingTokenRequest?this._pendingTokenRequest:(this._pendingTokenRequest=this.refreshToken(),this._pendingTokenRequest)},e.prototype.refreshToken=function(){var e=this;return r(this.portal+"/oauth2/token/",{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}).then(function(t){return e._pendingTokenRequest=null,e.token=t.token,e.expires=t.expires,t.token})},e.prototype.refreshSession=function(){var e=this;return this.refreshToken().then(function(){return e})},e}(),o=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e};function s(e,r){return"undefined"!=typeof window&&window.location&&window.location.host?r.referer=window.location.host:r.referer="@esri.arcgis-rest-auth",t.request(e,{params:r})}var i=function(){function e(e){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||"https://www.arcgis.com/sharing/rest",this.tokenDuration=e.tokenDuration||20160,this.redirectUri=e.redirectUri,this.refreshTokenTTL=e.refreshTokenTTL||1440,this.trustedServers={},this._pendingTokenRequests={}}return Object.defineProperty(e.prototype,"token",{get:function(){return this._token},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"tokenExpires",{get:function(){return this._tokenExpires},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"refreshToken",{get:function(){return this._refreshToken},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"refreshTokenExpires",{get:function(){return this._refreshTokenExpires},enumerable:!0,configurable:!0}),e.beginOAuth2=function(r,n){void 0===n&&(n=window);var s=o({portal:"https://www.arcgis.com/sharing/rest",duration:20160,popup:!0,state:r.clientId,locale:""},r),i=s.portal,h=s.clientId,a=s.duration,u=s.redirectUri,p=s.popup,c=s.state,f=s.locale,k=i+"/oauth2/authorize?client_id="+h+"&response_type=token&expiration="+a+"&redirect_uri="+encodeURIComponent(u)+"&state="+c+"&locale="+f;if(p){var d,l=((d={promise:null,resolve:null,reject:null}).promise=new Promise(function(e,t){d.resolve=e,d.reject=t}),d);return n["__ESRI_REST_AUTH_HANDLER_"+h]=function(r,n){if(r){var o=JSON.parse(r);l.reject(new t.ArcGISAuthError(o.errorMessage,o.error))}else if(n){var s=JSON.parse(n);l.resolve(new e({clientId:h,portal:i,token:s.token,tokenExpires:new Date(s.expires),username:s.username}))}},n.open(k,"oauth-window","height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"),l.promise}n.location.href=k},e.completeOAuth2=function(r,n){void 0===n&&(n=window);var s=o({portal:"https://www.arcgis.com/sharing/rest"},r),i=s.portal,h=s.clientId;function a(r,o){if(n.opener&&n.opener.parent)return n.opener.parent["__ESRI_REST_AUTH_HANDLER_"+h](r?JSON.stringify(r):void 0,JSON.stringify(o)),void n.close();if(n!==n.parent)return n.parent["__ESRI_REST_AUTH_HANDLER_"+h](r?JSON.stringify(r):void 0,JSON.stringify(o)),void n.close();if(r)throw new t.ArcGISAuthError(r.errorMessage,r.error);return new e({clientId:h,portal:i,token:o.token,tokenExpires:o.expires,username:o.username})}var u=n.location.href.match(/access_token=(.+)&expires_in=(.+)&username=([^&]+)/);if(!u){var p=n.location.href.match(/error=(.+)&error_description=(.+)/);return a({error:p[1],errorMessage:decodeURIComponent(p[2])})}return a(void 0,{token:u[1],expires:new Date(Date.now()+1e3*parseInt(u[2],10)-6e4),username:decodeURIComponent(u[3])})},e.authorize=function(e,t){var r=o({portal:"https://arcgis.com/sharing/rest",duration:20160},e),n=r.portal,s=r.clientId,i=r.duration,h=r.redirectUri;t.writeHead(301,{Location:n+"/oauth2/authorize?client_id="+s+"&duration="+i+"&response_type=code&redirect_uri="+encodeURIComponent(h)}),t.end()},e.exchangeAuthorizationCode=function(t,n){var s=o({portal:"https://www.arcgis.com/sharing/rest",duration:20160,refreshTokenTTL:1440},t),i=s.portal,h=s.clientId,a=(s.duration,s.redirectUri),u=s.refreshTokenTTL;return r(i+"/oauth2/token",{grant_type:"authorization_code",client_id:h,redirect_uri:a,code:n}).then(function(t){return new e({clientId:h,portal:i,redirectUri:a,refreshToken:t.refreshToken,refreshTokenTTL:u,refreshTokenExpires:new Date(Date.now()+1e3*(u-1)),token:t.token,tokenExpires:t.expires,username:t.username})})},e.deserialize=function(t){var r=JSON.parse(t);return new e({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,tokenDuration:r.tokenDuration,redirectUri:r.redirectUri,refreshTokenTTL:r.refreshTokenTTL})},e.fromCredential=function(t){return new e({portal:t.server+"/sharing/rest",token:t.token,username:t.userId,tokenExpires:new Date(t.expires)})},e.prototype.toCredential=function(){return{expires:this.tokenExpires.getTime(),server:this.portal,ssl:!0,token:this.token,userId:this.username}},e.prototype.getUser=function(){var e=this;if(this._user&&this._user.username===this.username)return new Promise(function(t){return t(e._user)});var r=this.portal+"/community/users/"+encodeURIComponent(this.username);return t.request(r,{httpMethod:"GET",authentication:this}).then(function(t){return e._user=t,t})},e.prototype.getToken=function(e){return/^https?:\/\/www\.arcgis\.com\/sharing\/rest\/?/.test(this.portal),/^https?:\/\/\S+\.arcgis\.com.+/.test(e)?this.getFreshToken():new RegExp(this.portal).test(e)?this.getFreshToken():this.getTokenForServer(e)},e.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,tokenDuration:this.tokenDuration,redirectUri:this.redirectUri,refreshTokenTTL:this.refreshTokenTTL}},e.prototype.serialize=function(){return JSON.stringify(this)},e.prototype.refreshSession=function(){return this.username&&this.password?this.refreshWithUsernameAndPassword():this.clientId&&this.refreshToken?this.refreshWithRefreshToken():Promise.reject(new t.ArcGISAuthError("Unable to refresh token."))},e.prototype.getTokenForServer=function(e){var r=this,n=e.split("/rest/services/")[0],o=this.trustedServers[n];return o&&o.expires.getTime()>Date.now()?Promise.resolve(o.token):this._pendingTokenRequests[n]?this._pendingTokenRequests[n]:(this._pendingTokenRequests[n]=t.request(n+"/rest/info").then(function(e){return e.owningSystemUrl}).then(function(n){if(!n||!new RegExp(n).test(r.portal))throw new t.ArcGISAuthError(e+" is not federated with "+r.portal+".","NOT_FEDERATED");return t.request(n+"/sharing/rest/info")}).then(function(e){return e.authInfo.tokenServicesUrl}).then(function(t){return s(t,{token:r.token,serverUrl:e,expiration:r.tokenDuration})}).then(function(e){return r.trustedServers[n]={expires:new Date(e.expires),token:e.token},e.token}),this._pendingTokenRequests[n])},e.prototype.getFreshToken=function(){var e=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().then(function(t){return e._pendingTokenRequests[e.portal]=null,t.token})),this._pendingTokenRequests[this.portal])},e.prototype.refreshWithUsernameAndPassword=function(){var e=this;return s(this.portal+"/generateToken",{username:this.username,password:this.password,expiration:this.tokenDuration}).then(function(t){return e._token=t.token,e._tokenExpires=new Date(t.expires),e})},e.prototype.refreshWithRefreshToken=function(){var e=this;return this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now()?this.refreshRefreshToken():r(this.portal+"/oauth2/token",{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}).then(function(t){return e._token=t.token,e._tokenExpires=t.expires,e})},e.prototype.refreshRefreshToken=function(){var e=this;return r(this.portal+"/oauth2/token",{client_id:this.clientId,refresh_token:this.refreshToken,redirect_uri:this.redirectUri,grant_type:"exchange_refresh_token"}).then(function(t){return e._token=t.token,e._tokenExpires=t.expires,e._refreshToken=t.refreshToken,e._refreshTokenExpires=new Date(Date.now()+60*(e.refreshTokenTTL-1)*1e3),e})},e}();e.ApplicationSession=n,e.UserSession=i,e.fetchToken=r,e.generateToken=s,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=auth.umd.min.js.map |
{ | ||
"name": "@esri/arcgis-rest-auth", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "Authentication helpers for @esri/arcgis-rest-*.", | ||
@@ -22,4 +22,4 @@ "main": "dist/node/index.js", | ||
"devDependencies": { | ||
"@esri/arcgis-rest-common-types": "^1.4.0", | ||
"@esri/arcgis-rest-request": "^1.4.0" | ||
"@esri/arcgis-rest-common-types": "^1.4.1", | ||
"@esri/arcgis-rest-request": "^1.4.1" | ||
}, | ||
@@ -26,0 +26,0 @@ "scripts": { |
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
203447
2152