-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#034] 인가 구현 #35
[#034] 인가 구현 #35
Changes from all commits
fcbdf35
33214aa
64adea5
7e70461
e94f17f
ea566f1
5e8fdc2
ed78fe6
d286154
1482e10
07aa78e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
ForbiddenException, | ||
Injectable, | ||
} from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class OwnershipGuard implements CanActivate { | ||
canActivate(context: ExecutionContext) { | ||
const request = context.switchToHttp().getRequest(); | ||
const requestedUserId = request.params.id; | ||
const userId = request.user.userId; | ||
if (requestedUserId !== userId) { | ||
throw new ForbiddenException('수정할 권한이 없습니다.'); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor(private configService: ConfigService) { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: configService.get('JWT_SECRET'), | ||
}); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. jwt 를 만드는 라이브러리랑 검증하는 라이브러리랑 달라서 그런데 혹시 postman 으로 했을때 통과됐나요? jwt token 은 @nestjs/jwt 에서 generate 되고 검증은 @nestjs/passport 를 사용해서 혹시나해서, 테스트 통과했으면 좋습니다~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 통과 됩니다. passport 에서 jwt strategy를 사용해서 jwt 확인 가능합니다 |
||
async validate(payload: any) { | ||
return { userId: payload.userId }; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Request 의 user 필드에 userId를 담는다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
||
export const UserId = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext) => { | ||
// Guard 이후에 실행되므로 jwtToken의 유효성은 보장 | ||
const request = ctx.switchToHttp().getRequest(); | ||
return request.user.userId; | ||
}, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ownership 에서는 AuthGuard 를 통과한 요청들에 request.user.userId 가 담겨있다. 이 userId 와 파라미터의 Id 를 비교해 해당 리소스를 소유한 사람인지 비교한다