Skip to content

Commit

Permalink
fix: subject 값 providerId로 수정
Browse files Browse the repository at this point in the history
providerId 인덱스도 추가

related to: #11
  • Loading branch information
heejjinkim committed Sep 7, 2024
1 parent ef7c73a commit 8811e4e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
22 changes: 11 additions & 11 deletions src/main/java/com/_119/wepro/auth/jwt/JwtTokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import static com._119.wepro.global.exception.errorcode.CommonErrorCode.INVALID_TOKEN;

import com._119.wepro.auth.dto.response.TokenInfo;
import com._119.wepro.global.util.RedisUtil;
import com._119.wepro.global.enums.Role;
import com._119.wepro.global.exception.RestApiException;
import com._119.wepro.global.util.RedisUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtException;
Expand Down Expand Up @@ -43,12 +43,12 @@ public JwtTokenProvider(@Value("${jwt.secret}") String key, RedisUtil redisUtil)
this.secretKey = Keys.hmacShaKeyFor(keyBytes);
}

public TokenInfo generateToken(Long memberId, Role memberRole) {
String accessToken = generateAccessToken(memberId, memberRole);
public TokenInfo generateToken(String providerId, Role memberRole) {
String accessToken = generateAccessToken(providerId, memberRole);
String refreshToken = generateRefreshToken();

deleteInvalidRefreshToken(memberId.toString());
redisUtil.setData(memberId.toString(), refreshToken);
deleteInvalidRefreshToken(providerId);
redisUtil.setData(providerId, refreshToken);

return new TokenInfo("Bearer", accessToken, refreshToken);
}
Expand Down Expand Up @@ -112,12 +112,12 @@ private Claims parseClaims(String accessToken) {
}
}

private String generateAccessToken(Long memberId, Role memberRole) {
private String generateAccessToken(String providerId, Role memberRole) {
Date now = new Date();
Date expiredDate = new Date(now.getTime() + ACCESS_TOKEN_DURATION);

return Jwts.builder()
.setSubject(memberId.toString())
.setSubject(providerId)
.claim(AUTHORITIES_KEY, memberRole.name())
.setIssuedAt(now)
.setExpiration(expiredDate)
Expand All @@ -140,12 +140,12 @@ private List<SimpleGrantedAuthority> getAuthorities(Claims claims) {
claims.get(AUTHORITIES_KEY).toString()));
}

public String getRefreshToken(String memberId){
return redisUtil.getData(memberId);
public String getRefreshToken(String provierId) {
return redisUtil.getData(provierId);
}

public void deleteInvalidRefreshToken(String memberId) {
redisUtil.deleteData(memberId);
public void deleteInvalidRefreshToken(String provierId) {
redisUtil.deleteData(provierId);
}

public Claims parseExpiredToken(String token) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/_119/wepro/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public SignInResponse signIn(SignInRequest request) {
OidcUser oidcDecodePayload = socialLogin(request);

Member member = getOrSaveUser(request, oidcDecodePayload);
TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getId(), member.getRole());
TokenInfo tokenInfo = jwtTokenProvider.generateToken(member.getProviderId(), member.getRole());
boolean isNewMember = Role.GUEST == member.getRole();

return new SignInResponse(isNewMember, tokenInfo);
}

@Transactional
public void logOut(String memberId) {
jwtTokenProvider.deleteInvalidRefreshToken(memberId);
public void logOut(String providerId) {
jwtTokenProvider.deleteInvalidRefreshToken(providerId);
}


Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/_119/wepro/auth/service/RefreshService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ public TokenInfo refresh(RefreshRequest request) {
if (!isTokenExpired(accessToken)) {
throw new RestApiException(REFRESH_DENIED);
}
String memberId = jwtTokenProvider.parseExpiredToken(accessToken)
String providerId = jwtTokenProvider.parseExpiredToken(accessToken)
.getSubject();
Member member = memberRepository.findById(Long.parseLong(memberId))
validateRefreshToken(refreshToken, providerId);

Member member = memberRepository.findByProviderId(providerId)
.orElseThrow(() -> new RestApiException(UserErrorCode.USER_NOT_FOUND));

validateRefreshToken(refreshToken, memberId);
return jwtTokenProvider.generateToken(Long.parseLong(memberId), member.getRole());
return jwtTokenProvider.generateToken(providerId, member.getRole());
}

private boolean isTokenExpired(String accessToken) {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/_119/wepro/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com._119.wepro.member.domain;

import com._119.wepro.auth.dto.request.AuthRequest.SignInRequest;
import com._119.wepro.global.BaseEntity;
import com._119.wepro.global.enums.Provider;
import com._119.wepro.global.enums.Role;
import com._119.wepro.global.enums.Status;
import com._119.wepro.auth.dto.request.AuthRequest.SignInRequest;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.PostPersist;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -26,6 +28,11 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
@Table(
indexes = {
@Index(name = "idx_provider_id", columnList = "providerId")
}
)
public class Member extends BaseEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@

@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByProviderAndProviderId(Provider provider, String providerId);

Optional<Member> findByProviderId(String providerId);
}

0 comments on commit 8811e4e

Please sign in to comment.