-
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
1 parent
c4af780
commit 3aeac01
Showing
17 changed files
with
267 additions
and
1 deletion.
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
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/hero/alignlab/client/kakao/SuspendableClient.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,17 @@ | ||
package com.hero.alignlab.client.kakao | ||
|
||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.springframework.web.reactive.function.client.awaitBody | ||
import org.springframework.web.reactive.function.client.awaitBodyOrNull | ||
|
||
abstract class SuspendableClient( | ||
protected val client: WebClient, | ||
) { | ||
protected suspend inline fun <reified T : Any> WebClient.RequestHeadersSpec<*>.request(): T { | ||
return this.retrieve().awaitBody<T>() | ||
} | ||
|
||
protected suspend inline fun <reified T : Any> WebClient.RequestHeadersSpec<*>.requestOrNull(): T? { | ||
return this.retrieve().awaitBodyOrNull<T>() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/hero/alignlab/client/kakao/client/KaKaoOAuthClient.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,5 @@ | ||
package com.hero.alignlab.client.kakao.client | ||
|
||
interface KaKaoOAuthClient { | ||
suspend fun getOAuthAuthorizeCode(redirectUrl: String? = null) | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/hero/alignlab/client/kakao/client/SuspendableKakaoOAuthClient.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,22 @@ | ||
package com.hero.alignlab.client.kakao.client | ||
|
||
import com.hero.alignlab.client.kakao.SuspendableClient | ||
import com.hero.alignlab.client.kakao.config.KakaoOAuthClientConfig | ||
import org.springframework.web.reactive.function.client.WebClient | ||
|
||
class SuspendableKakaoOAuthClient( | ||
client: WebClient, | ||
private val config: KakaoOAuthClientConfig.Config, | ||
) : KaKaoOAuthClient, SuspendableClient(client) { | ||
override suspend fun getOAuthAuthorizeCode(redirectUrl: String?) { | ||
client | ||
.get() | ||
.uri("/authorize") { builder -> | ||
builder | ||
.queryParam("response_type", "code") | ||
.queryParam("client_id", config.restApiKey) | ||
.queryParam("redirect_uri", redirectUrl ?: config.redirectUrl) | ||
.build() | ||
}.requestOrNull<Any>() | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/com/hero/alignlab/client/kakao/config/KakaoOAuthClientConfig.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,52 @@ | ||
package com.hero.alignlab.client.kakao.config | ||
|
||
import com.hero.alignlab.client.WebClientFactory | ||
import com.hero.alignlab.client.kakao.client.KaKaoOAuthClient | ||
import com.hero.alignlab.client.kakao.client.SuspendableKakaoOAuthClient | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import jakarta.validation.Valid | ||
import jakarta.validation.constraints.NotBlank | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.validation.annotation.Validated | ||
|
||
@Validated | ||
@Configuration | ||
class KakaoOAuthClientConfig { | ||
private val logger = KotlinLogging.logger { } | ||
|
||
@Bean | ||
@ConditionalOnProperty(prefix = "oauth.kakao", name = ["url"]) | ||
@ConfigurationProperties(prefix = "oauth.kakao") | ||
fun kakaoOAuthConfig() = Config() | ||
|
||
@Bean | ||
@ConditionalOnBean(name = ["kakaoOAuthConfig"]) | ||
@ConditionalOnMissingBean(KaKaoOAuthClient::class) | ||
fun kakaoOAuthClient( | ||
@Valid kakaoOAuthConfig: Config | ||
): KaKaoOAuthClient { | ||
logger.info { "initialized kakaoOAuthClient. $kakaoOAuthConfig" } | ||
|
||
val webclient = WebClientFactory.generate(kakaoOAuthConfig.url) | ||
|
||
return SuspendableKakaoOAuthClient(webclient, kakaoOAuthConfig) | ||
} | ||
|
||
data class Config( | ||
@field:NotBlank | ||
var url: String = "", | ||
@field:NotBlank | ||
var restApiKey: String = "", | ||
@field:NotBlank | ||
var clientSecretCode: String = "", | ||
@field:NotBlank | ||
var redirectUrl: String = "", | ||
@field:NotBlank | ||
var devRedirectUrl: String = "", | ||
) | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/hero/alignlab/domain/auth/application/OAuthFacade.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,22 @@ | ||
package com.hero.alignlab.domain.auth.application | ||
|
||
import com.hero.alignlab.client.kakao.client.KaKaoOAuthClient | ||
import com.hero.alignlab.domain.auth.model.OAuthProvider | ||
import com.hero.alignlab.domain.auth.model.response.OAuthAuthorizeCodeResponse | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class OAuthFacade( | ||
private val kaKaoOAuthClient: KaKaoOAuthClient, | ||
) { | ||
private val logger = KotlinLogging.logger { } | ||
|
||
suspend fun getOAuthAuthorizeCode(provider: OAuthProvider): OAuthAuthorizeCodeResponse { | ||
when (provider) { | ||
OAuthProvider.kakao -> kaKaoOAuthClient.getOAuthAuthorizeCode() | ||
} | ||
|
||
return OAuthAuthorizeCodeResponse(provider) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/com/hero/alignlab/domain/auth/model/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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.hero.alignlab.domain.auth.model | ||
|
||
enum class OAuthProvider { | ||
kakao, | ||
; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthAuthorizeCodeResponse.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,7 @@ | ||
package com.hero.alignlab.domain.auth.model.response | ||
|
||
import com.hero.alignlab.domain.auth.model.OAuthProvider | ||
|
||
data class OAuthAuthorizeCodeResponse( | ||
val provider: OAuthProvider, | ||
) |
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/com/hero/alignlab/domain/auth/resource/OAuthResource.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,30 @@ | ||
package com.hero.alignlab.domain.auth.resource | ||
|
||
import com.hero.alignlab.common.extension.wrapOk | ||
import com.hero.alignlab.domain.auth.application.OAuthFacade | ||
import com.hero.alignlab.domain.auth.model.OAuthProvider | ||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import org.springframework.http.MediaType | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RequestParam | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@Tag(name = "OAuth 인증 및 인가 관리") | ||
@RestController | ||
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE]) | ||
class OAuthResource( | ||
private val oAuthFacade: OAuthFacade, | ||
) { | ||
/** | ||
* OAuth로 인가코드를 부여받고, 이를 Redirect Url로 반환. | ||
* - redirectUrl은 Client의 주소값, 만약 변경시 yml 및 각 클라이언트 구조 변경 필요. | ||
* - [Kakao Rest Auth](https://developers.kakao.com/docs/latest/ko/kakaologin/rest-api) | ||
*/ | ||
@Operation(summary = "인가 코드 받기") | ||
@GetMapping("/api/v1/oauth/{provider}/authorize") | ||
suspend fun getOAuthAuthorizeCode( | ||
@RequestParam provider: OAuthProvider, | ||
) = oAuthFacade.getOAuthAuthorizeCode(provider).wrapOk() | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/hero/alignlab/domain/dev/application/DevOAuthService.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,24 @@ | ||
package com.hero.alignlab.domain.dev.application | ||
|
||
import com.hero.alignlab.client.kakao.client.KaKaoOAuthClient | ||
import com.hero.alignlab.client.kakao.config.KakaoOAuthClientConfig | ||
import com.hero.alignlab.domain.auth.model.OAuthProvider | ||
import com.hero.alignlab.domain.dev.model.response.DevOAuthCodeResponse | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class DevOAuthService( | ||
private val kaKaoOAuthClient: KaKaoOAuthClient, | ||
private val config: KakaoOAuthClientConfig.Config, | ||
) { | ||
private val logger = KotlinLogging.logger { } | ||
|
||
suspend fun getOAuthAuthorizeCode(provider: OAuthProvider): DevOAuthCodeResponse { | ||
when (provider) { | ||
OAuthProvider.kakao -> kaKaoOAuthClient.getOAuthAuthorizeCode(config.devRedirectUrl) | ||
} | ||
|
||
return DevOAuthCodeResponse(provider) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/hero/alignlab/domain/dev/model/request/DevRedirectedRequest.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.hero.alignlab.domain.dev.model.request | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming | ||
|
||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy::class) | ||
data class DevRedirectedRequest( | ||
val code: String?, | ||
val error: String?, | ||
val errorDescription: String?, | ||
val state: String?, | ||
) |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/hero/alignlab/domain/dev/model/response/DevOAuthCodeResponse.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,7 @@ | ||
package com.hero.alignlab.domain.dev.model.response | ||
|
||
import com.hero.alignlab.domain.auth.model.OAuthProvider | ||
|
||
data class DevOAuthCodeResponse( | ||
val provider: OAuthProvider, | ||
) |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/hero/alignlab/domain/dev/model/response/DevRedirectedResponse.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.hero.alignlab.domain.dev.model.response | ||
|
||
import com.hero.alignlab.domain.auth.model.OAuthProvider | ||
import com.hero.alignlab.domain.dev.model.request.DevRedirectedRequest | ||
|
||
data class DevRedirectedResponse( | ||
val provider: OAuthProvider, | ||
val requestParams: DevRedirectedRequest, | ||
) |
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/com/hero/alignlab/domain/dev/resource/DevOAuthResource.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,38 @@ | ||
package com.hero.alignlab.domain.dev.resource | ||
|
||
import com.hero.alignlab.common.extension.wrapOk | ||
import com.hero.alignlab.domain.auth.model.OAuthProvider | ||
import com.hero.alignlab.domain.dev.application.DevOAuthService | ||
import com.hero.alignlab.domain.dev.model.request.DevRedirectedRequest | ||
import com.hero.alignlab.domain.dev.model.response.DevRedirectedResponse | ||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import org.springdoc.core.annotations.ParameterObject | ||
import org.springframework.http.MediaType | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@Tag(name = "Dev OAuth 인증 및 인가 관리") | ||
@RestController | ||
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE]) | ||
class DevOAuthResource( | ||
private val devOAuthService: DevOAuthService, | ||
) { | ||
@Operation(summary = "인가 코드 받기") | ||
@GetMapping("/api/dev/v1/oauth/{provider}/authorize") | ||
suspend fun getDevOAuthAuthorizeCode( | ||
@PathVariable provider: OAuthProvider, | ||
) = devOAuthService.getOAuthAuthorizeCode(provider).wrapOk() | ||
|
||
@Operation(summary = "OAuth Redirect Test") | ||
@GetMapping("/api/dev/v1/oauth/{provider}/authorize/redirected") | ||
suspend fun redirectedDevOAuthAuthorizeCode( | ||
@PathVariable provider: OAuthProvider, | ||
@ParameterObject request: DevRedirectedRequest, | ||
) = DevRedirectedResponse( | ||
provider = provider, | ||
requestParams = request, | ||
).wrapOk() | ||
} |
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