| 1 | import { |
| 2 | type CanActivate, |
| 3 | type ExecutionContext, |
| 4 | Injectable, |
| 5 | UnauthorizedException, |
| 6 | } from '@nestjs/common'; |
| 7 | import { ConfigService } |
| 8 | import { createHash, timingSafeEqual |
| 9 | import type { Request |
| 10 | |
| 11 | import { githubImportConfig } |
| 12 | |
| 13 | /** The importer's callbacks carry the secret the API sent its jobs with. */ |
| 14 | @Injectable() |
| 15 | export class ImporterSecretGuard |
| 16 | constructor(private readonly |
| 17 | |
| 18 | canActivate(context: |
| 19 | const secret = |
| 20 | const header = |
| 21 | .headers.authorization; |
| 22 | if (!secret || |
| 23 | throw new UnauthorizedException() |
| 24 | |
| 25 | // Hashing first gives both sides one length, which `timingSafeEqual` needs. |
| 26 | const given = |
| 27 | const expected = |
| 28 | if (!timingSafeEqual(given |
| 29 | return true; |
| 30 | } |
| 31 | } |