Skip to content

Commit 7c19885

Browse files
committed
move sanitize to web routes
1 parent c234cc9 commit 7c19885

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

plugins/arcgis/service/src/ArcGISIdentityManagerFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const OAuthIdentityManagerFactory: ArcGISIdentityManagerFactory = {
2828
});
2929

3030
// Update authToken to new token
31-
const config = await processor.safeGetConfig(true);
31+
const config = await processor.safeGetConfig();
3232
let service = config.featureServices.find(service => service.url === portal)?.auth as OAuthAuthConfig;
3333
const date = new Date();
3434
date.setSeconds(date.getSeconds() + response.expires_in || 0);

plugins/arcgis/service/src/ObservationProcessor.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,10 @@ export class ObservationProcessor {
120120
* Gets the current configuration from the database.
121121
* @returns The current configuration from the database.
122122
*/
123-
public async safeGetConfig(showFeatureAuth?: boolean): Promise<ArcGISPluginConfig> {
123+
public async safeGetConfig(): Promise<ArcGISPluginConfig> {
124124
const state = await this._stateRepo.get();
125125
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;
126+
return await this._stateRepo.get().then((state) => state ? state : this._stateRepo.put(defaultArcGISPluginConfig));
130127
}
131128

132129
/**
@@ -164,19 +161,6 @@ export class ObservationProcessor {
164161
return config
165162
}
166163

167-
public 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-
180164
/**
181165
* Starts the processor.
182166
*/

plugins/arcgis/service/src/index.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ArcGISPluginConfig } from './ArcGISPluginConfig'
88
import { AuthType } from './ArcGISConfig'
99
import { ObservationProcessor } from './ObservationProcessor'
1010
import { ArcGISIdentityManager, request } from "@esri/arcgis-rest-request"
11-
import { FeatureServiceConfig } from './ArcGISConfig'
11+
import { FeatureServiceConfig, OAuthAuthConfig } from './ArcGISConfig'
1212
import { URL } from "node:url"
1313
import express from 'express'
1414
import { getIdentityManager, getPortalUrl } from './ArcGISIdentityManagerFactory'
@@ -37,6 +37,19 @@ const InjectedServices = {
3737

3838
const pluginWebRoute = "plugins/@ngageoint/mage.arcgis.service"
3939

40+
const sanitizeFeatureService = (config: FeatureServiceConfig, type: AuthType): FeatureServiceConfig => {
41+
if (type === AuthType.OAuth) {
42+
const newAuth = Object.assign({}, config.auth) as OAuthAuthConfig;
43+
delete newAuth.refreshToken;
44+
delete newAuth.refreshTokenExpires;
45+
return {
46+
...config,
47+
auth: newAuth
48+
}
49+
}
50+
return config;
51+
}
52+
4053
/**
4154
* The MAGE ArcGIS Plugin finds new MAGE observations and if configured to send the observations
4255
* to an ArcGIS server, it will then transform the observation to an ArcGIS feature and
@@ -74,7 +87,7 @@ const arcgisPluginHooks: InitPluginHook<typeof InjectedServices> = {
7487
return res.status(404).send('clientId is required')
7588
}
7689

77-
const config = await processor.safeGetConfig(true)
90+
const config = await processor.safeGetConfig()
7891
ArcGISIdentityManager.authorize({
7992
clientId,
8093
portal: getPortalUrl(url),
@@ -95,7 +108,7 @@ const arcgisPluginHooks: InitPluginHook<typeof InjectedServices> = {
95108
return res.sendStatus(500)
96109
}
97110

98-
const config = await processor.safeGetConfig(true)
111+
const config = await processor.safeGetConfig()
99112
const creds = {
100113
clientId: state.clientId,
101114
redirectUri: `${config.baseUrl}/${pluginWebRoute}/oauth/authenticate`,
@@ -150,6 +163,7 @@ const arcgisPluginHooks: InitPluginHook<typeof InjectedServices> = {
150163
.get(async (req, res, next) => {
151164
console.info('Getting ArcGIS plugin config...')
152165
const config = await processor.safeGetConfig()
166+
config.featureServices = config.featureServices.map((service) => sanitizeFeatureService(service, AuthType.OAuth));
153167
res.json(config)
154168
})
155169
.put(async (req, res, next) => {
@@ -161,7 +175,7 @@ const arcgisPluginHooks: InitPluginHook<typeof InjectedServices> = {
161175
})
162176

163177
routes.post('/featureService/validate', async (req, res) => {
164-
const config = await processor.safeGetConfig(true)
178+
const config = await processor.safeGetConfig()
165179
const { url, auth = {} } = req.body
166180
const { token, username, password } = auth
167181
if (!URL.canParse(url)) {
@@ -186,17 +200,16 @@ const arcgisPluginHooks: InitPluginHook<typeof InjectedServices> = {
186200
} else {
187201
config.featureServices.push(service)
188202
}
189-
console.log('values patch')
190203
await processor.patchConfig(config)
191-
return res.send(processor.sanitizeFeatureService(service, AuthType.OAuth))
204+
return res.send(sanitizeFeatureService(service, AuthType.OAuth))
192205
} catch (err) {
193206
return res.send('Invalid credentials provided to communicate with feature service').status(400)
194207
}
195208
})
196209

197210
routes.get('/featureService/layers', async (req, res, next) => {
198211
const url = req.query.featureServiceUrl as string
199-
const config = await processor.safeGetConfig(true)
212+
const config = await processor.safeGetConfig()
200213
const featureService = config.featureServices.find(featureService => featureService.url === url)
201214
if (!featureService) {
202215
return res.status(400)

0 commit comments

Comments
 (0)