-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat/#55] token interceptor + accessToken 재발급 + token 컨트롤
- Loading branch information
Showing
21 changed files
with
204 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.going.doorip.di | ||
|
||
import android.content.Context | ||
import com.going.data.dto.BaseResponse | ||
import com.going.data.local.GoingDataStore | ||
import com.going.domain.entity.response.AuthTokenModel | ||
import com.going.doorip.BuildConfig.BASE_URL | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.Request | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import okhttp3.Response | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class AuthInterceptor @Inject constructor( | ||
private val json: Json, | ||
private val dataStore: GoingDataStore, | ||
@ApplicationContext private val context: Context, | ||
) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val originalRequest = chain.request() | ||
|
||
Timber.d("GET ACCESS TOKEN : ${dataStore.accessToken}") | ||
|
||
val authRequest = if (dataStore.accessToken.isNotBlank()) { | ||
originalRequest.newAuthBuilder().build() | ||
} else { | ||
originalRequest | ||
} | ||
val response = chain.proceed(authRequest) | ||
|
||
when (response.code) { | ||
CODE_TOKEN_EXPIRED -> { | ||
try { | ||
val refreshTokenRequest = originalRequest.newBuilder().post("".toRequestBody()) | ||
.url("$BASE_URL/api/users/reissue") | ||
.addHeader(AUTHORIZATION, dataStore.refreshToken) | ||
.build() | ||
val refreshTokenResponse = chain.proceed(refreshTokenRequest) | ||
Timber.d("GET REFRESH TOKEN : $refreshTokenResponse") | ||
|
||
if (refreshTokenResponse.isSuccessful) { | ||
val responseToken = json.decodeFromString( | ||
refreshTokenResponse.body?.string().toString(), | ||
) as BaseResponse<AuthTokenModel> | ||
|
||
with(dataStore) { | ||
accessToken = responseToken.data.accessToken | ||
refreshToken = responseToken.data.refreshToken | ||
} | ||
refreshTokenResponse.close() | ||
val newRequest = originalRequest.newAuthBuilder().build() | ||
return chain.proceed(newRequest) | ||
} | ||
} catch (t: Throwable) { | ||
Timber.e(t) | ||
} | ||
} | ||
} | ||
return response | ||
} | ||
|
||
private fun Request.newAuthBuilder() = | ||
this.newBuilder().addHeader(AUTHORIZATION, "Bearer ${dataStore.accessToken}") | ||
|
||
companion object { | ||
private const val CODE_TOKEN_EXPIRED = 401 | ||
private const val AUTHORIZATION = "Authorization" | ||
} | ||
} |
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
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
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
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/going/doorip/di/qualifier/RetrofitQualifier.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,7 @@ | ||
package com.going.doorip.di.qualifier | ||
|
||
import javax.inject.Qualifier | ||
|
||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class JWT |
7 changes: 7 additions & 0 deletions
7
data/src/main/java/com/going/data/datasource/SettingDataSource.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,7 @@ | ||
package com.going.data.datasource | ||
|
||
import com.going.data.dto.response.SignOutResponseDto | ||
|
||
interface SettingDataSource { | ||
suspend fun patchSignOut(): SignOutResponseDto | ||
} |
12 changes: 12 additions & 0 deletions
12
data/src/main/java/com/going/data/datasourceImpl/SettingDataSourceImpl.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,12 @@ | ||
package com.going.data.datasourceImpl | ||
|
||
import com.going.data.datasource.SettingDataSource | ||
import com.going.data.dto.response.SignOutResponseDto | ||
import com.going.data.service.SettingService | ||
import javax.inject.Inject | ||
|
||
class SettingDataSourceImpl @Inject constructor( | ||
private val settingService: SettingService, | ||
) : SettingDataSource { | ||
override suspend fun patchSignOut(): SignOutResponseDto = settingService.patchSignOut() | ||
} |
3 changes: 1 addition & 2 deletions
3
data/src/main/java/com/going/data/dto/request/SignInRequestDto.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
16 changes: 16 additions & 0 deletions
16
data/src/main/java/com/going/data/dto/response/SignOutResponseDto.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,16 @@ | ||
package com.going.data.dto.response | ||
|
||
import com.going.domain.entity.response.SignOutModel | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class SignOutResponseDto( | ||
@SerialName("status") | ||
val status: Int, | ||
@SerialName("message") | ||
val message: String, | ||
) { | ||
fun toSignOutModel() = | ||
SignOutModel(status, message) | ||
} |
14 changes: 14 additions & 0 deletions
14
data/src/main/java/com/going/data/repositoryImpl/SettingRepositoryImpl.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,14 @@ | ||
package com.going.data.repositoryImpl | ||
|
||
import com.going.data.datasource.SettingDataSource | ||
import com.going.domain.entity.response.SignOutModel | ||
import com.going.domain.repository.SettingRepository | ||
import javax.inject.Inject | ||
|
||
class SettingRepositoryImpl @Inject constructor( | ||
private val settingDataSource: SettingDataSource, | ||
) : SettingRepository { | ||
override suspend fun patchSignOut(): Result<SignOutModel> = runCatching { | ||
settingDataSource.patchSignOut().toSignOutModel() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.going.data.service | ||
|
||
import com.going.data.dto.response.SignOutResponseDto | ||
import retrofit2.http.PATCH | ||
|
||
interface SettingService { | ||
@PATCH("api/users/signout") | ||
suspend fun patchSignOut(): SignOutResponseDto | ||
} |
6 changes: 6 additions & 0 deletions
6
domain/src/main/kotlin/com/going/domain/entity/response/SignOutModel.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,6 @@ | ||
package com.going.domain.entity.response | ||
|
||
data class SignOutModel( | ||
val status: Int, | ||
val message: String, | ||
) |
7 changes: 7 additions & 0 deletions
7
domain/src/main/kotlin/com/going/domain/repository/SettingRepository.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,7 @@ | ||
package com.going.domain.repository | ||
|
||
import com.going.domain.entity.response.SignOutModel | ||
|
||
interface SettingRepository { | ||
suspend fun patchSignOut(): Result<SignOutModel> | ||
} |
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
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
7 changes: 6 additions & 1 deletion
7
presentation/src/main/java/com/going/presentation/util/JsonExt.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,17 @@ | ||
package com.going.presentation.util | ||
|
||
import android.util.Log | ||
import org.json.JSONObject | ||
import retrofit2.HttpException | ||
|
||
fun toErrorCode(throwable: Throwable): String = if (throwable is HttpException) { | ||
val jsonTemp = throwable.response()?.errorBody()?.byteString().toString() | ||
val json = jsonTemp.slice(6 until jsonTemp.length) | ||
Log.e("TAG", "toErrorCode: $jsonTemp", ) | ||
val json = jsonTemp.slice(6 until jsonTemp.length - 2) + "}" | ||
Log.e("TAG", "toErrorCode: $json", ) | ||
JSONObject(json).getString("code") | ||
} else { | ||
"NOT_HTTP" | ||
} | ||
|
||
// 기능 오류 발견!!!! 반드시 수정 필요!!!!!! |
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