@itwin/map-layers-auth 4.0.0-dev.52 → 4.0.0-dev.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/ArcGis/ArcGisAccessClient.d.ts +72 -72
- package/lib/cjs/ArcGis/ArcGisAccessClient.js +286 -286
- package/lib/cjs/ArcGis/ArcGisOAuth2Endpoint.d.ts +22 -22
- package/lib/cjs/ArcGis/ArcGisOAuth2Endpoint.js +46 -46
- package/lib/cjs/ArcGis/ArcGisTokenGenerator.d.ts +41 -41
- package/lib/cjs/ArcGis/ArcGisTokenGenerator.js +109 -109
- package/lib/cjs/ArcGis/ArcGisTokenGenerator.js.map +1 -1
- package/lib/cjs/ArcGis/ArcGisTokenManager.d.ts +20 -20
- package/lib/cjs/ArcGis/ArcGisTokenManager.js +111 -111
- package/lib/cjs/ArcGis/ArcGisTokenManager.js.map +1 -1
- package/lib/cjs/ArcGis/ArcGisUrl.d.ts +6 -6
- package/lib/cjs/ArcGis/ArcGisUrl.js +48 -48
- package/lib/cjs/map-layers-auth.d.ts +5 -5
- package/lib/cjs/map-layers-auth.js +25 -21
- package/lib/cjs/map-layers-auth.js.map +1 -1
- package/lib/esm/ArcGis/ArcGisAccessClient.d.ts +72 -72
- package/lib/esm/ArcGis/ArcGisAccessClient.js +282 -282
- package/lib/esm/ArcGis/ArcGisOAuth2Endpoint.d.ts +22 -22
- package/lib/esm/ArcGis/ArcGisOAuth2Endpoint.js +42 -42
- package/lib/esm/ArcGis/ArcGisTokenGenerator.d.ts +41 -41
- package/lib/esm/ArcGis/ArcGisTokenGenerator.js +106 -105
- package/lib/esm/ArcGis/ArcGisTokenGenerator.js.map +1 -1
- package/lib/esm/ArcGis/ArcGisTokenManager.d.ts +20 -20
- package/lib/esm/ArcGis/ArcGisTokenManager.js +108 -107
- package/lib/esm/ArcGis/ArcGisTokenManager.js.map +1 -1
- package/lib/esm/ArcGis/ArcGisUrl.d.ts +6 -6
- package/lib/esm/ArcGis/ArcGisUrl.js +44 -44
- package/lib/esm/map-layers-auth.d.ts +5 -5
- package/lib/esm/map-layers-auth.js +9 -9
- package/package.json +8 -8
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ArcGisOAuth2Endpoint = exports.ArcGisOAuth2EndpointType = void 0;
|
|
4
|
-
/** @internal */
|
|
5
|
-
var ArcGisOAuth2EndpointType;
|
|
6
|
-
(function (ArcGisOAuth2EndpointType) {
|
|
7
|
-
ArcGisOAuth2EndpointType[ArcGisOAuth2EndpointType["Authorize"] = 0] = "Authorize";
|
|
8
|
-
ArcGisOAuth2EndpointType[ArcGisOAuth2EndpointType["Token"] = 1] = "Token";
|
|
9
|
-
})(ArcGisOAuth2EndpointType = exports.ArcGisOAuth2EndpointType || (exports.ArcGisOAuth2EndpointType = {}));
|
|
10
|
-
/** @internal */
|
|
11
|
-
class ArcGisOAuth2Endpoint {
|
|
12
|
-
constructor(url, loginUrl, isArcgisOnline) {
|
|
13
|
-
this._url = url;
|
|
14
|
-
this._loginUrl = loginUrl;
|
|
15
|
-
this._isArcgisOnline = isArcgisOnline;
|
|
16
|
-
}
|
|
17
|
-
// Returns the actual endpoint url
|
|
18
|
-
getUrl() {
|
|
19
|
-
return this._url;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Returns the URL used to login and generate the Oauth token.
|
|
23
|
-
* @param stateData Custom JSON data that will sent back by once the Oauth process completes
|
|
24
|
-
* @returns Promise resolves after the defaults are setup.
|
|
25
|
-
* @internal
|
|
26
|
-
*/
|
|
27
|
-
getLoginUrl(stateData) {
|
|
28
|
-
const urlObj = new URL(this._loginUrl);
|
|
29
|
-
const data = {
|
|
30
|
-
endpointOrigin: new URL(this._url).origin,
|
|
31
|
-
customData: stateData, // caller's data
|
|
32
|
-
};
|
|
33
|
-
let stateStr;
|
|
34
|
-
try {
|
|
35
|
-
stateStr = JSON.stringify(data);
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
}
|
|
39
|
-
if (stateStr !== undefined) {
|
|
40
|
-
urlObj.searchParams.set("state", stateStr);
|
|
41
|
-
}
|
|
42
|
-
return urlObj.toString();
|
|
43
|
-
}
|
|
44
|
-
get isArcgisOnline() { return this._isArcgisOnline; }
|
|
45
|
-
}
|
|
46
|
-
exports.ArcGisOAuth2Endpoint = ArcGisOAuth2Endpoint;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArcGisOAuth2Endpoint = exports.ArcGisOAuth2EndpointType = void 0;
|
|
4
|
+
/** @internal */
|
|
5
|
+
var ArcGisOAuth2EndpointType;
|
|
6
|
+
(function (ArcGisOAuth2EndpointType) {
|
|
7
|
+
ArcGisOAuth2EndpointType[ArcGisOAuth2EndpointType["Authorize"] = 0] = "Authorize";
|
|
8
|
+
ArcGisOAuth2EndpointType[ArcGisOAuth2EndpointType["Token"] = 1] = "Token";
|
|
9
|
+
})(ArcGisOAuth2EndpointType = exports.ArcGisOAuth2EndpointType || (exports.ArcGisOAuth2EndpointType = {}));
|
|
10
|
+
/** @internal */
|
|
11
|
+
class ArcGisOAuth2Endpoint {
|
|
12
|
+
constructor(url, loginUrl, isArcgisOnline) {
|
|
13
|
+
this._url = url;
|
|
14
|
+
this._loginUrl = loginUrl;
|
|
15
|
+
this._isArcgisOnline = isArcgisOnline;
|
|
16
|
+
}
|
|
17
|
+
// Returns the actual endpoint url
|
|
18
|
+
getUrl() {
|
|
19
|
+
return this._url;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns the URL used to login and generate the Oauth token.
|
|
23
|
+
* @param stateData Custom JSON data that will sent back by once the Oauth process completes
|
|
24
|
+
* @returns Promise resolves after the defaults are setup.
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
getLoginUrl(stateData) {
|
|
28
|
+
const urlObj = new URL(this._loginUrl);
|
|
29
|
+
const data = {
|
|
30
|
+
endpointOrigin: new URL(this._url).origin,
|
|
31
|
+
customData: stateData, // caller's data
|
|
32
|
+
};
|
|
33
|
+
let stateStr;
|
|
34
|
+
try {
|
|
35
|
+
stateStr = JSON.stringify(data);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
}
|
|
39
|
+
if (stateStr !== undefined) {
|
|
40
|
+
urlObj.searchParams.set("state", stateStr);
|
|
41
|
+
}
|
|
42
|
+
return urlObj.toString();
|
|
43
|
+
}
|
|
44
|
+
get isArcgisOnline() { return this._isArcgisOnline; }
|
|
45
|
+
}
|
|
46
|
+
exports.ArcGisOAuth2Endpoint = ArcGisOAuth2Endpoint;
|
|
47
47
|
//# sourceMappingURL=ArcGisOAuth2Endpoint.js.map
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { MapLayerAccessToken } from "@itwin/core-frontend";
|
|
2
|
-
/** @packageDocumentation
|
|
3
|
-
* @module Tiles
|
|
4
|
-
*/
|
|
5
|
-
/** @internal */
|
|
6
|
-
export interface ArcGisOAuth2Token extends MapLayerAccessToken {
|
|
7
|
-
expiresAt: number;
|
|
8
|
-
ssl: boolean;
|
|
9
|
-
userName: string;
|
|
10
|
-
persist?: boolean;
|
|
11
|
-
}
|
|
12
|
-
/** @internal */
|
|
13
|
-
export interface ArcGisToken extends MapLayerAccessToken {
|
|
14
|
-
expires: number;
|
|
15
|
-
ssl: boolean;
|
|
16
|
-
}
|
|
17
|
-
/** @internal */
|
|
18
|
-
export declare enum ArcGisTokenClientType {
|
|
19
|
-
ip = 0,
|
|
20
|
-
referer = 1,
|
|
21
|
-
requestIp = 2
|
|
22
|
-
}
|
|
23
|
-
/** @internal */
|
|
24
|
-
export interface ArcGisGenerateTokenOptions {
|
|
25
|
-
client: ArcGisTokenClientType;
|
|
26
|
-
ip?: string;
|
|
27
|
-
referer?: string;
|
|
28
|
-
expiration?: number;
|
|
29
|
-
}
|
|
30
|
-
/** @internal */
|
|
31
|
-
export declare class ArcGisTokenGenerator {
|
|
32
|
-
private static readonly restApiPath;
|
|
33
|
-
private static readonly restApiInfoPath;
|
|
34
|
-
private static _tokenServiceUrlCache;
|
|
35
|
-
static fetchTokenServiceUrl(arcGisRestServiceUrl: string): Promise<string | undefined>;
|
|
36
|
-
static getTokenServiceFromInfoJson(json: any): string | undefined;
|
|
37
|
-
getTokenServiceUrl(baseUrl: string): Promise<string | undefined>;
|
|
38
|
-
generate(arcGisRestServiceUrl: string, userName: string, password: string, options: ArcGisGenerateTokenOptions): Promise<any>;
|
|
39
|
-
static formEncode(str: string): string;
|
|
40
|
-
static rfc1738Encode(str: string): string;
|
|
41
|
-
}
|
|
1
|
+
import { MapLayerAccessToken } from "@itwin/core-frontend";
|
|
2
|
+
/** @packageDocumentation
|
|
3
|
+
* @module Tiles
|
|
4
|
+
*/
|
|
5
|
+
/** @internal */
|
|
6
|
+
export interface ArcGisOAuth2Token extends MapLayerAccessToken {
|
|
7
|
+
expiresAt: number;
|
|
8
|
+
ssl: boolean;
|
|
9
|
+
userName: string;
|
|
10
|
+
persist?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** @internal */
|
|
13
|
+
export interface ArcGisToken extends MapLayerAccessToken {
|
|
14
|
+
expires: number;
|
|
15
|
+
ssl: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** @internal */
|
|
18
|
+
export declare enum ArcGisTokenClientType {
|
|
19
|
+
ip = 0,
|
|
20
|
+
referer = 1,
|
|
21
|
+
requestIp = 2
|
|
22
|
+
}
|
|
23
|
+
/** @internal */
|
|
24
|
+
export interface ArcGisGenerateTokenOptions {
|
|
25
|
+
client: ArcGisTokenClientType;
|
|
26
|
+
ip?: string;
|
|
27
|
+
referer?: string;
|
|
28
|
+
expiration?: number;
|
|
29
|
+
}
|
|
30
|
+
/** @internal */
|
|
31
|
+
export declare class ArcGisTokenGenerator {
|
|
32
|
+
private static readonly restApiPath;
|
|
33
|
+
private static readonly restApiInfoPath;
|
|
34
|
+
private static _tokenServiceUrlCache;
|
|
35
|
+
static fetchTokenServiceUrl(arcGisRestServiceUrl: string): Promise<string | undefined>;
|
|
36
|
+
static getTokenServiceFromInfoJson(json: any): string | undefined;
|
|
37
|
+
getTokenServiceUrl(baseUrl: string): Promise<string | undefined>;
|
|
38
|
+
generate(arcGisRestServiceUrl: string, userName: string, password: string, options: ArcGisGenerateTokenOptions): Promise<any>;
|
|
39
|
+
static formEncode(str: string): string;
|
|
40
|
+
static rfc1738Encode(str: string): string;
|
|
41
|
+
}
|
|
42
42
|
//# sourceMappingURL=ArcGisTokenGenerator.d.ts.map
|
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ArcGisTokenGenerator = exports.ArcGisTokenClientType = void 0;
|
|
4
|
-
// client application's base URL, a user-specified IP address, or the IP address that is making the request.
|
|
5
|
-
/** @internal */
|
|
6
|
-
var ArcGisTokenClientType;
|
|
7
|
-
(function (ArcGisTokenClientType) {
|
|
8
|
-
ArcGisTokenClientType[ArcGisTokenClientType["ip"] = 0] = "ip";
|
|
9
|
-
ArcGisTokenClientType[ArcGisTokenClientType["referer"] = 1] = "referer";
|
|
10
|
-
ArcGisTokenClientType[ArcGisTokenClientType["requestIp"] = 2] = "requestIp";
|
|
11
|
-
})(ArcGisTokenClientType = exports.ArcGisTokenClientType || (exports.ArcGisTokenClientType = {}));
|
|
12
|
-
/** @internal */
|
|
13
|
-
class ArcGisTokenGenerator {
|
|
14
|
-
static async fetchTokenServiceUrl(arcGisRestServiceUrl) {
|
|
15
|
-
const lowerUrl = arcGisRestServiceUrl.toLowerCase();
|
|
16
|
-
const restApiIdx = lowerUrl.indexOf(ArcGisTokenGenerator.restApiPath);
|
|
17
|
-
if (restApiIdx === -1)
|
|
18
|
-
return undefined;
|
|
19
|
-
const infoUrl = arcGisRestServiceUrl.substring(0, restApiIdx + ArcGisTokenGenerator.restApiPath.length) + ArcGisTokenGenerator.restApiInfoPath;
|
|
20
|
-
let tokenServicesUrl;
|
|
21
|
-
try {
|
|
22
|
-
const response = await fetch(infoUrl, { method: "GET" });
|
|
23
|
-
const json = await response.json();
|
|
24
|
-
tokenServicesUrl = ArcGisTokenGenerator.getTokenServiceFromInfoJson(json);
|
|
25
|
-
}
|
|
26
|
-
catch (_error) {
|
|
27
|
-
}
|
|
28
|
-
return tokenServicesUrl;
|
|
29
|
-
}
|
|
30
|
-
static getTokenServiceFromInfoJson(json) {
|
|
31
|
-
return json.authInfo?.isTokenBasedSecurity ? json?.authInfo?.tokenServicesUrl : undefined;
|
|
32
|
-
}
|
|
33
|
-
async getTokenServiceUrl(baseUrl) {
|
|
34
|
-
const cached = ArcGisTokenGenerator._tokenServiceUrlCache.get(baseUrl);
|
|
35
|
-
if (cached !== undefined)
|
|
36
|
-
return cached;
|
|
37
|
-
const tokenServiceUrl = await ArcGisTokenGenerator.fetchTokenServiceUrl(baseUrl);
|
|
38
|
-
if (tokenServiceUrl !== undefined)
|
|
39
|
-
ArcGisTokenGenerator._tokenServiceUrlCache.set(baseUrl, tokenServiceUrl);
|
|
40
|
-
return tokenServiceUrl;
|
|
41
|
-
}
|
|
42
|
-
// base url: ArcGis REST service base URL (format must be "https://<host>/<instance>/rest/")
|
|
43
|
-
async generate(arcGisRestServiceUrl, userName, password, options) {
|
|
44
|
-
const tokenServiceUrl = await this.getTokenServiceUrl(arcGisRestServiceUrl);
|
|
45
|
-
if (!tokenServiceUrl)
|
|
46
|
-
return undefined;
|
|
47
|
-
let token;
|
|
48
|
-
try {
|
|
49
|
-
const encodedUsername = encodeURIComponent(userName);
|
|
50
|
-
const encodedPassword = encodeURIComponent(password);
|
|
51
|
-
// Compose the expiration param
|
|
52
|
-
let expirationStr = "";
|
|
53
|
-
if (options.expiration) {
|
|
54
|
-
expirationStr = `&expiration=${options.expiration}`;
|
|
55
|
-
}
|
|
56
|
-
// Compose the client param
|
|
57
|
-
let clientStr = "";
|
|
58
|
-
if (options.client === ArcGisTokenClientType.referer) {
|
|
59
|
-
let refererStr = "";
|
|
60
|
-
if (options.referer === undefined) {
|
|
61
|
-
refererStr = encodeURIComponent(location.origin); // default to application origin
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
refererStr = encodeURIComponent(options.referer);
|
|
65
|
-
}
|
|
66
|
-
clientStr = `&client=referer&referer=${refererStr}`;
|
|
67
|
-
}
|
|
68
|
-
else if (options.client === ArcGisTokenClientType.ip) {
|
|
69
|
-
if (options.ip === undefined)
|
|
70
|
-
return token;
|
|
71
|
-
clientStr = `&client=ip&ip=${options.ip}`;
|
|
72
|
-
}
|
|
73
|
-
else if (options.client === ArcGisTokenClientType.requestIp) {
|
|
74
|
-
clientStr = `&client=requestip&ip=`;
|
|
75
|
-
}
|
|
76
|
-
const httpRequestOptions = {
|
|
77
|
-
method: "POST",
|
|
78
|
-
body: `username=${encodedUsername}&password=${encodedPassword}${clientStr}${expirationStr}&f=pjson`,
|
|
79
|
-
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
80
|
-
};
|
|
81
|
-
const response = await fetch(tokenServiceUrl, httpRequestOptions);
|
|
82
|
-
// Check a token was really generated (an error could be part of the body)
|
|
83
|
-
token = await response.json();
|
|
84
|
-
}
|
|
85
|
-
catch (_error) {
|
|
86
|
-
}
|
|
87
|
-
return token;
|
|
88
|
-
}
|
|
89
|
-
// Encode following 'application/x-www-form-urlencoded' standard (https://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.3)
|
|
90
|
-
// Also mentioned here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
|
|
91
|
-
static formEncode(str) {
|
|
92
|
-
return ArcGisTokenGenerator.rfc1738Encode(str).replace(/%20/g, "+");
|
|
93
|
-
}
|
|
94
|
-
// Encode following RFC1738 standard (https://www.ietf.org/rfc/rfc1738.txt)
|
|
95
|
-
// Code from https://locutus.io/php/url/rawurlencode/
|
|
96
|
-
static rfc1738Encode(str) {
|
|
97
|
-
return encodeURIComponent(str)
|
|
98
|
-
.replace(/!/g, "%21")
|
|
99
|
-
.replace(/'/g, "%27")
|
|
100
|
-
.replace(/\(/g, "%28")
|
|
101
|
-
.replace(/\)/g, "%29")
|
|
102
|
-
.replace(/\*/g, "%2A");
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
ArcGisTokenGenerator.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
ArcGisTokenGenerator
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArcGisTokenGenerator = exports.ArcGisTokenClientType = void 0;
|
|
4
|
+
// client application's base URL, a user-specified IP address, or the IP address that is making the request.
|
|
5
|
+
/** @internal */
|
|
6
|
+
var ArcGisTokenClientType;
|
|
7
|
+
(function (ArcGisTokenClientType) {
|
|
8
|
+
ArcGisTokenClientType[ArcGisTokenClientType["ip"] = 0] = "ip";
|
|
9
|
+
ArcGisTokenClientType[ArcGisTokenClientType["referer"] = 1] = "referer";
|
|
10
|
+
ArcGisTokenClientType[ArcGisTokenClientType["requestIp"] = 2] = "requestIp";
|
|
11
|
+
})(ArcGisTokenClientType = exports.ArcGisTokenClientType || (exports.ArcGisTokenClientType = {}));
|
|
12
|
+
/** @internal */
|
|
13
|
+
class ArcGisTokenGenerator {
|
|
14
|
+
static async fetchTokenServiceUrl(arcGisRestServiceUrl) {
|
|
15
|
+
const lowerUrl = arcGisRestServiceUrl.toLowerCase();
|
|
16
|
+
const restApiIdx = lowerUrl.indexOf(ArcGisTokenGenerator.restApiPath);
|
|
17
|
+
if (restApiIdx === -1)
|
|
18
|
+
return undefined;
|
|
19
|
+
const infoUrl = arcGisRestServiceUrl.substring(0, restApiIdx + ArcGisTokenGenerator.restApiPath.length) + ArcGisTokenGenerator.restApiInfoPath;
|
|
20
|
+
let tokenServicesUrl;
|
|
21
|
+
try {
|
|
22
|
+
const response = await fetch(infoUrl, { method: "GET" });
|
|
23
|
+
const json = await response.json();
|
|
24
|
+
tokenServicesUrl = ArcGisTokenGenerator.getTokenServiceFromInfoJson(json);
|
|
25
|
+
}
|
|
26
|
+
catch (_error) {
|
|
27
|
+
}
|
|
28
|
+
return tokenServicesUrl;
|
|
29
|
+
}
|
|
30
|
+
static getTokenServiceFromInfoJson(json) {
|
|
31
|
+
return json.authInfo?.isTokenBasedSecurity ? json?.authInfo?.tokenServicesUrl : undefined;
|
|
32
|
+
}
|
|
33
|
+
async getTokenServiceUrl(baseUrl) {
|
|
34
|
+
const cached = ArcGisTokenGenerator._tokenServiceUrlCache.get(baseUrl);
|
|
35
|
+
if (cached !== undefined)
|
|
36
|
+
return cached;
|
|
37
|
+
const tokenServiceUrl = await ArcGisTokenGenerator.fetchTokenServiceUrl(baseUrl);
|
|
38
|
+
if (tokenServiceUrl !== undefined)
|
|
39
|
+
ArcGisTokenGenerator._tokenServiceUrlCache.set(baseUrl, tokenServiceUrl);
|
|
40
|
+
return tokenServiceUrl;
|
|
41
|
+
}
|
|
42
|
+
// base url: ArcGis REST service base URL (format must be "https://<host>/<instance>/rest/")
|
|
43
|
+
async generate(arcGisRestServiceUrl, userName, password, options) {
|
|
44
|
+
const tokenServiceUrl = await this.getTokenServiceUrl(arcGisRestServiceUrl);
|
|
45
|
+
if (!tokenServiceUrl)
|
|
46
|
+
return undefined;
|
|
47
|
+
let token;
|
|
48
|
+
try {
|
|
49
|
+
const encodedUsername = encodeURIComponent(userName);
|
|
50
|
+
const encodedPassword = encodeURIComponent(password);
|
|
51
|
+
// Compose the expiration param
|
|
52
|
+
let expirationStr = "";
|
|
53
|
+
if (options.expiration) {
|
|
54
|
+
expirationStr = `&expiration=${options.expiration}`;
|
|
55
|
+
}
|
|
56
|
+
// Compose the client param
|
|
57
|
+
let clientStr = "";
|
|
58
|
+
if (options.client === ArcGisTokenClientType.referer) {
|
|
59
|
+
let refererStr = "";
|
|
60
|
+
if (options.referer === undefined) {
|
|
61
|
+
refererStr = encodeURIComponent(location.origin); // default to application origin
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
refererStr = encodeURIComponent(options.referer);
|
|
65
|
+
}
|
|
66
|
+
clientStr = `&client=referer&referer=${refererStr}`;
|
|
67
|
+
}
|
|
68
|
+
else if (options.client === ArcGisTokenClientType.ip) {
|
|
69
|
+
if (options.ip === undefined)
|
|
70
|
+
return token;
|
|
71
|
+
clientStr = `&client=ip&ip=${options.ip}`;
|
|
72
|
+
}
|
|
73
|
+
else if (options.client === ArcGisTokenClientType.requestIp) {
|
|
74
|
+
clientStr = `&client=requestip&ip=`;
|
|
75
|
+
}
|
|
76
|
+
const httpRequestOptions = {
|
|
77
|
+
method: "POST",
|
|
78
|
+
body: `username=${encodedUsername}&password=${encodedPassword}${clientStr}${expirationStr}&f=pjson`,
|
|
79
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
80
|
+
};
|
|
81
|
+
const response = await fetch(tokenServiceUrl, httpRequestOptions);
|
|
82
|
+
// Check a token was really generated (an error could be part of the body)
|
|
83
|
+
token = await response.json();
|
|
84
|
+
}
|
|
85
|
+
catch (_error) {
|
|
86
|
+
}
|
|
87
|
+
return token;
|
|
88
|
+
}
|
|
89
|
+
// Encode following 'application/x-www-form-urlencoded' standard (https://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.3)
|
|
90
|
+
// Also mentioned here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
|
|
91
|
+
static formEncode(str) {
|
|
92
|
+
return ArcGisTokenGenerator.rfc1738Encode(str).replace(/%20/g, "+");
|
|
93
|
+
}
|
|
94
|
+
// Encode following RFC1738 standard (https://www.ietf.org/rfc/rfc1738.txt)
|
|
95
|
+
// Code from https://locutus.io/php/url/rawurlencode/
|
|
96
|
+
static rfc1738Encode(str) {
|
|
97
|
+
return encodeURIComponent(str)
|
|
98
|
+
.replace(/!/g, "%21")
|
|
99
|
+
.replace(/'/g, "%27")
|
|
100
|
+
.replace(/\(/g, "%28")
|
|
101
|
+
.replace(/\)/g, "%29")
|
|
102
|
+
.replace(/\*/g, "%2A");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
ArcGisTokenGenerator.restApiPath = "/rest/";
|
|
106
|
+
ArcGisTokenGenerator.restApiInfoPath = "info?f=pjson";
|
|
107
|
+
// Cache info url to avoid fetching/parsing twice for the same base url.
|
|
108
|
+
ArcGisTokenGenerator._tokenServiceUrlCache = new Map();
|
|
109
|
+
exports.ArcGisTokenGenerator = ArcGisTokenGenerator;
|
|
110
110
|
//# sourceMappingURL=ArcGisTokenGenerator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ArcGisTokenGenerator.js","sourceRoot":"","sources":["../../../src/ArcGis/ArcGisTokenGenerator.ts"],"names":[],"mappings":";;;AAmCA,4GAA4G;AAC5G,gBAAgB;AAChB,IAAY,qBAIX;AAJD,WAAY,qBAAqB;IAC/B,6DAAE,CAAA;IACF,uEAAO,CAAA;IACP,2EAAS,CAAA;AACX,CAAC,EAJW,qBAAqB,GAArB,6BAAqB,KAArB,6BAAqB,QAIhC;AA8BD,gBAAgB;AAChB,MAAa,oBAAoB;IAOxB,MAAM,CAAC,KAAK,CAAC,oBAAoB,CAAC,oBAA4B;QACnE,MAAM,QAAQ,GAAG,oBAAoB,CAAC,WAAW,EAAE,CAAC;QACpD,MAAM,UAAU,GAAG,QAAQ,CAAC,OAAO,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC;QACtE,IAAI,UAAU,KAAK,CAAC,CAAC;YACnB,OAAO,SAAS,CAAC;QACnB,MAAM,OAAO,GAAG,oBAAoB,CAAC,SAAS,CAAC,CAAC,EAAE,UAAU,GAAG,oBAAoB,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,oBAAoB,CAAC,eAAe,CAAC;QAE/I,IAAI,gBAAoC,CAAC;QACzC,IAAI;YACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,OA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AC;IACf,CAAC;IAED,gIAAgI;IAChI,2HAA2H;IACpH,MAAM,CAAC,UAAU,CAAC,GAAW;QAClC,OAAO,oBAAoB,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACtE,CAAC;IAED,2EAA2E;IAC3E,qDAAqD;IAC9C,MAAM,CAAC,aAAa,CAAC,GAAW;QACrC,OAAO,kBAAkB,CAAC,GAAG,CAAC;aAC3B,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC;aACpB,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC;aACpB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC;aACrB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC;aACrB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAC3B,CAAC;;AA3GH,oDA4GC;AA3GyB,gCAAW,GAAG,QAAQ,CAAC;AACvB,oCAAe,GAAG,cAAc,CAAC;AAEzD,wEAAwE;AACzD,0CAAqB,GAAG,IAAI,GAAG,EAAkB,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\nimport { MapLayerAccessToken } from \"@itwin/core-frontend\";\r\n\r\n/** @packageDocumentation\r\n * @module Tiles\r\n */\r\n\r\n/** @internal */\r\nexport interface ArcGisOAuth2Token extends MapLayerAccessToken {\r\n\r\n // The expiration time of the token in milliseconds (UNIX time)\r\n expiresAt: number;\r\n\r\n // This property will show as true if the token must always pass over ssl.\r\n ssl: boolean;\r\n\r\n // Username associated with this token\r\n userName: string;\r\n\r\n // A Binary value that, if true, implies that the user had checked \"Keep me signed in\"\r\n persist?: boolean;\r\n}\r\n\r\n/** @internal */\r\nexport interface ArcGisToken extends MapLayerAccessToken {\r\n // The expiration time of the token in milliseconds since January 1, 1970 (UTC).\r\n expires: number;\r\n\r\n // This property will show as true if the token must always pass over ssl.\r\n ssl: boolean;\r\n}\r\n\r\n// client application's base URL, a user-specified IP address, or the IP address that is making the request.\r\n/** @internal */\r\nexport enum ArcGisTokenClientType {\r\n ip,\r\n referer,\r\n requestIp,\r\n}\r\n\r\n/** @internal */\r\nexport interface ArcGisGenerateTokenOptions {\r\n\r\n // The client type that will be granted access to the token.\r\n // Users will be able to specify whether the token will be generated for a client application's base URL,\r\n // a user-specified IP address, or the IP address that is making the request.\r\n client: ArcGisTokenClientType;\r\n\r\n // The IP address that will be using the created token for access.\r\n // On the Generate Token page, the IP address is specified in the IP Address field. This is required when client has been set as ip.\r\n // Example ip=11.11.111.111\r\n ip?: string;\r\n\r\n // The base URL of the client application that will use the token.\r\n // On the Generate Token page, the referrer URL is specified in the Webapp URL field.\r\n // Defaults to location.origin if undefined and client = referer.\r\n // This is required when client has been set as referer.\r\n // Example : referer=https://myserver/mywebapp\r\n referer?: string;\r\n\r\n // The token expiration time in minutes. The default is 60 minutes (one hour).\r\n // The maximum expiration period is 15 days. The maximum value of the expiration time is controlled by the server.\r\n // Requests for tokens larger than this time will be rejected.\r\n // Applications are responsible for renewing expired tokens;\r\n // expired tokens will be rejected by the server on subsequent requests that use the token.\r\n expiration?: number; // in minutes, defaults to 60 minutes\r\n}\r\n\r\n/** @internal */\r\nexport class ArcGisTokenGenerator {\r\n private static readonly restApiPath = \"/rest/\";\r\n private static readonly restApiInfoPath = \"info?f=pjson\";\r\n\r\n // Cache info url to avoid fetching/parsing twice for the same base url.\r\n private static _tokenServiceUrlCache = new Map<string, string>();\r\n\r\n public static async fetchTokenServiceUrl(arcGisRestServiceUrl: string): Promise<string | undefined> {\r\n const lowerUrl = arcGisRestServiceUrl.toLowerCase();\r\n const restApiIdx = lowerUrl.indexOf(ArcGisTokenGenerator.restApiPath);\r\n if (restApiIdx === -1)\r\n return undefined;\r\n const infoUrl = arcGisRestServiceUrl.substring(0, restApiIdx + ArcGisTokenGenerator.restApiPath.length) + ArcGisTokenGenerator.restApiInfoPath;\r\n\r\n let tokenServicesUrl: string | undefined;\r\n try {\r\n const response = await fetch(infoUrl, { method: \"GET\" });\r\n const json = await response.json();\r\n tokenServicesUrl = ArcGisTokenGenerator.getTokenServiceFromInfoJson(json);\r\n } catch (_error) {\r\n }\r\n return tokenServicesUrl;\r\n }\r\n\r\n public static getTokenServiceFromInfoJson(json: any): string | undefined {\r\n return json.authInfo?.isTokenBasedSecurity ? json?.authInfo?.tokenServicesUrl : undefined;\r\n }\r\n\r\n public async getTokenServiceUrl(baseUrl: string): Promise<string | undefined> {\r\n const cached = ArcGisTokenGenerator._tokenServiceUrlCache.get(baseUrl);\r\n if (cached !== undefined)\r\n return cached;\r\n\r\n const tokenServiceUrl = await ArcGisTokenGenerator.fetchTokenServiceUrl(baseUrl);\r\n if (tokenServiceUrl !== undefined)\r\n ArcGisTokenGenerator._tokenServiceUrlCache.set(baseUrl, tokenServiceUrl);\r\n\r\n return tokenServiceUrl;\r\n }\r\n\r\n // base url: ArcGis REST service base URL (format must be \"https://<host>/<instance>/rest/\")\r\n public async generate(arcGisRestServiceUrl: string, userName: string, password: string, options: ArcGisGenerateTokenOptions): Promise<any> {\r\n const tokenServiceUrl = await this.getTokenServiceUrl(arcGisRestServiceUrl);\r\n if (!tokenServiceUrl)\r\n return undefined;\r\n\r\n let token: undefined;\r\n try {\r\n const encodedUsername = encodeURIComponent(userName);\r\n const encodedPassword = encodeURIComponent(password);\r\n\r\n // Compose the expiration param\r\n let expirationStr = \"\";\r\n if (options.expiration) {\r\n expirationStr = `&expiration=${options.expiration}`;\r\n }\r\n\r\n // Compose the client param\r\n let clientStr = \"\";\r\n if (options.client === ArcGisTokenClientType.referer) {\r\n let refererStr = \"\";\r\n if (options.referer === undefined) {\r\n refererStr = encodeURIComponent(location.origin); // default to application origin\r\n } else {\r\n refererStr = encodeURIComponent(options.referer);\r\n }\r\n\r\n clientStr = `&client=referer&referer=${refererStr}`;\r\n } else if (options.client === ArcGisTokenClientType.ip) {\r\n if (options.ip === undefined)\r\n return token;\r\n clientStr = `&client=ip&ip=${options.ip}`;\r\n } else if (options.client === ArcGisTokenClientType.requestIp) {\r\n clientStr = `&client=requestip&ip=`;\r\n }\r\n\r\n const httpRequestOptions: RequestInit = {\r\n method: \"POST\",\r\n body: `username=${encodedUsername}&password=${encodedPassword}${clientStr}${expirationStr}&f=pjson`,\r\n headers: { \"content-type\": \"application/x-www-form-urlencoded\" },\r\n };\r\n\r\n const response = await fetch(tokenServiceUrl, httpRequestOptions);\r\n\r\n // Check a token was really generated (an error could be part of the body)\r\n token = await response.json();\r\n\r\n } catch (_error) {\r\n }\r\n return token;\r\n }\r\n\r\n // Encode following 'application/x-www-form-urlencoded' standard (https://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.3)\r\n // Also mentioned here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent\r\n public static formEncode(str: string): string {\r\n return ArcGisTokenGenerator.rfc1738Encode(str).replace(/%20/g, \"+\");\r\n }\r\n\r\n // Encode following RFC1738 standard (https://www.ietf.org/rfc/rfc1738.txt)\r\n // Code from https://locutus.io/php/url/rawurlencode/\r\n public static rfc1738Encode(str: string): string {\r\n return encodeURIComponent(str)\r\n .replace(/!/g, \"%21\")\r\n .replace(/'/g, \"%27\")\r\n .replace(/\\(/g, \"%28\")\r\n .replace(/\\)/g, \"%29\")\r\n .replace(/\\*/g, \"%2A\");\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"ArcGisTokenGenerator.js","sourceRoot":"","sources":["../../../src/ArcGis/ArcGisTokenGenerator.ts"],"names":[],"mappings":";;;AAmCA,4GAA4G;AAC5G,gBAAgB;AAChB,IAAY,qBAIX;AAJD,WAAY,qBAAqB;IAC/B,6DAAE,CAAA;IACF,uEAAO,CAAA;IACP,2EAAS,CAAA;AACX,CAAC,EAJW,qBAAqB,GAArB,6BAAqB,KAArB,6BAAqB,QAIhC;AA8BD,gBAAgB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C;aAC3C;iBAAM,IAAI,OAAO,CAAC,MAAM,KAAK,qBAAqB,CAAC,SAAS,EAAE;gBAC7D,SAAS,GAAG,uBAAuB,CAAC;aACrC;YAED,MAAM,kBAAkB,GAAgB;gBACtC,MAAM,EAAE,MAAM;gBACd,IAAI,EAAE,YAAY,eAAe,aAAa,eAAe,GAAG,SAAS,GAAG,aAAa,UAAU;gBACnG,OAAO,EAAE,EAAE,cAAc,EAAE,mCAAmC,EAAE;aACjE,CAAC;YAEF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,eAAe,EAAE,kBAAkB,CAAC,CAAC;YAElE,0EAA0E;YAC1E,KAAK,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;SAE/B;QAAC,OAAO,MAAM,EAAE;SAChB;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,gIAAgI;IAChI,2HAA2H;IACpH,MAAM,CAAC,UAAU,CAAC,GAAW;QAClC,OAAO,oBAAoB,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACtE,CAAC;IAED,2EAA2E;IAC3E,qDAAqD;IAC9C,MAAM,CAAC,aAAa,CAAC,GAAW;QACrC,OAAO,kBAAkB,CAAC,GAAG,CAAC;aAC3B,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC;aACpB,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC;aACpB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC;aACrB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC;aACrB,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAC3B,CAAC;;AA1GuB,gCAAW,GAAG,QAAQ,CAAC;AACvB,oCAAe,GAAG,cAAc,CAAC;AAEzD,wEAAwE;AACzD,0CAAqB,GAAG,IAAI,GAAG,EAAkB,CAAC;AALtD,oDAAoB","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\nimport { MapLayerAccessToken } from \"@itwin/core-frontend\";\r\n\r\n/** @packageDocumentation\r\n * @module Tiles\r\n */\r\n\r\n/** @internal */\r\nexport interface ArcGisOAuth2Token extends MapLayerAccessToken {\r\n\r\n // The expiration time of the token in milliseconds (UNIX time)\r\n expiresAt: number;\r\n\r\n // This property will show as true if the token must always pass over ssl.\r\n ssl: boolean;\r\n\r\n // Username associated with this token\r\n userName: string;\r\n\r\n // A Binary value that, if true, implies that the user had checked \"Keep me signed in\"\r\n persist?: boolean;\r\n}\r\n\r\n/** @internal */\r\nexport interface ArcGisToken extends MapLayerAccessToken {\r\n // The expiration time of the token in milliseconds since January 1, 1970 (UTC).\r\n expires: number;\r\n\r\n // This property will show as true if the token must always pass over ssl.\r\n ssl: boolean;\r\n}\r\n\r\n// client application's base URL, a user-specified IP address, or the IP address that is making the request.\r\n/** @internal */\r\nexport enum ArcGisTokenClientType {\r\n ip,\r\n referer,\r\n requestIp,\r\n}\r\n\r\n/** @internal */\r\nexport interface ArcGisGenerateTokenOptions {\r\n\r\n // The client type that will be granted access to the token.\r\n // Users will be able to specify whether the token will be generated for a client application's base URL,\r\n // a user-specified IP address, or the IP address that is making the request.\r\n client: ArcGisTokenClientType;\r\n\r\n // The IP address that will be using the created token for access.\r\n // On the Generate Token page, the IP address is specified in the IP Address field. This is required when client has been set as ip.\r\n // Example ip=11.11.111.111\r\n ip?: string;\r\n\r\n // The base URL of the client application that will use the token.\r\n // On the Generate Token page, the referrer URL is specified in the Webapp URL field.\r\n // Defaults to location.origin if undefined and client = referer.\r\n // This is required when client has been set as referer.\r\n // Example : referer=https://myserver/mywebapp\r\n referer?: string;\r\n\r\n // The token expiration time in minutes. The default is 60 minutes (one hour).\r\n // The maximum expiration period is 15 days. The maximum value of the expiration time is controlled by the server.\r\n // Requests for tokens larger than this time will be rejected.\r\n // Applications are responsible for renewing expired tokens;\r\n // expired tokens will be rejected by the server on subsequent requests that use the token.\r\n expiration?: number; // in minutes, defaults to 60 minutes\r\n}\r\n\r\n/** @internal */\r\nexport class ArcGisTokenGenerator {\r\n private static readonly restApiPath = \"/rest/\";\r\n private static readonly restApiInfoPath = \"info?f=pjson\";\r\n\r\n // Cache info url to avoid fetching/parsing twice for the same base url.\r\n private static _tokenServiceUrlCache = new Map<string, string>();\r\n\r\n public static async fetchTokenServiceUrl(arcGisRestServiceUrl: string): Promise<string | undefined> {\r\n const lowerUrl = arcGisRestServiceUrl.toLowerCase();\r\n const restApiIdx = lowerUrl.indexOf(ArcGisTokenGenerator.restApiPath);\r\n if (restApiIdx === -1)\r\n return undefined;\r\n const infoUrl = arcGisRestServiceUrl.substring(0, restApiIdx + ArcGisTokenGenerator.restApiPath.length) + ArcGisTokenGenerator.restApiInfoPath;\r\n\r\n let tokenServicesUrl: string | undefined;\r\n try {\r\n const response = await fetch(infoUrl, { method: \"GET\" });\r\n const json = await response.json();\r\n tokenServicesUrl = ArcGisTokenGenerator.getTokenServiceFromInfoJson(json);\r\n } catch (_error) {\r\n }\r\n return tokenServicesUrl;\r\n }\r\n\r\n public static getTokenServiceFromInfoJson(json: any): string | undefined {\r\n return json.authInfo?.isTokenBasedSecurity ? json?.authInfo?.tokenServicesUrl : undefined;\r\n }\r\n\r\n public async getTokenServiceUrl(baseUrl: string): Promise<string | undefined> {\r\n const cached = ArcGisTokenGenerator._tokenServiceUrlCache.get(baseUrl);\r\n if (cached !== undefined)\r\n return cached;\r\n\r\n const tokenServiceUrl = await ArcGisTokenGenerator.fetchTokenServiceUrl(baseUrl);\r\n if (tokenServiceUrl !== undefined)\r\n ArcGisTokenGenerator._tokenServiceUrlCache.set(baseUrl, tokenServiceUrl);\r\n\r\n return tokenServiceUrl;\r\n }\r\n\r\n // base url: ArcGis REST service base URL (format must be \"https://<host>/<instance>/rest/\")\r\n public async generate(arcGisRestServiceUrl: string, userName: string, password: string, options: ArcGisGenerateTokenOptions): Promise<any> {\r\n const tokenServiceUrl = await this.getTokenServiceUrl(arcGisRestServiceUrl);\r\n if (!tokenServiceUrl)\r\n return undefined;\r\n\r\n let token: undefined;\r\n try {\r\n const encodedUsername = encodeURIComponent(userName);\r\n const encodedPassword = encodeURIComponent(password);\r\n\r\n // Compose the expiration param\r\n let expirationStr = \"\";\r\n if (options.expiration) {\r\n expirationStr = `&expiration=${options.expiration}`;\r\n }\r\n\r\n // Compose the client param\r\n let clientStr = \"\";\r\n if (options.client === ArcGisTokenClientType.referer) {\r\n let refererStr = \"\";\r\n if (options.referer === undefined) {\r\n refererStr = encodeURIComponent(location.origin); // default to application origin\r\n } else {\r\n refererStr = encodeURIComponent(options.referer);\r\n }\r\n\r\n clientStr = `&client=referer&referer=${refererStr}`;\r\n } else if (options.client === ArcGisTokenClientType.ip) {\r\n if (options.ip === undefined)\r\n return token;\r\n clientStr = `&client=ip&ip=${options.ip}`;\r\n } else if (options.client === ArcGisTokenClientType.requestIp) {\r\n clientStr = `&client=requestip&ip=`;\r\n }\r\n\r\n const httpRequestOptions: RequestInit = {\r\n method: \"POST\",\r\n body: `username=${encodedUsername}&password=${encodedPassword}${clientStr}${expirationStr}&f=pjson`,\r\n headers: { \"content-type\": \"application/x-www-form-urlencoded\" },\r\n };\r\n\r\n const response = await fetch(tokenServiceUrl, httpRequestOptions);\r\n\r\n // Check a token was really generated (an error could be part of the body)\r\n token = await response.json();\r\n\r\n } catch (_error) {\r\n }\r\n return token;\r\n }\r\n\r\n // Encode following 'application/x-www-form-urlencoded' standard (https://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.3)\r\n // Also mentioned here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent\r\n public static formEncode(str: string): string {\r\n return ArcGisTokenGenerator.rfc1738Encode(str).replace(/%20/g, \"+\");\r\n }\r\n\r\n // Encode following RFC1738 standard (https://www.ietf.org/rfc/rfc1738.txt)\r\n // Code from https://locutus.io/php/url/rawurlencode/\r\n public static rfc1738Encode(str: string): string {\r\n return encodeURIComponent(str)\r\n .replace(/!/g, \"%21\")\r\n .replace(/'/g, \"%27\")\r\n .replace(/\\(/g, \"%28\")\r\n .replace(/\\)/g, \"%29\")\r\n .replace(/\\*/g, \"%2A\");\r\n }\r\n}\r\n"]}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
/** @packageDocumentation
|
|
2
|
-
* @module Tiles
|
|
3
|
-
*/
|
|
4
|
-
import { MapLayerAccessToken } from "@itwin/core-frontend";
|
|
5
|
-
import { ArcGisGenerateTokenOptions, ArcGisOAuth2Token, ArcGisToken } from "./ArcGisTokenGenerator";
|
|
6
|
-
/** @internal */
|
|
7
|
-
export declare class ArcGisTokenManager {
|
|
8
|
-
private static readonly tokenExpiryThreshold;
|
|
9
|
-
private static _cache;
|
|
10
|
-
private static _oauth2Cache;
|
|
11
|
-
private static _generator;
|
|
12
|
-
private static readonly _browserStorageKey;
|
|
13
|
-
static getToken(arcGisRestServiceUrl: string, userName: string, password: string, options: ArcGisGenerateTokenOptions): Promise<ArcGisToken | undefined>;
|
|
14
|
-
static invalidateToken(token: MapLayerAccessToken): boolean;
|
|
15
|
-
static getOAuth2Token(key: string): ArcGisOAuth2Token | undefined;
|
|
16
|
-
static invalidateOAuth2Token(token: MapLayerAccessToken): boolean;
|
|
17
|
-
static setOAuth2Token(key: string, token: ArcGisOAuth2Token): void;
|
|
18
|
-
static loadFromBrowserStorage(): void;
|
|
19
|
-
static saveToBrowserStorage(): void;
|
|
20
|
-
}
|
|
1
|
+
/** @packageDocumentation
|
|
2
|
+
* @module Tiles
|
|
3
|
+
*/
|
|
4
|
+
import { MapLayerAccessToken } from "@itwin/core-frontend";
|
|
5
|
+
import { ArcGisGenerateTokenOptions, ArcGisOAuth2Token, ArcGisToken } from "./ArcGisTokenGenerator";
|
|
6
|
+
/** @internal */
|
|
7
|
+
export declare class ArcGisTokenManager {
|
|
8
|
+
private static readonly tokenExpiryThreshold;
|
|
9
|
+
private static _cache;
|
|
10
|
+
private static _oauth2Cache;
|
|
11
|
+
private static _generator;
|
|
12
|
+
private static readonly _browserStorageKey;
|
|
13
|
+
static getToken(arcGisRestServiceUrl: string, userName: string, password: string, options: ArcGisGenerateTokenOptions): Promise<ArcGisToken | undefined>;
|
|
14
|
+
static invalidateToken(token: MapLayerAccessToken): boolean;
|
|
15
|
+
static getOAuth2Token(key: string): ArcGisOAuth2Token | undefined;
|
|
16
|
+
static invalidateOAuth2Token(token: MapLayerAccessToken): boolean;
|
|
17
|
+
static setOAuth2Token(key: string, token: ArcGisOAuth2Token): void;
|
|
18
|
+
static loadFromBrowserStorage(): void;
|
|
19
|
+
static saveToBrowserStorage(): void;
|
|
20
|
+
}
|
|
21
21
|
//# sourceMappingURL=ArcGisTokenManager.d.ts.map
|