Replace AccountManager with EncryptedSharedPreferences
This commit is contained in:
@@ -2,25 +2,29 @@ package gq.kirmanak.mealient.di
|
||||
|
||||
import android.accounts.AccountManager
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import androidx.security.crypto.EncryptedSharedPreferences
|
||||
import androidx.security.crypto.MasterKeys
|
||||
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 gq.kirmanak.mealient.R
|
||||
import gq.kirmanak.mealient.data.auth.AuthDataSource
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import gq.kirmanak.mealient.data.auth.AuthStorage
|
||||
import gq.kirmanak.mealient.data.auth.impl.AuthDataSourceImpl
|
||||
import gq.kirmanak.mealient.data.auth.impl.AuthRepoImpl
|
||||
import gq.kirmanak.mealient.data.auth.impl.AuthService
|
||||
import gq.kirmanak.mealient.data.auth.impl.AuthStorageImpl
|
||||
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
||||
import gq.kirmanak.mealient.data.network.RetrofitBuilder
|
||||
import gq.kirmanak.mealient.data.network.ServiceFactory
|
||||
import gq.kirmanak.mealient.data.network.createServiceFactory
|
||||
import gq.kirmanak.mealient.service.auth.AccountManagerInteractor
|
||||
import gq.kirmanak.mealient.service.auth.AccountManagerInteractorImpl
|
||||
import gq.kirmanak.mealient.service.auth.AccountParameters
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.OkHttpClient
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@@ -28,13 +32,17 @@ import javax.inject.Singleton
|
||||
interface AuthModule {
|
||||
|
||||
companion object {
|
||||
const val ENCRYPTED = "encrypted"
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideAuthServiceFactory(
|
||||
retrofitBuilder: RetrofitBuilder,
|
||||
@Named(NO_AUTH_OK_HTTP) okHttpClient: OkHttpClient,
|
||||
json: Json,
|
||||
baseURLStorage: BaseURLStorage,
|
||||
): ServiceFactory<AuthService> = retrofitBuilder.createServiceFactory(baseURLStorage)
|
||||
): ServiceFactory<AuthService> {
|
||||
return RetrofitBuilder(okHttpClient, json).createServiceFactory(baseURLStorage)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@@ -44,10 +52,20 @@ interface AuthModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideAccountType(@ApplicationContext context: Context) = AccountParameters(
|
||||
accountType = context.getString(R.string.account_type),
|
||||
authTokenType = context.getString(R.string.auth_token_type),
|
||||
)
|
||||
@Named(ENCRYPTED)
|
||||
fun provideEncryptedSharedPreferences(
|
||||
@ApplicationContext applicationContext: Context,
|
||||
): SharedPreferences {
|
||||
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
|
||||
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
|
||||
return EncryptedSharedPreferences.create(
|
||||
ENCRYPTED,
|
||||
mainKeyAlias,
|
||||
applicationContext,
|
||||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
|
||||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Binds
|
||||
@@ -60,7 +78,5 @@ interface AuthModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
fun bindAccountManagerInteractor(
|
||||
accountManagerInteractorImpl: AccountManagerInteractorImpl
|
||||
): AccountManagerInteractor
|
||||
fun bindAuthStorage(authStorageImpl: AuthStorageImpl): AuthStorage
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ import gq.kirmanak.mealient.data.baseurl.*
|
||||
import gq.kirmanak.mealient.data.network.RetrofitBuilder
|
||||
import gq.kirmanak.mealient.data.network.ServiceFactory
|
||||
import gq.kirmanak.mealient.data.network.createServiceFactory
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.OkHttpClient
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@@ -20,9 +23,12 @@ interface BaseURLModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideVersionServiceFactory(
|
||||
retrofitBuilder: RetrofitBuilder,
|
||||
@Named(AUTH_OK_HTTP) okHttpClient: OkHttpClient,
|
||||
json: Json,
|
||||
baseURLStorage: BaseURLStorage,
|
||||
): ServiceFactory<VersionService> = retrofitBuilder.createServiceFactory(baseURLStorage)
|
||||
): ServiceFactory<VersionService> {
|
||||
return RetrofitBuilder(okHttpClient, json).createServiceFactory(baseURLStorage)
|
||||
}
|
||||
}
|
||||
|
||||
@Binds
|
||||
|
||||
@@ -19,6 +19,9 @@ import gq.kirmanak.mealient.data.recipes.impl.RecipeRepoImpl
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSourceImpl
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeService
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.OkHttpClient
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@@ -46,9 +49,12 @@ interface RecipeModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRecipeServiceFactory(
|
||||
retrofitBuilder: RetrofitBuilder,
|
||||
@Named(AUTH_OK_HTTP) okHttpClient: OkHttpClient,
|
||||
json: Json,
|
||||
baseURLStorage: BaseURLStorage,
|
||||
): ServiceFactory<RecipeService> = retrofitBuilder.createServiceFactory(baseURLStorage)
|
||||
): ServiceFactory<RecipeService> {
|
||||
return RetrofitBuilder(okHttpClient, json).createServiceFactory(baseURLStorage)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
|
||||
Reference in New Issue
Block a user