-
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.
feat(api): add guard and decorator for authentication external integr…
…ations
- Loading branch information
1 parent
d86674a
commit 211c676
Showing
11 changed files
with
60 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
interface ExternalIntegrationsHeaders { | ||
from?: string; | ||
authorization?: string; | ||
} |
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import { applyDecorators, SetMetadata, UseGuards } from '@nestjs/common'; | ||
|
||
import { Role } from '../../../enums/role.enum'; | ||
import { constants } from '../../../utils'; | ||
import { OnlyAuthenticatedGuard } from '../guards/only-authenticated.guard'; | ||
import { RolesGuard } from '../guards/roles.guard'; | ||
import { ROLES_KEY } from './roles.decorator'; | ||
|
||
export const Auth = (...roles: Role[]) => | ||
applyDecorators(SetMetadata(ROLES_KEY, roles), UseGuards(OnlyAuthenticatedGuard, RolesGuard)); | ||
applyDecorators( | ||
SetMetadata(constants.roles_key, roles), | ||
UseGuards(OnlyAuthenticatedGuard, RolesGuard), | ||
); |
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
5 changes: 5 additions & 0 deletions
5
.../api/src/modules/external-integrations/decorators/external-integrations-auth.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,5 @@ | ||
import { UseGuards } from '@nestjs/common'; | ||
|
||
import { ExternalIntegrationsAuthGuard } from '../guards/external-integrations-auth.guard'; | ||
|
||
export const ExternalIntegrationsAuth = () => UseGuards(ExternalIntegrationsAuthGuard); |
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
31 changes: 31 additions & 0 deletions
31
packages/api/src/modules/external-integrations/guards/external-integrations-auth.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,31 @@ | ||
import { | ||
BadRequestException, | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
|
||
import { UsersService } from '../../users/users.service'; | ||
|
||
@Injectable() | ||
export class ExternalIntegrationsAuthGuard implements CanActivate { | ||
constructor(private readonly usersService: UsersService) {} | ||
|
||
public async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest<{ headers: ExternalIntegrationsHeaders }>(); | ||
const { authorization, from } = request.headers; | ||
|
||
if (!authorization || !from) { | ||
throw new BadRequestException(); | ||
} | ||
|
||
const foundUser = await this.usersService.findOneByEmail(from); | ||
|
||
if (!foundUser || authorization !== foundUser.externalIntegrationsToken) { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
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