mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-31 00:16:47 +01:00
53354224fc
Upgrades biome to 1.6.1, and updates husky pre-commit hook. Most changes here are making type imports explicit.
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import type { IUnleashConfig } from '../types';
|
|
import type { IAuthRequest } from '../routes/unleash-types';
|
|
import NotFoundError from '../error/notfound-error';
|
|
import type { AccountService } from '../services/account-service';
|
|
|
|
const patMiddleware = (
|
|
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
|
{ accountService }: { accountService: AccountService },
|
|
): any => {
|
|
const logger = getLogger('/middleware/pat-middleware.ts');
|
|
logger.debug('Enabling PAT middleware');
|
|
|
|
return async (req: IAuthRequest, res, next) => {
|
|
try {
|
|
const apiToken = req.header('authorization');
|
|
if (apiToken?.startsWith('user:')) {
|
|
const user =
|
|
await accountService.getAccountByPersonalAccessToken(
|
|
apiToken,
|
|
);
|
|
req.user = user;
|
|
accountService.addPATSeen(apiToken);
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof NotFoundError) {
|
|
logger.warn(
|
|
'Tried to use a PAT token for user that no longer existed',
|
|
error,
|
|
);
|
|
} else {
|
|
logger.error(error);
|
|
}
|
|
}
|
|
next();
|
|
};
|
|
};
|
|
|
|
export default patMiddleware;
|