-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from yy0ung/feature/aos-data
[Global/aos] Bearer Interceptor 구현
- Loading branch information
Showing
5 changed files
with
97 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 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
112 changes: 69 additions & 43 deletions
112
Aos/app/src/main/java/com/avengers/nibobnebob/config/BearerInterceptor.kt
This file contains 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,62 +1,88 @@ | ||
package com.avengers.nibobnebob.config | ||
|
||
|
||
import com.avengers.nibobnebob.app.DataStoreManager | ||
import com.avengers.nibobnebob.data.model.BaseState | ||
import com.avengers.nibobnebob.data.model.request.RefreshTokenRequest | ||
import com.avengers.nibobnebob.data.model.response.BaseResponse | ||
import com.avengers.nibobnebob.data.model.response.NaverLoginResponse | ||
import com.avengers.nibobnebob.data.model.runRemote | ||
import com.avengers.nibobnebob.data.remote.RefreshApi | ||
import com.avengers.nibobnebob.presentation.util.Constants | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Interceptor | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Response | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import java.io.IOException | ||
import javax.inject.Inject | ||
|
||
class BearerInterceptor @Inject constructor() : Interceptor { | ||
class BearerInterceptor @Inject constructor( | ||
private val dataStoreManager: DataStoreManager | ||
) : Interceptor { | ||
|
||
@Throws(IOException::class) | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
|
||
val originalRequest = chain.request() | ||
val response = chain.proceed(originalRequest) | ||
|
||
// runBlocking { | ||
// 로컬에 refreshToken이 있다면 | ||
// sharedPreferences.getString(X_REFRESH_TOKEN, null)?.let { refresh -> | ||
// Log.d(TAG, refresh) | ||
// // refresh API 호출 | ||
// val result = Retrofit.Builder() | ||
// .baseUrl(BASE_DEV_URL) | ||
// .addConverterFactory(GsonConverterFactory.create()) | ||
// .build() | ||
// .create(RefreshAPI::class.java).refreshToken(refresh) | ||
// | ||
// if (result.isSuccessful) { | ||
// Log.d(TAG,"리프래시 성공") | ||
// result.body()?.let { body -> | ||
// Log.d(TAG,body.accessToken) | ||
// // refresh 성공시 로컬에 저장 | ||
// sharedPreferences.edit() | ||
// .putString(X_ACCESS_TOKEN, body.accessToken) | ||
// .putString(X_REFRESH_TOKEN, body.refreshToken) | ||
// .apply() | ||
// | ||
// isRefreshed = true | ||
// accessToken = body.accessToken | ||
// } | ||
// }else{ | ||
// val error = | ||
// Gson().fromJson(result.errorBody()?.string(), ErrorResponse::class.java) | ||
// Log.d(TAG,error.message) | ||
// } | ||
// } | ||
// } | ||
// | ||
// if (isRefreshed) { | ||
// | ||
// // 기존 API 재호출 | ||
// val newRequest = originalRequest.newBuilder() | ||
// .addHeader("Authorization", accessToken) | ||
// .build() | ||
// | ||
// return chain.proceed(newRequest) | ||
// } | ||
// 해당 특정 에러코드가 그대로 내려간다면, 세션 만료 처리 | ||
var newAccessToken: String? = null | ||
|
||
if (response.code == TOKEN_ERROR) { | ||
runBlocking { | ||
val refreshToken = dataStoreManager.getRefreshToken().first() | ||
refreshToken?.let { token -> | ||
when (val result = getNewAccessToken(token)) { | ||
is BaseState.Success -> { | ||
response.close() | ||
newAccessToken = result.data.body.accessToken | ||
newAccessToken?.let { | ||
dataStoreManager.putAccessToken(newAccessToken!!) | ||
} | ||
} | ||
|
||
else -> { | ||
dataStoreManager.deleteAccessToken() | ||
dataStoreManager.deleteRefreshToken() | ||
} | ||
} | ||
} | ||
} | ||
(newAccessToken)?.let { | ||
val newRequest = originalRequest.newBuilder() | ||
.addHeader(AUTHORIZATION, "$BEARER $newAccessToken") | ||
.build() | ||
return chain.proceed(newRequest) | ||
} | ||
} | ||
|
||
return response | ||
} | ||
|
||
|
||
private suspend fun getNewAccessToken(refreshToken: String?): BaseState<BaseResponse<NaverLoginResponse>> { | ||
val loggingInterceptor = HttpLoggingInterceptor() | ||
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | ||
val okHttpClient = OkHttpClient.Builder().addInterceptor(loggingInterceptor).build() | ||
|
||
val retrofit = Retrofit.Builder() | ||
.baseUrl(Constants.BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.client(okHttpClient) | ||
.build() | ||
val api = retrofit.create(RefreshApi::class.java) | ||
return runRemote { api.refreshToken(RefreshTokenRequest(refreshToken)) } | ||
} | ||
|
||
companion object { | ||
const val TOKEN_ERROR = 401 | ||
const val AUTHORIZATION = "Authorization" | ||
const val BEARER = "Bearer" | ||
} | ||
} | ||
|
||
|
5 changes: 5 additions & 0 deletions
5
Aos/app/src/main/java/com/avengers/nibobnebob/data/model/request/RefreshTokenRequest.kt
This file contains 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,5 @@ | ||
package com.avengers.nibobnebob.data.model.request | ||
|
||
data class RefreshTokenRequest( | ||
val refreshToken: String? | ||
) |
6 changes: 4 additions & 2 deletions
6
Aos/app/src/main/java/com/avengers/nibobnebob/data/remote/RefreshApi.kt
This file contains 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,12 +1,14 @@ | ||
package com.avengers.nibobnebob.data.remote | ||
|
||
import com.avengers.nibobnebob.data.model.request.RefreshTokenRequest | ||
import com.avengers.nibobnebob.data.model.response.BaseResponse | ||
import com.avengers.nibobnebob.data.model.response.NaverLoginResponse | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
interface RefreshApi { | ||
|
||
@POST("토큰 갱신 url") | ||
suspend fun refreshToken(refreshToken : String) : Response<BaseResponse<NaverLoginResponse>> | ||
@POST("api/auth/refresh-token") | ||
suspend fun refreshToken(@Body refreshToken: RefreshTokenRequest) : Response<BaseResponse<NaverLoginResponse>> | ||
} |