1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-10 17:53:36 +02:00

separate GET ui config to no auth

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
This commit is contained in:
andreas-unleash 2022-12-06 16:00:10 +02:00
parent b976fee44b
commit a8f546ef5e
No known key found for this signature in database
GPG Key ID: DB82A1577B38F66B
2 changed files with 123 additions and 76 deletions

View File

@ -1,29 +1,17 @@
import { Response } from 'express'; import { Response } from 'express';
import { AuthedRequest } from '../../types/core'; import { ADMIN, IUnleashConfig, IUnleashServices } from '../../types';
import { IUnleashServices } from '../../types/services';
import { IAuthType, IUnleashConfig } from '../../types/option';
import version from '../../util/version';
import Controller from '../controller'; import Controller from '../controller';
import VersionService from '../../services/version-service'; import VersionService from '../../services/version-service';
import SettingService from '../../services/setting-service'; import SettingService from '../../services/setting-service';
import { EmailService, OpenApiService } from '../../services';
import { import {
simpleAuthSettingsKey, createRequestSchema,
SimpleAuthSettings, emptyResponse,
} from '../../types/settings/simple-auth-settings'; SetUiConfigSchema,
import { ADMIN, NONE } from '../../types/permissions'; } from '../../openapi';
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';
import { IAuthRequest } from '../unleash-types'; import { IAuthRequest } from '../unleash-types';
import { extractUsername } from '../../util/extract-user'; import { extractUsername } from '../../util';
import NotFoundError from '../../error/notfound-error'; 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 { class ConfigController extends Controller {
private versionService: VersionService; private versionService: VersionService;
@ -55,22 +43,6 @@ class ConfigController extends Controller {
this.emailService = emailService; this.emailService = emailService;
this.openApiService = openApiService; 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({ this.route({
method: 'post', method: 'post',
path: '', path: '',
@ -87,47 +59,6 @@ class ConfigController extends Controller {
}); });
} }
async getUiConfig(
req: AuthedRequest,
res: Response<UiConfigSchema>,
): Promise<void> {
const [frontendSettings, simpleAuthSettings] = await Promise.all([
this.settingService.getFrontendSettings(),
this.settingService.get<SimpleAuthSettings>(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( async setUiConfig(
req: IAuthRequest<void, void, SetUiConfigSchema>, req: IAuthRequest<void, void, SetUiConfigSchema>,
res: Response<string>, res: Response<string>,

View File

@ -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<UiConfigSchema>,
): Promise<void> {
const [frontendSettings, simpleAuthSettings] = await Promise.all([
this.settingService.getFrontendSettings(),
this.settingService.get<SimpleAuthSettings>(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;