-
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.
feat: 요청자 정보를 cls namespace에 담도록 구현 (#49)
- Loading branch information
Showing
10 changed files
with
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Request } from 'express'; | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Inject, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
|
||
import { ITokenVerifier, TokenVerifier } from '@sight/core/auth/ITokenVerifier'; | ||
|
||
import { Message } from '@sight/constant/message'; | ||
import { ClsService } from 'nestjs-cls'; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor( | ||
@Inject(TokenVerifier) | ||
private readonly tokenVerifier: ITokenVerifier, | ||
private readonly clsService: ClsService, | ||
) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const req: Request = context.switchToHttp().getRequest(); | ||
const authorizationHeader = req.headers['authorization']; | ||
|
||
if (!authorizationHeader) { | ||
throw new UnauthorizedException(Message.TOKEN_REQUIRED); | ||
} | ||
|
||
const token = authorizationHeader.split(' ')[1]; | ||
if (!token) { | ||
throw new UnauthorizedException(Message.TOKEN_REQUIRED); | ||
} | ||
|
||
const requester = this.tokenVerifier.verify(token); | ||
req['requester'] = requester; | ||
|
||
this.clsService.set('requester', requester); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { APP_GUARD } from '@nestjs/core'; | ||
|
||
import { AuthGuard } from '@sight/core/auth/AuthGuard'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ provide: APP_GUARD, useClass: AuthGuard }, | ||
// TODO: TokenVerifier 구현 후 추가 | ||
], | ||
}) | ||
export class AuthModule {} |
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 { UserRole } from '@sight/core/auth/UserRole'; | ||
|
||
export interface IRequester { | ||
userId: string; | ||
role: UserRole; | ||
} |
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,7 @@ | ||
import { IRequester } from '@sight/core/auth/IRequester'; | ||
|
||
export const TokenVerifier = Symbol('TokenVerifier'); | ||
|
||
export interface ITokenVerifier { | ||
verify: (token: string) => IRequester; | ||
} |
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 { createParamDecorator } from '@nestjs/common'; | ||
|
||
export const Requester = createParamDecorator((data, req) => { | ||
return req['requester']; | ||
}); |
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 @@ | ||
export const UserRole = { | ||
USER: 'USER', | ||
ADMIN: 'ADMIN', | ||
} as const; | ||
export type UserRole = (typeof UserRole)[keyof typeof UserRole]; |
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