Files
api/app/core/utils/authentication/ensure-auth.ts
T
Alexis Tyler 4e1b0bd72c chore: lint
2021-01-28 15:45:14 +10:30

34 lines
717 B
TypeScript

/*!
* Copyright 2019-2020 Lime Technology Inc. All rights reserved.
* Written by: Alexis Tyler
*/
import { paths } from '../../paths';
import { PermissionError } from '../../errors';
import { checkAuth } from '..';
interface Options {
username: string;
password: string;
file: string;
}
/**
* Check if the username and password match a htpasswd file
*/
export const ensureAuth = async (options: Options) => {
const { username, password, file } = options;
// `valid` will be true if and only if
// username and password were correct.
const valid = await checkAuth({
username,
password,
file: file || paths.get('htpasswd')!
});
if (!valid) {
throw new PermissionError('Invalid auth!');
}
};