Skip to content

Commit ed561a7

Browse files
committed
[service] Refactor ArcGISIdentityManager management
1 parent 7691ad7 commit ed561a7

File tree

10 files changed

+999
-1220
lines changed

10 files changed

+999
-1220
lines changed

plugins/arcgis/service/src/ArcGISConfig.ts

Lines changed: 5 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,11 @@ export interface FeatureServiceConfig {
88
*/
99
url: string
1010

11-
/**
12-
* Username and password for ArcGIS authentication
13-
*/
14-
auth?: ArcGISAuthConfig
15-
16-
/**
17-
* Create layers that don't exist
18-
*/
19-
createLayers?: boolean
20-
21-
/**
22-
* The administration url to the arc feature service.
23-
*/
24-
adminUrl?: string
25-
26-
/**
27-
* Administration access token
28-
*/
29-
adminToken?: string
30-
11+
/**
12+
* Serialized ArcGISIdentityManager
13+
*/
14+
identityManager: string
15+
3116
/**
3217
* The feature layers.
3318
*/
@@ -49,10 +34,6 @@ export interface FeatureLayerConfig {
4934
*/
5035
geometryType?: string
5136

52-
/**
53-
* Access token
54-
*/
55-
token?: string // TODO - can this be removed? Will Layers have a token too?
5637
/**
5738
* The event ids or names that sync to this arc feature layer.
5839
*/
@@ -67,86 +48,8 @@ export interface FeatureLayerConfig {
6748
* Delete editable layer fields missing from form fields
6849
*/
6950
deleteFields?: boolean
70-
71-
}
72-
73-
export enum AuthType {
74-
Token = 'token',
75-
UsernamePassword = 'usernamePassword',
76-
OAuth = 'oauth'
77-
}
78-
79-
80-
/**
81-
* Contains token-based authentication configuration.
82-
*/
83-
export interface TokenAuthConfig {
84-
type: AuthType.Token
85-
token: string
86-
authTokenExpires?: string
87-
}
88-
89-
/**
90-
* Contains username and password for ArcGIS server authentication.
91-
*/
92-
export interface UsernamePasswordAuthConfig {
93-
type: AuthType.UsernamePassword
94-
/**
95-
* The username for authentication.
96-
*/
97-
username: string
98-
99-
/**
100-
* The password for authentication.
101-
*/
102-
password: string
10351
}
10452

105-
/**
106-
* Contains OAuth authentication configuration.
107-
*/
108-
export interface OAuthAuthConfig {
109-
110-
type: AuthType.OAuth
111-
112-
/**
113-
* The Client Id for OAuth
114-
*/
115-
clientId: string
116-
117-
/**
118-
* The redirectUri for OAuth
119-
*/
120-
redirectUri?: string
121-
122-
/**
123-
* The temporary auth token for OAuth
124-
*/
125-
authToken?: string
126-
127-
/**
128-
* The expiration date for the temporary token
129-
*/
130-
authTokenExpires?: number
131-
132-
/**
133-
* The Refresh token for OAuth
134-
*/
135-
refreshToken?: string
136-
137-
/**
138-
* The expiration date for the Refresh token
139-
*/
140-
refreshTokenExpires?: number
141-
}
142-
143-
/**
144-
* Union type for authentication configurations.
145-
*/
146-
export type ArcGISAuthConfig =
147-
| TokenAuthConfig
148-
| UsernamePasswordAuthConfig
149-
| OAuthAuthConfig
15053

15154
/**
15255
* Attribute configurations

plugins/arcgis/service/src/ArcGISIdentityManagerFactory.ts

Lines changed: 0 additions & 118 deletions
This file was deleted.

plugins/arcgis/service/src/ArcGISPluginConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export const defaultArcGISPluginConfig = Object.freeze<ArcGISPluginConfig>({
145145
textAreaFieldLength: 256,
146146
observationIdField: 'description',
147147
idSeparator: '-',
148-
// eventIdField: 'event_id',
148+
eventIdField: 'event_id',
149149
lastEditedDateField: 'last_edited_date',
150150
eventNameField: 'event_name',
151151
userIdField: 'user_id',
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { ArcGISIdentityManager } from '@esri/arcgis-rest-request'
2+
import { FeatureServiceConfig } from './ArcGISConfig'
3+
import { PluginStateRepository } from '@ngageoint/mage.service/lib/plugins.api'
4+
5+
export interface ArcGISIdentityService {
6+
getIdentityManager(featureService: FeatureServiceConfig): Promise<ArcGISIdentityManager>
7+
updateIndentityManagers(): Promise<void>
8+
}
9+
10+
export function createArcGISIdentityService(
11+
stateRepo: PluginStateRepository<any>
12+
): ArcGISIdentityService {
13+
const identityManagerCache: Map<string, Promise<ArcGISIdentityManager>> = new Map()
14+
15+
return {
16+
async getIdentityManager(featureService: FeatureServiceConfig): Promise<ArcGISIdentityManager> {
17+
let cached = await identityManagerCache.get(featureService.url)
18+
if (!cached) {
19+
const identityManager = ArcGISIdentityManager.deserialize(featureService.identityManager)
20+
const promise = identityManager.getUser().then(() => identityManager)
21+
identityManagerCache.set(featureService.url, promise)
22+
return promise
23+
} else {
24+
return cached
25+
}
26+
},
27+
async updateIndentityManagers() {
28+
const config = await stateRepo.get()
29+
for (let [url, persistedIdentityManagerPromise] of identityManagerCache) {
30+
const persistedIdentityManager = await persistedIdentityManagerPromise
31+
const featureService: FeatureServiceConfig | undefined = config.featureServices.find((service: FeatureServiceConfig) => service.url === url)
32+
if (featureService) {
33+
const identityManager = ArcGISIdentityManager.deserialize(featureService.identityManager)
34+
if (identityManager.token !== persistedIdentityManager.token || identityManager.refreshToken !== persistedIdentityManager.refreshToken) {
35+
featureService.identityManager = persistedIdentityManager.serialize()
36+
await stateRepo.put(config)
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
44+
export function getPortalUrl(featureService: FeatureServiceConfig | string): string {
45+
const url = getFeatureServiceUrl(featureService)
46+
return `https://${url.hostname}/arcgis/sharing/rest`
47+
}
48+
49+
export function getServerUrl(featureService: FeatureServiceConfig | string): string {
50+
const url = getFeatureServiceUrl(featureService)
51+
return `https://${url.hostname}/arcgis`
52+
}
53+
54+
export function getFeatureServiceUrl(featureService: FeatureServiceConfig | string): URL {
55+
const url = typeof featureService === 'string' ? featureService : featureService.url
56+
return new URL(url)
57+
}

plugins/arcgis/service/src/FeatureQuerier.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,19 @@ export class FeatureQuerier {
106106
queryUrl.searchParams.set('outFields', this.outFields([field]));
107107
queryUrl.searchParams.set('returnGeometry', 'false');
108108
this._console.info('ArcGIS query: ' + queryUrl)
109-
110-
const queryResponse = await request(queryUrl.toString(), {
111-
authentication: this._identityManager,
112-
params: { f: 'json' }
113109

114-
});
110+
try {
111+
const queryResponse = await request(queryUrl.toString(), {
112+
authentication: this._identityManager,
113+
params: { f: 'json' }
115114

116-
this._console.info('ArcGIS response for ' + queryUrl + ' ' + JSON.stringify(queryResponse, null, 2))
117-
response(queryResponse as QueryObjectResult)
115+
});
116+
117+
this._console.info('ArcGIS response for ' + queryUrl + ' ' + JSON.stringify(queryResponse, null, 2))
118+
response(queryResponse as QueryObjectResult)
119+
} catch (err) {
120+
console.error("could not query", err)
121+
}
118122
}
119123

120124
/**

plugins/arcgis/service/src/FeatureService.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { LayerInfoResult } from "./LayerInfoResult";
2-
import { FeatureServiceResult } from "./FeatureServiceResult";
3-
import { getIdentityManager } from "./ArcGISIdentityManagerFactory"
41
import { ArcGISIdentityManager, request } from "@esri/arcgis-rest-request"
52
import { queryFeatures, applyEdits, IQueryFeaturesOptions } from "@esri/arcgis-rest-feature-service";
63
import { FeatureServiceConfig } from "./ArcGISConfig";
@@ -10,30 +7,21 @@ import { FeatureServiceConfig } from "./ArcGISConfig";
107
*/
118
export class FeatureService {
129

13-
/**
14-
* Used to log messages.
15-
*/
1610
private _console: Console;
11+
private _config: FeatureServiceConfig;
12+
private _identityManager: ArcGISIdentityManager;
1713

18-
private _config: FeatureServiceConfig;
19-
private _identityManager: ArcGISIdentityManager;
20-
21-
/**
22-
* Constructor.
23-
* @param console Used to log messages.
24-
* @param token The access token.
25-
*/
2614
constructor(console: Console, config: FeatureServiceConfig, identityManager: ArcGISIdentityManager) {
27-
this._config = config;
28-
this._identityManager = identityManager;
15+
this._config = config;
16+
this._identityManager = identityManager;
2917
this._console = console;
3018
}
3119

3220
// TODO this entire class is a Work in Progress and not used. Currently using @esri/arcgis-rest-request and not arcgis-rest-js.
3321
// By finishing this class, we can transition from low-level generic requests that leverage ArcGISIdentityManager for auth to higher-level strongly typed requests.
3422

3523

36-
// Query features using arcgis-rest-js's queryFeatures
24+
// Query features using arcgis-rest-js's queryFeatures
3725
async queryFeatureService(whereClause: string): Promise<any> {
3826
const queryParams = {
3927
url: this._config.url,

0 commit comments

Comments
 (0)