-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from red-kite-solutions/api_key_authentication
Api key authentication
- Loading branch information
Showing
69 changed files
with
2,489 additions
and
1,518 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
packages/backend/jobs-manager/service/src/modules/app.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/backend/jobs-manager/service/src/modules/auth/auth.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Request } from 'express'; | ||
import { Role } from './constants'; | ||
|
||
export interface UserAuthContext { | ||
id: string; | ||
role: Role; | ||
email?: string; | ||
apiKeyId?: string; | ||
} | ||
|
||
export type AuthenticatedRequest = Request & { user: UserAuthContext }; |
9 changes: 9 additions & 0 deletions
9
packages/backend/jobs-manager/service/src/modules/auth/guards/api-key.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class ApiKeyGuard extends AuthGuard('ApiKeyStrategy') { | ||
constructor() { | ||
super(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/backend/jobs-manager/service/src/modules/auth/strategies/api-key.strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { HeaderAPIKeyStrategy } from 'passport-headerapikey'; | ||
import { ApiKeyDocument } from '../../database/api-key/api-key.model'; | ||
import { AuthService } from '../auth.service'; | ||
import { UserAuthContext } from '../auth.types'; | ||
|
||
@Injectable() | ||
export class ApiKeyStrategy extends PassportStrategy( | ||
HeaderAPIKeyStrategy, | ||
'ApiKeyStrategy', | ||
) { | ||
constructor(private authService: AuthService) { | ||
super( | ||
{ header: 'x-api-key', prefix: '' }, | ||
true, | ||
async (apikey, done, req) => { | ||
const apiKeyDocument: ApiKeyDocument = | ||
await authService.findValidApiKey(apikey); | ||
|
||
if (!apiKeyDocument) return done(null, false); | ||
|
||
const user: UserAuthContext = { | ||
id: apiKeyDocument.userId.toString(), | ||
apiKeyId: apiKeyDocument._id.toString(), | ||
role: apiKeyDocument.role, | ||
}; | ||
|
||
return done(null, user); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
packages/backend/jobs-manager/service/src/modules/database/admin/config/config.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
packages/backend/jobs-manager/service/src/modules/database/alarm/alarm.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/backend/jobs-manager/service/src/modules/database/api-key/api-key-model.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { ApiKeySchema } from './api-key.model'; | ||
|
||
export const ApiKeyModelModule = MongooseModule.forFeature([ | ||
{ | ||
name: 'apikey', | ||
schema: ApiKeySchema, | ||
}, | ||
]); |
Oops, something went wrong.