-
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 #8 from GSM-MSG/feature/7-reissue-token
๐ ํ ํฐ ์ฌ๋ฐ๊ธ
- Loading branch information
Showing
9 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/com/gcms/v3/domain/auth/domain/entity/RefreshToken.java
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,20 @@ | ||
package com.gcms.v3.domain.auth.domain.entity; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
import org.springframework.data.redis.core.index.Indexed; | ||
|
||
@Getter | ||
@Builder | ||
@RedisHash(value = "refreshToken", timeToLive = 60L * 60 * 24 * 7) | ||
public class RefreshToken { | ||
|
||
@Indexed | ||
private String email; | ||
|
||
@Id | ||
@Indexed | ||
private String token; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/gcms/v3/domain/auth/domain/repository/RefreshTokenRepository.java
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 @@ | ||
package com.gcms.v3.domain.auth.domain.repository; | ||
|
||
import com.gcms.v3.domain.auth.domain.entity.RefreshToken; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface RefreshTokenRepository extends CrudRepository<RefreshToken, String> { | ||
Optional<RefreshToken> findByToken(String token); | ||
|
||
RefreshToken findByEmail(String email); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/gcms/v3/domain/auth/exception/ExpiredTokenException.java
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,10 @@ | ||
package com.gcms.v3.domain.auth.exception; | ||
|
||
import com.gcms.v3.global.error.BasicException; | ||
import com.gcms.v3.global.error.ErrorCode; | ||
|
||
public class ExpiredTokenException extends BasicException { | ||
public ExpiredTokenException() { | ||
super(ErrorCode.EXPIRED_TOKEN); | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/com/gcms/v3/domain/auth/service/ReissueTokenService.java
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 @@ | ||
package com.gcms.v3.domain.auth.service; | ||
|
||
import com.gcms.v3.domain.auth.presentation.data.response.TokenInfoResponseDto; | ||
|
||
public interface ReissueTokenService { | ||
TokenInfoResponseDto execute(String refreshToken); | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/gcms/v3/domain/auth/service/impl/ReissueTokenServiceImpl.java
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,49 @@ | ||
package com.gcms.v3.domain.auth.service.impl; | ||
|
||
import com.gcms.v3.domain.auth.domain.entity.RefreshToken; | ||
import com.gcms.v3.domain.auth.domain.repository.RefreshTokenRepository; | ||
import com.gcms.v3.domain.auth.exception.UserNotFoundException; | ||
import com.gcms.v3.domain.auth.presentation.data.response.TokenInfoResponseDto; | ||
import com.gcms.v3.domain.auth.service.ReissueTokenService; | ||
import com.gcms.v3.domain.user.domain.entity.User; | ||
import com.gcms.v3.domain.user.domain.repository.UserRepository; | ||
import com.gcms.v3.global.security.exception.ExpiredTokenException; | ||
import com.gcms.v3.global.security.jwt.JwtTokenProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class ReissueTokenServiceImpl implements ReissueTokenService { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
private final UserRepository userRepository; | ||
private final RefreshTokenRepository refreshTokenRepository; | ||
|
||
public TokenInfoResponseDto execute(String refreshToken) { | ||
String refresh = jwtTokenProvider.parseToken(refreshToken); | ||
|
||
String email = jwtTokenProvider.exactEmailFromRefreshToken(refresh); | ||
|
||
User user = userRepository.findByEmail(email) | ||
.orElseThrow(UserNotFoundException::new); | ||
|
||
RefreshToken existingRefreshToken = refreshTokenRepository.findByToken(refresh) | ||
.orElseThrow(ExpiredTokenException::new); | ||
|
||
TokenInfoResponseDto responseDto = jwtTokenProvider.generateToken(user.getEmail()); | ||
|
||
saveRefreshToken(existingRefreshToken.getEmail(), responseDto.refreshToken()); | ||
|
||
return responseDto; | ||
} | ||
|
||
private void saveRefreshToken(String email, String refreshToken) { | ||
RefreshToken token = RefreshToken.builder() | ||
.email(email) | ||
.token(refreshToken) | ||
.build(); | ||
|
||
refreshTokenRepository.save(token); | ||
} | ||
} |
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