-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add client id guard to auth request endpoints (#361)
- Loading branch information
Samuel
authored
Jun 26, 2024
1 parent
83a3606
commit 301486e
Showing
19 changed files
with
3,342 additions
and
3,529 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
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
15 changes: 15 additions & 0 deletions
15
apps/armory/src/shared/decorator/api-client-id-guard.decorator.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,15 @@ | ||
import { UseGuards, applyDecorators } from '@nestjs/common' | ||
import { ApiHeader, ApiSecurity } from '@nestjs/swagger' | ||
import { CLIENT_ID_SECURITY, REQUEST_HEADER_CLIENT_ID } from '../../armory.constant' | ||
import { ClientIdGuard } from '../guard/client-id.guard' | ||
|
||
export function ApiClientIdGuard() { | ||
return applyDecorators( | ||
UseGuards(ClientIdGuard), | ||
ApiSecurity(CLIENT_ID_SECURITY.name), | ||
ApiHeader({ | ||
name: REQUEST_HEADER_CLIENT_ID, | ||
required: true | ||
}) | ||
) | ||
} |
13 changes: 0 additions & 13 deletions
13
apps/armory/src/shared/decorator/api-client-id.decorator.ts
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { CanActivate, ExecutionContext, HttpStatus, Injectable } from '@nestjs/common' | ||
import { REQUEST_HEADER_CLIENT_ID } from '../../armory.constant' | ||
import { ClientService } from '../../client/core/service/client.service' | ||
import { ApplicationException } from '../exception/application.exception' | ||
|
||
@Injectable() | ||
export class ClientIdGuard implements CanActivate { | ||
constructor(private clientService: ClientService) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const req = context.switchToHttp().getRequest() | ||
|
||
const clientId = req.headers[REQUEST_HEADER_CLIENT_ID] | ||
|
||
if (!clientId) { | ||
throw new ApplicationException({ | ||
message: `Missing or invalid ${REQUEST_HEADER_CLIENT_ID} header`, | ||
suggestedHttpStatusCode: HttpStatus.UNAUTHORIZED | ||
}) | ||
} | ||
|
||
const client = await this.clientService.findById(clientId) | ||
|
||
if (!client) { | ||
throw new ApplicationException({ | ||
message: `Client not found for ${REQUEST_HEADER_CLIENT_ID} header`, | ||
suggestedHttpStatusCode: HttpStatus.NOT_FOUND | ||
}) | ||
} | ||
|
||
return true | ||
} | ||
} |
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
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
Oops, something went wrong.