-
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
Changes from all commits
d25a539
0e52ad4
fd33ee8
8fdc640
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
| } | ||
| 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 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,30 @@ 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.AuthConfig | ||
| 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 +45,90 @@ 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", | ||
| ) | ||
|
|
||
| internal fun AuthConfig.configureBearerAuth( | ||
| localAuthDataSource: LocalAuthDataSource, | ||
| authEventBus: AuthEventBus, | ||
| refreshClient: HttpClient, | ||
| ) { | ||
| 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 | ||
| } | ||
|
|
||
| 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 | ||
|
Comment on lines
+88
to
+92
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. 일시적 refresh 실패까지 로그아웃 처리하면 세션이 불필요하게 끊깁니다. 여기서는 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
||
| sendWithoutRequest { request -> | ||
| val requestPath = request.url.pathSegments.joinToString("/") | ||
| AUTH_EXCLUDED_PATHS.none { path -> | ||
| requestPath.contains(path) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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 +145,10 @@ val httpClientModule = module { | |
| ) | ||
| } | ||
|
|
||
| install(Auth) { | ||
| configureBearerAuth(localAuthDataSource, authEventBus, refreshClient) | ||
| } | ||
|
|
||
| install(Logging) { | ||
| logger = object : Logger { | ||
| override fun log(message: String) { | ||
|
|
@@ -83,7 +182,7 @@ val httpClientModule = module { | |
| } | ||
|
|
||
| defaultRequest { | ||
| url("https://api.cchaksa.com/api/") | ||
| url(BASE_URL) | ||
| contentType(ContentType.Application.Json) | ||
| } | ||
| }.also { client -> | ||
|
|
||
| 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 | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.