Skip to content

Commit 1db79ba

Browse files
committed
updates from pr comments
1 parent 8babd64 commit 1db79ba

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

plugins/arcgis/service/src/ArcGISIdentityManagerFactory.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { ArcGISIdentityManager } from "@esri/arcgis-rest-request"
1+
import { ArcGISIdentityManager, request } from "@esri/arcgis-rest-request"
22
import { ArcGISAuthConfig, AuthType, FeatureServiceConfig, OAuthAuthConfig, TokenAuthConfig, UsernamePasswordAuthConfig } from './ArcGISConfig'
3-
import { HttpClient } from "./HttpClient";
43
import { ObservationProcessor } from "./ObservationProcessor";
54

65
interface ArcGISIdentityManagerFactory {
7-
create(portal: string, server: string, config: ArcGISAuthConfig, httpClient?: HttpClient, processor?: ObservationProcessor): Promise<ArcGISIdentityManager>
6+
create(portal: string, server: string, config: ArcGISAuthConfig, processor?: ObservationProcessor): Promise<ArcGISIdentityManager>
87
}
98

109
const OAuthIdentityManagerFactory: ArcGISIdentityManagerFactory = {
11-
async create(portal: string, server: string, auth: OAuthAuthConfig, httpClient: HttpClient, processor: ObservationProcessor): Promise<ArcGISIdentityManager> {
10+
async create(portal: string, server: string, auth: OAuthAuthConfig, processor: ObservationProcessor): Promise<ArcGISIdentityManager> {
1211
console.debug('Client ID provided for authentication')
1312
const { clientId, authToken, authTokenExpires, refreshToken, refreshTokenExpires } = auth
1413

@@ -24,7 +23,10 @@ const OAuthIdentityManagerFactory: ArcGISIdentityManagerFactory = {
2423
// TODO: find a way without using constructor nor httpClient
2524
const url = `${portal}/oauth2/token?client_id=${clientId}&refresh_token=${refreshToken}&grant_type=refresh_token`
2625
try {
27-
const response = await httpClient.sendGet(url)
26+
const response = await request(url, {
27+
httpMethod: 'GET'
28+
});
29+
2830
// Update authToken to new token
2931
const config = await processor.safeGetConfig();
3032
let service = config.featureServices.find(service => service.url === portal)?.auth as OAuthAuthConfig;
@@ -85,7 +87,6 @@ const authConfigMap: { [key: string]: ArcGISIdentityManagerFactory } = {
8587

8688
export function getIdentityManager(
8789
config: FeatureServiceConfig,
88-
httpClient: HttpClient, // TODO remove in favor of an open source lib like axios
8990
processor: ObservationProcessor
9091
): Promise<ArcGISIdentityManager> {
9192
const auth = config.auth
@@ -97,7 +98,7 @@ export function getIdentityManager(
9798
if (!factory) {
9899
throw new Error(`No factory found for type ${authType}`)
99100
}
100-
return factory.create(getPortalUrl(config.url), getServerUrl(config.url), auth, httpClient, processor)
101+
return factory.create(getPortalUrl(config.url), getServerUrl(config.url), auth, processor)
101102
}
102103

103104

plugins/arcgis/service/src/ObservationProcessor.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { EventTransform } from './EventTransform';
1414
import { GeometryChangedHandler } from './GeometryChangedHandler';
1515
import { EventDeletionHandler } from './EventDeletionHandler';
1616
import { EventLayerProcessorOrganizer } from './EventLayerProcessorOrganizer';
17-
import { FeatureServiceConfig, FeatureLayerConfig, AuthType } from "./ArcGISConfig"
17+
import { FeatureServiceConfig, FeatureLayerConfig, AuthType, OAuthAuthConfig } from "./ArcGISConfig"
1818
import { PluginStateRepository } from '@ngageoint/mage.service/lib/plugins.api'
1919
import { FeatureServiceAdmin } from './FeatureServiceAdmin';
2020

@@ -120,8 +120,13 @@ export class ObservationProcessor {
120120
* Gets the current configuration from the database.
121121
* @returns The current configuration from the database.
122122
*/
123-
public async safeGetConfig(): Promise<ArcGISPluginConfig> {
124-
return await this._stateRepo.get().then(x => !!x ? x : this._stateRepo.put(defaultArcGISPluginConfig))
123+
public async safeGetConfig(showFeatureAuth?: boolean): Promise<ArcGISPluginConfig> {
124+
const state = await this._stateRepo.get();
125+
if (!state) return await this._stateRepo.put(defaultArcGISPluginConfig);
126+
if (!showFeatureAuth) {
127+
state.featureServices = state.featureServices.map((service) => this.sanitizeFeatureService(service, AuthType.OAuth));
128+
}
129+
return state;
125130
}
126131

127132
/**
@@ -132,6 +137,14 @@ export class ObservationProcessor {
132137
return await this._stateRepo.put(newConfig);
133138
}
134139

140+
/**
141+
* Updates the confguration in the state repo.
142+
* @param newConfig The new config to put into the state repo.
143+
*/
144+
public async patchConfig(newConfig: ArcGISPluginConfig): Promise<ArcGISPluginConfig> {
145+
return await this._stateRepo.patch(newConfig);
146+
}
147+
135148
/**
136149
* Gets the current configuration and updates the processor if needed
137150
* @returns The current configuration from the database.
@@ -151,6 +164,19 @@ export class ObservationProcessor {
151164
return config
152165
}
153166

167+
private sanitizeFeatureService(config: FeatureServiceConfig, type: AuthType): FeatureServiceConfig {
168+
if (type === AuthType.OAuth) {
169+
const newAuth = Object.assign({}, config.auth) as OAuthAuthConfig;
170+
delete newAuth.refreshToken;
171+
delete newAuth.refreshTokenExpires;
172+
return {
173+
...config,
174+
auth: newAuth
175+
}
176+
}
177+
return config;
178+
}
179+
154180
/**
155181
* Starts the processor.
156182
*/

plugins/arcgis/service/src/index.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { SettingPermission } from '@ngageoint/mage.service/lib/entities/authoriz
77
import { ArcGISPluginConfig } from './ArcGISPluginConfig'
88
import { AuthType } from './ArcGISConfig'
99
import { ObservationProcessor } from './ObservationProcessor'
10-
import { HttpClient } from './HttpClient'
1110
import { ArcGISIdentityManager, request } from "@esri/arcgis-rest-request"
1211
import { FeatureServiceConfig } from './ArcGISConfig'
1312
import { URL } from "node:url"
@@ -157,7 +156,7 @@ const arcgisPluginHooks: InitPluginHook<typeof InjectedServices> = {
157156
console.info('Applying ArcGIS plugin config...')
158157
const arcConfig = req.body as ArcGISPluginConfig
159158
const configString = JSON.stringify(arcConfig)
160-
processor.putConfig(arcConfig)
159+
processor.patchConfig(arcConfig)
161160
res.sendStatus(200)
162161
})
163162

@@ -179,17 +178,16 @@ const arcgisPluginHooks: InitPluginHook<typeof InjectedServices> = {
179178
}
180179

181180
try {
182-
const httpClient = new HttpClient(console)
183181
// Create the IdentityManager instance to validate credentials
184-
await getIdentityManager(service!, httpClient, processor)
182+
await getIdentityManager(service!, processor)
185183
let existingService = config.featureServices.find(service => service.url === url)
186184
if (existingService) {
187185
existingService = { ...existingService }
188186
} else {
189187
config.featureServices.push(service)
190188
}
191-
192-
await processor.putConfig(config)
189+
console.log('values patch')
190+
await processor.patchConfig(config)
193191
return res.send(service)
194192
} catch (err) {
195193
return res.send('Invalid credentials provided to communicate with feature service').status(400)
@@ -203,10 +201,9 @@ const arcgisPluginHooks: InitPluginHook<typeof InjectedServices> = {
203201
if (!featureService) {
204202
return res.status(400)
205203
}
206-
207-
const httpClient = new HttpClient(console)
204+
208205
try {
209-
const identityManager = await getIdentityManager(featureService, httpClient, processor)
206+
const identityManager = await getIdentityManager(featureService, processor)
210207
const response = await request(url, {
211208
authentication: identityManager
212209
})

plugins/arcgis/web-app/projects/main/src/lib/ArcGISConfig.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,6 @@ export interface OAuthAuthConfig {
126126
* The expiration date for the temporary token
127127
*/
128128
authTokenExpires?: string
129-
130-
/**
131-
* The Refresh token for OAuth
132-
*/
133-
refreshToken?: string
134-
135-
/**
136-
* The expiration date for the Refresh token
137-
*/
138-
refreshTokenExpires?: string
139129
}
140130

141131
/**

0 commit comments

Comments
 (0)