-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Apple 로그인 서버 인증 및 토큰 자동 리프레시 구현 #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d25a539
feat: Ktor Auth 플러그인 기반 토큰 자동 첨부/리프레시 및 통합 테스트 구현
lluke0 0e52ad4
feat: Apple 로그인 서버 인증 연동 및 provider 기반 로그인 분기 구현
lluke0 fd33ee8
fix: refreshClient에 HttpTimeout 추가 및 테스트 AUTH_EXCLUDED_PATHS 중복 제거
lluke0 8fdc640
refactor: Bearer 인증 설정 로직을 공통 함수로 추출하여 프로덕션/테스트 간 중복 제거
lluke0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
18 changes: 15 additions & 3 deletions
18
...p/src/commonMain/kotlin/com/chukchukhaksa/mobile/domain/auth/usecase/AppleLoginUseCase.kt
This file contains hidden or 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,13 +1,25 @@ | ||
| package com.chukchukhaksa.mobile.domain.auth.usecase | ||
|
|
||
| import com.chukchukhaksa.mobile.common.kmp.AppleSignInResult | ||
| import com.chukchukhaksa.mobile.common.kmp.AppleSignInClient | ||
| import com.chukchukhaksa.mobile.domain.auth.model.SignInResult | ||
| import com.chukchukhaksa.mobile.domain.auth.repository.AuthRepository | ||
| import com.chukchukhaksa.mobile.domain.common.runCatchingIgnoreCancelled | ||
|
|
||
| class AppleLoginUseCase( | ||
| private val appleSignInClient: AppleSignInClient, | ||
| private val authRepository: AuthRepository, | ||
| ) { | ||
| suspend operator fun invoke(): Result<AppleSignInResult> = runCatchingIgnoreCancelled { | ||
| appleSignInClient.signIn() | ||
| suspend operator fun invoke(): Result<SignInResult> = runCatchingIgnoreCancelled { | ||
| val appleResult = appleSignInClient.signIn() | ||
| val signInResult = authRepository.signIn( | ||
| provider = "APPLE", | ||
|
lluke0 marked this conversation as resolved.
|
||
| idToken = appleResult.identityToken, | ||
| nonce = appleResult.nonce, | ||
| ) | ||
| authRepository.saveTokens( | ||
| accessToken = signInResult.accessToken, | ||
| refreshToken = signInResult.refreshToken, | ||
| ) | ||
| signInResult | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
18 changes: 18 additions & 0 deletions
18
composeApp/src/commonMain/kotlin/com/chukchukhaksa/mobile/remote/auth/AuthEventBus.kt
This file contains hidden or 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,18 @@ | ||
| package com.chukchukhaksa.mobile.remote.auth | ||
|
|
||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.flow.SharedFlow | ||
| import kotlinx.coroutines.flow.asSharedFlow | ||
|
|
||
| class AuthEventBus { | ||
| private val _events = MutableSharedFlow<AuthEvent>(extraBufferCapacity = 1) | ||
| val events: SharedFlow<AuthEvent> = _events.asSharedFlow() | ||
|
|
||
| fun emit(event: AuthEvent = AuthEvent.TokenExpired) { | ||
| _events.tryEmit(event) | ||
| } | ||
|
lluke0 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| sealed interface AuthEvent { | ||
| data object TokenExpired : AuthEvent | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,17 +2,29 @@ package com.chukchukhaksa.mobile.remote.di | |
|
|
||
| import com.chukchukhaksa.mobile.common.kmp.httpClientEngineFactory | ||
| import com.chukchukhaksa.mobile.common.kmp.isDebug | ||
| import com.chukchukhaksa.mobile.data.auth.datasource.LocalAuthDataSource | ||
| import com.chukchukhaksa.mobile.remote.auth.AuthEventBus | ||
| import com.chukchukhaksa.mobile.remote.auth.model.RefreshRequest | ||
| import com.chukchukhaksa.mobile.remote.auth.model.RefreshResponse | ||
| import com.chukchukhaksa.mobile.remote.common.ApiResponse | ||
| import com.chukchukhaksa.mobile.remote.common.getDataOrThrow | ||
| import io.github.aakira.napier.Napier | ||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.call.body | ||
| import io.ktor.client.plugins.HttpSend | ||
| import io.ktor.client.plugins.HttpTimeout | ||
| import io.ktor.client.plugins.auth.Auth | ||
| import io.ktor.client.plugins.auth.providers.BearerTokens | ||
| import io.ktor.client.plugins.auth.providers.bearer | ||
| import io.ktor.client.plugins.contentnegotiation.ContentNegotiation | ||
| import io.ktor.client.plugins.defaultRequest | ||
| import io.ktor.client.plugins.logging.LogLevel | ||
| import io.ktor.client.plugins.logging.Logger | ||
| import io.ktor.client.plugins.logging.Logging | ||
| import io.ktor.client.plugins.observer.ResponseObserver | ||
| import io.ktor.client.plugins.plugin | ||
| import io.ktor.client.request.post | ||
| import io.ktor.client.request.setBody | ||
| import io.ktor.client.statement.bodyAsText | ||
| import io.ktor.http.ContentType | ||
| import io.ktor.http.HttpHeaders | ||
|
|
@@ -32,8 +44,40 @@ private fun String.prettyPrintJson(): String = try { | |
| this | ||
| } | ||
|
|
||
| private val BASE_URL = if (isDebug) "https://dev.api.cchaksa.com/api/" else "https://api.cchaksa.com/api/" | ||
|
|
||
| internal val AUTH_EXCLUDED_PATHS = listOf( | ||
| "auth/refresh", | ||
| "users/signin", | ||
| ) | ||
|
|
||
| val httpClientModule = module { | ||
| single { AuthEventBus() } | ||
|
|
||
| single { | ||
| val localAuthDataSource: LocalAuthDataSource = get() | ||
| val authEventBus: AuthEventBus = get() | ||
|
|
||
| val refreshClient = HttpClient(httpClientEngineFactory) { | ||
| install(HttpTimeout) { | ||
| requestTimeoutMillis = 10_000 | ||
| connectTimeoutMillis = 5_000 | ||
| socketTimeoutMillis = 10_000 | ||
| } | ||
| install(ContentNegotiation) { | ||
| json( | ||
| Json { | ||
| ignoreUnknownKeys = true | ||
| isLenient = true | ||
| }, | ||
| ) | ||
| } | ||
| defaultRequest { | ||
| url(BASE_URL) | ||
| contentType(ContentType.Application.Json) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| HttpClient(httpClientEngineFactory) { | ||
| install(HttpTimeout) { | ||
| requestTimeoutMillis = 15_000 | ||
|
|
@@ -50,6 +94,52 @@ val httpClientModule = module { | |
| ) | ||
| } | ||
|
|
||
| install(Auth) { | ||
| bearer { | ||
| loadTokens { | ||
| val accessToken = localAuthDataSource.getAccessToken() | ||
| val refreshToken = localAuthDataSource.getRefreshToken() | ||
| if (accessToken != null && refreshToken != null) { | ||
| BearerTokens(accessToken, refreshToken) | ||
| } else { | ||
| null | ||
| } | ||
| } | ||
|
|
||
| refreshTokens { | ||
| val currentRefreshToken = localAuthDataSource.getRefreshToken() | ||
| if (currentRefreshToken == null) { | ||
| localAuthDataSource.clearTokens() | ||
| authEventBus.emit() | ||
| return@refreshTokens null | ||
| } | ||
|
lluke0 marked this conversation as resolved.
Outdated
|
||
|
|
||
| try { | ||
| val response = refreshClient.post("auth/refresh") { | ||
| setBody(RefreshRequest(refreshToken = currentRefreshToken)) | ||
| }.body<ApiResponse<RefreshResponse>>() | ||
|
|
||
| val result = response.getDataOrThrow() | ||
| localAuthDataSource.saveAccessToken(result.accessToken) | ||
| localAuthDataSource.saveRefreshToken(result.refreshToken) | ||
| BearerTokens(result.accessToken, result.refreshToken) | ||
| } catch (e: Exception) { | ||
| Napier.e("Token refresh failed", e) | ||
| localAuthDataSource.clearTokens() | ||
| authEventBus.emit() | ||
| null | ||
| } | ||
| } | ||
|
|
||
| sendWithoutRequest { request -> | ||
| val requestPath = request.url.pathSegments.joinToString("/") | ||
| AUTH_EXCLUDED_PATHS.none { path -> | ||
| requestPath.contains(path) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인증 제외 경로는 부분 문자열이 아니라 정확히 비교하세요.
🛠️ 제안 sendWithoutRequest { request ->
- val requestPath = request.url.pathSegments.joinToString("/")
- AUTH_EXCLUDED_PATHS.none { path ->
- requestPath.contains(path)
- }
+ val requestPath = request.url.pathSegments
+ .filter { it.isNotBlank() }
+ .dropWhile { it == "api" }
+ .joinToString("/")
+ requestPath !in AUTH_EXCLUDED_PATHS
}🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
| } | ||
|
|
||
| install(Logging) { | ||
| logger = object : Logger { | ||
| override fun log(message: String) { | ||
|
|
@@ -83,7 +173,7 @@ val httpClientModule = module { | |
| } | ||
|
|
||
| defaultRequest { | ||
| url("https://api.cchaksa.com/api/") | ||
| url(BASE_URL) | ||
| contentType(ContentType.Application.Json) | ||
| } | ||
| }.also { client -> | ||
|
|
||
28 changes: 28 additions & 0 deletions
28
...App/src/commonTest/kotlin/com/chukchukhaksa/mobile/remote/auth/FakeLocalAuthDataSource.kt
This file contains hidden or 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,28 @@ | ||
| package com.chukchukhaksa.mobile.remote.auth | ||
|
|
||
| import com.chukchukhaksa.mobile.data.auth.datasource.LocalAuthDataSource | ||
|
|
||
| class FakeLocalAuthDataSource( | ||
| initialAccessToken: String? = null, | ||
| initialRefreshToken: String? = null, | ||
| ) : LocalAuthDataSource { | ||
| private var accessToken: String? = initialAccessToken | ||
| private var refreshToken: String? = initialRefreshToken | ||
|
|
||
| override suspend fun saveAccessToken(token: String) { | ||
| accessToken = token | ||
| } | ||
|
|
||
| override suspend fun getAccessToken(): String? = accessToken | ||
|
|
||
| override suspend fun saveRefreshToken(token: String) { | ||
| refreshToken = token | ||
| } | ||
|
|
||
| override suspend fun getRefreshToken(): String? = refreshToken | ||
|
|
||
| override suspend fun clearTokens() { | ||
| accessToken = null | ||
| refreshToken = null | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.