Skip to content

Commit

Permalink
Merge branch 'maeumgagym-token-refactor' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Daybreak312 committed Jun 19, 2024
2 parents 396248a + cdaa072 commit 86744a0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,8 @@ interface MaeumgagymTokenDecoder {
/**
* ๋ฏธ๋ฆฌ ์„ค์ •๋œ ์•”ํ˜ธํ™” ํ‚ค๋ฅผ ์ด์šฉํ•ด ํ† ํฐ ๋ณตํ˜ธํ™”
*
* @param token ์•”ํ˜ธํ™”๋œ ์ƒํƒœ์˜ ์ธ์ฆ์šฉ ํ† ํฐ. ํ† ํฐ ํ˜•ํƒœ์— ๋”ฐ๋ผ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ์ ‘๋‘์‚ฌ ํ•„์š”.
* @param token ์•”ํ˜ธํ™”๋œ ์ƒํƒœ์˜ ์ธ์ฆ์šฉ ํ† ํฐ. ๋งˆ์Œ๊ฐ€์ง ํ† ํฐ ์ ‘๋‘์‚ฌ ํ•„์š”.
* @return ํ† ํฐ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ VO
*/
fun decode(token: String): MaeumgagymToken

/**
* ์ž…๋ ฅ๋œ ์ž„์˜์˜ ์•”ํ˜ธํ™” ํ‚ค๋ฅผ ์ด์šฉํ•ด ๋ณตํ˜ธํ™”
*
* @param token ์•”ํ˜ธํ™”๋œ ์ƒํƒœ์˜ ์ธ์ฆ์šฉ ํ† ํฐ. ํ† ํฐ ํ˜•ํƒœ์— ๋”ฐ๋ผ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ์ ‘๋‘์‚ฌ ํ•„์š”.
* @param key ์•”ํ˜ธํ™” ํ‚ค
*
* @return ํ† ํฐ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ VO
*/
fun decode(token: String, key: String): MaeumgagymToken
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.info.maeumgagym.security.mgtoken.vo.MaeumgagymTokenPair
*
* ๋ฐ˜ํ™˜๋˜๋Š” [MaeumgagymTokenPair]์˜ ๊ฐ ํ† ํฐ์€ ๋™์ผํ•œ [tokenId][com.info.maeumgagym.security.mgtoken.vo.MaeumgagymToken.tokenId]๋ฅผ ๊ฐ€์ง€๋ฉฐ, ๋ฏธ๋ฆฌ ์„ค์ •๋œ [prefix][com.info.maeumgagym.security.mgtoken.env.MaeumgagymTokenProperties.prefix]๊ฐ€ ๋ถ€์ฐฉ๋จ
*
* [MaeumgagymTokenProperties][com.info.maeumgagym.security.mgtoken.env.MaeumgagymTokenProperties]์˜ ๋งŒ๋ฃŒ ๊ธฐ๊ฐ„ ๊ด€๋ จ ์„ค์ •๋“ค์€ ์ดˆ ๋‹จ์œ„๋กœ ๋ฐ˜์˜๋จ
*
* @see MaeumgagymTokenDecoder
* @see MaeumgagymTokenValidator
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ interface MaeumgagymTokenValidator {
* - [tokenId][MaeumgagymToken.tokenId]์˜ ๋ฌดํšจํ™”๋˜์ง€ ์•Š์•˜๋Š”๊ฐ€
* - ํ† ํฐ์˜ [์œ ํšจ ์‹œ๊ฐ„ ๊ธธ์ด][MaeumgagymToken.expireAt]๊ฐ€ [์˜๋„ํ•œ ๋ฐ”][MaeumgagymTokenEncoder.encode]์™€ ๊ฐ™์€๊ฐ€
* - [ํ† ํฐ ์œ ํšจ ๊ธฐ๊ฐ„][MaeumgagymToken.expireAt]์„ ์ง€๋‚˜์ง€ ์•Š์•˜๋Š”๊ฐ€
* - ํ† ํฐ ๋ฐœ๊ธ‰ ๋Œ€์ƒ๊ณผ ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ผ์ฐจํ•˜๋Š”๊ฐ€: [IP ์ฃผ์†Œ][MaeumgagymToken.ip] ๋น„๊ต
* - [username][MaeumgagymToken.username]์ด ์กด์žฌํ•˜๋Š”๊ฐ€
* - ํ† ํฐ ๋ฐœ๊ธ‰ ๋Œ€์ƒ๊ณผ ํ˜„์žฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ผ์ฐจํ•˜๋Š”๊ฐ€: [IP ์ฃผ์†Œ][MaeumgagymToken.ip] ๋น„๊ต (์‚ฌ์šฉ์ž ๋„คํŠธ์›Œํฌ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์žฆ์€ ์ฃผ์†Œ ๋ณ€ํ™”๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์–ด ํ•ด๋‹น ๊ณผ์ •์€ ์ œ๊ฑฐ๋จ.)
*
* @param maeumgagymToken ๋ณตํ˜ธํ™”๋œ ์ธ์ฆ์šฉ ํ† ํฐ์˜ VO
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ internal class MaeumgagymTokenDecoderImpl(
) : MaeumgagymTokenDecoder {

override fun decode(token: String): MaeumgagymToken {
return decode(token, maeumgagymTokenProperties.secretKey)
}

override fun decode(token: String, key: String): MaeumgagymToken {
val prefixResolved = resolveTokenPrefix(token)

val decrypted =
try {
decrypt.decrypt(prefixResolved, key)
decrypt.decrypt(prefixResolved, maeumgagymTokenProperties.secretKey)
} catch (e: Exception) {
throw AuthenticationException.INVALID_TOKEN
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.info.maeumgagym.security.mgtoken.env.MaeumgagymTokenProperties
import com.info.maeumgagym.security.mgtoken.revoked.RevokedMGTokenContext
import com.info.maeumgagym.security.mgtoken.vo.MaeumgagymToken
import com.info.maeumgagym.security.mgtoken.vo.MaeumgagymTokenType
import com.info.maeumgagym.security.username.validator.UsernameValidator
import org.springframework.stereotype.Component
import java.time.LocalDateTime

Expand All @@ -19,8 +20,8 @@ import java.time.LocalDateTime
@Component
internal class MaeumgagymTokenValidatorImpl(
private val revokedMGTokenContext: RevokedMGTokenContext,
private val currentRequestContext: CurrentRequestContext,
private val maeumgagymTokenProperties: MaeumgagymTokenProperties
private val maeumgagymTokenProperties: MaeumgagymTokenProperties,
private val usernameValidator: UsernameValidator
) : MaeumgagymTokenValidator {

override fun validate(maeumgagymToken: MaeumgagymToken) {
Expand All @@ -46,6 +47,10 @@ internal class MaeumgagymTokenValidatorImpl(
throw AuthenticationException.EXPIRED_TOKEN
}

if (usernameValidator(maeumgagymToken.tokenId)) {
throw AuthenticationException.INVALID_TOKEN
}

// ๋ฐœ๊ธ‰ ๋Œ€์ƒ๊ณผ ์‚ฌ์šฉ์ž์˜ IP ์ฃผ์†Œ๋ฅผ ํ™•์ธํ•˜๋Š” ๋กœ์ง
// ์‚ฌ์šฉ์ž์˜ ๋„คํŠธ์›Œํฌ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋ณ€๋™๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ•ด๋‹น ์ธ์ฆ์€ ์ œ๊ฑฐ๋จ
// if (currentRequestContext.getCurrentRequest().remoteAddr != maeumgagymToken.ip) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.info.maeumgagym.security.username.validator

/**
* ์ฃผ์–ด์ง„ username์ด ์‹ค์ œ๋กœ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
*
* @author Daybreak312
* @since 19-06-2024
*/
interface UsernameValidator {

operator fun invoke(username: String): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.info.maeumgagym.security.username.validator

import com.info.maeumgagym.core.user.port.out.ExistUserPort
import org.springframework.stereotype.Component

@Component
class UsernameValidatorImpl(
private val existUserPort: ExistUserPort
) : UsernameValidator {

override fun invoke(username: String): Boolean =
existUserPort.existsByOAuthId(username)
}

0 comments on commit 86744a0

Please sign in to comment.