-
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.
Merge pull request #90 from Team-HMH/fix/network
[fix/network]: Network 기본 로직 수정
- Loading branch information
Showing
6 changed files
with
112 additions
and
12 deletions.
There are no files selected for viewing
8 changes: 5 additions & 3 deletions
8
...onham/core/network/auth/api/RefreshApi.kt → ...m/core/network/auth/api/RefreshService.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,10 +1,12 @@ | ||
package com.hmh.hamyeonham.core.network.auth.api | ||
|
||
import com.hmh.hamyeonham.core.network.auth.api.model.TokenResponse | ||
import com.hmh.hamyeonham.core.network.model.BaseResponse | ||
import retrofit2.http.GET | ||
import retrofit2.http.Header | ||
import retrofit2.http.POST | ||
|
||
interface RefreshApi { | ||
@POST("api/v1/auth/token") | ||
suspend fun refreshToken(@Header("Authorization") token: String): TokenResponse | ||
interface RefreshService { | ||
@GET("api/v1/user/reissue") | ||
suspend fun refreshToken(@Header("Authorization") token: String): BaseResponse<TokenResponse> | ||
} |
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
76 changes: 76 additions & 0 deletions
76
core/network/src/main/java/com/hmh/hamyeonham/core/network/di/DataStoreModule.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,76 @@ | ||
package com.hmh.hamyeonham.core.network.di | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKey | ||
import com.hmh.hamyeonham.core.network.BuildConfig | ||
import com.hmh.hamyeonham.core.network.auth.datastore.DefaultHMHNetworkPreference | ||
import com.hmh.hamyeonham.core.network.auth.datastore.HMHNetworkPreference | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import java.security.GeneralSecurityException | ||
import java.security.KeyStore | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DataStoreModule { | ||
private const val DEBUG_APP_PREFERNCES_NAME = "hmh_debug" | ||
private const val APP_PREFERENCES_NAME = "hmh" | ||
|
||
@Provides | ||
@Singleton | ||
fun provideAppPreferences( | ||
@ApplicationContext context: Context | ||
): SharedPreferences = if (BuildConfig.DEBUG) { | ||
context.getSharedPreferences(DEBUG_APP_PREFERNCES_NAME, Context.MODE_PRIVATE) | ||
} else { | ||
try { | ||
createEncryptedSharedPreferences(APP_PREFERENCES_NAME, context) | ||
} catch (e: GeneralSecurityException) { | ||
deleteMasterKeyEntry() | ||
deleteExistingPref(APP_PREFERENCES_NAME, context) | ||
createEncryptedSharedPreferences(APP_PREFERENCES_NAME, context) | ||
} | ||
} | ||
|
||
private fun deleteExistingPref(fileName: String, context: Context) { | ||
context.deleteSharedPreferences(fileName) | ||
} | ||
|
||
private fun deleteMasterKeyEntry() { | ||
KeyStore.getInstance("AndroidKeyStore").apply { | ||
load(null) | ||
deleteEntry(MasterKey.DEFAULT_MASTER_KEY_ALIAS) | ||
} | ||
} | ||
|
||
private fun createEncryptedSharedPreferences( | ||
fileName: String, | ||
context: Context | ||
): SharedPreferences { | ||
return EncryptedSharedPreferences.create( | ||
context, | ||
fileName, | ||
MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS) | ||
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM) | ||
.build(), | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
|
||
) | ||
} | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
interface Binder { | ||
@Singleton | ||
@Binds | ||
fun bindAppPreferences(dataStore: DefaultHMHNetworkPreference): HMHNetworkPreference | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
core/network/src/main/java/com/hmh/hamyeonham/core/network/di/RefreshModule.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.hmh.hamyeonham.core.network.di | ||
|
||
import com.hmh.hamyeonham.common.qualifier.Unsecured | ||
import com.hmh.hamyeonham.core.network.auth.api.RefreshService | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import retrofit2.Retrofit | ||
import retrofit2.create | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object RefreshModule { | ||
@Provides | ||
@Singleton | ||
fun provideRefreshApi(@Unsecured retrofit: Retrofit): RefreshService = retrofit.create() | ||
} |