-
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.
Showing
71 changed files
with
690 additions
and
177 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,90 @@ | ||
package co.orange.ddanzi.di | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Handler | ||
import android.os.Looper | ||
import co.orange.core.extension.toast | ||
import co.orange.data.local.UserSharedPref | ||
import co.orange.domain.entity.request.TokenRequestModel | ||
import co.orange.domain.repository.AuthRepository | ||
import co.orange.presentation.auth.login.LoginActivity | ||
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 authRepository: AuthRepository, | ||
private val sharedPref: UserSharedPref, | ||
@ApplicationContext private val context: Context, | ||
) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val originalRequest = chain.request() | ||
|
||
Timber.d("GET ACCESS TOKEN : ${sharedPref.accessToken}") | ||
|
||
val authRequest = | ||
if (sharedPref.accessToken.isNotBlank()) { | ||
originalRequest.newBuilder().newAuthBuilder().build() | ||
} else { | ||
originalRequest | ||
} | ||
|
||
val response = chain.proceed(authRequest) | ||
|
||
when (response.code) { | ||
CODE_TOKEN_EXPIRED -> { | ||
try { | ||
runBlocking { | ||
authRepository.postReissueTokens( | ||
sharedPref.refreshToken, | ||
TokenRequestModel(sharedPref.userId), | ||
) | ||
}.onSuccess { data -> | ||
sharedPref.apply { | ||
accessToken = data.accessToken | ||
refreshToken = data.refreshToken | ||
userId = data.userId | ||
} | ||
|
||
response.close() | ||
|
||
val newRequest = | ||
authRequest.newBuilder().removeHeader(AUTHORIZATION).newAuthBuilder() | ||
.build() | ||
|
||
return chain.proceed(newRequest) | ||
} | ||
} catch (t: Throwable) { | ||
Timber.d(t.message) | ||
} | ||
|
||
sharedPref.clearInfo() | ||
|
||
Handler(Looper.getMainLooper()).post { | ||
context.toast(TOKEN_EXPIRED_ERROR) | ||
Intent(context, LoginActivity::class.java).apply { | ||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) | ||
context.startActivity(this) | ||
} | ||
} | ||
} | ||
} | ||
return response | ||
} | ||
|
||
private fun Request.Builder.newAuthBuilder() = this.addHeader(AUTHORIZATION, "$BEARER ${sharedPref.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package co.orange.ddanzi.di | ||
|
||
import co.orange.data.dataSource.AuthDataSource | ||
import co.orange.data.dataSourceImpl.AuthDataSourceImpl | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataSourceModule { | ||
@Provides | ||
@Singleton | ||
fun provideAuthDataSource(authDataSourceImpl: AuthDataSourceImpl): AuthDataSource = authDataSourceImpl | ||
} |
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,17 @@ | ||
package co.orange.ddanzi.di | ||
|
||
import co.orange.data.repositoryImpl.AuthRepositoryImpl | ||
import co.orange.domain.repository.AuthRepository | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object RepositoryModule { | ||
@Provides | ||
@Singleton | ||
fun provideAuthRepository(authRepositoryImpl: AuthRepositoryImpl): AuthRepository = authRepositoryImpl | ||
} |
112 changes: 112 additions & 0 deletions
112
app/src/main/java/co/orange/ddanzi/di/RetrofitModule.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,112 @@ | ||
package co.orange.ddanzi.di | ||
|
||
import co.orange.core.extension.isJsonArray | ||
import co.orange.core.extension.isJsonObject | ||
import co.orange.ddanzi.BuildConfig.BASE_URL | ||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.json.JSONArray | ||
import org.json.JSONObject | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import timber.log.Timber | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object RetrofitModule { | ||
private const val APPLICATION_JSON = "application/json" | ||
|
||
@Provides | ||
@Singleton | ||
fun provideJson(): Json = | ||
Json { | ||
ignoreUnknownKeys = true | ||
prettyPrint = true | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideJsonConverter(json: Json): Converter.Factory = | ||
json.asConverterFactory( | ||
APPLICATION_JSON.toMediaType(), | ||
) | ||
|
||
@Provides | ||
@Singleton | ||
fun provideHttpLoggingInterceptor(): Interceptor = | ||
HttpLoggingInterceptor { message -> | ||
when { | ||
message.isJsonObject() -> | ||
Timber.tag("okhttp").d(JSONObject(message).toString(4)) | ||
|
||
message.isJsonArray() -> | ||
Timber.tag("okhttp").d(JSONArray(message).toString(4)) | ||
|
||
else -> { | ||
Timber.tag("okhttp").d("CONNECTION INFO -> $message") | ||
} | ||
} | ||
}.apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.JWT | ||
fun provideAuthInterceptor(authInterceptor: AuthInterceptor): Interceptor = authInterceptor | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.JWT | ||
fun provideJWTOkHttpClient( | ||
loggingInterceptor: Interceptor, | ||
@RetrofitQualifier.JWT authInterceptor: Interceptor, | ||
): OkHttpClient = | ||
OkHttpClient.Builder() | ||
.addInterceptor(loggingInterceptor) | ||
.addInterceptor(authInterceptor) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.NOTOKEN | ||
fun provideReissueOkHttpClient(loggingInterceptor: Interceptor): OkHttpClient = | ||
OkHttpClient.Builder() | ||
.addInterceptor(loggingInterceptor) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.JWT | ||
fun provideJWTRetrofit( | ||
@RetrofitQualifier.JWT client: OkHttpClient, | ||
factory: Converter.Factory, | ||
): Retrofit = | ||
Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(client) | ||
.addConverterFactory(factory) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
@RetrofitQualifier.NOTOKEN | ||
fun provideReissueRetrofit( | ||
@RetrofitQualifier.NOTOKEN client: OkHttpClient, | ||
factory: Converter.Factory, | ||
): Retrofit = | ||
Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(client) | ||
.addConverterFactory(factory) | ||
.build() | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/co/orange/ddanzi/di/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,13 @@ | ||
package co.orange.ddanzi.di | ||
|
||
import javax.inject.Qualifier | ||
|
||
object RetrofitQualifier { | ||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class JWT | ||
|
||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class NOTOKEN | ||
} |
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 co.orange.ddanzi.di | ||
|
||
import co.orange.data.service.AuthService | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import retrofit2.Retrofit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object ServiceModule { | ||
@Provides | ||
@Singleton | ||
fun provideAuthService( | ||
@RetrofitQualifier.NOTOKEN retrofit: Retrofit, | ||
): AuthService = retrofit.create(AuthService::class.java) | ||
} |
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,26 @@ | ||
package co.orange.ddanzi.di | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import co.orange.data.local.UserSharedPref | ||
import co.orange.data.local.UserSharedPrefImpl | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object SharedPrefModule { | ||
@Provides | ||
@Singleton | ||
fun provideSharedPreferences( | ||
@ApplicationContext context: Context, | ||
): SharedPreferences = context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE) | ||
|
||
@Provides | ||
@Singleton | ||
fun provideSharedPref(sharedPrefImpl: UserSharedPrefImpl): UserSharedPref = sharedPrefImpl | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package kr.genti.core.extension | ||
package co.orange.core.extension | ||
|
||
import java.text.BreakIterator | ||
|
||
|
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
Oops, something went wrong.