Skip to content

Commit

Permalink
fix: kakao oauth client
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 30, 2024
1 parent af141d1 commit feb4b82
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
36 changes: 26 additions & 10 deletions src/main/kotlin/com/hero/alignlab/client/kakao/KakaoOAuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.hero.alignlab.client.kakao.client.KaKaoOAuthClient
import com.hero.alignlab.client.kakao.config.KakaoOAuthClientConfig
import com.hero.alignlab.client.kakao.model.request.GenerateKakaoOAuthTokenRequest
import com.hero.alignlab.client.kakao.model.response.GenerateKakaoOAuthTokenResponse
import com.hero.alignlab.common.extension.resolveCancellation
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Service
Expand All @@ -13,20 +15,30 @@ class KakaoOAuthService(
private val kaKaoOAuthClient: KaKaoOAuthClient,
private val config: KakaoOAuthClientConfig.Config
) {
private val logger = KotlinLogging.logger {}

suspend fun getOAuthLoginLinkDev(): String {
return config.url + String.format(config.authorizedUrl, config.restApiKey, config.redirectUrl)
}

suspend fun getOAuthAuthorizeCode(redirectUrl: String? = null) {
withContext(Dispatchers.IO) {
kaKaoOAuthClient.getOAuthAuthorizeCode(redirectUrl)
}
suspend fun getOAuthAuthorizeCode() {
getOAuthAuthorizeCode(config.restApiKey, config.redirectUrl)
}

suspend fun getOAuthAuthorizeCode(clientId: String, redirectUri: String) {
runCatching {
withContext(Dispatchers.IO) {
kaKaoOAuthClient.getOAuthAuthorizeCode(clientId, redirectUri)
}
}.onFailure { e ->
logger.resolveCancellation("getOAuthAuthorizeCode", e)
}.getOrThrow()
}

suspend fun generateOAuthToken(code: String, redirectUri: String? = null): GenerateKakaoOAuthTokenResponse {
suspend fun generateOAuthToken(code: String): GenerateKakaoOAuthTokenResponse {
val request = GenerateKakaoOAuthTokenRequest(
clientId = config.clientSecretCode,
redirectUri = redirectUri ?: config.redirectUrl,
clientId = config.restApiKey,
redirectUri = config.redirectUrl,
code = code,
clientSecret = config.clientSecretCode
)
Expand All @@ -35,8 +47,12 @@ class KakaoOAuthService(
}

suspend fun generateOAuthToken(request: GenerateKakaoOAuthTokenRequest): GenerateKakaoOAuthTokenResponse {
return withContext(Dispatchers.IO) {
kaKaoOAuthClient.generateOAuthToken(request)
}
return runCatching {
withContext(Dispatchers.IO) {
kaKaoOAuthClient.generateOAuthToken(request)
}
}.onFailure { e ->
logger.resolveCancellation("generateOAuthToken", e)
}.getOrThrow()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.hero.alignlab.client.kakao.model.request.GenerateKakaoOAuthTokenReque
import com.hero.alignlab.client.kakao.model.response.GenerateKakaoOAuthTokenResponse

interface KaKaoOAuthClient {
suspend fun getOAuthAuthorizeCode(redirectUrl: String? = null)
suspend fun getOAuthAuthorizeCode(clientId: String, redirectUri: String)

suspend fun generateOAuthToken(request: GenerateKakaoOAuthTokenRequest): GenerateKakaoOAuthTokenResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class SuspendableKakaoOAuthClient(
client: WebClient,
private val config: KakaoOAuthClientConfig.Config,
) : KaKaoOAuthClient, SuspendableClient(client) {
override suspend fun getOAuthAuthorizeCode(redirectUrl: String?) {
override suspend fun getOAuthAuthorizeCode(clientId: String, redirectUri: String) {
client
.get()
.uri("/authorize") { builder ->
builder
.queryParam("response_type", "code")
.queryParam("client_id", config.restApiKey)
.queryParam("redirect_uri", redirectUrl ?: config.redirectUrl)
.queryParam("client_id", clientId)
.queryParam("redirect_uri", redirectUri)
.build()
}.requestOrNull<Any>()
}
Expand All @@ -27,7 +27,7 @@ class SuspendableKakaoOAuthClient(
.post()
.uri("/token") { builder ->
builder
.queryParam("grant_type", request.grantType)
.queryParam("grant_type", "authorization_code")
.queryParam("client_id", request.clientId)
.queryParam("redirect_uri", request.redirectUri)
.queryParam("code", request.code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy::class)
data class GenerateKakaoOAuthTokenRequest(
val grantType: String = "authorization_code",
val clientId: String,
val redirectUri: String,
val code: String,
Expand Down

0 comments on commit feb4b82

Please sign in to comment.