1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00
unleash.unleash/src/lib/middleware/no-authentication.ts
Christopher Kolstad 53354224fc
chore: Bump biome and configure husky (#6589)
Upgrades biome to 1.6.1, and updates husky pre-commit hook.

Most changes here are making type imports explicit.
2024-03-18 13:58:05 +01:00

54 lines
1.6 KiB
TypeScript

import type { Application } from 'express';
import NoAuthUser from '../types/no-auth-user';
import { ApiTokenType } from '../types/models/api-token';
import {
ApiUser,
type IApiRequest,
type IAuthRequest,
permissions,
} from '../server-impl';
import { DEFAULT_ENV } from '../util';
// eslint-disable-next-line
function noneAuthentication(baseUriPath: string, app: Application): void {
app.use(
`${baseUriPath || ''}/api/admin/`,
(req: IAuthRequest, res, next) => {
if (!req.user) {
req.user = new NoAuthUser();
}
next();
},
);
}
export function noApiToken(baseUriPath: string, app: Application) {
app.use(`${baseUriPath}/api/frontend`, (req: IApiRequest, res, next) => {
if (!req.headers.authorization && !req.user) {
req.user = new ApiUser({
tokenName: 'unknown',
permissions: [permissions.FRONTEND],
projects: ['*'],
environment: DEFAULT_ENV,
type: ApiTokenType.FRONTEND,
secret: 'unknown',
});
}
next();
});
app.use(`${baseUriPath}/api/client`, (req: IApiRequest, res, next) => {
if (!req.headers.authorization && !req.user) {
req.user = new ApiUser({
tokenName: 'unknown',
permissions: [permissions.CLIENT],
projects: ['*'],
environment: DEFAULT_ENV,
type: ApiTokenType.CLIENT,
secret: 'unknown',
});
}
next();
});
}
export default noneAuthentication;