@itwin/map-layers-auth 4.0.0-dev.8 → 4.0.0-dev.80
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/CHANGELOG.md +43 -1
- package/lib/cjs/ArcGis/ArcGisAccessClient.d.ts +72 -72
- package/lib/cjs/ArcGis/ArcGisAccessClient.js +286 -291
- package/lib/cjs/ArcGis/ArcGisAccessClient.js.map +1 -1
- 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 -110
- 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 -112
- 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 -49
- package/lib/cjs/ArcGis/ArcGisUrl.js.map +1 -1
- 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 -287
- package/lib/esm/ArcGis/ArcGisAccessClient.js.map +1 -1
- 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 -106
- 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 -108
- 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 -45
- package/lib/esm/ArcGis/ArcGisUrl.js.map +1 -1
- package/lib/esm/map-layers-auth.d.ts +5 -5
- package/lib/esm/map-layers-auth.js +9 -9
- package/package.json +12 -12
|
@@ -1,292 +1,287 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*---------------------------------------------------------------------------------------------
|
|
3
|
-
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
4
|
-
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
5
|
-
*--------------------------------------------------------------------------------------------*/
|
|
6
|
-
/** @packageDocumentation
|
|
7
|
-
* @module Tiles
|
|
8
|
-
*/
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.ArcGisAccessClient = void 0;
|
|
11
|
-
const core_bentley_1 = require("@itwin/core-bentley");
|
|
12
|
-
const ArcGisTokenGenerator_1 = require("./ArcGisTokenGenerator");
|
|
13
|
-
const ArcGisOAuth2Endpoint_1 = require("./ArcGisOAuth2Endpoint");
|
|
14
|
-
const ArcGisTokenManager_1 = require("./ArcGisTokenManager");
|
|
15
|
-
const ArcGisUrl_1 = require("./ArcGisUrl");
|
|
16
|
-
/** @beta */
|
|
17
|
-
class ArcGisAccessClient {
|
|
18
|
-
constructor() {
|
|
19
|
-
this.onOAuthProcessEnd = new core_bentley_1.BeEvent();
|
|
20
|
-
// Should be kept to 'false'. Debugging purposes only.
|
|
21
|
-
this._forceLegacyToken = false;
|
|
22
|
-
// Derive the Oauth URL from a typical MapLayerURL
|
|
23
|
-
// i.e. https://hostname/server/rest/services/NewYork/NewYork3857/MapServer
|
|
24
|
-
// => https://hostname/portal/sharing/oauth2/authorize
|
|
25
|
-
this._oauthAuthorizeEndPointsCache = new Map();
|
|
26
|
-
this._oauthTokenEndPointsCache = new Map();
|
|
27
|
-
}
|
|
28
|
-
initialize(oAuthConfig) {
|
|
29
|
-
if (oAuthConfig) {
|
|
30
|
-
this._redirectUri = oAuthConfig.redirectUri;
|
|
31
|
-
this._expiration = oAuthConfig.tokenExpiration;
|
|
32
|
-
this._clientIds = oAuthConfig.clientIds;
|
|
33
|
-
this.initOauthCallbackFunction();
|
|
34
|
-
}
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
initOauthCallbackFunction() {
|
|
38
|
-
window.arcGisOAuth2Callback = (redirectLocation) => {
|
|
39
|
-
|
|
40
|
-
let
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
//
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
let
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
if (
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
urlObj.searchParams.set("redirect_uri", this.redirectUri);
|
|
288
|
-
return urlObj.toString();
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
exports.ArcGisAccessClient = ArcGisAccessClient;
|
|
1
|
+
"use strict";
|
|
2
|
+
/*---------------------------------------------------------------------------------------------
|
|
3
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
4
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
5
|
+
*--------------------------------------------------------------------------------------------*/
|
|
6
|
+
/** @packageDocumentation
|
|
7
|
+
* @module Tiles
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.ArcGisAccessClient = void 0;
|
|
11
|
+
const core_bentley_1 = require("@itwin/core-bentley");
|
|
12
|
+
const ArcGisTokenGenerator_1 = require("./ArcGisTokenGenerator");
|
|
13
|
+
const ArcGisOAuth2Endpoint_1 = require("./ArcGisOAuth2Endpoint");
|
|
14
|
+
const ArcGisTokenManager_1 = require("./ArcGisTokenManager");
|
|
15
|
+
const ArcGisUrl_1 = require("./ArcGisUrl");
|
|
16
|
+
/** @beta */
|
|
17
|
+
class ArcGisAccessClient {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.onOAuthProcessEnd = new core_bentley_1.BeEvent();
|
|
20
|
+
// Should be kept to 'false'. Debugging purposes only.
|
|
21
|
+
this._forceLegacyToken = false;
|
|
22
|
+
// Derive the Oauth URL from a typical MapLayerURL
|
|
23
|
+
// i.e. https://hostname/server/rest/services/NewYork/NewYork3857/MapServer
|
|
24
|
+
// => https://hostname/portal/sharing/oauth2/authorize
|
|
25
|
+
this._oauthAuthorizeEndPointsCache = new Map();
|
|
26
|
+
this._oauthTokenEndPointsCache = new Map();
|
|
27
|
+
}
|
|
28
|
+
initialize(oAuthConfig) {
|
|
29
|
+
if (oAuthConfig) {
|
|
30
|
+
this._redirectUri = oAuthConfig.redirectUri;
|
|
31
|
+
this._expiration = oAuthConfig.tokenExpiration;
|
|
32
|
+
this._clientIds = oAuthConfig.clientIds;
|
|
33
|
+
this.initOauthCallbackFunction();
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
initOauthCallbackFunction() {
|
|
38
|
+
window.arcGisOAuth2Callback = (redirectLocation) => {
|
|
39
|
+
let eventSuccess = false;
|
|
40
|
+
let stateData;
|
|
41
|
+
if (redirectLocation && redirectLocation.hash.length > 0) {
|
|
42
|
+
const locationHash = redirectLocation.hash;
|
|
43
|
+
const hashParams = new URLSearchParams(locationHash.substring(1));
|
|
44
|
+
const token = hashParams.get("access_token") ?? undefined;
|
|
45
|
+
const expiresInStr = hashParams.get("expires_in") ?? undefined;
|
|
46
|
+
const userName = hashParams.get("username") ?? undefined;
|
|
47
|
+
const ssl = hashParams.get("ssl") === "true";
|
|
48
|
+
const stateStr = hashParams.get("state") ?? undefined;
|
|
49
|
+
const persist = hashParams.get("persist") === "true";
|
|
50
|
+
if (token !== undefined && expiresInStr !== undefined && userName !== undefined && ssl !== undefined && stateStr !== undefined) {
|
|
51
|
+
let endpointOrigin;
|
|
52
|
+
try {
|
|
53
|
+
const state = JSON.parse(stateStr);
|
|
54
|
+
stateData = state?.customData;
|
|
55
|
+
endpointOrigin = state?.endpointOrigin;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
}
|
|
59
|
+
const expiresIn = Number(expiresInStr);
|
|
60
|
+
const expiresAt = (expiresIn * 1000) + (+new Date()); // Converts the token expiration delay (seconds) into a timestamp (UNIX time)
|
|
61
|
+
if (endpointOrigin !== undefined) {
|
|
62
|
+
ArcGisTokenManager_1.ArcGisTokenManager.setOAuth2Token(endpointOrigin, { token, expiresAt, ssl, userName, persist });
|
|
63
|
+
eventSuccess = true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.onOAuthProcessEnd.raiseEvent(eventSuccess, stateData);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
unInitialize() {
|
|
71
|
+
this._redirectUri = undefined;
|
|
72
|
+
this._expiration = undefined;
|
|
73
|
+
window.arcGisOAuth2Callback = undefined;
|
|
74
|
+
}
|
|
75
|
+
async getAccessToken(params) {
|
|
76
|
+
// First lookup Oauth2 tokens, otherwise check try "legacy tokens" if credentials were provided
|
|
77
|
+
if (!this._forceLegacyToken) {
|
|
78
|
+
const oauth2Token = await this.getOAuthTokenForMapLayerUrl(params.mapLayerUrl.toString());
|
|
79
|
+
if (oauth2Token)
|
|
80
|
+
return oauth2Token;
|
|
81
|
+
}
|
|
82
|
+
if (params.userName && params.password) {
|
|
83
|
+
return ArcGisTokenManager_1.ArcGisTokenManager.getToken(params.mapLayerUrl.toString(), params.userName, params.password, { client: ArcGisTokenGenerator_1.ArcGisTokenClientType.referer });
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
async getTokenServiceEndPoint(mapLayerUrl) {
|
|
88
|
+
let tokenEndpoint;
|
|
89
|
+
if (!this._forceLegacyToken) {
|
|
90
|
+
// Note: we used to validate the endpoint by making a request, but because of CORS isssues with some servers
|
|
91
|
+
// we could not make a reliable validation.
|
|
92
|
+
try {
|
|
93
|
+
tokenEndpoint = await this.getOAuth2Endpoint(mapLayerUrl, ArcGisOAuth2Endpoint_1.ArcGisOAuth2EndpointType.Authorize);
|
|
94
|
+
if (tokenEndpoint) {
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return tokenEndpoint;
|
|
101
|
+
}
|
|
102
|
+
invalidateToken(token) {
|
|
103
|
+
let found = ArcGisTokenManager_1.ArcGisTokenManager.invalidateToken(token);
|
|
104
|
+
if (!found) {
|
|
105
|
+
found = ArcGisTokenManager_1.ArcGisTokenManager.invalidateOAuth2Token(token);
|
|
106
|
+
}
|
|
107
|
+
return found;
|
|
108
|
+
}
|
|
109
|
+
get redirectUri() {
|
|
110
|
+
return this._redirectUri;
|
|
111
|
+
}
|
|
112
|
+
getMatchingEnterpriseClientId(url) {
|
|
113
|
+
const clientIds = this.arcGisEnterpriseClientIds;
|
|
114
|
+
if (!clientIds) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
let clientId;
|
|
118
|
+
let defaultClientId;
|
|
119
|
+
for (const entry of clientIds) {
|
|
120
|
+
if (entry.serviceBaseUrl === "") {
|
|
121
|
+
defaultClientId = entry.clientId;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
if (url.toLowerCase().startsWith(entry.serviceBaseUrl)) {
|
|
125
|
+
clientId = entry.clientId;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// If we could not find a match with serviceBaseUrl, and a default clientId
|
|
130
|
+
// was specified (i.e empty url), then use default clientId
|
|
131
|
+
if (clientId === undefined && defaultClientId !== undefined) {
|
|
132
|
+
clientId = defaultClientId;
|
|
133
|
+
}
|
|
134
|
+
return clientId;
|
|
135
|
+
}
|
|
136
|
+
get expiration() {
|
|
137
|
+
return this._expiration;
|
|
138
|
+
}
|
|
139
|
+
get arcGisOnlineClientId() {
|
|
140
|
+
return this._clientIds?.arcgisOnlineClientId;
|
|
141
|
+
}
|
|
142
|
+
set arcGisOnlineClientId(clientId) {
|
|
143
|
+
if (this._clientIds === undefined) {
|
|
144
|
+
this._clientIds = { arcgisOnlineClientId: clientId };
|
|
145
|
+
}
|
|
146
|
+
this._clientIds.arcgisOnlineClientId = clientId;
|
|
147
|
+
}
|
|
148
|
+
get arcGisEnterpriseClientIds() {
|
|
149
|
+
return this._clientIds?.enterpriseClientIds;
|
|
150
|
+
}
|
|
151
|
+
setEnterpriseClientId(serviceBaseUrl, clientId) {
|
|
152
|
+
if (this._clientIds?.enterpriseClientIds) {
|
|
153
|
+
const foundIdx = this._clientIds.enterpriseClientIds.findIndex((entry) => entry.serviceBaseUrl === serviceBaseUrl);
|
|
154
|
+
if (foundIdx !== -1) {
|
|
155
|
+
this._clientIds.enterpriseClientIds[foundIdx].clientId = clientId;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
this._clientIds.enterpriseClientIds.push({ serviceBaseUrl, clientId });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
if (this._clientIds === undefined) {
|
|
163
|
+
this._clientIds = {};
|
|
164
|
+
}
|
|
165
|
+
this._clientIds.enterpriseClientIds = [{ serviceBaseUrl, clientId }];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
removeEnterpriseClientId(clientId) {
|
|
169
|
+
if (this._clientIds?.enterpriseClientIds) {
|
|
170
|
+
this._clientIds.enterpriseClientIds = this._clientIds?.enterpriseClientIds?.filter((item) => item.serviceBaseUrl !== clientId.serviceBaseUrl);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/// //////////
|
|
174
|
+
/** @internal */
|
|
175
|
+
async getOAuthTokenForMapLayerUrl(mapLayerUrl) {
|
|
176
|
+
try {
|
|
177
|
+
const oauthEndpoint = await this.getOAuth2Endpoint(mapLayerUrl, ArcGisOAuth2Endpoint_1.ArcGisOAuth2EndpointType.Authorize);
|
|
178
|
+
if (oauthEndpoint !== undefined) {
|
|
179
|
+
const oauthEndpointUrl = new URL(oauthEndpoint.getUrl());
|
|
180
|
+
return ArcGisTokenManager_1.ArcGisTokenManager.getOAuth2Token(oauthEndpointUrl.origin);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch { }
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get OAuth2 endpoint that must be cause to get the Oauth2 token
|
|
188
|
+
* @internal
|
|
189
|
+
*/
|
|
190
|
+
cacheEndpoint(url, endpoint, obj) {
|
|
191
|
+
if (endpoint === ArcGisOAuth2Endpoint_1.ArcGisOAuth2EndpointType.Authorize) {
|
|
192
|
+
this._oauthAuthorizeEndPointsCache.set(url, obj);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
this._oauthTokenEndPointsCache.set(url, obj);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Get OAuth2 endpoint that must be cause to get the Oauth2 token
|
|
200
|
+
* @internal
|
|
201
|
+
*/
|
|
202
|
+
async createEndpoint(url, endpoint) {
|
|
203
|
+
// Validate the URL we just composed
|
|
204
|
+
const oauthEndpoint = new ArcGisOAuth2Endpoint_1.ArcGisOAuth2Endpoint(url, this.constructLoginUrl(url, false), false);
|
|
205
|
+
this.cacheEndpoint(url, endpoint, oauthEndpoint);
|
|
206
|
+
return oauthEndpoint;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Get OAuth2 endpoint that must be cause to get the Oauth2 token
|
|
210
|
+
* @internal
|
|
211
|
+
*/
|
|
212
|
+
async getOAuth2Endpoint(url, endpointType) {
|
|
213
|
+
// Return from cache if available
|
|
214
|
+
const cachedEndpoint = (endpointType === ArcGisOAuth2Endpoint_1.ArcGisOAuth2EndpointType.Authorize ? this._oauthAuthorizeEndPointsCache.get(url) : this._oauthTokenEndPointsCache.get(url));
|
|
215
|
+
if (cachedEndpoint !== undefined) {
|
|
216
|
+
return cachedEndpoint;
|
|
217
|
+
}
|
|
218
|
+
const endpointStr = (endpointType === ArcGisOAuth2Endpoint_1.ArcGisOAuth2EndpointType.Authorize ? "authorize" : "token");
|
|
219
|
+
const urlObj = new URL(url);
|
|
220
|
+
if (urlObj.hostname.toLowerCase().endsWith("arcgis.com")) {
|
|
221
|
+
// ArcGIS Online (fixed)
|
|
222
|
+
// Doc: https://developers.arcgis.com/documentation/mapping-apis-and-services/security/oauth-2.0/
|
|
223
|
+
if (this.arcGisOnlineClientId === undefined) {
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
const oauth2Url = `https://www.arcgis.com/sharing/rest/oauth2/${endpointStr}`;
|
|
227
|
+
return new ArcGisOAuth2Endpoint_1.ArcGisOAuth2Endpoint(oauth2Url, this.constructLoginUrl(oauth2Url, true), true);
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
// First attempt: derive the Oauth2 token URL from the 'tokenServicesUrl', exposed by the 'info request'
|
|
231
|
+
try {
|
|
232
|
+
const restUrlFromTokenService = await ArcGisUrl_1.ArcGisUrl.getRestUrlFromGenerateTokenUrl(urlObj);
|
|
233
|
+
if (restUrlFromTokenService === undefined) {
|
|
234
|
+
// We could not derive the token endpoint from 'tokenServicesUrl'.
|
|
235
|
+
// ArcGIS Enterprise Format https://<host>:<port>/<subdirectory>/sharing/rest/oauth2/authorize
|
|
236
|
+
const regExMatch = url.match(new RegExp(/([^&\/]+)\/rest\/services\/.*/, "i"));
|
|
237
|
+
if (regExMatch !== null && regExMatch.length >= 2) {
|
|
238
|
+
const subdirectory = regExMatch[1];
|
|
239
|
+
const port = (urlObj.port !== "80" && urlObj.port !== "443") ? `:${urlObj.port}` : "";
|
|
240
|
+
const newUrlObj = new URL(`${urlObj.protocol}//${urlObj.hostname}${port}/${subdirectory}/sharing/rest/oauth2/${endpointStr}`);
|
|
241
|
+
// Check again the URL we just composed
|
|
242
|
+
return await this.createEndpoint(newUrlObj.toString(), endpointType);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
const endpoint = await this.createEndpoint(`${restUrlFromTokenService.toString()}oauth2/${endpointStr}`, endpointType);
|
|
247
|
+
if (endpoint)
|
|
248
|
+
return endpoint;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return undefined; // we could not find any valid oauth2 endpoint
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Construct the complete Authorize url to starts the Oauth process
|
|
258
|
+
* @internal
|
|
259
|
+
*/
|
|
260
|
+
constructLoginUrl(url, isArcgisOnline) {
|
|
261
|
+
const urlObj = new URL(url);
|
|
262
|
+
// Set the client id
|
|
263
|
+
if (isArcgisOnline) {
|
|
264
|
+
const clientId = this.arcGisOnlineClientId;
|
|
265
|
+
(0, core_bentley_1.assert)(clientId !== undefined);
|
|
266
|
+
if (clientId !== undefined) {
|
|
267
|
+
urlObj.searchParams.set("client_id", clientId);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
const clientId = this.getMatchingEnterpriseClientId(url);
|
|
272
|
+
(0, core_bentley_1.assert)(clientId !== undefined);
|
|
273
|
+
if (undefined !== clientId) {
|
|
274
|
+
urlObj.searchParams.set("client_id", clientId);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
urlObj.searchParams.set("response_type", "token");
|
|
278
|
+
if (this.expiration !== undefined) {
|
|
279
|
+
urlObj.searchParams.set("expiration", `${this.expiration}`);
|
|
280
|
+
}
|
|
281
|
+
if (this.redirectUri)
|
|
282
|
+
urlObj.searchParams.set("redirect_uri", this.redirectUri);
|
|
283
|
+
return urlObj.toString();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
exports.ArcGisAccessClient = ArcGisAccessClient;
|
|
292
287
|
//# sourceMappingURL=ArcGisAccessClient.js.map
|