-
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 #31 from viniciuscosmome/feature/authorization
[LOR 176] feature: authorization
- Loading branch information
Showing
16 changed files
with
242 additions
and
66 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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import configModule from 'src/globals/constants'; | ||
|
||
import { AccountModule } from './modules'; | ||
|
||
@Module({ | ||
imports: [AccountModule], | ||
imports: [configModule, AccountModule], | ||
}) | ||
export class AppModule {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
import { Roles } from '@prisma/client'; | ||
import { iAuthTokenSubject } from 'src/modules/account/account.entity'; | ||
|
||
export type iAuthMetadata = { | ||
type: iAuthTokenSubject; | ||
role: Roles; | ||
}; | ||
|
||
export const RequestToken = (authMetadata: iAuthMetadata) => { | ||
return SetMetadata('authMetadata', authMetadata); | ||
}; |
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,71 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
InternalServerErrorException, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { Request } from 'express'; | ||
import { formatException } from 'src/globals/utils'; | ||
import { iAuthMetadata } from './authorization.decorator'; | ||
import { handleJWTErrors } from 'src/globals/errors'; | ||
|
||
@Injectable() | ||
export class AuthorizationGuard implements CanActivate { | ||
private secret: string; | ||
|
||
constructor( | ||
private reflector: Reflector, | ||
private configService: ConfigService, | ||
private jwtService: JwtService, | ||
) { | ||
this.secret = this.configService.get<string>('SECRET_JWT'); | ||
} | ||
|
||
private extractTokenFromHeader(request: Request): string | undefined { | ||
const [type, token] = request.headers.authorization?.split(' ') || []; | ||
return type === 'Bearer' ? token : undefined; | ||
} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const authMetadata = this.reflector.get<iAuthMetadata>( | ||
'authMetadata', | ||
context.getHandler(), | ||
); | ||
|
||
if (!authMetadata || !authMetadata.type || !authMetadata.role) { | ||
throw new InternalServerErrorException( | ||
formatException('O método CanActivate precisa ser configurado.'), | ||
); | ||
} | ||
|
||
const request = context.switchToHttp().getRequest(); | ||
const token = this.extractTokenFromHeader(request); | ||
|
||
if (!token) { | ||
throw new UnauthorizedException( | ||
formatException('Uma chave de acesso precisa ser informada.'), | ||
); | ||
} | ||
|
||
const validity = await this.jwtService | ||
.verifyAsync(token, { | ||
secret: this.secret, | ||
subject: authMetadata.type, | ||
}) | ||
.then((response) => response) | ||
.catch((error) => handleJWTErrors(error)); | ||
|
||
if (validity) { | ||
request['session.payload'] = validity; | ||
return true; | ||
} | ||
|
||
throw new InternalServerErrorException( | ||
formatException('O método CanActivate precisa ser verificado.'), | ||
); | ||
} | ||
} |
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,2 @@ | ||
export * from './authorization.decorator'; | ||
export * from './authorization.guard'; |
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,10 @@ | ||
import { ConfigService } from '@nestjs/config'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
|
||
export default JwtModule.registerAsync({ | ||
inject: [ConfigService], | ||
useFactory: async (configService: ConfigService) => ({ | ||
secret: configService.get<string>('SECRET_JWT'), | ||
global: 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
Oops, something went wrong.