-
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 #16 from TeamMiso/feat/token-reissue
토큰 재발급 api 구현
- Loading branch information
Showing
5 changed files
with
87 additions
and
1 deletion.
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
13 changes: 13 additions & 0 deletions
13
...tlin/andreas311/miso/domain/auth/presentation/data/response/NewRefreshTokenResponseDto.kt
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,13 @@ | ||
package andreas311.miso.domain.auth.presentation.data.response | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat | ||
import java.time.ZonedDateTime | ||
|
||
data class NewRefreshTokenResponseDto( | ||
val accessToken: String, | ||
val refreshToken: String, | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
val accessExp: ZonedDateTime, | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
val refreshExp: ZonedDateTime | ||
) |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/andreas311/miso/domain/auth/service/TokenReissueService.kt
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,8 @@ | ||
package andreas311.miso.domain.auth.service | ||
|
||
import andreas311.miso.domain.auth.presentation.data.response.NewRefreshTokenResponseDto | ||
|
||
interface TokenReissueService { | ||
|
||
fun execute(refreshToken: String): NewRefreshTokenResponseDto | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/andreas311/miso/domain/auth/service/impl/TokenReissueServiceImpl.kt
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,54 @@ | ||
package andreas311.miso.domain.auth.service.impl | ||
|
||
import andreas311.miso.domain.auth.presentation.data.response.NewRefreshTokenResponseDto | ||
import andreas311.miso.domain.auth.repository.RefreshTokenRepository | ||
import andreas311.miso.domain.auth.service.TokenReissueService | ||
import andreas311.miso.domain.auth.util.AuthConverter | ||
import andreas311.miso.domain.user.enums.Role | ||
import andreas311.miso.domain.user.exception.UserNotFoundException | ||
import andreas311.miso.domain.user.repository.UserRepository | ||
import andreas311.miso.global.annotation.RollbackService | ||
import andreas311.miso.global.security.exception.TokenExpiredException | ||
import andreas311.miso.global.security.exception.TokenInvalidException | ||
import andreas311.miso.global.security.jwt.TokenProvider | ||
import java.time.ZonedDateTime | ||
|
||
@RollbackService | ||
class TokenReissueServiceImpl( | ||
private val authConverter: AuthConverter, | ||
private val tokenProvider: TokenProvider, | ||
private val userRepository: UserRepository, | ||
private val refreshTokenRepository: RefreshTokenRepository, | ||
) : TokenReissueService { | ||
|
||
override fun execute(refreshToken: String): NewRefreshTokenResponseDto { | ||
val refresh = tokenProvider.parseToken(refreshToken) | ||
?: throw TokenInvalidException() | ||
|
||
val email: String = tokenProvider.exactEmailFromRefreshToken(refresh) | ||
|
||
val existingRefreshToken = refreshTokenRepository.findByToken(refresh) | ||
?: throw TokenExpiredException() | ||
|
||
val newAccessToken = tokenProvider.generateAccessToken(email) | ||
val newRefreshToken = tokenProvider.generateRefreshToken(email) | ||
val accessExp: ZonedDateTime = tokenProvider.accessExpiredTime | ||
val refreshExp: ZonedDateTime = tokenProvider.refreshExpiredTime | ||
|
||
val newRefreshTokenEntity = authConverter.toEntity( | ||
userId = existingRefreshToken.userId, | ||
refreshToken = newRefreshToken | ||
) | ||
|
||
refreshTokenRepository.save(newRefreshTokenEntity) | ||
val user = userRepository.findByEmail(email) | ||
?: throw UserNotFoundException() | ||
|
||
return NewRefreshTokenResponseDto( | ||
accessToken = newAccessToken, | ||
refreshToken = newRefreshToken, | ||
accessExp = accessExp, | ||
refreshExp = refreshExp | ||
) | ||
} | ||
} |
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