mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01: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:
		
							parent
							
								
									28d0238732
								
							
						
					
					
						commit
						62b121285c
					
				@ -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;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -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;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user