From 7f616094408c3b3104b5e5f75ff16ff702bc42da Mon Sep 17 00:00:00 2001 From: 2hyunjinn Date: Wed, 14 Aug 2024 15:58:31 +0900 Subject: [PATCH 1/4] [chore] #175 BEARER --- .../dateroad/data/dataremote/interceptor/AuthInterceptor.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt b/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt index b0c9db514..fb989c513 100644 --- a/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt +++ b/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt @@ -50,8 +50,8 @@ class AuthInterceptor @Inject constructor( ) with(localStorage) { - accessToken = "$BEARER${responseRefresh.accessToken}" - refreshToken = "$BEARER${responseRefresh.refreshToken}" + accessToken = BEARER + responseRefresh.accessToken + refreshToken = BEARER + responseRefresh.refreshToken } refreshTokenResponse.close() @@ -80,8 +80,6 @@ class AuthInterceptor @Inject constructor( companion object { const val CODE_TOKEN_EXPIRE = 401 - const val CONTENT_TYPE = "Content-Type" - const val APPLICATION_JSON = "application/json" const val AUTHORIZATION = "Authorization" const val BEARER = "Bearer " } From e89b71ac71a86027f222eea49adcb72a5150978b Mon Sep 17 00:00:00 2001 From: 2hyunjinn Date: Wed, 14 Aug 2024 17:41:03 +0900 Subject: [PATCH 2/4] =?UTF-8?q?[chore]=20#175=20reissue=20API=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=20=ED=98=B8=EC=B6=9C=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dataremote/interceptor/AuthInterceptor.kt | 71 +++++++++++-------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt b/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt index fb989c513..755633e48 100644 --- a/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt +++ b/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt @@ -3,6 +3,7 @@ package org.sopt.dateroad.data.dataremote.interceptor import android.app.Application import android.content.Intent import java.lang.IllegalStateException +import java.util.concurrent.atomic.AtomicBoolean import javax.inject.Inject import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -26,52 +27,60 @@ class AuthInterceptor @Inject constructor( private val context: Application ) : Interceptor { + private val isRefreshing = AtomicBoolean(false) + override fun intercept(chain: Interceptor.Chain): Response { val originalRequest = chain.request() val authRequest = if (localStorage.accessToken.isNotBlank()) originalRequest.newAuthBuilder() else originalRequest - val response = chain.proceed(authRequest) + var response = chain.proceed(authRequest) - when (response.code) { - CODE_TOKEN_EXPIRE -> { - response.close() - val refreshTokenRequest = originalRequest.newBuilder().get() - .url("${BuildConfig.BASE_URL}$API/$VERSION/$USERS/$REISSUE") - .patch("".toRequestBody(null)) - .addHeader(AUTHORIZATION, localStorage.refreshToken) - .build() - val refreshTokenResponse = chain.proceed(refreshTokenRequest) + if (response.code == CODE_TOKEN_EXPIRE) { + response.close() - if (refreshTokenResponse.isSuccessful) { - val responseRefresh = - json.decodeFromString( - refreshTokenResponse.body?.string() - ?: throw IllegalStateException("\"refreshTokenResponse is null $refreshTokenResponse\"") - ) + if (isRefreshing.compareAndSet(false, true)) { + try { + val refreshTokenRequest = originalRequest.newBuilder().get() + .url("${BuildConfig.BASE_URL}$API/$VERSION/$USERS/$REISSUE") + .patch("".toRequestBody(null)) + .addHeader(AUTHORIZATION, localStorage.refreshToken) + .build() + val refreshTokenResponse = chain.proceed(refreshTokenRequest) - with(localStorage) { - accessToken = BEARER + responseRefresh.accessToken - refreshToken = BEARER + responseRefresh.refreshToken - } + if (refreshTokenResponse.isSuccessful) { + val responseRefresh = + json.decodeFromString( + refreshTokenResponse.body?.string() + ?: throw IllegalStateException("\"refreshTokenResponse is null $refreshTokenResponse\"") + ) - refreshTokenResponse.close() + with(localStorage) { + accessToken = BEARER + responseRefresh.accessToken + refreshToken = BEARER + responseRefresh.refreshToken + } - val newRequest = originalRequest.newAuthBuilder() - return chain.proceed(newRequest) - } else { - with(context) { - CoroutineScope(Dispatchers.Main).launch { - startActivity( - Intent.makeRestartActivityTask( - packageManager.getLaunchIntentForPackage(packageName)?.component + refreshTokenResponse.close() + } else { + with(context) { + CoroutineScope(Dispatchers.Main).launch { + startActivity( + Intent.makeRestartActivityTask( + packageManager.getLaunchIntentForPackage(packageName)?.component + ) ) - ) - localStorage.clear() + localStorage.clear() + } } } + } finally { + isRefreshing.set(false) } } + + val newRequest = originalRequest.newAuthBuilder() + return chain.proceed(newRequest) } + return response } From e82b8ebad587baf4d4fa4a5aaf41301cc30b4799 Mon Sep 17 00:00:00 2001 From: 2hyunjinn Date: Sat, 17 Aug 2024 18:06:09 +0900 Subject: [PATCH 3/4] =?UTF-8?q?[chore]=20#175=20reissue=20API=20=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=20=ED=98=B8=EC=B6=9C=20=EB=AC=B8=EC=A0=9C=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dataremote/interceptor/AuthInterceptor.kt | 69 +++++++++---------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt b/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt index 755633e48..6f24b60c9 100644 --- a/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt +++ b/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt @@ -26,59 +26,56 @@ class AuthInterceptor @Inject constructor( private val localStorage: UserInfoLocalDataSource, private val context: Application ) : Interceptor { - private val isRefreshing = AtomicBoolean(false) override fun intercept(chain: Interceptor.Chain): Response { val originalRequest = chain.request() - val authRequest = - if (localStorage.accessToken.isNotBlank()) originalRequest.newAuthBuilder() else originalRequest - var response = chain.proceed(authRequest) + val authRequest = if (localStorage.accessToken.isNotBlank()) originalRequest.newAuthBuilder() else originalRequest + val response = chain.proceed(authRequest) + + when (response.code) { + CODE_TOKEN_EXPIRE -> { + response.close() - if (response.code == CODE_TOKEN_EXPIRE) { - response.close() + if (isRefreshing.compareAndSet(false, true)) { + try { + val refreshTokenRequest = originalRequest.newBuilder() + .patch("".toRequestBody(null)) + .url("${BuildConfig.BASE_URL}$API/$VERSION/$USERS/$REISSUE") + .addHeader(AUTHORIZATION, localStorage.refreshToken) + .build() - if (isRefreshing.compareAndSet(false, true)) { - try { - val refreshTokenRequest = originalRequest.newBuilder().get() - .url("${BuildConfig.BASE_URL}$API/$VERSION/$USERS/$REISSUE") - .patch("".toRequestBody(null)) - .addHeader(AUTHORIZATION, localStorage.refreshToken) - .build() - val refreshTokenResponse = chain.proceed(refreshTokenRequest) + val refreshTokenResponse = chain.proceed(refreshTokenRequest) - if (refreshTokenResponse.isSuccessful) { - val responseRefresh = - json.decodeFromString( + if (refreshTokenResponse.isSuccessful) { + val responseRefresh = json.decodeFromString( refreshTokenResponse.body?.string() ?: throw IllegalStateException("\"refreshTokenResponse is null $refreshTokenResponse\"") ) - with(localStorage) { - accessToken = BEARER + responseRefresh.accessToken - refreshToken = BEARER + responseRefresh.refreshToken - } - - refreshTokenResponse.close() - } else { - with(context) { - CoroutineScope(Dispatchers.Main).launch { - startActivity( - Intent.makeRestartActivityTask( - packageManager.getLaunchIntentForPackage(packageName)?.component + with(localStorage) { + accessToken = BEARER + responseRefresh.accessToken + refreshToken = responseRefresh.refreshToken + } + val newRequest = originalRequest.newAuthBuilder() + return chain.proceed(newRequest) + } else { + with(context) { + CoroutineScope(Dispatchers.Main).launch { + startActivity( + Intent.makeRestartActivityTask( + packageManager.getLaunchIntentForPackage(packageName)?.component + ) ) - ) - localStorage.clear() + localStorage.clear() + } } } + } finally { + isRefreshing.set(false) } - } finally { - isRefreshing.set(false) } } - - val newRequest = originalRequest.newAuthBuilder() - return chain.proceed(newRequest) } return response From 52d5456aa2b1d2b32a4c727cef834a64ee2147cd Mon Sep 17 00:00:00 2001 From: 2hyunjinn Date: Sun, 18 Aug 2024 01:38:29 +0900 Subject: [PATCH 4/4] =?UTF-8?q?[chore]=20#175=20=EC=9D=91=EB=8B=B5=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20=EB=8B=AB=EC=95=84=EC=A3=BC=EB=8A=94=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dateroad/data/dataremote/interceptor/AuthInterceptor.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt b/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt index 6f24b60c9..ae8a5702a 100644 --- a/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt +++ b/app/src/main/java/org/sopt/dateroad/data/dataremote/interceptor/AuthInterceptor.kt @@ -57,6 +57,8 @@ class AuthInterceptor @Inject constructor( accessToken = BEARER + responseRefresh.accessToken refreshToken = responseRefresh.refreshToken } + refreshTokenResponse.close() + val newRequest = originalRequest.newAuthBuilder() return chain.proceed(newRequest) } else {