@esri/arcgis-rest-auth
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -1,2 +0,2 @@ | ||
import { IAuthenticationManager } from "@esri/arcgis-rest-request"; | ||
import { IAuthenticationManager, ITokenRequestOptions } from "@esri/arcgis-rest-request"; | ||
export interface IApplicationSessionOptions { | ||
@@ -41,5 +41,5 @@ /** | ||
constructor(options: IApplicationSessionOptions); | ||
getToken(url: string): Promise<string>; | ||
refreshToken(): Promise<string>; | ||
getToken(url: string, requestOptions?: ITokenRequestOptions): Promise<string>; | ||
refreshToken(requestOptions?: ITokenRequestOptions): Promise<string>; | ||
refreshSession(): Promise<this>; | ||
} |
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
import * as tslib_1 from "tslib"; | ||
import { fetchToken } from "./fetch-token"; | ||
@@ -13,3 +14,4 @@ var ApplicationSession = /** @class */ (function () { | ||
} | ||
ApplicationSession.prototype.getToken = function (url) { | ||
// url isnt actually read or passed through. | ||
ApplicationSession.prototype.getToken = function (url, requestOptions) { | ||
if (this.token && this.expires && this.expires.getTime() > Date.now()) { | ||
@@ -21,12 +23,13 @@ return Promise.resolve(this.token); | ||
} | ||
this._pendingTokenRequest = this.refreshToken(); | ||
this._pendingTokenRequest = this.refreshToken(requestOptions); | ||
return this._pendingTokenRequest; | ||
}; | ||
ApplicationSession.prototype.refreshToken = function () { | ||
ApplicationSession.prototype.refreshToken = function (requestOptions) { | ||
var _this = this; | ||
return fetchToken(this.portal + "/oauth2/token/", { | ||
client_id: this.clientId, | ||
client_secret: this.clientSecret, | ||
grant_type: "client_credentials" | ||
}).then(function (response) { | ||
var options = tslib_1.__assign({ params: { | ||
client_id: this.clientId, | ||
client_secret: this.clientSecret, | ||
grant_type: "client_credentials" | ||
} }, requestOptions); | ||
return fetchToken(this.portal + "/oauth2/token/", options).then(function (response) { | ||
_this._pendingTokenRequest = null; | ||
@@ -33,0 +36,0 @@ _this.token = response.token; |
@@ -1,11 +0,2 @@ | ||
import { IParams } from "@esri/arcgis-rest-request"; | ||
export declare type GrantTypes = "authorization_code" | "refresh_token" | "client_credentials" | "exchange_refresh_token"; | ||
export interface IFetchTokenParams extends IParams { | ||
client_id: string; | ||
client_secret?: string; | ||
grant_type: GrantTypes; | ||
redirect_uri?: string; | ||
refresh_token?: string; | ||
code?: string; | ||
} | ||
import { IFetchTokenParams, ITokenRequestOptions } from "@esri/arcgis-rest-request"; | ||
export interface IFetchTokenResponse { | ||
@@ -17,2 +8,2 @@ token: string; | ||
} | ||
export declare function fetchToken(url: string, options: IFetchTokenParams): Promise<IFetchTokenResponse>; | ||
export declare function fetchToken(url: string, requestOptions: IFetchTokenParams | ITokenRequestOptions): Promise<IFetchTokenResponse>; |
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
import { request } from "@esri/arcgis-rest-request"; | ||
export function fetchToken(url, options) { | ||
return request(url, { | ||
params: options | ||
}).then(function (response) { | ||
export function fetchToken(url, requestOptions) { | ||
// TODO: remove union type and type guard next breaking change and just expect IGenerateTokenRequestOptions | ||
var options = requestOptions | ||
.params | ||
? requestOptions | ||
: { params: requestOptions }; | ||
return request(url, options).then(function (response) { | ||
var r = { | ||
@@ -9,0 +12,0 @@ token: response.access_token, |
@@ -1,9 +0,2 @@ | ||
import { IParams } from "@esri/arcgis-rest-request"; | ||
export interface IGenerateTokenParams extends IParams { | ||
username?: string; | ||
password?: string; | ||
expiration?: number; | ||
token?: string; | ||
serverUrl?: string; | ||
} | ||
import { IGenerateTokenParams, ITokenRequestOptions } from "@esri/arcgis-rest-request"; | ||
export interface IGenerateTokenResponse { | ||
@@ -14,2 +7,2 @@ token: string; | ||
} | ||
export declare function generateToken(url: string, params: IGenerateTokenParams): Promise<IGenerateTokenResponse>; | ||
export declare function generateToken(url: string, requestOptions: IGenerateTokenParams | ITokenRequestOptions): Promise<IGenerateTokenResponse>; |
@@ -1,5 +0,10 @@ | ||
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
import { request } from "@esri/arcgis-rest-request"; | ||
export function generateToken(url, params) { | ||
export function generateToken(url, requestOptions) { | ||
// TODO: remove union type and type guard next breaking change and just expect IGenerateTokenRequestOptions | ||
var options = requestOptions | ||
.params | ||
? requestOptions | ||
: { params: requestOptions }; | ||
/* istanbul ignore else */ | ||
@@ -9,9 +14,9 @@ if (typeof window !== "undefined" && | ||
window.location.host) { | ||
params.referer = window.location.host; | ||
options.params.referer = window.location.host; | ||
} | ||
else { | ||
params.referer = "@esri.arcgis-rest-auth"; | ||
options.params.referer = "@esri.arcgis-rest-auth"; | ||
} | ||
return request(url, { params: params }); | ||
return request(url, options); | ||
} | ||
//# sourceMappingURL=generate-token.js.map |
/// <reference types="node" /> | ||
import * as http from "http"; | ||
import { IAuthenticationManager } from "@esri/arcgis-rest-request"; | ||
import { IAuthenticationManager, ITokenRequestOptions } from "@esri/arcgis-rest-request"; | ||
import { IUser } from "@esri/arcgis-rest-common-types"; | ||
@@ -279,3 +279,3 @@ export declare type AuthenticationProvider = "arcgis" | "facebook" | "google"; | ||
*/ | ||
getToken(url: string): Promise<string>; | ||
getToken(url: string, requestOptions?: ITokenRequestOptions): Promise<string>; | ||
toJSON(): IUserSessionOptions; | ||
@@ -286,3 +286,3 @@ serialize(): string; | ||
*/ | ||
refreshSession(): Promise<UserSession>; | ||
refreshSession(requestOptions?: ITokenRequestOptions): Promise<UserSession>; | ||
/** | ||
@@ -292,7 +292,7 @@ * Validates that a given URL is properly federated with our current `portal`. | ||
*/ | ||
private getTokenForServer(url); | ||
private getTokenForServer(url, requestOptions?); | ||
/** | ||
* Returns an unexpired token for the current `portal`. | ||
*/ | ||
private getFreshToken(); | ||
private getFreshToken(requestOptions?); | ||
/** | ||
@@ -302,7 +302,7 @@ * Refreshes the current `token` and `tokenExpires` with `username` and | ||
*/ | ||
private refreshWithUsernameAndPassword(); | ||
private refreshWithUsernameAndPassword(requestOptions?); | ||
/** | ||
* Refreshes the current `token` and `tokenExpires` with `refreshToken`. | ||
*/ | ||
private refreshWithRefreshToken(); | ||
private refreshWithRefreshToken(requestOptions?); | ||
/** | ||
@@ -312,3 +312,3 @@ * Exchanges an expired `refreshToken` for a new one also updates `token` and | ||
*/ | ||
private refreshRefreshToken(); | ||
private refreshRefreshToken(requestOptions?); | ||
} |
@@ -1,2 +0,2 @@ | ||
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
@@ -321,12 +321,12 @@ import * as tslib_1 from "tslib"; | ||
*/ | ||
UserSession.prototype.getToken = function (url) { | ||
UserSession.prototype.getToken = function (url, requestOptions) { | ||
if (/^https?:\/\/\S+\.arcgis\.com\/sharing\/rest/.test(this.portal) && | ||
/^https?:\/\/\S+\.arcgis\.com.+/.test(url)) { | ||
return this.getFreshToken(); | ||
return this.getFreshToken(requestOptions); | ||
} | ||
else if (new RegExp(this.portal).test(url)) { | ||
return this.getFreshToken(); | ||
return this.getFreshToken(requestOptions); | ||
} | ||
else { | ||
return this.getTokenForServer(url); | ||
return this.getTokenForServer(url, requestOptions); | ||
} | ||
@@ -355,5 +355,5 @@ }; | ||
*/ | ||
UserSession.prototype.refreshSession = function () { | ||
UserSession.prototype.refreshSession = function (requestOptions) { | ||
if (this.username && this.password) { | ||
return this.refreshWithUsernameAndPassword(); | ||
return this.refreshWithUsernameAndPassword(requestOptions); | ||
} | ||
@@ -369,3 +369,3 @@ if (this.clientId && this.refreshToken) { | ||
*/ | ||
UserSession.prototype.getTokenForServer = function (url) { | ||
UserSession.prototype.getTokenForServer = function (url, requestOptions) { | ||
var _this = this; | ||
@@ -395,3 +395,3 @@ var root = url.split("/rest/services/")[0]; | ||
} | ||
return request(owningSystemUrl + "/sharing/rest/info"); | ||
return request(owningSystemUrl + "/sharing/rest/info", requestOptions); | ||
}) | ||
@@ -404,5 +404,7 @@ .then(function (response) { | ||
return generateToken(tokenServicesUrl, { | ||
token: _this.token, | ||
serverUrl: url, | ||
expiration: _this.tokenDuration | ||
params: { | ||
token: _this.token, | ||
serverUrl: url, | ||
expiration: _this.tokenDuration | ||
} | ||
}); | ||
@@ -413,5 +415,7 @@ // generate an entirely fresh token if necessary | ||
return generateToken(tokenServicesUrl, { | ||
username: _this.username, | ||
password: _this.password, | ||
expiration: _this.tokenDuration | ||
params: { | ||
username: _this.username, | ||
password: _this.password, | ||
expiration: _this.tokenDuration | ||
} | ||
}).then(function (response) { | ||
@@ -436,3 +440,3 @@ _this._token = response.token; | ||
*/ | ||
UserSession.prototype.getFreshToken = function () { | ||
UserSession.prototype.getFreshToken = function (requestOptions) { | ||
var _this = this; | ||
@@ -445,3 +449,3 @@ if (this.token && | ||
if (!this._pendingTokenRequests[this.portal]) { | ||
this._pendingTokenRequests[this.portal] = this.refreshSession().then(function (session) { | ||
this._pendingTokenRequests[this.portal] = this.refreshSession(requestOptions).then(function (session) { | ||
_this._pendingTokenRequests[_this.portal] = null; | ||
@@ -457,9 +461,10 @@ return session.token; | ||
*/ | ||
UserSession.prototype.refreshWithUsernameAndPassword = function () { | ||
UserSession.prototype.refreshWithUsernameAndPassword = function (requestOptions) { | ||
var _this = this; | ||
return generateToken(this.portal + "/generateToken", { | ||
username: this.username, | ||
password: this.password, | ||
expiration: this.tokenDuration | ||
}).then(function (response) { | ||
var options = tslib_1.__assign({ params: { | ||
username: this.username, | ||
password: this.password, | ||
expiration: this.tokenDuration | ||
} }, requestOptions); | ||
return generateToken(this.portal + "/generateToken", options).then(function (response) { | ||
_this._token = response.token; | ||
@@ -473,3 +478,3 @@ _this._tokenExpires = new Date(response.expires); | ||
*/ | ||
UserSession.prototype.refreshWithRefreshToken = function () { | ||
UserSession.prototype.refreshWithRefreshToken = function (requestOptions) { | ||
var _this = this; | ||
@@ -479,9 +484,10 @@ if (this.refreshToken && | ||
this.refreshTokenExpires.getTime() < Date.now()) { | ||
return this.refreshRefreshToken(); | ||
return this.refreshRefreshToken(requestOptions); | ||
} | ||
return fetchToken(this.portal + "/oauth2/token", { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
grant_type: "refresh_token" | ||
}).then(function (response) { | ||
var options = tslib_1.__assign({ params: { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
grant_type: "refresh_token" | ||
} }, requestOptions); | ||
return fetchToken(this.portal + "/oauth2/token", options).then(function (response) { | ||
_this._token = response.token; | ||
@@ -496,10 +502,11 @@ _this._tokenExpires = response.expires; | ||
*/ | ||
UserSession.prototype.refreshRefreshToken = function () { | ||
UserSession.prototype.refreshRefreshToken = function (requestOptions) { | ||
var _this = this; | ||
return fetchToken(this.portal + "/oauth2/token", { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
redirect_uri: this.redirectUri, | ||
grant_type: "exchange_refresh_token" | ||
}).then(function (response) { | ||
var options = tslib_1.__assign({ params: { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
redirect_uri: this.redirectUri, | ||
grant_type: "exchange_refresh_token" | ||
} }, requestOptions); | ||
return fetchToken(this.portal + "/oauth2/token", options).then(function (response) { | ||
_this._token = response.token; | ||
@@ -506,0 +513,0 @@ _this._tokenExpires = response.expires; |
@@ -5,2 +5,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var fetch_token_1 = require("./fetch-token"); | ||
@@ -16,3 +17,4 @@ var ApplicationSession = /** @class */ (function () { | ||
} | ||
ApplicationSession.prototype.getToken = function (url) { | ||
// url isnt actually read or passed through. | ||
ApplicationSession.prototype.getToken = function (url, requestOptions) { | ||
if (this.token && this.expires && this.expires.getTime() > Date.now()) { | ||
@@ -24,12 +26,13 @@ return Promise.resolve(this.token); | ||
} | ||
this._pendingTokenRequest = this.refreshToken(); | ||
this._pendingTokenRequest = this.refreshToken(requestOptions); | ||
return this._pendingTokenRequest; | ||
}; | ||
ApplicationSession.prototype.refreshToken = function () { | ||
ApplicationSession.prototype.refreshToken = function (requestOptions) { | ||
var _this = this; | ||
return fetch_token_1.fetchToken(this.portal + "/oauth2/token/", { | ||
client_id: this.clientId, | ||
client_secret: this.clientSecret, | ||
grant_type: "client_credentials" | ||
}).then(function (response) { | ||
var options = tslib_1.__assign({ params: { | ||
client_id: this.clientId, | ||
client_secret: this.clientSecret, | ||
grant_type: "client_credentials" | ||
} }, requestOptions); | ||
return fetch_token_1.fetchToken(this.portal + "/oauth2/token/", options).then(function (response) { | ||
_this._pendingTokenRequest = null; | ||
@@ -36,0 +39,0 @@ _this.token = response.token; |
@@ -6,6 +6,9 @@ "use strict"; | ||
var arcgis_rest_request_1 = require("@esri/arcgis-rest-request"); | ||
function fetchToken(url, options) { | ||
return arcgis_rest_request_1.request(url, { | ||
params: options | ||
}).then(function (response) { | ||
function fetchToken(url, requestOptions) { | ||
// TODO: remove union type and type guard next breaking change and just expect IGenerateTokenRequestOptions | ||
var options = requestOptions | ||
.params | ||
? requestOptions | ||
: { params: requestOptions }; | ||
return arcgis_rest_request_1.request(url, options).then(function (response) { | ||
var r = { | ||
@@ -12,0 +15,0 @@ token: response.access_token, |
"use strict"; | ||
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var arcgis_rest_request_1 = require("@esri/arcgis-rest-request"); | ||
function generateToken(url, params) { | ||
function generateToken(url, requestOptions) { | ||
// TODO: remove union type and type guard next breaking change and just expect IGenerateTokenRequestOptions | ||
var options = requestOptions | ||
.params | ||
? requestOptions | ||
: { params: requestOptions }; | ||
/* istanbul ignore else */ | ||
@@ -11,10 +16,10 @@ if (typeof window !== "undefined" && | ||
window.location.host) { | ||
params.referer = window.location.host; | ||
options.params.referer = window.location.host; | ||
} | ||
else { | ||
params.referer = "@esri.arcgis-rest-auth"; | ||
options.params.referer = "@esri.arcgis-rest-auth"; | ||
} | ||
return arcgis_rest_request_1.request(url, { params: params }); | ||
return arcgis_rest_request_1.request(url, options); | ||
} | ||
exports.generateToken = generateToken; | ||
//# sourceMappingURL=generate-token.js.map |
"use strict"; | ||
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
@@ -323,12 +323,12 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
*/ | ||
UserSession.prototype.getToken = function (url) { | ||
UserSession.prototype.getToken = function (url, requestOptions) { | ||
if (/^https?:\/\/\S+\.arcgis\.com\/sharing\/rest/.test(this.portal) && | ||
/^https?:\/\/\S+\.arcgis\.com.+/.test(url)) { | ||
return this.getFreshToken(); | ||
return this.getFreshToken(requestOptions); | ||
} | ||
else if (new RegExp(this.portal).test(url)) { | ||
return this.getFreshToken(); | ||
return this.getFreshToken(requestOptions); | ||
} | ||
else { | ||
return this.getTokenForServer(url); | ||
return this.getTokenForServer(url, requestOptions); | ||
} | ||
@@ -357,5 +357,5 @@ }; | ||
*/ | ||
UserSession.prototype.refreshSession = function () { | ||
UserSession.prototype.refreshSession = function (requestOptions) { | ||
if (this.username && this.password) { | ||
return this.refreshWithUsernameAndPassword(); | ||
return this.refreshWithUsernameAndPassword(requestOptions); | ||
} | ||
@@ -371,3 +371,3 @@ if (this.clientId && this.refreshToken) { | ||
*/ | ||
UserSession.prototype.getTokenForServer = function (url) { | ||
UserSession.prototype.getTokenForServer = function (url, requestOptions) { | ||
var _this = this; | ||
@@ -397,3 +397,3 @@ var root = url.split("/rest/services/")[0]; | ||
} | ||
return arcgis_rest_request_1.request(owningSystemUrl + "/sharing/rest/info"); | ||
return arcgis_rest_request_1.request(owningSystemUrl + "/sharing/rest/info", requestOptions); | ||
}) | ||
@@ -406,5 +406,7 @@ .then(function (response) { | ||
return generate_token_1.generateToken(tokenServicesUrl, { | ||
token: _this.token, | ||
serverUrl: url, | ||
expiration: _this.tokenDuration | ||
params: { | ||
token: _this.token, | ||
serverUrl: url, | ||
expiration: _this.tokenDuration | ||
} | ||
}); | ||
@@ -415,5 +417,7 @@ // generate an entirely fresh token if necessary | ||
return generate_token_1.generateToken(tokenServicesUrl, { | ||
username: _this.username, | ||
password: _this.password, | ||
expiration: _this.tokenDuration | ||
params: { | ||
username: _this.username, | ||
password: _this.password, | ||
expiration: _this.tokenDuration | ||
} | ||
}).then(function (response) { | ||
@@ -438,3 +442,3 @@ _this._token = response.token; | ||
*/ | ||
UserSession.prototype.getFreshToken = function () { | ||
UserSession.prototype.getFreshToken = function (requestOptions) { | ||
var _this = this; | ||
@@ -447,3 +451,3 @@ if (this.token && | ||
if (!this._pendingTokenRequests[this.portal]) { | ||
this._pendingTokenRequests[this.portal] = this.refreshSession().then(function (session) { | ||
this._pendingTokenRequests[this.portal] = this.refreshSession(requestOptions).then(function (session) { | ||
_this._pendingTokenRequests[_this.portal] = null; | ||
@@ -459,9 +463,10 @@ return session.token; | ||
*/ | ||
UserSession.prototype.refreshWithUsernameAndPassword = function () { | ||
UserSession.prototype.refreshWithUsernameAndPassword = function (requestOptions) { | ||
var _this = this; | ||
return generate_token_1.generateToken(this.portal + "/generateToken", { | ||
username: this.username, | ||
password: this.password, | ||
expiration: this.tokenDuration | ||
}).then(function (response) { | ||
var options = tslib_1.__assign({ params: { | ||
username: this.username, | ||
password: this.password, | ||
expiration: this.tokenDuration | ||
} }, requestOptions); | ||
return generate_token_1.generateToken(this.portal + "/generateToken", options).then(function (response) { | ||
_this._token = response.token; | ||
@@ -475,3 +480,3 @@ _this._tokenExpires = new Date(response.expires); | ||
*/ | ||
UserSession.prototype.refreshWithRefreshToken = function () { | ||
UserSession.prototype.refreshWithRefreshToken = function (requestOptions) { | ||
var _this = this; | ||
@@ -481,9 +486,10 @@ if (this.refreshToken && | ||
this.refreshTokenExpires.getTime() < Date.now()) { | ||
return this.refreshRefreshToken(); | ||
return this.refreshRefreshToken(requestOptions); | ||
} | ||
return fetch_token_1.fetchToken(this.portal + "/oauth2/token", { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
grant_type: "refresh_token" | ||
}).then(function (response) { | ||
var options = tslib_1.__assign({ params: { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
grant_type: "refresh_token" | ||
} }, requestOptions); | ||
return fetch_token_1.fetchToken(this.portal + "/oauth2/token", options).then(function (response) { | ||
_this._token = response.token; | ||
@@ -498,10 +504,11 @@ _this._tokenExpires = response.expires; | ||
*/ | ||
UserSession.prototype.refreshRefreshToken = function () { | ||
UserSession.prototype.refreshRefreshToken = function (requestOptions) { | ||
var _this = this; | ||
return fetch_token_1.fetchToken(this.portal + "/oauth2/token", { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
redirect_uri: this.redirectUri, | ||
grant_type: "exchange_refresh_token" | ||
}).then(function (response) { | ||
var options = tslib_1.__assign({ params: { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
redirect_uri: this.redirectUri, | ||
grant_type: "exchange_refresh_token" | ||
} }, requestOptions); | ||
return fetch_token_1.fetchToken(this.portal + "/oauth2/token", options).then(function (response) { | ||
_this._token = response.token; | ||
@@ -508,0 +515,0 @@ _this._tokenExpires = response.expires; |
/* @preserve | ||
* @esri/arcgis-rest-auth - v1.6.0 - Fri Jul 27 2018 15:12:37 GMT-0700 (PDT) | ||
* Copyright (c) 2017 - 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 | ||
* @esri/arcgis-rest-auth - v1.7.0 - Apache-2.0 | ||
* Copyright (c) 2017-2018 Esri, Inc. | ||
* Wed Aug 08 2018 00:07:13 GMT-0700 (PDT) | ||
*/ | ||
@@ -12,8 +12,37 @@ (function (global, factory) { | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
this file except in compliance with the License. You may obtain a copy of the | ||
License at http://www.apache.org/licenses/LICENSE-2.0 | ||
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
MERCHANTABLITY OR NON-INFRINGEMENT. | ||
See the Apache Version 2.0 License for specific language governing permissions | ||
and limitations under the License. | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
var __assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
function fetchToken(url, options) { | ||
return arcgisRestRequest.request(url, { | ||
params: options | ||
}).then(function (response) { | ||
function fetchToken(url, requestOptions) { | ||
// TODO: remove union type and type guard next breaking change and just expect IGenerateTokenRequestOptions | ||
var options = requestOptions | ||
.params | ||
? requestOptions | ||
: { params: requestOptions }; | ||
return arcgisRestRequest.request(url, options).then(function (response) { | ||
var r = { | ||
@@ -42,3 +71,4 @@ token: response.access_token, | ||
} | ||
ApplicationSession.prototype.getToken = function (url) { | ||
// url isnt actually read or passed through. | ||
ApplicationSession.prototype.getToken = function (url, requestOptions) { | ||
if (this.token && this.expires && this.expires.getTime() > Date.now()) { | ||
@@ -50,12 +80,13 @@ return Promise.resolve(this.token); | ||
} | ||
this._pendingTokenRequest = this.refreshToken(); | ||
this._pendingTokenRequest = this.refreshToken(requestOptions); | ||
return this._pendingTokenRequest; | ||
}; | ||
ApplicationSession.prototype.refreshToken = function () { | ||
ApplicationSession.prototype.refreshToken = function (requestOptions) { | ||
var _this = this; | ||
return fetchToken(this.portal + "/oauth2/token/", { | ||
client_id: this.clientId, | ||
client_secret: this.clientSecret, | ||
grant_type: "client_credentials" | ||
}).then(function (response) { | ||
var options = __assign({ params: { | ||
client_id: this.clientId, | ||
client_secret: this.clientSecret, | ||
grant_type: "client_credentials" | ||
} }, requestOptions); | ||
return fetchToken(this.portal + "/oauth2/token/", options).then(function (response) { | ||
_this._pendingTokenRequest = null; | ||
@@ -74,31 +105,10 @@ _this.token = response.token; | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
this file except in compliance with the License. You may obtain a copy of the | ||
License at http://www.apache.org/licenses/LICENSE-2.0 | ||
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
MERCHANTABLITY OR NON-INFRINGEMENT. | ||
See the Apache Version 2.0 License for specific language governing permissions | ||
and limitations under the License. | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
var __assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
function generateToken(url, params) { | ||
function generateToken(url, requestOptions) { | ||
// TODO: remove union type and type guard next breaking change and just expect IGenerateTokenRequestOptions | ||
var options = requestOptions | ||
.params | ||
? requestOptions | ||
: { params: requestOptions }; | ||
/* istanbul ignore else */ | ||
@@ -108,11 +118,11 @@ if (typeof window !== "undefined" && | ||
window.location.host) { | ||
params.referer = window.location.host; | ||
options.params.referer = window.location.host; | ||
} | ||
else { | ||
params.referer = "@esri.arcgis-rest-auth"; | ||
options.params.referer = "@esri.arcgis-rest-auth"; | ||
} | ||
return arcgisRestRequest.request(url, { params: params }); | ||
return arcgisRestRequest.request(url, options); | ||
} | ||
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. | ||
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
@@ -433,12 +443,12 @@ function defer() { | ||
*/ | ||
UserSession.prototype.getToken = function (url) { | ||
UserSession.prototype.getToken = function (url, requestOptions) { | ||
if (/^https?:\/\/\S+\.arcgis\.com\/sharing\/rest/.test(this.portal) && | ||
/^https?:\/\/\S+\.arcgis\.com.+/.test(url)) { | ||
return this.getFreshToken(); | ||
return this.getFreshToken(requestOptions); | ||
} | ||
else if (new RegExp(this.portal).test(url)) { | ||
return this.getFreshToken(); | ||
return this.getFreshToken(requestOptions); | ||
} | ||
else { | ||
return this.getTokenForServer(url); | ||
return this.getTokenForServer(url, requestOptions); | ||
} | ||
@@ -467,5 +477,5 @@ }; | ||
*/ | ||
UserSession.prototype.refreshSession = function () { | ||
UserSession.prototype.refreshSession = function (requestOptions) { | ||
if (this.username && this.password) { | ||
return this.refreshWithUsernameAndPassword(); | ||
return this.refreshWithUsernameAndPassword(requestOptions); | ||
} | ||
@@ -481,3 +491,3 @@ if (this.clientId && this.refreshToken) { | ||
*/ | ||
UserSession.prototype.getTokenForServer = function (url) { | ||
UserSession.prototype.getTokenForServer = function (url, requestOptions) { | ||
var _this = this; | ||
@@ -507,3 +517,3 @@ var root = url.split("/rest/services/")[0]; | ||
} | ||
return arcgisRestRequest.request(owningSystemUrl + "/sharing/rest/info"); | ||
return arcgisRestRequest.request(owningSystemUrl + "/sharing/rest/info", requestOptions); | ||
}) | ||
@@ -516,5 +526,7 @@ .then(function (response) { | ||
return generateToken(tokenServicesUrl, { | ||
token: _this.token, | ||
serverUrl: url, | ||
expiration: _this.tokenDuration | ||
params: { | ||
token: _this.token, | ||
serverUrl: url, | ||
expiration: _this.tokenDuration | ||
} | ||
}); | ||
@@ -525,5 +537,7 @@ // generate an entirely fresh token if necessary | ||
return generateToken(tokenServicesUrl, { | ||
username: _this.username, | ||
password: _this.password, | ||
expiration: _this.tokenDuration | ||
params: { | ||
username: _this.username, | ||
password: _this.password, | ||
expiration: _this.tokenDuration | ||
} | ||
}).then(function (response) { | ||
@@ -548,3 +562,3 @@ _this._token = response.token; | ||
*/ | ||
UserSession.prototype.getFreshToken = function () { | ||
UserSession.prototype.getFreshToken = function (requestOptions) { | ||
var _this = this; | ||
@@ -557,3 +571,3 @@ if (this.token && | ||
if (!this._pendingTokenRequests[this.portal]) { | ||
this._pendingTokenRequests[this.portal] = this.refreshSession().then(function (session) { | ||
this._pendingTokenRequests[this.portal] = this.refreshSession(requestOptions).then(function (session) { | ||
_this._pendingTokenRequests[_this.portal] = null; | ||
@@ -569,9 +583,10 @@ return session.token; | ||
*/ | ||
UserSession.prototype.refreshWithUsernameAndPassword = function () { | ||
UserSession.prototype.refreshWithUsernameAndPassword = function (requestOptions) { | ||
var _this = this; | ||
return generateToken(this.portal + "/generateToken", { | ||
username: this.username, | ||
password: this.password, | ||
expiration: this.tokenDuration | ||
}).then(function (response) { | ||
var options = __assign({ params: { | ||
username: this.username, | ||
password: this.password, | ||
expiration: this.tokenDuration | ||
} }, requestOptions); | ||
return generateToken(this.portal + "/generateToken", options).then(function (response) { | ||
_this._token = response.token; | ||
@@ -585,3 +600,3 @@ _this._tokenExpires = new Date(response.expires); | ||
*/ | ||
UserSession.prototype.refreshWithRefreshToken = function () { | ||
UserSession.prototype.refreshWithRefreshToken = function (requestOptions) { | ||
var _this = this; | ||
@@ -591,9 +606,10 @@ if (this.refreshToken && | ||
this.refreshTokenExpires.getTime() < Date.now()) { | ||
return this.refreshRefreshToken(); | ||
return this.refreshRefreshToken(requestOptions); | ||
} | ||
return fetchToken(this.portal + "/oauth2/token", { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
grant_type: "refresh_token" | ||
}).then(function (response) { | ||
var options = __assign({ params: { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
grant_type: "refresh_token" | ||
} }, requestOptions); | ||
return fetchToken(this.portal + "/oauth2/token", options).then(function (response) { | ||
_this._token = response.token; | ||
@@ -608,10 +624,11 @@ _this._tokenExpires = response.expires; | ||
*/ | ||
UserSession.prototype.refreshRefreshToken = function () { | ||
UserSession.prototype.refreshRefreshToken = function (requestOptions) { | ||
var _this = this; | ||
return fetchToken(this.portal + "/oauth2/token", { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
redirect_uri: this.redirectUri, | ||
grant_type: "exchange_refresh_token" | ||
}).then(function (response) { | ||
var options = __assign({ params: { | ||
client_id: this.clientId, | ||
refresh_token: this.refreshToken, | ||
redirect_uri: this.redirectUri, | ||
grant_type: "exchange_refresh_token" | ||
} }, requestOptions); | ||
return fetchToken(this.portal + "/oauth2/token", options).then(function (response) { | ||
_this._token = response.token; | ||
@@ -618,0 +635,0 @@ _this._tokenExpires = response.expires; |
/* @preserve | ||
* @esri/arcgis-rest-auth - v1.6.0 - Fri Jul 27 2018 15:12:39 GMT-0700 (PDT) | ||
* Copyright (c) 2017 - 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 | ||
* @esri/arcgis-rest-auth - v1.7.0 - Apache-2.0 | ||
* Copyright (c) 2017-2018 Esri, Inc. | ||
* Wed Aug 08 2018 00:07:19 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.arcgisRest=e.arcgisRest||{},e.arcgisRest)}(this,function(e,r){"use strict";function t(e,t){return r.request(e,{params:t}).then(function(e){var r={token:e.access_token,username:e.username,expires:new Date(Date.now()+(60*e.expires_in*1e3-6e4))};return e.refresh_token&&(r.refreshToken=e.refresh_token),r})}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 t(this.portal+"/oauth2/token/",{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}).then(function(r){return e._pendingTokenRequest=null,e.token=r.token,e.expires=r.expires,r.token})},e.prototype.refreshSession=function(){var e=this;return this.refreshToken().then(function(){return e})},e}(),o=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var o in r=arguments[t])Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o]);return e};function i(e,t){return"undefined"!=typeof window&&window.location&&window.location.host?t.referer=window.location.host:t.referer="@esri.arcgis-rest-auth",r.request(e,{params:t})}var s=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.provider=e.provider||"arcgis",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(t,n){void 0===n&&(n=window);var i,s=o({portal:"https://www.arcgis.com/sharing/rest",provider:"arcgis",duration:20160,popup:!0,state:t.clientId,locale:""},t),a=s.portal,h=s.provider,u=s.clientId,p=s.duration,c=s.redirectUri,f=s.popup,k=s.state,d=s.locale;if(i="arcgis"===h?a+"/oauth2/authorize?client_id="+u+"&response_type=token&expiration="+p+"&redirect_uri="+encodeURIComponent(c)+"&state="+k+"&locale="+d:a+"/oauth2/social/authorize?client_id="+u+"&socialLoginProviderName="+h+"&autoAccountCreateForSocial=true&response_type=token&expiration="+p+"&redirect_uri="+encodeURIComponent(c)+"&state="+k+"&locale="+d,f){var l,T=((l={promise:null,resolve:null,reject:null}).promise=new Promise(function(e,r){l.resolve=e,l.reject=r}),l);return n["__ESRI_REST_AUTH_HANDLER_"+u]=function(t,n){if(t){var o=JSON.parse(t);T.reject(new r.ArcGISAuthError(o.errorMessage,o.error))}else if(n){var i=JSON.parse(n);T.resolve(new e({clientId:u,portal:a,token:i.token,tokenExpires:new Date(i.expires),username:i.username}))}},n.open(i,"oauth-window","height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"),T.promise}n.location.href=i},e.completeOAuth2=function(t,n){void 0===n&&(n=window);var i=o({portal:"https://www.arcgis.com/sharing/rest"},t),s=i.portal,a=i.clientId;function h(t,o){if(n.opener&&n.opener.parent)return n.opener.parent["__ESRI_REST_AUTH_HANDLER_"+a](t?JSON.stringify(t):void 0,JSON.stringify(o)),void n.close();if(n!==n.parent)return n.parent["__ESRI_REST_AUTH_HANDLER_"+a](t?JSON.stringify(t):void 0,JSON.stringify(o)),void n.close();if(t)throw new r.ArcGISAuthError(t.errorMessage,t.error);return new e({clientId:a,portal:s,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 h({error:p[1],errorMessage:decodeURIComponent(p[2])})}return h(void 0,{token:u[1],expires:new Date(Date.now()+1e3*parseInt(u[2],10)-6e4),username:decodeURIComponent(u[3])})},e.authorize=function(e,r){var t=o({portal:"https://arcgis.com/sharing/rest",duration:20160},e),n=t.portal,i=t.clientId,s=t.duration,a=t.redirectUri;r.writeHead(301,{Location:n+"/oauth2/authorize?client_id="+i+"&duration="+s+"&response_type=code&redirect_uri="+encodeURIComponent(a)}),r.end()},e.exchangeAuthorizationCode=function(r,n){var i=o({portal:"https://www.arcgis.com/sharing/rest",duration:20160,refreshTokenTTL:1440},r),s=i.portal,a=i.clientId,h=(i.duration,i.redirectUri),u=i.refreshTokenTTL;return t(s+"/oauth2/token",{grant_type:"authorization_code",client_id:a,redirect_uri:h,code:n}).then(function(r){return new e({clientId:a,portal:s,redirectUri:h,refreshToken:r.refreshToken,refreshTokenTTL:u,refreshTokenExpires:new Date(Date.now()+1e3*(u-1)),token:r.token,tokenExpires:r.expires,username:r.username})})},e.deserialize=function(r){var t=JSON.parse(r);return new e({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,tokenDuration:t.tokenDuration,redirectUri:t.redirectUri,refreshTokenTTL:t.refreshTokenTTL})},e.fromCredential=function(r){return new e({portal:r.server+"/sharing/rest",token:r.token,username:r.userId,tokenExpires:new Date(r.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(r){return r(e._user)});var t=this.portal+"/community/users/"+encodeURIComponent(this.username);return r.request(t,{httpMethod:"GET",authentication:this}).then(function(r){return e._user=r,r})},e.prototype.getToken=function(e){return/^https?:\/\/\S+\.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 r.ArcGISAuthError("Unable to refresh token."))},e.prototype.getTokenForServer=function(e){var t=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]=r.request(n+"/rest/info").then(function(e){return e.owningSystemUrl}).then(function(n){if(!n||!new RegExp(n).test(t.portal))throw new r.ArcGISAuthError(e+" is not federated with "+t.portal+".","NOT_FEDERATED");return r.request(n+"/sharing/rest/info")}).then(function(e){return e.authInfo.tokenServicesUrl}).then(function(r){return t.token?i(r,{token:t.token,serverUrl:e,expiration:t.tokenDuration}):i(r,{username:t.username,password:t.password,expiration:t.tokenDuration}).then(function(e){return t._token=e.token,t._tokenExpires=new Date(e.expires),e})}).then(function(e){return t.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(r){return e._pendingTokenRequests[e.portal]=null,r.token})),this._pendingTokenRequests[this.portal])},e.prototype.refreshWithUsernameAndPassword=function(){var e=this;return i(this.portal+"/generateToken",{username:this.username,password:this.password,expiration:this.tokenDuration}).then(function(r){return e._token=r.token,e._tokenExpires=new Date(r.expires),e})},e.prototype.refreshWithRefreshToken=function(){var e=this;return this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now()?this.refreshRefreshToken():t(this.portal+"/oauth2/token",{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}).then(function(r){return e._token=r.token,e._tokenExpires=r.expires,e})},e.prototype.refreshRefreshToken=function(){var e=this;return t(this.portal+"/oauth2/token",{client_id:this.clientId,refresh_token:this.refreshToken,redirect_uri:this.redirectUri,grant_type:"exchange_refresh_token"}).then(function(r){return e._token=r.token,e._tokenExpires=r.expires,e._refreshToken=r.refreshToken,e._refreshTokenExpires=new Date(Date.now()+60*(e.refreshTokenTTL-1)*1e3),e})},e}();e.ApplicationSession=n,e.UserSession=s,e.fetchToken=t,e.generateToken=i,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.arcgisRest=e.arcgisRest||{},e.arcgisRest)}(this,function(e,r){"use strict";var t=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var o in r=arguments[t])Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o]);return e};function n(e,t){var n=t.params?t:{params:t};return r.request(e,n).then(function(e){var r={token:e.access_token,username:e.username,expires:new Date(Date.now()+(60*e.expires_in*1e3-6e4))};return e.refresh_token&&(r.refreshToken=e.refresh_token),r})}var o=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,r){return this.token&&this.expires&&this.expires.getTime()>Date.now()?Promise.resolve(this.token):this._pendingTokenRequest?this._pendingTokenRequest:(this._pendingTokenRequest=this.refreshToken(r),this._pendingTokenRequest)},e.prototype.refreshToken=function(e){var r=this,o=t({params:{client_id:this.clientId,client_secret:this.clientSecret,grant_type:"client_credentials"}},e);return n(this.portal+"/oauth2/token/",o).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 s(e,t){var n=t.params?t:{params:t};return"undefined"!=typeof window&&window.location&&window.location.host?n.params.referer=window.location.host:n.params.referer="@esri.arcgis-rest-auth",r.request(e,n)}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.provider=e.provider||"arcgis",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(n,o){void 0===o&&(o=window);var s,i=t({portal:"https://www.arcgis.com/sharing/rest",provider:"arcgis",duration:20160,popup:!0,state:n.clientId,locale:""},n),a=i.portal,h=i.provider,p=i.clientId,u=i.duration,c=i.redirectUri,f=i.popup,k=i.state,d=i.locale;if(s="arcgis"===h?a+"/oauth2/authorize?client_id="+p+"&response_type=token&expiration="+u+"&redirect_uri="+encodeURIComponent(c)+"&state="+k+"&locale="+d:a+"/oauth2/social/authorize?client_id="+p+"&socialLoginProviderName="+h+"&autoAccountCreateForSocial=true&response_type=token&expiration="+u+"&redirect_uri="+encodeURIComponent(c)+"&state="+k+"&locale="+d,f){var l,T=((l={promise:null,resolve:null,reject:null}).promise=new Promise(function(e,r){l.resolve=e,l.reject=r}),l);return o["__ESRI_REST_AUTH_HANDLER_"+p]=function(t,n){if(t){var o=JSON.parse(t);T.reject(new r.ArcGISAuthError(o.errorMessage,o.error))}else if(n){var s=JSON.parse(n);T.resolve(new e({clientId:p,portal:a,token:s.token,tokenExpires:new Date(s.expires),username:s.username}))}},o.open(s,"oauth-window","height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"),T.promise}o.location.href=s},e.completeOAuth2=function(n,o){void 0===o&&(o=window);var s=t({portal:"https://www.arcgis.com/sharing/rest"},n),i=s.portal,a=s.clientId;function h(t,n){if(o.opener&&o.opener.parent)return o.opener.parent["__ESRI_REST_AUTH_HANDLER_"+a](t?JSON.stringify(t):void 0,JSON.stringify(n)),void o.close();if(o!==o.parent)return o.parent["__ESRI_REST_AUTH_HANDLER_"+a](t?JSON.stringify(t):void 0,JSON.stringify(n)),void o.close();if(t)throw new r.ArcGISAuthError(t.errorMessage,t.error);return new e({clientId:a,portal:i,token:n.token,tokenExpires:n.expires,username:n.username})}var p=o.location.href.match(/access_token=(.+)&expires_in=(.+)&username=([^&]+)/);if(!p){var u=o.location.href.match(/error=(.+)&error_description=(.+)/);return h({error:u[1],errorMessage:decodeURIComponent(u[2])})}return h(void 0,{token:p[1],expires:new Date(Date.now()+1e3*parseInt(p[2],10)-6e4),username:decodeURIComponent(p[3])})},e.authorize=function(e,r){var n=t({portal:"https://arcgis.com/sharing/rest",duration:20160},e),o=n.portal,s=n.clientId,i=n.duration,a=n.redirectUri;r.writeHead(301,{Location:o+"/oauth2/authorize?client_id="+s+"&duration="+i+"&response_type=code&redirect_uri="+encodeURIComponent(a)}),r.end()},e.exchangeAuthorizationCode=function(r,o){var s=t({portal:"https://www.arcgis.com/sharing/rest",duration:20160,refreshTokenTTL:1440},r),i=s.portal,a=s.clientId,h=(s.duration,s.redirectUri),p=s.refreshTokenTTL;return n(i+"/oauth2/token",{grant_type:"authorization_code",client_id:a,redirect_uri:h,code:o}).then(function(r){return new e({clientId:a,portal:i,redirectUri:h,refreshToken:r.refreshToken,refreshTokenTTL:p,refreshTokenExpires:new Date(Date.now()+1e3*(p-1)),token:r.token,tokenExpires:r.expires,username:r.username})})},e.deserialize=function(r){var t=JSON.parse(r);return new e({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,tokenDuration:t.tokenDuration,redirectUri:t.redirectUri,refreshTokenTTL:t.refreshTokenTTL})},e.fromCredential=function(r){return new e({portal:r.server+"/sharing/rest",token:r.token,username:r.userId,tokenExpires:new Date(r.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(r){return r(e._user)});var t=this.portal+"/community/users/"+encodeURIComponent(this.username);return r.request(t,{httpMethod:"GET",authentication:this}).then(function(r){return e._user=r,r})},e.prototype.getToken=function(e,r){return/^https?:\/\/\S+\.arcgis\.com\/sharing\/rest/.test(this.portal)&&/^https?:\/\/\S+\.arcgis\.com.+/.test(e)?this.getFreshToken(r):new RegExp(this.portal).test(e)?this.getFreshToken(r):this.getTokenForServer(e,r)},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(e){return this.username&&this.password?this.refreshWithUsernameAndPassword(e):this.clientId&&this.refreshToken?this.refreshWithRefreshToken():Promise.reject(new r.ArcGISAuthError("Unable to refresh token."))},e.prototype.getTokenForServer=function(e,t){var n=this,o=e.split("/rest/services/")[0],i=this.trustedServers[o];return i&&i.expires.getTime()>Date.now()?Promise.resolve(i.token):this._pendingTokenRequests[o]?this._pendingTokenRequests[o]:(this._pendingTokenRequests[o]=r.request(o+"/rest/info").then(function(e){return e.owningSystemUrl}).then(function(o){if(!o||!new RegExp(o).test(n.portal))throw new r.ArcGISAuthError(e+" is not federated with "+n.portal+".","NOT_FEDERATED");return r.request(o+"/sharing/rest/info",t)}).then(function(e){return e.authInfo.tokenServicesUrl}).then(function(r){return n.token?s(r,{params:{token:n.token,serverUrl:e,expiration:n.tokenDuration}}):s(r,{params:{username:n.username,password:n.password,expiration:n.tokenDuration}}).then(function(e){return n._token=e.token,n._tokenExpires=new Date(e.expires),e})}).then(function(e){return n.trustedServers[o]={expires:new Date(e.expires),token:e.token},e.token}),this._pendingTokenRequests[o])},e.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])},e.prototype.refreshWithUsernameAndPassword=function(e){var r=this,n=t({params:{username:this.username,password:this.password,expiration:this.tokenDuration}},e);return s(this.portal+"/generateToken",n).then(function(e){return r._token=e.token,r._tokenExpires=new Date(e.expires),r})},e.prototype.refreshWithRefreshToken=function(e){var r=this;if(this.refreshToken&&this.refreshTokenExpires&&this.refreshTokenExpires.getTime()<Date.now())return this.refreshRefreshToken(e);var o=t({params:{client_id:this.clientId,refresh_token:this.refreshToken,grant_type:"refresh_token"}},e);return n(this.portal+"/oauth2/token",o).then(function(e){return r._token=e.token,r._tokenExpires=e.expires,r})},e.prototype.refreshRefreshToken=function(e){var r=this,o=t({params:{client_id:this.clientId,refresh_token:this.refreshToken,redirect_uri:this.redirectUri,grant_type:"exchange_refresh_token"}},e);return n(this.portal+"/oauth2/token",o).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})},e}();e.ApplicationSession=o,e.UserSession=i,e.fetchToken=n,e.generateToken=s,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=auth.umd.min.js.map |
{ | ||
"name": "@esri/arcgis-rest-auth", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Authentication helpers for @esri/arcgis-rest-*.", | ||
@@ -22,4 +22,4 @@ "main": "dist/node/index.js", | ||
"devDependencies": { | ||
"@esri/arcgis-rest-common-types": "^1.6.0", | ||
"@esri/arcgis-rest-request": "^1.6.0" | ||
"@esri/arcgis-rest-common-types": "^1.7.0", | ||
"@esri/arcgis-rest-request": "^1.7.0" | ||
}, | ||
@@ -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
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
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
222628
2272