1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-04 13:48:56 +02:00

chore: expose custom-handler-auth type (#5287)

This will help us get type checking on the auth handler function
This commit is contained in:
Gastón Fournier 2023-11-07 10:37:09 +01:00 committed by GitHub
parent addda5b022
commit 1dc7dd646d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 8 deletions

View File

@ -118,12 +118,16 @@ export default async function getApp(
} }
case IAuthType.ENTERPRISE: { case IAuthType.ENTERPRISE: {
app.use(baseUriPath, apiTokenMiddleware(config, services)); app.use(baseUriPath, apiTokenMiddleware(config, services));
config.authentication.customAuthHandler(app, config, services); if (config.authentication.customAuthHandler) {
config.authentication.customAuthHandler(app, config, services);
}
break; break;
} }
case IAuthType.HOSTED: { case IAuthType.HOSTED: {
app.use(baseUriPath, apiTokenMiddleware(config, services)); app.use(baseUriPath, apiTokenMiddleware(config, services));
config.authentication.customAuthHandler(app, config, services); if (config.authentication.customAuthHandler) {
config.authentication.customAuthHandler(app, config, services);
}
break; break;
} }
case IAuthType.DEMO: { case IAuthType.DEMO: {
@ -138,7 +142,9 @@ export default async function getApp(
} }
case IAuthType.CUSTOM: { case IAuthType.CUSTOM: {
app.use(baseUriPath, apiTokenMiddleware(config, services)); app.use(baseUriPath, apiTokenMiddleware(config, services));
config.authentication.customAuthHandler(app, config, services); if (config.authentication.customAuthHandler) {
config.authentication.customAuthHandler(app, config, services);
}
break; break;
} }
case IAuthType.NONE: { case IAuthType.NONE: {

View File

@ -1,11 +1,11 @@
import { Express } from 'express';
import { IUnleashConfig } from './types/option'; import { IUnleashConfig } from './types/option';
const customAuthWarning = const customAuthWarning =
'You have to configure a custom authentication middleware. Read https://docs.getunleash.io/docs/reference/deploy/configuring-unleash for more details'; 'You have to configure a custom authentication middleware. Read https://docs.getunleash.io/docs/reference/deploy/configuring-unleash for more details';
export function defaultCustomAuthDenyAll( export function defaultCustomAuthDenyAll(
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types app: Express,
app: any,
config: IUnleashConfig, config: IUnleashConfig,
): void { ): void {
const logger = config.getLogger('src/lib/app/customAuthHandler'); const logger = config.getLogger('src/lib/app/customAuthHandler');

View File

@ -18,6 +18,7 @@ import {
IUnleashOptions, IUnleashOptions,
IUnleashServices, IUnleashServices,
RoleName, RoleName,
CustomAuthHandler,
} from './types'; } from './types';
import User, { IUser } from './types/user'; import User, { IUser } from './types/user';
@ -212,4 +213,5 @@ export type {
IAuthRequest, IAuthRequest,
IApiRequest, IApiRequest,
SimpleAuthSettings, SimpleAuthSettings,
CustomAuthHandler,
}; };

View File

@ -1,8 +1,10 @@
import { Express } from 'express';
import EventEmitter from 'events'; import EventEmitter from 'events';
import { LogLevel, LogProvider } from '../logger'; import { LogLevel, LogProvider } from '../logger';
import { ILegacyApiTokenCreate } from './models/api-token'; import { ILegacyApiTokenCreate } from './models/api-token';
import { IFlagResolver, IExperimentalOptions, IFlags } from './experimental'; import { IFlagResolver, IExperimentalOptions, IFlags } from './experimental';
import SMTPTransport from 'nodemailer/lib/smtp-transport'; import SMTPTransport from 'nodemailer/lib/smtp-transport';
import { IUnleashServices } from './services';
export interface ISSLOption { export interface ISSLOption {
rejectUnauthorized: boolean; rejectUnauthorized: boolean;
@ -53,10 +55,16 @@ export enum IAuthType {
NONE = 'none', NONE = 'none',
} }
export type CustomAuthHandler = (
app: Express,
config: Partial<IUnleashConfig>,
services?: IUnleashServices,
) => void;
export interface IAuthOption { export interface IAuthOption {
enableApiToken: boolean; enableApiToken: boolean;
type: IAuthType; type: IAuthType;
customAuthHandler?: Function; customAuthHandler?: CustomAuthHandler;
createAdminUser?: boolean; createAdminUser?: boolean;
initialAdminUser?: { initialAdminUser?: {
username: string; username: string;

View File

@ -47,7 +47,6 @@ class UserStoreMock implements IUserStore {
} }
async insert(user: User): Promise<User> { async insert(user: User): Promise<User> {
// eslint-disable-next-line no-param-reassign
user.id = this.idSeq; user.id = this.idSeq;
this.idSeq += 1; this.idSeq += 1;
this.data.push(user); this.data.push(user);
@ -55,7 +54,6 @@ class UserStoreMock implements IUserStore {
} }
async update(id: number, user: User): Promise<User> { async update(id: number, user: User): Promise<User> {
// eslint-disable-next-line no-param-reassign
this.data = this.data.map((o) => { this.data = this.data.map((o) => {
if (o.id === id) return { ...o, name: user.name }; if (o.id === id) return { ...o, name: user.name };
return o; return o;