-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): AuthGuard에 전략패턴 적용하여 AT, RT authorization 구현
- Loading branch information
Showing
13 changed files
with
185 additions
and
183 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 was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
src/apis/auth/security/strategies/access-token.strategy.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,32 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { Request } from 'express'; | ||
import { JwtPayloadInfo } from '../../../../common/types'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class AccessTokenStrategy extends PassportStrategy(Strategy, 'jwt-access') { | ||
constructor(private readonly configService: ConfigService) { | ||
// token 유효 확인 | ||
super({ | ||
secretOrKey: configService.get<string>('ACCESS_SECRET_KEY'), | ||
jwtFromRequest: ExtractJwt.fromExtractors([ | ||
(request: Request) => { | ||
const headerToken = this.extractTokenFromHeader(request); | ||
return headerToken; | ||
}, | ||
]), | ||
passReqToCallback: true, | ||
}); | ||
} | ||
|
||
validate(req: Request, payload: JwtPayloadInfo): JwtPayloadInfo { | ||
return payload; // req.user에 저장됨. | ||
} | ||
|
||
private extractTokenFromHeader(request: Request): string | undefined { | ||
const [type, token] = request.headers.authorization?.split(' ') ?? []; | ||
return type === 'Bearer' ? token : undefined; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/apis/auth/security/strategies/refresh-token.strategy.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,48 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { Request } from 'express'; | ||
import { JwtPayloadInfo } from '../../../../common/types'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { NotFoundException } from '@nestjs/common'; | ||
import { UsersService } from '../../../users/users.service'; | ||
import { AuthService } from '../../auth.service'; | ||
|
||
@Injectable() | ||
export class RefreshTokenStrategy extends PassportStrategy(Strategy, 'jwt-refresh') { | ||
constructor( | ||
private readonly configService: ConfigService, | ||
private readonly usersService: UsersService, | ||
private readonly authService: AuthService, | ||
) { | ||
// token 유효 확인 | ||
super({ | ||
secretOrKey: configService.get<string>('REFRESH_SECRET_KEY'), | ||
jwtFromRequest: ExtractJwt.fromExtractors([ | ||
(request: Request) => { | ||
const cookieToken = request.cookies['refreshToken']; | ||
return cookieToken; | ||
}, | ||
]), | ||
passReqToCallback: true, | ||
}); | ||
} | ||
|
||
async validate(req: Request, payload: JwtPayloadInfo): Promise<JwtPayloadInfo> { | ||
const { userId } = payload; | ||
const user = await this.usersService.findOneById(userId); | ||
if (!user) { | ||
throw new NotFoundException('토큰값에 해당하는 유저가 존재하지 않습니다.'); | ||
} | ||
|
||
// TODO: 1. RT의 jti 검증 | ||
// -> RT 토큰의 jti를 Cache에 저장해두고 검증 | ||
// -> RT 토큰의 jti가 Cache에 존재하는 jti와 일치하지 않으면 검증 실패 | ||
|
||
// TODO: 2. AT 리프래시 할 때 RT도 함께 리프래시하고, 새로운 RT의 jti를 Cache에 저장 | ||
// -> 최초 로그인 시에도 RT의 jti를 Cache에 저장해야 함. | ||
// -> (TODO2 로직은 AuthController의 silentRefresh API에서 구현) | ||
|
||
return payload; // req.user에 저장됨. | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
||
export const JwtUserPayload = createParamDecorator((data, ctx: ExecutionContext): ParameterDecorator => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
return request.user; | ||
}); |
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
Oops, something went wrong.