Skip to content

Commit

Permalink
[refactor] Preference 만드는 로직 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
murjune committed Jan 28, 2024
1 parent b99011f commit ac725bc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.moya.funch.datastore

import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import com.moja.funch.datastore.BuildConfig
import dagger.hilt.android.qualifiers.ApplicationContext
import java.security.GeneralSecurityException
import java.security.KeyStore
import javax.inject.Inject

class PreferenceFactory @Inject constructor(
@ApplicationContext private val context: Context,
) {
fun create(): SharedPreferences {
return if (BuildConfig.DEBUG) {
context.getSharedPreferences(DEBUG_DATASTORE_KEY, Context.MODE_PRIVATE)
} else {
try {
createEncryptedSharedPreferences(DATASTORE_KEY, context)
} catch (e: GeneralSecurityException) {
deleteMasterKeyEntry()
deletePreference(DATASTORE_KEY, context)
createEncryptedSharedPreferences(DATASTORE_KEY, context)
}
}
}

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

)
}

private fun deletePreference(fileName: String, context: Context) {
context.deleteSharedPreferences(fileName)
}

private fun deleteMasterKeyEntry() {
KeyStore.getInstance(ANDROID_KEY_STORE).apply {
load(null)
deleteEntry(MasterKey.DEFAULT_MASTER_KEY_ALIAS)
}
}

companion object {
private const val DEBUG_DATASTORE_KEY = "DEBUG_DATASTORE_KEY"
private const val DATASTORE_KEY = "DATASTORE_KEY"
private const val ANDROID_KEY_STORE = "AndroidKeyStore"
}
}
Original file line number Diff line number Diff line change
@@ -1,71 +1,25 @@
package com.moya.funch.datastore.di

import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import com.moja.funch.datastore.BuildConfig
import com.moya.funch.datastore.DefaultUserCodeDataStore
import com.moya.funch.datastore.PreferenceFactory
import com.moya.funch.datastore.UserCodeDataStore
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_DATASTORE_KEY = "DEBUG_DATASTORE_KEY"
private const val DATASTORE_KEY = "DATASTORE_KEY"
private const val ANDROID_KEY_STORE = "AndroidKeyStore"

@Provides
@Singleton
fun provideAppPreferences(
@ApplicationContext context: Context,
): SharedPreferences = if (BuildConfig.DEBUG) {
context.getSharedPreferences(DEBUG_DATASTORE_KEY, Context.MODE_PRIVATE)
} else {
try {
createEncryptedSharedPreferences(DATASTORE_KEY, context)
} catch (e: GeneralSecurityException) {
deleteMasterKeyEntry()
deletePreference(DATASTORE_KEY, context)
createEncryptedSharedPreferences(DATASTORE_KEY, context)
}
}

private fun deletePreference(fileName: String, context: Context) {
context.deleteSharedPreferences(fileName)
}

private fun deleteMasterKeyEntry() {
KeyStore.getInstance(ANDROID_KEY_STORE).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

)
}
factory: PreferenceFactory,
): SharedPreferences = factory.create()

@Module
@InstallIn(SingletonComponent::class)
Expand Down

0 comments on commit ac725bc

Please sign in to comment.