-
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.
Merge pull request #78 from TinU-Official/TINU-83/social-login-feat
Tinu 83/social login feat
- Loading branch information
Showing
36 changed files
with
1,062 additions
and
2 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
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/tinuproject/tinu/GlobalExceptionHandler.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,18 @@ | ||
package com.tinuproject.tinu | ||
|
||
import com.tinuproject.tinu.DTO.ResponseDTO | ||
import com.tinuproject.tinu.domain.exception.base.BaseException | ||
import com.tinuproject.tinu.web.ResponseEntityGenerator | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.ExceptionHandler | ||
import org.springframework.web.bind.annotation.RestControllerAdvice | ||
|
||
|
||
@RestControllerAdvice | ||
class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(BaseException::class) | ||
fun baseException(e : BaseException) : ResponseEntity<ResponseDTO>{ | ||
return ResponseEntityGenerator.onFailure(e) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/tinuproject/tinu/domain/entity/RefreshToken.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,19 @@ | ||
package com.tinuproject.tinu.domain.entity | ||
|
||
import com.tinuproject.tinu.domain.entity.base.BaseEntity | ||
import jakarta.persistence.* | ||
import java.util.* | ||
|
||
@Entity | ||
class RefreshToken( | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
var id : Long?= null, | ||
|
||
@Column(unique = true) | ||
var userId : UUID, | ||
|
||
@Column(unique = true) | ||
var token : String | ||
){ | ||
} |
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/kotlin/com/tinuproject/tinu/domain/exception/base/BaseCode.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,7 @@ | ||
package com.tinuproject.tinu.domain.exception.base | ||
|
||
import com.tinuproject.tinu.DTO.ResponseDTO | ||
|
||
interface BaseCode { | ||
fun getResponse(): ResponseDTO? | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/tinuproject/tinu/domain/exception/base/BaseErrorCode.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,7 @@ | ||
package com.tinuproject.tinu.domain.exception.base | ||
|
||
import com.tinuproject.tinu.DTO.ResponseDTO | ||
|
||
interface BaseErrorCode { | ||
fun getResponse(): ResponseDTO? | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/tinuproject/tinu/domain/exception/base/BaseException.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,16 @@ | ||
package com.tinuproject.tinu.domain.exception.base | ||
|
||
import com.tinuproject.tinu.DTO.ResponseDTO | ||
import java.lang.RuntimeException | ||
|
||
open class BaseException( | ||
protected val errorCode : ErrorCode | ||
): RuntimeException(), BaseErrorCode { | ||
|
||
override fun getResponse(): ResponseDTO { | ||
var map : MutableMap<String, Any> = mutableMapOf() | ||
map["error-message"] = errorCode.message | ||
if(errorCode.stateCode!=null) map["stateCode"] = errorCode.stateCode | ||
return ResponseDTO(isSuccess = false, stateCode = errorCode.httpStatusCode, result = map) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/tinuproject/tinu/domain/exception/base/ErrorCode.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 com.tinuproject.tinu.domain.exception.base | ||
|
||
enum class ErrorCode( | ||
val httpStatusCode : Int, | ||
val stateCode : String?, | ||
val message :String | ||
) { | ||
TOKEN_MISSING(httpStatusCode = 401, stateCode = "TOKEN_MISSING", message = "토큰이 존재하지 않습니다."), | ||
TOKEN_INVALIDED(httpStatusCode = 401, stateCode = "TOKEN_INVALIDED", message = "토큰이 유효하지 않습니다."), | ||
TOKEN_EXPIRED(httpStatusCode = 401, stateCode = "TOKEN_EXPIRED", message = "토큰이 만료되었습니다."); | ||
|
||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/tinuproject/tinu/domain/exception/token/ExpiredTokenException.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,7 @@ | ||
package com.tinuproject.tinu.domain.exception.token | ||
|
||
import com.tinuproject.tinu.domain.exception.base.BaseException | ||
import com.tinuproject.tinu.domain.exception.base.ErrorCode | ||
|
||
class ExpiredTokenException(): BaseException(errorCode = ErrorCode.TOKEN_EXPIRED) { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/tinuproject/tinu/domain/exception/token/InvalidedTokenException.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 com.tinuproject.tinu.domain.exception.token | ||
|
||
import com.tinuproject.tinu.domain.exception.base.BaseException | ||
import com.tinuproject.tinu.domain.exception.base.ErrorCode | ||
|
||
class InvalidedTokenException( | ||
|
||
):BaseException(errorCode= ErrorCode.TOKEN_INVALIDED) {} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/tinuproject/tinu/domain/exception/token/NotFoundTokenException.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,9 @@ | ||
package com.tinuproject.tinu.domain.exception.token | ||
|
||
import com.tinuproject.tinu.domain.exception.base.BaseException | ||
import com.tinuproject.tinu.domain.exception.base.ErrorCode | ||
|
||
class NotFoundTokenException( | ||
) : BaseException(errorCode = ErrorCode.TOKEN_MISSING) { | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...main/kotlin/com/tinuproject/tinu/domain/socialmember/repository/SocialMemberRepository.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 com.tinuproject.tinu.domain.socialmember.repository | ||
|
||
import com.tinuproject.tinu.domain.entity.SocialMember | ||
import org.springframework.data.repository.CrudRepository | ||
import org.springframework.stereotype.Repository | ||
import java.util.* | ||
|
||
@Repository | ||
interface SocialMemberRepository:CrudRepository<SocialMember, Long> { | ||
fun findByUserId(userId : UUID) : SocialMember? | ||
|
||
fun findByProviderId(providerId : String) : SocialMember? | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/com/tinuproject/tinu/domain/socialmember/service/SocialMemberService.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,4 @@ | ||
package com.tinuproject.tinu.domain.socialmember.service | ||
|
||
interface SocialMemberService { | ||
} |
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 com.tinuproject.tinu.domain.token | ||
|
||
class Tokens( | ||
val accessToken : String, | ||
val refreshToken : String | ||
) { | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...otlin/com/tinuproject/tinu/domain/token/refreshtoken/controller/RefreshTokenController.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,52 @@ | ||
package com.tinuproject.tinu.domain.token.refreshtoken.controller | ||
|
||
import com.tinuproject.tinu.DTO.ResponseDTO | ||
import com.tinuproject.tinu.domain.exception.token.NotFoundTokenException | ||
import com.tinuproject.tinu.domain.token.Tokens | ||
import com.tinuproject.tinu.domain.token.refreshtoken.service.RefreshTokenService | ||
import com.tinuproject.tinu.web.CookieGenerator | ||
import com.tinuproject.tinu.web.ResponseEntityGenerator | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Controller | ||
import org.springframework.web.bind.annotation.CookieValue | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
|
||
@Controller | ||
@RequestMapping("api/token") | ||
class RefreshTokenController( | ||
private val refreshTokenService : RefreshTokenService, | ||
|
||
@Value("\${cookie.token.access-token}") | ||
private val accessTokenKey : String, | ||
|
||
@Value("\${cookie.token.refresh-token}") | ||
private val refreshTokenkey : String | ||
) { | ||
var log : Logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
|
||
@GetMapping("/generate") | ||
fun generateAccessToken(httpServletResponse: HttpServletResponse, @CookieValue(name = "RefreshToken") refreshToken : String?): ResponseEntity<ResponseDTO> { | ||
log.info("AccessToken 갱신 시도") | ||
if(refreshToken==null){ | ||
throw NotFoundTokenException() | ||
} | ||
|
||
val tokens : Tokens = refreshTokenService.reissueAccessTokenByRefreshToken(refreshToken) | ||
|
||
|
||
httpServletResponse.addHeader(HttpHeaders.SET_COOKIE,CookieGenerator.createCookies(accessTokenKey,tokens.accessToken)) | ||
httpServletResponse.addHeader(HttpHeaders.SET_COOKIE,CookieGenerator.createCookies(refreshTokenkey, tokens.refreshToken)) | ||
var body : MutableMap<String, Any> = mutableMapOf() | ||
|
||
|
||
val responseEntity = ResponseEntityGenerator.onSuccess(body) | ||
return responseEntity | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...otlin/com/tinuproject/tinu/domain/token/refreshtoken/repository/RefreshTokenRepository.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,23 @@ | ||
package com.tinuproject.tinu.domain.token.refreshtoken.repository | ||
|
||
import com.tinuproject.tinu.domain.entity.RefreshToken | ||
import jakarta.persistence.ManyToOne | ||
import jakarta.transaction.Transactional | ||
import org.springframework.data.jpa.repository.Modifying | ||
import org.springframework.data.repository.CrudRepository | ||
import org.springframework.stereotype.Repository | ||
import java.util.* | ||
|
||
interface RefreshTokenRepository:CrudRepository<RefreshToken, Long> { | ||
|
||
fun findByToken(token : String) : RefreshToken? | ||
|
||
@Transactional | ||
@Modifying | ||
fun deleteByUserId(userId : UUID) | ||
|
||
|
||
@Transactional | ||
@Modifying | ||
fun deleteByToken(token : String) | ||
} |
11 changes: 11 additions & 0 deletions
11
...main/kotlin/com/tinuproject/tinu/domain/token/refreshtoken/service/RefreshTokenService.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,11 @@ | ||
package com.tinuproject.tinu.domain.token.refreshtoken.service | ||
|
||
import com.tinuproject.tinu.domain.token.Tokens | ||
import org.springframework.stereotype.Service | ||
|
||
|
||
interface RefreshTokenService { | ||
fun reissueAccessTokenByRefreshToken(refreshToken : String) : Tokens | ||
|
||
fun deleteRefreshToken(refreshToken: String) | ||
} |
64 changes: 64 additions & 0 deletions
64
.../kotlin/com/tinuproject/tinu/domain/token/refreshtoken/service/RefreshTokenServiceImpl.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,64 @@ | ||
package com.tinuproject.tinu.domain.token.refreshtoken.service | ||
|
||
import com.tinuproject.tinu.domain.entity.RefreshToken | ||
import com.tinuproject.tinu.domain.exception.token.ExpiredTokenException | ||
import com.tinuproject.tinu.domain.exception.token.InvalidedTokenException | ||
import com.tinuproject.tinu.domain.exception.token.NotFoundTokenException | ||
import com.tinuproject.tinu.domain.token.Tokens | ||
import com.tinuproject.tinu.domain.token.refreshtoken.repository.RefreshTokenRepository | ||
import com.tinuproject.tinu.security.jwt.JwtUtil | ||
import io.jsonwebtoken.ExpiredJwtException | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
import java.security.SignatureException | ||
import java.util.* | ||
|
||
@Service | ||
class RefreshTokenServiceImpl( | ||
@Value("\${jwt.refresh-token.expiration-time}") | ||
private val REFRESH_TOKEN_EXPIRATION_TIME: Long, // 리프레쉬 토큰 유효기간 | ||
|
||
@Value("\${jwt.access-token.expiration-time}") | ||
private val ACCESS_TOKEN_EXPIRATION_TIME: Long, // 액세스 토큰 유효기간 | ||
|
||
private val refreshTokenRepository: RefreshTokenRepository, | ||
private val jwtUtil : JwtUtil, | ||
):RefreshTokenService { | ||
var log : Logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
override fun reissueAccessTokenByRefreshToken(refreshToken: String): Tokens { | ||
jwtUtil.validateToken(refreshToken) | ||
|
||
if(refreshTokenRepository.findByToken(refreshToken)==null){ | ||
throw NotFoundTokenException() | ||
} | ||
|
||
val userId :UUID = UUID.fromString(jwtUtil.getUserIdFromToken(refreshToken)) | ||
|
||
//리프레쉬 토큰 삭제 | ||
refreshTokenRepository.deleteByUserId(userId) | ||
|
||
//리프레쉬 토큰 재발행. | ||
val token = jwtUtil.generateRefreshToken(userId,REFRESH_TOKEN_EXPIRATION_TIME) | ||
|
||
val newRefreshToken = RefreshToken(userId = userId, token = token) | ||
|
||
//리프레쉬 토큰 저장 | ||
refreshTokenRepository.save(newRefreshToken) | ||
|
||
//AccesToken 재발행. | ||
val accessToken : String = jwtUtil.generateAccessToken(userId, ACCESS_TOKEN_EXPIRATION_TIME) | ||
|
||
return Tokens(accessToken=accessToken, refreshToken = refreshToken) | ||
} | ||
|
||
override fun deleteRefreshToken(refreshToken: String) { | ||
jwtUtil.validateToken(refreshToken) | ||
|
||
val userId : UUID = UUID.fromString(jwtUtil.getUserIdFromToken(refreshToken)) | ||
|
||
refreshTokenRepository.deleteByUserId(userId) | ||
} | ||
} |
Oops, something went wrong.