-
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.
- Loading branch information
Showing
7 changed files
with
123 additions
and
3 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
69 changes: 69 additions & 0 deletions
69
...lication/api/src/main/kotlin/io/raemian/api/auth/converter/TokenRequestEntityConverter.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,69 @@ | ||
package io.raemian.api.auth.converter | ||
|
||
import com.nimbusds.jose.util.IOUtils | ||
import com.nimbusds.jose.util.StandardCharset | ||
import io.jsonwebtoken.Jwts | ||
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo | ||
import org.bouncycastle.openssl.PEMParser | ||
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter | ||
import org.springframework.core.convert.converter.Converter | ||
import org.springframework.core.io.ClassPathResource | ||
import org.springframework.http.RequestEntity | ||
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest | ||
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequestEntityConverter | ||
import org.springframework.util.LinkedMultiValueMap | ||
import java.io.StringReader | ||
import java.security.PrivateKey | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
import java.util.Date | ||
|
||
|
||
class TokenRequestEntityConverter: Converter<OAuth2AuthorizationCodeGrantRequest, RequestEntity<*>> { | ||
|
||
private val defaultConverter = OAuth2AuthorizationCodeGrantRequestEntityConverter() | ||
private val APPLE_URL = "https://appleid.apple.com" | ||
private val APPLE_KEY_PATH = "static/AuthKey_L6LHNB5853.p8" | ||
private val APPLE_CLIENT_ID = "com.app.bandiboodi" | ||
private val APPLE_TEAM_ID = "R39H272X9N" | ||
private val APPLE_KEY_ID = "L6LHNB5853" | ||
|
||
override fun convert(req: OAuth2AuthorizationCodeGrantRequest): RequestEntity<*> { | ||
val entity = defaultConverter.convert(req)!! | ||
val registrationId = req.clientRegistration.registrationId | ||
val params = entity.body as LinkedMultiValueMap<String, String> | ||
|
||
if(registrationId.contains("apple")) { | ||
params.set("client_secret", createClientSecret()) | ||
} | ||
|
||
return RequestEntity<Any>(params, entity.headers, entity.method, entity.url) | ||
} | ||
|
||
fun getPrivateKey(): PrivateKey { | ||
val resource = ClassPathResource(APPLE_KEY_PATH) | ||
val converter = JcaPEMKeyConverter() | ||
|
||
val input = resource.inputStream | ||
val pemParser = PEMParser(StringReader(IOUtils.readInputStreamToString(input, StandardCharset.UTF_8))) | ||
val key = pemParser.readObject() as PrivateKeyInfo | ||
|
||
return converter.getPrivateKey(key) | ||
} | ||
|
||
fun createClientSecret(): String { | ||
val expirationDate: Date = | ||
Date.from(LocalDateTime.now().plusDays(30).atZone(ZoneId.systemDefault()).toInstant()) | ||
val jwtHeader = mapOf("kid" to APPLE_KEY_ID, "alg" to "ES256") | ||
|
||
return Jwts.builder() | ||
.setHeaderParams(jwtHeader) | ||
.setIssuer(APPLE_TEAM_ID) | ||
.setIssuedAt(Date(System.currentTimeMillis())) // 발행 시간 - UNIX 시간 | ||
.setExpiration(expirationDate) // 만료 시간 | ||
.setAudience(APPLE_URL) | ||
.setSubject(APPLE_CLIENT_ID) | ||
.signWith(getPrivateKey()) | ||
.compact(); | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
backend/application/api/src/main/resources/static/AuthKey_L6LHNB5853.p8
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 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQga4FUpdhLY3Pb8feb | ||
MYS+VanfU+FTjUgGY2Xx2cLR7+KgCgYIKoZIzj0DAQehRANCAAR5iFOVk0PP/LN+ | ||
8rlgxXKwviytJ95imMkvJr/CSNZ1W5VkkICjoyG/PV9tcY0XvlyAd6Z7scJ62Cyb | ||
Dq6Ghiu+ | ||
-----END PRIVATE KEY----- |
2 changes: 1 addition & 1 deletion
2
...nd/storage/db-core/src/main/kotlin/io/raemian/storage/db/core/user/enums/OAuthProvider.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,5 +1,5 @@ | ||
package io.raemian.storage.db.core.user.enums | ||
|
||
enum class OAuthProvider { | ||
GOOGLE, NAVER, KAKAO | ||
GOOGLE, NAVER, KAKAO, APPLE | ||
} |