-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#43 자동 로그인 구현 #44
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
Open
starshape7
wants to merge
2
commits into
develop
Choose a base branch
from
feat/#43-자동-로그인-구현
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "feat/#43-\uC790\uB3D9-\uB85C\uADF8\uC778-\uAD6C\uD604"
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
core/network/src/main/java/com/umcspot/spot/network/TokenAuthenticator.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,117 @@ | ||
| package com.umcspot.spot.network | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import com.umcspot.spot.datastore.token.SpotTokenData | ||
| import com.umcspot.spot.datastore.userId.SpotUserIdData | ||
| import com.umcspot.spot.network.service.TokenRefreshService | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.runBlocking | ||
| import okhttp3.Authenticator | ||
| import okhttp3.Request | ||
| import okhttp3.Response | ||
| import okhttp3.Route | ||
| import javax.inject.Inject | ||
|
|
||
| class TokenAuthenticator @Inject constructor( | ||
| private val spotTokenDataStore: DataStore<SpotTokenData>, | ||
| private val spotUserIdDataStore: DataStore<SpotUserIdData>, | ||
| private val tokenRefreshService: TokenRefreshService | ||
| ) : Authenticator { | ||
|
|
||
| private val refreshLock = Any() | ||
| private object RefreshAttempted | ||
|
|
||
| override fun authenticate(route: Route?, response: Response): Request? { | ||
| val refreshAttempted = response.request.tag(RefreshAttempted::class.java) != null | ||
| if (refreshAttempted) { | ||
| clearAuthData() | ||
| return null | ||
| } | ||
| if (responseCount(response) >= 2) return null | ||
| if (response.request.url.encodedPath.endsWith("/api/auth/reissue")) return null | ||
|
|
||
| val requestAccessToken = response.request.header("Authorization") | ||
| ?.removePrefix("Bearer ") | ||
| .orEmpty() | ||
| val latestAccessToken = runBlocking { | ||
| spotTokenDataStore.data.first().accessToken | ||
| } | ||
|
|
||
| if (latestAccessToken.isNotBlank() && latestAccessToken != requestAccessToken) { | ||
| return response.request.newBuilder() | ||
| .header("Authorization", "Bearer $latestAccessToken") | ||
| .tag(RefreshAttempted::class.java, RefreshAttempted) | ||
| .build() | ||
| } | ||
|
|
||
| synchronized(refreshLock) { | ||
| val tokenData = runBlocking { spotTokenDataStore.data.first() } | ||
| if (tokenData.accessToken.isNotBlank() && tokenData.accessToken != requestAccessToken) { | ||
| return response.request.newBuilder() | ||
| .header("Authorization", "Bearer ${tokenData.accessToken}") | ||
| .tag(RefreshAttempted::class.java, RefreshAttempted) | ||
| .build() | ||
| } | ||
|
|
||
| val refreshToken = tokenData.refreshToken | ||
| if (refreshToken.isBlank()) { | ||
| clearAuthData() | ||
| return null | ||
| } | ||
|
|
||
| val refreshResponse = runBlocking { | ||
| try { | ||
| tokenRefreshService.refreshTokenData(refreshToken) | ||
| } catch (e: Exception) { | ||
| clearAuthData() | ||
| null | ||
| } | ||
| } | ||
| if (refreshResponse == null) return null | ||
|
|
||
| if (!refreshResponse.isSuccess) { | ||
| clearAuthData() | ||
| return null | ||
| } | ||
|
|
||
| val result = refreshResponse.result | ||
| runBlocking { | ||
| spotTokenDataStore.updateData { current -> | ||
| current.copy( | ||
| accessToken = result.accessToken, | ||
| refreshToken = result.refreshToken | ||
| ) | ||
| } | ||
| spotUserIdDataStore.updateData { current -> | ||
| current.copy(userId = result.userId) | ||
| } | ||
| } | ||
|
|
||
| return response.request.newBuilder() | ||
| .header("Authorization", "Bearer ${result.accessToken}") | ||
| .tag(RefreshAttempted::class.java, RefreshAttempted) | ||
| .build() | ||
| } | ||
| } | ||
|
|
||
| private fun clearAuthData() { | ||
| runBlocking { | ||
| spotTokenDataStore.updateData { current -> | ||
| current.copy(accessToken = "", refreshToken = "") | ||
| } | ||
| spotUserIdDataStore.updateData { current -> | ||
| current.copy(userId = "") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun responseCount(response: Response): Int { | ||
| var count = 1 | ||
| var prior = response.priorResponse | ||
| while (prior != null) { | ||
| count++ | ||
| prior = prior.priorResponse | ||
| } | ||
| return count | ||
| } | ||
| } | ||
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
14 changes: 14 additions & 0 deletions
14
core/network/src/main/java/com/umcspot/spot/network/model/TokenRefreshResponse.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,14 @@ | ||
| package com.umcspot.spot.network.model | ||
|
|
||
| import kotlinx.serialization.SerialName | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class TokenRefreshResponse( | ||
| @SerialName("id") | ||
| val userId: String, | ||
| @SerialName("accessToken") | ||
| val accessToken: String, | ||
| @SerialName("refreshToken") | ||
| val refreshToken: String | ||
| ) |
15 changes: 13 additions & 2 deletions
15
core/network/src/main/java/com/umcspot/spot/network/service/TokenRefreshService.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,4 +1,15 @@ | ||
| package network.service | ||
| package com.umcspot.spot.network.service | ||
|
|
||
| class TokenRefreshService { | ||
|
|
||
| import com.umcspot.spot.network.model.BaseResponse | ||
| import com.umcspot.spot.network.model.TokenRefreshResponse | ||
| import retrofit2.http.Header | ||
| import retrofit2.http.POST | ||
|
|
||
| interface TokenRefreshService { | ||
|
|
||
| @POST("/api/auth/reissue") | ||
| suspend fun refreshTokenData( | ||
| @Header("refreshToken") refreshToken: String, | ||
| ): BaseResponse<TokenRefreshResponse> | ||
| } |
7 changes: 5 additions & 2 deletions
7
data/login/src/main/java/com/umcspot/spot/login/datasource/LoginDataSource.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,10 +1,13 @@ | ||
| package com.umcspot.spot.login.datasource | ||
|
|
||
| import com.umcspot.spot.login.dto.response.TokenResponseDto | ||
| import com.umcspot.spot.model.SocialLoginType | ||
| import com.umcspot.spot.network.model.BaseResponse | ||
| import com.umcspot.spot.network.model.NullResultResponse | ||
|
|
||
| interface LoginDataSource { | ||
| suspend fun finishSocialLogin(type : String, accessToken : String): BaseResponse<TokenResponseDto> | ||
| suspend fun getCallBackToken(type : String, accessToken : String): BaseResponse<TokenResponseDto> | ||
|
|
||
| suspend fun refreshTokenData(refreshToken : String) : BaseResponse<TokenResponseDto> | ||
|
|
||
| suspend fun spotLogout(): NullResultResponse | ||
| } |
10 changes: 8 additions & 2 deletions
10
data/login/src/main/java/com/umcspot/spot/login/datasourceimpl/LoginDataSourceImpl.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,19 +1,25 @@ | ||
| package com.umcspot.spot.login.datasourceimpl | ||
|
|
||
| import androidx.datastore.core.DataStore | ||
| import com.umcspot.spot.login.datasource.LoginDataSource | ||
| import com.umcspot.spot.login.dto.response.TokenResponseDto | ||
| import com.umcspot.spot.login.service.LoginService | ||
| import com.umcspot.spot.network.model.BaseResponse | ||
| import com.umcspot.spot.network.model.NullResultResponse | ||
| import javax.inject.Inject | ||
|
|
||
| class LoginDataSourceImpl @Inject constructor( | ||
| private val loginService: LoginService | ||
| ) : LoginDataSource { | ||
|
|
||
| override suspend fun finishSocialLogin( | ||
| override suspend fun getCallBackToken( | ||
| type: String, | ||
| accessToken: String | ||
| ): BaseResponse<TokenResponseDto> = | ||
| loginService.getCallBackToken(type, accessToken) | ||
|
|
||
| override suspend fun refreshTokenData(refreshToken: String): BaseResponse<TokenResponseDto> = | ||
| loginService.refreshTokenData(refreshToken) | ||
|
|
||
| override suspend fun spotLogout() : NullResultResponse = | ||
| loginService.spotLogout() | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
예외가 삼켜지고 있어 디버깅이 어려움
토큰 갱신 실패 시 예외가 로깅 없이 삼켜지고 있습니다. 프로덕션 환경에서 토큰 갱신 문제를 진단하기 어려워질 수 있습니다.
🛠️ 로깅 추가 제안
val refreshResponse = runBlocking { try { tokenRefreshService.refreshTokenData(refreshToken) } catch (e: Exception) { + android.util.Log.e("TokenAuthenticator", "Token refresh failed", e) clearAuthData() null } }📝 Committable suggestion
🧰 Tools
🪛 detekt (1.23.8)
[warning] 65-65: The caught exception is swallowed. The original exception could be lost.
(detekt.exceptions.SwallowedException)
🤖 Prompt for AI Agents