feat: apikey strategy
This commit is contained in:
committed by
Clément Le Bihan
parent
ab221bd393
commit
1379cbd3f6
11413
back/package-lock.json
generated
11413
back/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -46,6 +46,7 @@
|
||||
"nodemailer": "^6.9.5",
|
||||
"opensheetmusicdisplay": "^1.8.4",
|
||||
"passport-google-oauth20": "^2.0.0",
|
||||
"passport-headerapikey": "^1.2.2",
|
||||
"passport-jwt": "^4.0.1",
|
||||
"passport-local": "^1.0.0",
|
||||
"prisma-class-generator": "^0.2.7",
|
||||
|
||||
5
back/src/auth/apikey-auth.guard.ts
Normal file
5
back/src/auth/apikey-auth.guard.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
|
||||
@Injectable()
|
||||
export class ApiKeyAuthGuard extends AuthGuard('api-key') {}
|
||||
18
back/src/auth/apikey.strategy.ts
Normal file
18
back/src/auth/apikey.strategy.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { HeaderAPIKeyStrategy } from 'passport-headerapikey';
|
||||
|
||||
@Injectable()
|
||||
export class ApiKeyStrategy extends PassportStrategy(HeaderAPIKeyStrategy, 'api-key') {
|
||||
constructor(private readonly authService: AuthService) {
|
||||
super({ header: "Authorization", prefix: "API Key " }, true, async (apikey, done) => {
|
||||
if (this.authService.validateApiKey(apikey))
|
||||
return done(null, true);
|
||||
else
|
||||
return done(new UnauthorizedException(), false);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,15 @@ export class AuthService {
|
||||
private emailService: MailerService,
|
||||
) {}
|
||||
|
||||
validateApiKey(apikey: string): boolean {
|
||||
console.log("wow");
|
||||
if (process.env.API_KEYS == null) return false;
|
||||
const keys = process.env.API_KEYS.split(',');
|
||||
console.log(keys);
|
||||
return keys.includes(apikey);
|
||||
|
||||
}
|
||||
|
||||
async validateUser(
|
||||
username: string,
|
||||
password: string,
|
||||
|
||||
@@ -38,6 +38,7 @@ import { Song as _Song } from "src/_gen/prisma-class/song";
|
||||
import { SongHistory } from "src/_gen/prisma-class/song_history";
|
||||
import { IncludeMap, mapInclude } from "src/utils/include";
|
||||
import { Public } from "src/auth/public";
|
||||
import { ApiKeyAuthGuard } from "src/auth/apikey-auth.guard";
|
||||
|
||||
class SongHistoryResult {
|
||||
@ApiProperty()
|
||||
@@ -48,7 +49,7 @@ class SongHistoryResult {
|
||||
|
||||
@Controller("song")
|
||||
@ApiTags("song")
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(JwtAuthGuard, ApiKeyAuthGuard)
|
||||
export class SongController {
|
||||
static filterableFields: string[] = [
|
||||
"+id",
|
||||
|
||||
Reference in New Issue
Block a user