1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-02 01:17:58 +02:00

Create a apiuser for demo auth. (#1045)

- If api token middleware is disabled, still allow calls to /api/client with a
  populated fake api user with client access.
This commit is contained in:
Christopher Kolstad 2021-10-20 13:16:07 +02:00 committed by GitHub
parent 28d0238732
commit 62b121285c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View File

@ -94,7 +94,12 @@ export default function getApp(
} }
case IAuthType.DEMO: { case IAuthType.DEMO: {
app.use(baseUriPath, apiTokenMiddleware(config, services)); app.use(baseUriPath, apiTokenMiddleware(config, services));
demoAuthentication(app, config.server.baseUriPath, services); demoAuthentication(
app,
config.server.baseUriPath,
services,
config,
);
break; break;
} }
case IAuthType.CUSTOM: { case IAuthType.CUSTOM: {
@ -107,7 +112,13 @@ export default function getApp(
break; break;
} }
default: { default: {
demoAuthentication(app, config.server.baseUriPath, services); app.use(baseUriPath, apiTokenMiddleware(config, services));
demoAuthentication(
app,
config.server.baseUriPath,
services,
config,
);
break; break;
} }
} }

View File

@ -1,11 +1,15 @@
import { Application } from 'express'; import { Application } from 'express';
import AuthenticationRequired from '../types/authentication-required'; import AuthenticationRequired from '../types/authentication-required';
import { IUnleashServices } from '../types/services'; import { IUnleashServices } from '../types/services';
import { IUnleashConfig } from '../types/option';
import ApiUser from '../types/api-user';
import { ApiTokenType } from '../types/models/api-token';
function demoAuthentication( function demoAuthentication(
app: Application, app: Application,
basePath: string = '', basePath: string = '',
{ userService }: Pick<IUnleashServices, 'userService'>, { userService }: Pick<IUnleashServices, 'userService'>,
{ authentication }: Pick<IUnleashConfig, 'authentication'>,
): void { ): void {
app.post(`${basePath}/api/admin/login`, async (req, res) => { app.post(`${basePath}/api/admin/login`, async (req, res) => {
const { email } = req.body; const { email } = req.body;
@ -39,6 +43,21 @@ function demoAuthentication(
next(); next();
}); });
app.use(`${basePath}/api/client`, (req, res, next) => {
// @ts-ignore
if (!authentication.enableApiToken && !req.user) {
// @ts-ignore
req.user = new ApiUser({
username: 'unauthed-default-client',
permissions: [],
environment: 'default',
type: ApiTokenType.CLIENT,
project: '*',
});
}
next();
});
app.use(`${basePath}/api`, (req, res, next) => { app.use(`${basePath}/api`, (req, res, next) => {
// @ts-ignore // @ts-ignore
if (req.user) { if (req.user) {
@ -57,4 +76,5 @@ function demoAuthentication(
.end(); .end();
}); });
} }
export default demoAuthentication; export default demoAuthentication;