-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT/#96] ํ ํฐ ์ฌ๋ฐ๊ธ / ์๋ฒํต์ ๊ตฌํ
- Loading branch information
Showing
16 changed files
with
243 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.terning.point.di | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Handler | ||
import android.os.Looper | ||
import com.jakewharton.processphoenix.ProcessPhoenix | ||
import com.terning.core.extension.stringToast | ||
import com.terning.data.local.TerningDataStore | ||
import com.terning.domain.repository.TokenReissueRepository | ||
import com.terning.feature.main.MainActivity | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Interceptor | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class AuthInterceptor @Inject constructor( | ||
private val tokenReissueRepository: TokenReissueRepository, | ||
private val terningDataStore: TerningDataStore, | ||
@ApplicationContext private val context: Context | ||
) : Interceptor { | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val originalRequest = chain.request() | ||
|
||
Timber.d("GET ACCESS TOKEN : ${terningDataStore.accessToken}") | ||
|
||
val authRequest = if (terningDataStore.accessToken.isNotBlank()) { | ||
originalRequest.newBuilder().newAuthBuilder().build() | ||
} else { | ||
originalRequest | ||
} | ||
|
||
val response = chain.proceed(authRequest) | ||
|
||
when (response.code) { | ||
CODE_TOKEN_EXPIRED -> { | ||
try { | ||
runBlocking { | ||
tokenReissueRepository.postReissueToken( | ||
terningDataStore.refreshToken | ||
) | ||
}.onSuccess { data -> | ||
terningDataStore.apply { | ||
refreshToken = data.refreshToken | ||
} | ||
|
||
response.close() | ||
|
||
val newRequest = | ||
authRequest.newBuilder().removeHeader(AUTHORIZATION).newAuthBuilder() | ||
.build() | ||
|
||
return chain.proceed(newRequest) | ||
} | ||
} catch (t: Throwable) { | ||
Timber.d(t.message) | ||
} | ||
|
||
terningDataStore.clearInfo() | ||
|
||
Handler(Looper.getMainLooper()).post { | ||
context.stringToast(TOKEN_EXPIRED_ERROR) | ||
Handler(Looper.getMainLooper()).post { | ||
ProcessPhoenix.triggerRebirth( | ||
context, | ||
Intent(context, MainActivity::class.java) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
return response | ||
} | ||
|
||
private fun Request.Builder.newAuthBuilder() = | ||
this.addHeader(AUTHORIZATION, "$BEARER ${terningDataStore.accessToken}") | ||
|
||
companion object { | ||
private const val CODE_TOKEN_EXPIRED = 401 | ||
private const val TOKEN_EXPIRED_ERROR = "ํ ํฐ์ด ๋ง๋ฃ๋์์ด์\n๋ค์ ๋ก๊ทธ์ธ ํด์ฃผ์ธ์" | ||
private const val BEARER = "Bearer" | ||
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
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
11 changes: 11 additions & 0 deletions
11
data/src/main/java/com/terning/data/datasource/TokenReissueDataSource.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,11 @@ | ||
package com.terning.data.datasource | ||
|
||
import com.terning.data.dto.BaseResponse | ||
import com.terning.data.dto.response.TokenReissueResponseDto | ||
import com.terning.domain.entity.response.TokenReissueResponseModel | ||
|
||
interface TokenReissueDataSource { | ||
suspend fun postReissueToken( | ||
authorization: String, | ||
): BaseResponse<TokenReissueResponseDto> | ||
} |
16 changes: 16 additions & 0 deletions
16
data/src/main/java/com/terning/data/datasourceimpl/TokenReissueDataSourceImpl.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.terning.data.datasourceimpl | ||
|
||
import com.terning.data.datasource.TokenReissueDataSource | ||
import com.terning.data.dto.BaseResponse | ||
import com.terning.data.dto.response.TokenReissueResponseDto | ||
import com.terning.data.service.TokenReissueService | ||
import javax.inject.Inject | ||
|
||
class TokenReissueDataSourceImpl @Inject constructor( | ||
private val tokenReissueService: TokenReissueService | ||
) : TokenReissueDataSource { | ||
override suspend fun postReissueToken( | ||
authorization: String | ||
): BaseResponse<TokenReissueResponseDto> = | ||
tokenReissueService.postReissueToken(authorization) | ||
} |
13 changes: 13 additions & 0 deletions
13
data/src/main/java/com/terning/data/dto/response/TokenReissueResponseDto.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,13 @@ | ||
package com.terning.data.dto.response | ||
|
||
import com.terning.domain.entity.response.TokenReissueResponseModel | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class TokenReissueResponseDto( | ||
@SerialName("refreshToken") | ||
val refreshToken: String | ||
) { | ||
fun toTokenReissueResponseModel() = TokenReissueResponseModel(refreshToken = refreshToken) | ||
} |
19 changes: 19 additions & 0 deletions
19
data/src/main/java/com/terning/data/repositoryimpl/TokenReissueRepositoryImpl.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,19 @@ | ||
package com.terning.data.repositoryimpl | ||
|
||
import com.terning.data.datasource.TokenReissueDataSource | ||
import com.terning.domain.entity.response.TokenReissueResponseModel | ||
import com.terning.domain.repository.TokenReissueRepository | ||
import javax.inject.Inject | ||
|
||
class TokenReissueRepositoryImpl @Inject constructor( | ||
private val tokenReissueDataSource: TokenReissueDataSource | ||
) : TokenReissueRepository { | ||
override suspend fun postReissueToken( | ||
authorization: String | ||
): Result<TokenReissueResponseModel> = | ||
runCatching { | ||
tokenReissueDataSource.postReissueToken( | ||
authorization = authorization | ||
).result.toTokenReissueResponseModel() | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
data/src/main/java/com/terning/data/service/TokenReissueService.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,13 @@ | ||
package com.terning.data.service | ||
|
||
import com.terning.data.dto.BaseResponse | ||
import com.terning.data.dto.response.TokenReissueResponseDto | ||
import retrofit2.http.Header | ||
import retrofit2.http.POST | ||
|
||
interface TokenReissueService { | ||
@POST("/api/v1/auth/token-reissue") | ||
suspend fun postReissueToken( | ||
@Header("Authorization") authorization: String, | ||
): BaseResponse<TokenReissueResponseDto> | ||
} |
5 changes: 5 additions & 0 deletions
5
domain/src/main/java/com/terning/domain/entity/response/TokenReissueResponseModel.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.terning.domain.entity.response | ||
|
||
data class TokenReissueResponseModel ( | ||
val refreshToken : String | ||
) |
9 changes: 9 additions & 0 deletions
9
domain/src/main/java/com/terning/domain/repository/TokenReissueRepository.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,9 @@ | ||
package com.terning.domain.repository | ||
|
||
import com.terning.domain.entity.response.TokenReissueResponseModel | ||
|
||
interface TokenReissueRepository { | ||
suspend fun postReissueToken( | ||
authorization: String, | ||
): Result<TokenReissueResponseModel> | ||
} |
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