-
Notifications
You must be signed in to change notification settings - Fork 0
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 #15 from Brints/access-token-guard
feat: access token guard
- Loading branch information
Showing
14 changed files
with
174 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const REQUEST_USER_TYPE = 'user'; | ||
export const AUTH_TYPE_KEY = 'authType'; |
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,6 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
import { AuthType } from '../enum/auth-type.enum'; | ||
import { AUTH_TYPE_KEY } from '../constants/auth.constants'; | ||
|
||
export const Auth = (...authTypes: AuthType[]) => | ||
SetMetadata(AUTH_TYPE_KEY, authTypes); |
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 @@ | ||
export enum AuthType { | ||
Bearer, | ||
None, | ||
} |
59 changes: 54 additions & 5 deletions
59
brints-estate-api/src/auth/guards/access-token/access-token.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 |
---|---|---|
@@ -1,11 +1,60 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
HttpStatus, | ||
Inject, | ||
Injectable, | ||
} from '@nestjs/common'; | ||
import { ConfigType } from '@nestjs/config'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { Request } from 'express'; | ||
import jwtConfig from 'src/auth/config/jwt.config'; | ||
import { REQUEST_USER_TYPE } from 'src/auth/constants/auth.constants'; | ||
import { CustomException } from 'src/exceptions/custom.exception'; | ||
|
||
@Injectable() | ||
export class AccessTokenGuard implements CanActivate { | ||
canActivate( | ||
context: ExecutionContext, | ||
): boolean | Promise<boolean> | Observable<boolean> { | ||
constructor( | ||
private readonly jwtService: JwtService, | ||
|
||
@Inject(jwtConfig.KEY) | ||
private readonly jwtConfiguration: ConfigType<typeof jwtConfig>, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
const token = this.extractRequestFromHeader(request); | ||
|
||
if (!token) { | ||
throw new CustomException(HttpStatus.UNAUTHORIZED, 'Unauthorized'); | ||
} | ||
|
||
try { | ||
const payload = await this.jwtService.verifyAsync( | ||
token, | ||
this.jwtConfiguration, | ||
); | ||
|
||
// request.user = payload; | ||
request[REQUEST_USER_TYPE] = payload; | ||
} catch { | ||
throw new CustomException(HttpStatus.UNAUTHORIZED, 'Unauthorized'); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private extractRequestFromHeader(request: Request): string | null { | ||
const authorizationHeader = request.headers.authorization; | ||
if (!authorizationHeader) { | ||
return null; | ||
} | ||
|
||
const [bearer, token] = authorizationHeader.split(' '); | ||
if (bearer !== 'Bearer' || !token) { | ||
return null; | ||
} | ||
|
||
return token; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
brints-estate-api/src/auth/guards/authentication/authentication.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,59 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
HttpStatus, | ||
Injectable, | ||
} from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { AccessTokenGuard } from '../access-token/access-token.guard'; | ||
import { AuthType } from 'src/auth/enum/auth-type.enum'; | ||
import { AUTH_TYPE_KEY } from 'src/auth/constants/auth.constants'; | ||
import { CustomException } from 'src/exceptions/custom.exception'; | ||
|
||
@Injectable() | ||
export class AuthenticationGuard implements CanActivate { | ||
private static readonly defaultAuthType = AuthType.Bearer; | ||
|
||
private get authTypeGuardMap(): Record< | ||
AuthType, | ||
CanActivate | CanActivate[] | ||
> { | ||
return { | ||
[AuthType.Bearer]: this.accessTokenGuard, | ||
[AuthType.None]: { canActivate: () => true }, | ||
}; | ||
} | ||
|
||
constructor( | ||
private readonly reflector: Reflector, | ||
private readonly accessTokenGuard: AccessTokenGuard, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const authTypes = this.reflector.getAllAndOverride<AuthType[]>( | ||
AUTH_TYPE_KEY, | ||
[context.getHandler(), context.getClass()], | ||
) ?? [AuthenticationGuard.defaultAuthType]; | ||
|
||
const guards = authTypes | ||
.map((authType) => this.authTypeGuardMap[authType]) | ||
.flat(); | ||
|
||
const error = new CustomException(HttpStatus.UNAUTHORIZED, 'Unauthorized'); | ||
|
||
for (const instance of guards) { | ||
const canActivate = await Promise.resolve( | ||
instance.canActivate(context), | ||
).catch((err) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions | ||
error: err; | ||
}); | ||
|
||
if (!canActivate) { | ||
throw error; | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,4 @@ | ||
GET http://localhost:3000/users/1 | ||
GET http://localhost:3001/users/1 | ||
|
||
POST http://localhost:3000/users | ||
Content-Type: application/json | ||
|
||
{ | ||
"firstName": "John", | ||
"lastName": "Doe", | ||
"email": "test@example.com", | ||
"age": 27 | ||
} | ||
GET http://localhost:3001/users/all | ||
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJlZjI2N2IxMS02NmYzLTQ3MjAtYjM1MS01ZjlkNDc0OGQ3M2YiLCJmaXJzdF9uYW1lIjoiQW5pZWJpZXQiLCJsYXN0X25hbWUiOiJBZmlhIiwiZW1haWwiOiJhbmllYmlldGFmaWFAZ21haWwuY29tIiwicm9sZSI6InVzZXIiLCJ2ZXJpZmllZCI6ZmFsc2UsImlhdCI6MTcyNTQzOTQzOSwiZXhwIjoxNzI1NDQzMDM5LCJhdWQiOiJsb2NhbGhvc3Q6MzAwMSIsImlzcyI6ImxvY2FsaG9zdDozMDAxIn0.dmuK28GbSaGyQosL2DnXyEGPXRm26Nd3gnWlpaHddWg |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { Controller } from '@nestjs/common'; | ||
// import { UsersService } from './providers/users.service'; | ||
// import { CreateUserDto } from './dto/create-user.dto'; | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { UsersService } from './providers/users.service'; | ||
|
||
@Controller('users') | ||
@ApiTags('Users') | ||
export class UsersController { | ||
// constructor(private readonly usersService: UsersService) {} | ||
// @Post() | ||
// create(@Body() createUserDto: CreateUserDto) { | ||
// return this.usersService.create(createUserDto); | ||
// } | ||
constructor(private readonly usersService: UsersService) {} | ||
|
||
@Get('all') | ||
async getUser() { | ||
return this.usersService.getAllUsers(); | ||
} | ||
} |
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,16 +1,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { forwardRef, Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { UsersController } from './users.controller'; | ||
import { UsersService } from './providers/users.service'; | ||
import { AuthModule } from 'src/auth/auth.module'; | ||
|
||
import { User } from './entities/user.entity'; | ||
import { UserAuth } from './entities/userAuth.entity'; | ||
|
||
@Module({ | ||
controllers: [UsersController], | ||
providers: [UsersService], | ||
imports: [TypeOrmModule.forFeature([User, UserAuth])], | ||
imports: [ | ||
TypeOrmModule.forFeature([User, UserAuth]), | ||
forwardRef(() => AuthModule), | ||
], | ||
exports: [TypeOrmModule, UsersService], | ||
}) | ||
export class UsersModule {} |