File size: 657 Bytes
4327358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { IApiKeyAuth } from '@waha/core/auth/auth';
import { HeaderAPIKeyStrategy } from 'passport-headerapikey';

@Injectable()
export class ApiKeyStrategy extends PassportStrategy(HeaderAPIKeyStrategy) {
  constructor(private auth: IApiKeyAuth) {
    super({ header: 'X-Api-Key', prefix: '' }, true, (apikey, done) => {
      const isValid = this.auth.isValid(apikey);
      return done(isValid);
    });
  }

  validate(apikey: string, done: (result: boolean) => void): void {
    const isValid = this.auth.isValid(apikey);
    return done(isValid);
  }
}