Skip to content

Commit

Permalink
Merge pull request #27 from GSM-MSG/feature/#23_add_interceptor
Browse files Browse the repository at this point in the history
🔀 :: (#23) - Add Interceptor
  • Loading branch information
wjdcksdn authored Oct 24, 2023
2 parents 1ba790b + ce96723 commit a544aca
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ android {

dependencies {
implementation(project(":core:model"))
implementation(project(":core:datastore"))

implementation(libs.coil.kt)
implementation(libs.coil.kt.svg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.msg.network.di
import android.util.Log
import com.msg.network.BuildConfig
import com.msg.network.api.AuthAPI
import com.msg.network.util.AuthInterceptor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -25,13 +26,15 @@ object NetworkModule {
@Provides
@Singleton
fun provideOkhttpClient(
httpLoggingInterceptor: HttpLoggingInterceptor
httpLoggingInterceptor: HttpLoggingInterceptor,
authInterceptor: AuthInterceptor
): OkHttpClient {
return OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(authInterceptor)
.build()
}

Expand Down
81 changes: 81 additions & 0 deletions core/network/src/main/java/com/msg/network/util/AuthInterceptor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.msg.network.util

import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.msg.datastore.AuthTokenDataSource
import com.msg.network.BuildConfig
import com.msg.network.exception.NeedLoginException
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.LocalDateTime
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import javax.inject.Inject

class AuthInterceptor @Inject constructor(
private val dataSource: AuthTokenDataSource
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val builder = request.newBuilder()
val currentTime = System.currentTimeMillis().toLocalDateTime()
val ignorePath = listOf("/auth")
val ignoreMethod = listOf("POST")
val path = request.url.encodedPath
val method = request.method

ignorePath.forEachIndexed { index, s ->
if (path.contains(s) && ignoreMethod[index] == method) {
return chain.proceed(request)
}
}

runBlocking {
val refreshTime = dataSource.getRefreshTokenExp().toString()
val accessTime = dataSource.getAccessTokenExp().toString()

if (refreshTime == "") {
return@runBlocking
}

if (currentTime.isAfter(refreshTime.toLocalDateTime())) {
throw NeedLoginException()
}

//Re Issue Access Token
if (currentTime.isAfter(accessTime.toLocalDateTime())) {
val client = OkHttpClient()
val refreshRequest = Request.Builder()
.url(BuildConfig.BASE_URL + "auth")
.patch(chain.request().body ?: RequestBody.Companion.create(null, byteArrayOf()))
.addHeader(
"RefreshToken",
dataSource.getRefreshToken().toString()
)
.build()
val jsonParser = JsonParser()
val response = client.newCall(refreshRequest).execute()
if (response.isSuccessful) {
val token = jsonParser.parse(response.body!!.string()) as JsonObject
dataSource.setAccessToken(token["accessToken"].toString())
dataSource.setAccessTokenExp(token["accessExpiration"].toString())
dataSource.setRefreshToken(token["refreshToken"].toString())
dataSource.setRefreshTokenExp(token["refreshExpiration"].toString())
} else throw NeedLoginException()
}
val accessToken = dataSource.getAccessToken()
val refreshToken = dataSource.getRefreshToken()
if (method == "DELETE") {
builder.addHeader("RefreshToken", "Bearer $refreshToken")
}
builder.addHeader("Authorization", "Bearer $accessToken")
}
return chain.proceed(builder.build())
}
}

fun LocalDateTime.isAfter(compare: LocalDateTime): Boolean {
return this > compare
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.msg.network.util

import kotlinx.datetime.LocalDateTime
import java.time.format.DateTimeFormatter

fun Any?.toLocalDateTime(): LocalDateTime {
return LocalDateTime.parse(toString())
}

0 comments on commit a544aca

Please sign in to comment.