-
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 #79 from mash-up-kr/feature/apple-login
feat: Apple login
- Loading branch information
Showing
24 changed files
with
250 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
10 changes: 10 additions & 0 deletions
10
...pi/src/main/kotlin/com/mashup/pic/auth/applicationService/dto/AppleLoginServiceRequest.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,10 @@ | ||
package com.mashup.pic.auth.applicationService.dto | ||
|
||
import com.mashup.pic.domain.user.LoginProvider | ||
|
||
data class AppleLoginServiceRequest( | ||
val idToken: String, | ||
val provider: LoginProvider, | ||
val fullName: String, | ||
val user: String | ||
) |
8 changes: 2 additions & 6 deletions
8
pic-api/src/main/kotlin/com/mashup/pic/auth/applicationService/dto/LoginServiceRequest.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 |
---|---|---|
@@ -1,14 +1,10 @@ | ||
package com.mashup.pic.auth.applicationService.dto | ||
|
||
import com.mashup.pic.domain.user.LoginProvider | ||
|
||
data class LoginServiceRequest( | ||
val idToken: String, | ||
val provider: LoginProvider, | ||
val nickname: String, | ||
val profileImage: String | ||
) | ||
|
||
enum class LoginProvider { | ||
KAKAO, | ||
NAVER, | ||
} |
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
20 changes: 20 additions & 0 deletions
20
pic-api/src/main/kotlin/com/mashup/pic/auth/controller/dto/AppleLoginRequest.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,20 @@ | ||
package com.mashup.pic.auth.controller.dto | ||
|
||
import com.mashup.pic.auth.applicationService.dto.AppleLoginServiceRequest | ||
import com.mashup.pic.domain.user.LoginProvider | ||
import jakarta.validation.constraints.NotBlank | ||
|
||
data class AppleLoginRequest( | ||
@NotBlank val idToken: String, | ||
@NotBlank val fullName: String, | ||
@NotBlank val user: String | ||
) { | ||
fun toServiceRequest(): AppleLoginServiceRequest { | ||
return AppleLoginServiceRequest( | ||
idToken = idToken, | ||
provider = LoginProvider.APPLE, | ||
fullName = fullName, | ||
user = user | ||
) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
pic-api/src/main/kotlin/com/mashup/pic/auth/controller/dto/LoginRequest.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
100 changes: 100 additions & 0 deletions
100
pic-api/src/main/kotlin/com/mashup/pic/security/oidc/AppleIdTokenValidator.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,100 @@ | ||
package com.mashup.pic.security.oidc | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.mashup.pic.common.exception.PicException | ||
import com.mashup.pic.common.exception.PicExceptionType | ||
import com.mashup.pic.external.apple.AppleClient | ||
import com.mashup.pic.external.common.response.JwkKey | ||
import io.jsonwebtoken.Jwts | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.stereotype.Component | ||
import java.math.BigInteger | ||
import java.security.Key | ||
import java.security.KeyFactory | ||
import java.security.spec.RSAPublicKeySpec | ||
import java.util.Base64 | ||
|
||
@Component | ||
@Profile("!test") | ||
class AppleIdTokenValidator( | ||
private val appleJwksClient: AppleClient, | ||
private val objectMapper: ObjectMapper, | ||
@Value("\${apple.issuer}") private val issuer: String, | ||
@Value("\${apple.audience}") private val audience: String | ||
) : IdTokenValidator { | ||
private val decoder = Base64.getUrlDecoder() | ||
private val keyFactory = KeyFactory.getInstance(SIGNING_ALGORITHM) | ||
|
||
override fun validateAndGetId( | ||
idToken: String, | ||
nickname: String | ||
): String { | ||
verifyPayload(idToken, nickname) | ||
verifySignature(idToken) | ||
return extractSub(idToken) | ||
} | ||
|
||
private fun extractSub(idToken: String): String { | ||
val payload = decodePayload(idToken) | ||
return payload[SUB_KEY] as String? ?: throw PicException.of(PicExceptionType.ARGUMENT_NOT_VALID, "Can't extract SUB") | ||
} | ||
|
||
private fun verifyPayload( | ||
idToken: String, | ||
sub: String | ||
) { | ||
val payload = decodePayload(idToken) | ||
require(payload[ISSUER_KEY] == issuer) { "Invalid issuer" } | ||
require(payload[AUDIENCE_KEY] == audience) { "Invalid audience" } | ||
require(payload[SUB_KEY] == sub) { "Invalid nickname" } | ||
} | ||
|
||
private fun verifySignature(idToken: String) { | ||
val kid = extractKid(idToken) | ||
val publicKey = getPublicKey(kid) | ||
Jwts.parserBuilder() | ||
.setSigningKey(publicKey) | ||
.build() | ||
.parseClaimsJws(idToken) | ||
} | ||
|
||
private fun extractKid(idToken: String): String { | ||
val header = decodeHeader(idToken) | ||
return header[KID_KEY] as? String ?: throw PicException.of(PicExceptionType.ARGUMENT_NOT_VALID, "Can't extract KID") | ||
} | ||
|
||
private fun getPublicKey(kid: String): Key { | ||
val jwk = getJwkByKid(kid) | ||
val n = BigInteger(1, decoder.decode(jwk.n)) | ||
val e = BigInteger(1, decoder.decode(jwk.e)) | ||
val keySpec = RSAPublicKeySpec(n, e) | ||
return keyFactory.generatePublic(keySpec) | ||
} | ||
|
||
private fun getJwkByKid(kid: String): JwkKey { | ||
return appleJwksClient.getJwks().getJwkKeyByKid(kid) | ||
?: appleJwksClient.refreshAndGetJwks().getJwkKeyByKid(kid) | ||
?: throw PicException.of(PicExceptionType.ARGUMENT_NOT_VALID, "Can't find the Jwk matching the KID") | ||
} | ||
|
||
private fun decodePayload(idToken: String): Map<String, Any> { | ||
val payload = idToken.split(".")[1] | ||
val decodedPayload = String(decoder.decode(payload)) | ||
return objectMapper.readValue(decodedPayload, Map::class.java) as Map<String, Any> | ||
} | ||
|
||
private fun decodeHeader(idToken: String): Map<String, Any> { | ||
val header = idToken.split(".")[0] | ||
val decodedHeader = String(decoder.decode(header)) | ||
return objectMapper.readValue(decodedHeader, Map::class.java) as Map<String, Any> | ||
} | ||
|
||
companion object { | ||
private const val ISSUER_KEY = "iss" | ||
private const val AUDIENCE_KEY = "aud" | ||
private const val SUB_KEY = "sub" | ||
private const val KID_KEY = "kid" | ||
private const val SIGNING_ALGORITHM = "RSA" | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,5 +4,5 @@ interface IdTokenValidator { | |
fun validateAndGetId( | ||
idToken: String, | ||
nickname: String | ||
): Long | ||
): 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
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
16 changes: 0 additions & 16 deletions
16
pic-api/src/test/kotlin/com/mashup/pic/fake/FakeIdTokenValidator.kt
This file was deleted.
Oops, something went wrong.
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
57 changes: 0 additions & 57 deletions
57
...pi/src/test/kotlin/com/mashup/pic/group/applicationservice/GroupApplicationServiceTest.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.