From a8f546ef5e57a65704baf4bc5fa9ea369efbfc78 Mon Sep 17 00:00:00 2001 From: andreas-unleash Date: Tue, 6 Dec 2022 16:00:10 +0200 Subject: [PATCH] separate GET ui config to no auth Signed-off-by: andreas-unleash --- src/lib/routes/admin-api/config.ts | 83 ++------------------- src/lib/routes/get-config.ts | 116 +++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 76 deletions(-) create mode 100644 src/lib/routes/get-config.ts diff --git a/src/lib/routes/admin-api/config.ts b/src/lib/routes/admin-api/config.ts index 3604909b59..6d229cd5ab 100644 --- a/src/lib/routes/admin-api/config.ts +++ b/src/lib/routes/admin-api/config.ts @@ -1,29 +1,17 @@ import { Response } from 'express'; -import { AuthedRequest } from '../../types/core'; -import { IUnleashServices } from '../../types/services'; -import { IAuthType, IUnleashConfig } from '../../types/option'; -import version from '../../util/version'; +import { ADMIN, IUnleashConfig, IUnleashServices } from '../../types'; import Controller from '../controller'; import VersionService from '../../services/version-service'; import SettingService from '../../services/setting-service'; +import { EmailService, OpenApiService } from '../../services'; import { - simpleAuthSettingsKey, - SimpleAuthSettings, -} from '../../types/settings/simple-auth-settings'; -import { ADMIN, NONE } from '../../types/permissions'; -import { createResponseSchema } from '../../openapi/util/create-response-schema'; -import { - uiConfigSchema, - UiConfigSchema, -} from '../../openapi/spec/ui-config-schema'; -import { OpenApiService } from '../../services/openapi-service'; -import { EmailService } from '../../services/email-service'; -import { emptyResponse } from '../../openapi/util/standard-responses'; + createRequestSchema, + emptyResponse, + SetUiConfigSchema, +} from '../../openapi'; import { IAuthRequest } from '../unleash-types'; -import { extractUsername } from '../../util/extract-user'; +import { extractUsername } from '../../util'; import NotFoundError from '../../error/notfound-error'; -import { SetUiConfigSchema } from '../../openapi/spec/set-ui-config-schema'; -import { createRequestSchema } from '../../openapi/util/create-request-schema'; class ConfigController extends Controller { private versionService: VersionService; @@ -55,22 +43,6 @@ class ConfigController extends Controller { this.emailService = emailService; this.openApiService = openApiService; - this.route({ - method: 'get', - path: '', - handler: this.getUiConfig, - permission: NONE, - middleware: [ - openApiService.validPath({ - tags: ['Admin UI'], - operationId: 'getUiConfig', - responses: { - 200: createResponseSchema('uiConfigSchema'), - }, - }), - ], - }); - this.route({ method: 'post', path: '', @@ -87,47 +59,6 @@ class ConfigController extends Controller { }); } - async getUiConfig( - req: AuthedRequest, - res: Response, - ): Promise { - const [frontendSettings, simpleAuthSettings] = await Promise.all([ - this.settingService.getFrontendSettings(), - this.settingService.get(simpleAuthSettingsKey), - ]); - - const disablePasswordAuth = - simpleAuthSettings?.disabled || - this.config.authentication.type == IAuthType.NONE; - - const expFlags = this.config.flagResolver.getAll({ - email: req.user.email, - }); - const flags = { ...this.config.ui.flags, ...expFlags }; - - const response: UiConfigSchema = { - ...this.config.ui, - flags, - version, - emailEnabled: this.emailService.isEnabled(), - unleashUrl: this.config.server.unleashUrl, - baseUriPath: this.config.server.baseUriPath, - authenticationType: this.config.authentication?.type, - segmentValuesLimit: this.config.segmentValuesLimit, - strategySegmentsLimit: this.config.strategySegmentsLimit, - frontendApiOrigins: frontendSettings.frontendApiOrigins, - versionInfo: this.versionService.getVersionInfo(), - disablePasswordAuth, - }; - - this.openApiService.respondWithValidation( - 200, - res, - uiConfigSchema.$id, - response, - ); - } - async setUiConfig( req: IAuthRequest, res: Response, diff --git a/src/lib/routes/get-config.ts b/src/lib/routes/get-config.ts new file mode 100644 index 0000000000..d5a27f1081 --- /dev/null +++ b/src/lib/routes/get-config.ts @@ -0,0 +1,116 @@ +// @ts-ignore +// @ts-ignore + +import { Response } from 'express'; +import { + AuthedRequest, + IAuthType, + IUnleashConfig, + IUnleashServices, + NONE, +} from '../types'; +import version from '../util/version'; +import Controller from './controller'; +import VersionService from './../services/version-service'; +import SettingService from './../services/setting-service'; +import { + SimpleAuthSettings, + simpleAuthSettingsKey, +} from '../types/settings/simple-auth-settings'; +import { + createResponseSchema, + UiConfigSchema, + uiConfigSchema, +} from '../openapi'; +import { EmailService, OpenApiService } from '../services'; + +class GetConfigController extends Controller { + private versionService: VersionService; + + private settingService: SettingService; + + private emailService: EmailService; + + private readonly openApiService: OpenApiService; + + constructor( + config: IUnleashConfig, + { + versionService, + settingService, + emailService, + openApiService, + }: Pick< + IUnleashServices, + | 'versionService' + | 'settingService' + | 'emailService' + | 'openApiService' + >, + ) { + super(config); + this.versionService = versionService; + this.settingService = settingService; + this.emailService = emailService; + this.openApiService = openApiService; + + this.route({ + method: 'get', + path: '', + handler: this.getUiConfig, + permission: NONE, + middleware: [ + openApiService.validPath({ + tags: ['Admin UI'], + operationId: 'getUiConfig', + responses: { + 200: createResponseSchema('uiConfigSchema'), + }, + }), + ], + }); + } + + async getUiConfig( + req: AuthedRequest, + res: Response, + ): Promise { + const [frontendSettings, simpleAuthSettings] = await Promise.all([ + this.settingService.getFrontendSettings(), + this.settingService.get(simpleAuthSettingsKey), + ]); + + const disablePasswordAuth = + simpleAuthSettings?.disabled || + this.config.authentication.type == IAuthType.NONE; + + const expFlags = this.config.flagResolver.getAll({ + email: req.user.email, + }); + const flags = { ...this.config.ui.flags, ...expFlags }; + + const response: UiConfigSchema = { + ...this.config.ui, + flags, + version, + emailEnabled: this.emailService.isEnabled(), + unleashUrl: this.config.server.unleashUrl, + baseUriPath: this.config.server.baseUriPath, + authenticationType: this.config.authentication?.type, + segmentValuesLimit: this.config.segmentValuesLimit, + strategySegmentsLimit: this.config.strategySegmentsLimit, + frontendApiOrigins: frontendSettings.frontendApiOrigins, + versionInfo: this.versionService.getVersionInfo(), + disablePasswordAuth, + }; + + this.openApiService.respondWithValidation( + 200, + res, + uiConfigSchema.$id, + response, + ); + } +} + +export default GetConfigController;