27 lines
788 B
TypeScript
27 lines
788 B
TypeScript
import {
|
|
CanActivate,
|
|
ExecutionContext,
|
|
Injectable,
|
|
UnprocessableEntityException,
|
|
} from '@nestjs/common';
|
|
|
|
@Injectable()
|
|
export class SetupSchedulingGuard implements CanActivate {
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
const request = context.switchToHttp().getRequest();
|
|
const setupAuth = request.headers['x-setup-authorization'];
|
|
|
|
if (setupAuth) {
|
|
try {
|
|
const setupKey = process.env.SETUP_SCHEDULING_KEY;
|
|
if (setupAuth === setupKey) return true;
|
|
else new UnprocessableEntityException('Setup Authorization Not Found.');
|
|
} catch (err) {
|
|
throw new UnprocessableEntityException('Invalid authentication.');
|
|
}
|
|
}
|
|
|
|
throw new UnprocessableEntityException('Invalid authentication');
|
|
}
|
|
}
|