-
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.
- Loading branch information
Showing
23 changed files
with
296 additions
and
105 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
2 changes: 1 addition & 1 deletion
2
...m/feign/oauth/apple/dto/ApplePublicKey.kt → ...gagym/auth/dto/response/ApplePublicKey.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
2 changes: 1 addition & 1 deletion
2
.../feign/oauth/apple/dto/ApplePublicKeys.kt → ...agym/auth/dto/response/ApplePublicKeys.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
8 changes: 8 additions & 0 deletions
8
maeumgagym-core/src/main/kotlin/com/info/maeumgagym/auth/port/in/AppleLoginUseCase.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.info.maeumgagym.auth.port.`in` | ||
|
||
import com.info.maeumgagym.auth.dto.response.TokenResponse | ||
|
||
interface AppleLoginUseCase { | ||
|
||
fun execute(token: String): TokenResponse | ||
} |
6 changes: 6 additions & 0 deletions
6
maeumgagym-core/src/main/kotlin/com/info/maeumgagym/auth/port/out/AppleJwtParsePort.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,6 @@ | ||
package com.info.maeumgagym.auth.port.out | ||
|
||
interface AppleJwtParsePort { | ||
|
||
fun parseHeaders(token: String): MutableMap<String?, 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
12 changes: 12 additions & 0 deletions
12
maeumgagym-core/src/main/kotlin/com/info/maeumgagym/auth/port/out/GeneratePublicKeyPort.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,12 @@ | ||
package com.info.maeumgagym.auth.port.out | ||
|
||
import com.info.maeumgagym.auth.dto.response.ApplePublicKeys | ||
import java.security.PublicKey | ||
|
||
interface GeneratePublicKeyPort { | ||
|
||
fun generatePublicKey( | ||
tokenHeaders: MutableMap<String?, String?>, | ||
applePublicKeys: ApplePublicKeys | ||
): PublicKey | ||
} |
6 changes: 0 additions & 6 deletions
6
maeumgagym-core/src/main/kotlin/com/info/maeumgagym/auth/port/out/JwtExpiredCheckPort.kt
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
maeumgagym-core/src/main/kotlin/com/info/maeumgagym/auth/port/out/ParsePublicKeyPort.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.info.maeumgagym.auth.port.out | ||
|
||
import io.jsonwebtoken.Claims | ||
import java.security.PublicKey | ||
|
||
interface ParsePublicKeyPort { | ||
|
||
fun parseClaimsFromPublicKey(token: String, publicKey: PublicKey): Claims | ||
} |
8 changes: 8 additions & 0 deletions
8
maeumgagym-core/src/main/kotlin/com/info/maeumgagym/auth/port/out/ReadApplePublicKeyPort.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.info.maeumgagym.auth.port.out | ||
|
||
import com.info.maeumgagym.auth.dto.response.ApplePublicKeys | ||
|
||
interface ReadApplePublicKeyPort { | ||
|
||
fun readPublicKey(): ApplePublicKeys | ||
} |
46 changes: 46 additions & 0 deletions
46
maeumgagym-core/src/main/kotlin/com/info/maeumgagym/auth/service/AppleLoginService.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,46 @@ | ||
package com.info.maeumgagym.auth.service | ||
|
||
import com.info.maeumgagym.auth.dto.response.TokenResponse | ||
import com.info.maeumgagym.auth.port.`in`.AppleLoginUseCase | ||
import com.info.maeumgagym.auth.port.out.* | ||
import com.info.maeumgagym.user.model.Role | ||
import com.info.maeumgagym.user.model.User | ||
import com.info.maeumgagym.user.port.out.CreateUserPort | ||
import com.info.maeumgagym.user.port.out.FindUserByOAuthIdPort | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class AppleLoginService( | ||
private val appleJwtParsePort: AppleJwtParsePort, | ||
private val generatePublicKeyPort: GeneratePublicKeyPort, | ||
private val parsePublicKeyPort: ParsePublicKeyPort, | ||
private val readApplePublicKey: ReadApplePublicKeyPort, | ||
private val findUserByOAuthIdPort: FindUserByOAuthIdPort, | ||
private val createUserPort: CreateUserPort, | ||
private val generateJwtPort: GenerateJwtPort | ||
): AppleLoginUseCase{ | ||
|
||
@Transactional | ||
override fun execute(token: String): TokenResponse { | ||
|
||
val idToken = parseIdToken(token) | ||
|
||
val sub = idToken.subject | ||
|
||
val user: User = findUserByOAuthIdPort.findUserByOAuthId(sub) ?: createUserPort.createUser( | ||
User( | ||
nickname = idToken.get("name", String::class.java), | ||
roles = mutableListOf(Role.USER), | ||
oauthId = sub, | ||
profilePath = null | ||
) | ||
) | ||
|
||
return generateJwtPort.generateToken(user.id) | ||
} | ||
|
||
private fun parseIdToken(token: String) = parsePublicKeyPort.parseClaimsFromPublicKey( | ||
token, generatePublicKeyPort.generatePublicKey(appleJwtParsePort.parseHeaders(token), readApplePublicKey.readPublicKey()) | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
...gagym-infrastructure/src/main/kotlin/com/info/maeumgagym/auth/adapter/AppleAuthAdapter.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.info.maeumgagym.auth.adapter | ||
|
||
import com.info.common.WebAdapter | ||
import com.info.maeumgagym.auth.port.out.ReadApplePublicKeyPort | ||
import com.info.maeumgagym.feign.oauth.apple.AppleClient | ||
|
||
@WebAdapter | ||
class AppleAuthAdapter( | ||
private val appleClient: AppleClient | ||
): ReadApplePublicKeyPort { | ||
|
||
override fun readPublicKey() = appleClient.applePublicKeys() | ||
} |
36 changes: 36 additions & 0 deletions
36
...frastructure/src/main/kotlin/com/info/maeumgagym/auth/adapter/GeneratePublicKeyAdapter.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,36 @@ | ||
package com.info.maeumgagym.auth.adapter | ||
|
||
import com.info.common.WebAdapter | ||
import com.info.maeumgagym.auth.dto.response.ApplePublicKey | ||
import com.info.maeumgagym.auth.dto.response.ApplePublicKeys | ||
import com.info.maeumgagym.auth.port.out.GeneratePublicKeyPort | ||
import com.info.maeumgagym.global.exception.InvalidTokenException | ||
import java.security.KeyFactory | ||
import java.security.NoSuchAlgorithmException | ||
import java.security.PublicKey | ||
import java.security.spec.InvalidKeySpecException | ||
|
||
@WebAdapter | ||
class GeneratePublicKeyAdapter: GeneratePublicKeyPort { | ||
|
||
private companion object { | ||
const val ALG_HEADER_KEY = "alg" | ||
const val KID_HEADER_KEY = "kid" | ||
} | ||
|
||
override fun generatePublicKey(tokenHeaders: MutableMap<String?, String?>, applePublicKeys: ApplePublicKeys): PublicKey { | ||
|
||
val publicKey: ApplePublicKey = applePublicKeys.matchesKey( | ||
tokenHeaders[ALG_HEADER_KEY]!!, | ||
tokenHeaders[KID_HEADER_KEY]!! | ||
) ?: throw InvalidTokenException | ||
|
||
return try { | ||
KeyFactory.getInstance(publicKey.kty).generatePublic(publicKey.publicKeySpec()) | ||
} catch (e: NoSuchAlgorithmException) { | ||
throw IllegalStateException("Apple OAuth 로그인 중 public key 생성에 문제가 발생했습니다.") | ||
} catch (e: InvalidKeySpecException) { | ||
throw IllegalStateException("Apple OAuth 로그인 중 public key 생성에 문제가 발생했습니다.") | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...gagym-infrastructure/src/main/kotlin/com/info/maeumgagym/feign/oauth/apple/AppleClient.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
31 changes: 31 additions & 0 deletions
31
maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/global/jwt/AppleJwtParser.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,31 @@ | ||
package com.info.maeumgagym.global.jwt | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.info.maeumgagym.auth.port.out.AppleJwtParsePort | ||
import com.info.maeumgagym.global.exception.InvalidTokenException | ||
import org.springframework.stereotype.Component | ||
import org.springframework.util.Base64Utils | ||
|
||
|
||
@Component | ||
class AppleJwtParser(private val objectMapper: ObjectMapper): AppleJwtParsePort { | ||
|
||
companion object { | ||
private const val IDENTITY_TOKEN_VALUE_DELIMITER = "\\." | ||
private const val HEADER_INDEX = 0 | ||
} | ||
|
||
@Suppress("unchecked_cast") | ||
override fun parseHeaders(token: String): MutableMap<String?, String?> { | ||
return try { | ||
val encodedHeader: String = token.split(IDENTITY_TOKEN_VALUE_DELIMITER.toRegex())[HEADER_INDEX] | ||
val decodedHeader = String(Base64Utils.decodeFromUrlSafeString(encodedHeader)) | ||
objectMapper.readValue(decodedHeader, MutableMap::class.java) as MutableMap<String?, String?> | ||
} catch (e: JsonProcessingException) { | ||
throw InvalidTokenException | ||
} catch (e: ArrayIndexOutOfBoundsException) { | ||
throw InvalidTokenException | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/global/jwt/JwtAdapter.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,70 @@ | ||
package com.info.maeumgagym.global.jwt | ||
|
||
import com.info.maeumgagym.auth.dto.response.TokenResponse | ||
import com.info.maeumgagym.auth.port.out.GenerateJwtPort | ||
import com.info.maeumgagym.auth.port.out.ParsePublicKeyPort | ||
import com.info.maeumgagym.global.env.jwt.JwtProperties | ||
import com.info.maeumgagym.global.exception.ExpiredTokenException | ||
import com.info.maeumgagym.global.exception.InvalidTokenException | ||
import com.info.maeumgagym.global.security.principle.CustomUserDetailService | ||
import com.info.maeumgagym.global.security.principle.CustomUserDetails | ||
import io.jsonwebtoken.* | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.stereotype.Component | ||
import java.security.PublicKey | ||
import java.util.* | ||
|
||
@Component | ||
class JwtAdapter( | ||
val jwtProperties: JwtProperties, | ||
private val customUserDetailService: CustomUserDetailService | ||
) : GenerateJwtPort, ParsePublicKeyPort { | ||
|
||
override fun generateToken(userId: UUID): TokenResponse { | ||
return TokenResponse( | ||
generateAccessToken(userId) | ||
) | ||
} | ||
|
||
private fun generateAccessToken(userId: UUID): String { | ||
val now = Date() | ||
return Jwts.builder() | ||
.setSubject(userId.toString()) | ||
.setIssuedAt(now) | ||
.setExpiration(Date(now.time + jwtProperties.accessExpiredExp * 1000L)) | ||
.signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey) | ||
.compact() | ||
} | ||
|
||
fun getAuthentication(token: String): Authentication { | ||
|
||
val subject = getBody(token).subject | ||
|
||
val authDetails = customUserDetailService.loadUserByUsername(subject) as CustomUserDetails | ||
|
||
return UsernamePasswordAuthenticationToken(authDetails, null, authDetails.authorities) | ||
} | ||
|
||
private fun getBody(token: String): Claims { | ||
try { | ||
return Jwts.parser().setSigningKey(jwtProperties.secretKey).parseClaimsJws(token).body | ||
} catch (e: JwtException) { | ||
when (e) { | ||
is ExpiredJwtException -> throw ExpiredTokenException | ||
else -> throw InvalidTokenException | ||
} | ||
} | ||
} | ||
|
||
override fun parseClaimsFromPublicKey(token: String, publicKey: PublicKey): Claims { | ||
return try { | ||
Jwts.parser().setSigningKey(publicKey).parseClaimsJws(token).body | ||
} catch (e: Exception) { | ||
when (e) { | ||
is ExpiredJwtException -> throw ExpiredTokenException | ||
else -> throw InvalidTokenException | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
maeumgagym-infrastructure/src/main/kotlin/com/info/maeumgagym/global/jwt/JwtFilter.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,29 @@ | ||
package com.info.maeumgagym.global.jwt | ||
|
||
import com.info.maeumgagym.global.security.principle.CustomUserDetailService | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
import javax.servlet.FilterChain | ||
import javax.servlet.http.HttpServletRequest | ||
import javax.servlet.http.HttpServletResponse | ||
|
||
class JwtFilter( | ||
customUserDetailService: CustomUserDetailService, | ||
private val jwtResolver: JwtResolver, | ||
private val jwtAdapter: JwtAdapter | ||
) : OncePerRequestFilter() { | ||
|
||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain | ||
) { | ||
|
||
jwtResolver.resolveToken(request) | ||
?.let { | ||
SecurityContextHolder.getContext().authentication = jwtAdapter.getAuthentication(it) | ||
|
||
filterChain.doFilter(request, response) | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...agym/global/security/token/JwtResolver.kt → ...info/maeumgagym/global/jwt/JwtResolver.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
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
37 changes: 0 additions & 37 deletions
37
...gagym-infrastructure/src/main/kotlin/com/info/maeumgagym/global/security/jwt/JwtFilter.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.