Skip to content

Commit

Permalink
Merge pull request #115 from THT-Team/feature/TOP-90_refresh_token
Browse files Browse the repository at this point in the history
TOP-90 feature Http Code가 500일 때 error 필드값에 따라서 토큰 만료, Internal Server Errro 구분
  • Loading branch information
cwj-c authored Jan 13, 2024
2 parents 8057793 + cde965f commit d567cfe
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.tht.tht.data.di.retrofit

import com.google.gson.Gson
import com.tht.tht.data.BuildConfig
import com.tht.tht.data.remote.retrofit.ThtHeaderInterceptor
import com.tht.tht.data.remote.retrofit.TokenRefreshAuthenticator
import com.tht.tht.data.remote.retrofit.header.HttpHeaderKey
import com.tht.tht.domain.token.model.TokenException
import com.tht.tht.domain.token.token.FetchThtAccessTokenUseCase
import com.tht.tht.domain.token.token.RefreshThtAccessTokenUseCase
import dagger.Lazy
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.logging.HttpLoggingInterceptor

Expand All @@ -21,23 +20,9 @@ object OkHttpInterceptorModule {

@Provides
fun provideHeaderInterceptor(
gson: Gson,
fetchThtAccessTokenUseCase: Lazy<FetchThtAccessTokenUseCase>
): Interceptor = Interceptor { chain ->
val requestBuilder = chain.request().newBuilder()
.header(HttpHeaderKey.CONTENT_TYPE_HEADER_KEY, HttpHeaderKey.CONTENT_TYPE_HEADER_VALUE)
val accessToken = runBlocking { fetchThtAccessTokenUseCase.get().invoke().getOrNull() }
if (accessToken != null) {
requestBuilder.header(
HttpHeaderKey.AUTHORIZATION_HEADER_KEY,
"${HttpHeaderKey.BEARER_PREFIX} $accessToken"
)
}
chain.proceed(requestBuilder.build()).also { response ->
when (response.code) {
500 -> throw TokenException.RefreshTokenExpiredException()
}
}
}
): Interceptor = ThtHeaderInterceptor(gson, fetchThtAccessTokenUseCase)

@Provides
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.tht.tht.data.remote.retrofit

import com.google.gson.Gson
import com.tht.tht.data.remote.response.base.ErrorResponse
import com.tht.tht.data.remote.retrofit.header.HttpHeaderKey
import com.tht.tht.domain.token.model.TokenException
import com.tht.tht.domain.token.token.FetchThtAccessTokenUseCase
import dagger.Lazy
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody
import javax.inject.Inject

class ThtHeaderInterceptor @Inject constructor(
private val gson: Gson,
private val fetchThtAccessTokenUseCase: Lazy<FetchThtAccessTokenUseCase>
) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder()
.header(HttpHeaderKey.CONTENT_TYPE_HEADER_KEY, HttpHeaderKey.CONTENT_TYPE_HEADER_VALUE)
val accessToken = runBlocking { fetchThtAccessTokenUseCase.get().invoke().getOrNull() }
if (accessToken != null) {
requestBuilder.header(
HttpHeaderKey.AUTHORIZATION_HEADER_KEY,
"${HttpHeaderKey.BEARER_PREFIX} $accessToken"
)
}

val response = chain.proceed(requestBuilder.build())
return response.body?.string()?.let {
checkTokenExpiredException(response, parseErrorResponse(it))
//response 는 한 번 밖에 읽지 못함
response.newBuilder()
.body(ResponseBody.create(response.body!!.contentType(), it))
.build()
} ?: kotlin.run {
checkTokenExpiredException(response, null)
response
}
}

private fun parseErrorResponse(errorJson: String): ErrorResponse? {
return try {
gson.fromJson(errorJson, ErrorResponse::class.java)
} catch (e: Exception) {
e.printStackTrace()
null
}
}

private fun checkTokenExpiredException(
response: Response,
errorResponse: ErrorResponse?
) {
when {
errorResponse?.error == "refresh_token_expired" &&
response.code == 500 -> {
throw TokenException.RefreshTokenExpiredException()
}
}
}
}

0 comments on commit d567cfe

Please sign in to comment.