Skip to content

Commit

Permalink
chore: expose custom-handler-auth type (#5287)
Browse files Browse the repository at this point in the history
This will help us get type checking on the auth handler function
  • Loading branch information
gastonfournier committed Nov 7, 2023
1 parent addda5b commit 1dc7dd6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/lib/app.ts
Expand Up @@ -118,12 +118,16 @@ export default async function getApp(
}
case IAuthType.ENTERPRISE: {
app.use(baseUriPath, apiTokenMiddleware(config, services));
config.authentication.customAuthHandler(app, config, services);
if (config.authentication.customAuthHandler) {
config.authentication.customAuthHandler(app, config, services);
}
break;
}
case IAuthType.HOSTED: {
app.use(baseUriPath, apiTokenMiddleware(config, services));
config.authentication.customAuthHandler(app, config, services);
if (config.authentication.customAuthHandler) {
config.authentication.customAuthHandler(app, config, services);
}
break;
}
case IAuthType.DEMO: {
Expand All @@ -138,7 +142,9 @@ export default async function getApp(
}
case IAuthType.CUSTOM: {
app.use(baseUriPath, apiTokenMiddleware(config, services));
config.authentication.customAuthHandler(app, config, services);
if (config.authentication.customAuthHandler) {
config.authentication.customAuthHandler(app, config, services);
}
break;
}
case IAuthType.NONE: {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/default-custom-auth-deny-all.ts
@@ -1,11 +1,11 @@
import { Express } from 'express';
import { IUnleashConfig } from './types/option';

const customAuthWarning =
'You have to configure a custom authentication middleware. Read https://docs.getunleash.io/docs/reference/deploy/configuring-unleash for more details';

export function defaultCustomAuthDenyAll(
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
app: any,
app: Express,
config: IUnleashConfig,
): void {
const logger = config.getLogger('src/lib/app/customAuthHandler');
Expand Down
2 changes: 2 additions & 0 deletions src/lib/server-impl.ts
Expand Up @@ -18,6 +18,7 @@ import {
IUnleashOptions,
IUnleashServices,
RoleName,
CustomAuthHandler,
} from './types';

import User, { IUser } from './types/user';
Expand Down Expand Up @@ -212,4 +213,5 @@ export type {
IAuthRequest,
IApiRequest,
SimpleAuthSettings,
CustomAuthHandler,
};
10 changes: 9 additions & 1 deletion src/lib/types/option.ts
@@ -1,8 +1,10 @@
import { Express } from 'express';
import EventEmitter from 'events';
import { LogLevel, LogProvider } from '../logger';
import { ILegacyApiTokenCreate } from './models/api-token';
import { IFlagResolver, IExperimentalOptions, IFlags } from './experimental';
import SMTPTransport from 'nodemailer/lib/smtp-transport';
import { IUnleashServices } from './services';

export interface ISSLOption {
rejectUnauthorized: boolean;
Expand Down Expand Up @@ -53,10 +55,16 @@ export enum IAuthType {
NONE = 'none',
}

export type CustomAuthHandler = (
app: Express,
config: Partial<IUnleashConfig>,
services?: IUnleashServices,
) => void;

export interface IAuthOption {
enableApiToken: boolean;
type: IAuthType;
customAuthHandler?: Function;
customAuthHandler?: CustomAuthHandler;
createAdminUser?: boolean;
initialAdminUser?: {
username: string;
Expand Down
2 changes: 0 additions & 2 deletions src/test/fixtures/fake-user-store.ts
Expand Up @@ -47,15 +47,13 @@ class UserStoreMock implements IUserStore {
}

async insert(user: User): Promise<User> {
// eslint-disable-next-line no-param-reassign
user.id = this.idSeq;
this.idSeq += 1;
this.data.push(user);
return Promise.resolve(user);
}

async update(id: number, user: User): Promise<User> {
// eslint-disable-next-line no-param-reassign
this.data = this.data.map((o) => {
if (o.id === id) return { ...o, name: user.name };
return o;
Expand Down

0 comments on commit 1dc7dd6

Please sign in to comment.