-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into bugfix/sendToQaFix
- Loading branch information
Showing
16 changed files
with
323 additions
and
32 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
12 changes: 12 additions & 0 deletions
12
app/src/main/java/app/cashadvisor/authorization/data/dto/CredentialsDto.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,12 @@ | ||
package app.cashadvisor.authorization.data.dto | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
@Parcelize | ||
data class CredentialsDto( | ||
val accessToken: String, | ||
val refreshToken: String, | ||
) : Parcelable |
75 changes: 75 additions & 0 deletions
75
app/src/main/java/app/cashadvisor/authorization/data/impl/CredentialsRepositoryImpl.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,75 @@ | ||
package app.cashadvisor.authorization.data.impl | ||
|
||
import kotlinx.serialization.json.Json | ||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import app.cashadvisor.authorization.data.dto.CredentialsDto | ||
import app.cashadvisor.authorization.domain.api.CredentialsRepository | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import kotlinx.serialization.encodeToString | ||
|
||
class CredentialsRepositoryImpl( | ||
private val storage: SharedPreferences, | ||
private val key: String, | ||
private val json: Json | ||
) : CredentialsRepository, CoroutineScope by CoroutineScope( | ||
Dispatchers.IO + | ||
SupervisorJob() | ||
) { | ||
|
||
private val _credentialsUpdateFlow = MutableStateFlow<CredentialsDto?>(null) | ||
|
||
init { | ||
storage.registerOnSharedPreferenceChangeListener(this) | ||
launch { | ||
_credentialsUpdateFlow.value = getCredentials() | ||
} | ||
} | ||
|
||
override suspend fun saveCredentials(credentials: CredentialsDto) { | ||
withContext(Dispatchers.IO) { | ||
val data = json.encodeToString(credentials) | ||
storage.edit { putString(key, data) } | ||
} | ||
} | ||
|
||
override suspend fun getCredentials(): CredentialsDto? { | ||
return withContext(Dispatchers.IO) { | ||
storage.getString(key, null)?.let { | ||
json.decodeFromString<CredentialsDto>(it) | ||
} | ||
} | ||
} | ||
|
||
override fun getCredentialsFlow(): Flow<CredentialsDto?> = _credentialsUpdateFlow | ||
|
||
override suspend fun hasCredentials(): Boolean { | ||
return withContext(Dispatchers.IO) { | ||
storage.contains(key) | ||
} | ||
} | ||
|
||
override suspend fun clearCredentials() { | ||
withContext(Dispatchers.IO) { | ||
storage.edit { remove(key) } | ||
_credentialsUpdateFlow.emit(null) | ||
} | ||
} | ||
|
||
override fun onSharedPreferenceChanged( | ||
p0: SharedPreferences?, | ||
p1: String? | ||
) { | ||
if (p1 == key) { | ||
launch { | ||
_credentialsUpdateFlow.value = getCredentials() | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/app/cashadvisor/authorization/di/AuthorizationDataModule.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,53 @@ | ||
package app.cashadvisor.authorization.di | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKeys | ||
import app.cashadvisor.authorization.data.impl.CredentialsRepositoryImpl | ||
import app.cashadvisor.authorization.domain.api.CredentialsRepository | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.json.Json | ||
import javax.inject.Singleton | ||
|
||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
class AuthorizationDataModule { | ||
|
||
|
||
@Provides | ||
@Singleton | ||
fun providesMasterKey(): String = | ||
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) | ||
|
||
@Provides | ||
@Singleton | ||
fun providesEncriptedSharedPreferences(@ApplicationContext context: Context): SharedPreferences { | ||
val masterKey = providesMasterKey() | ||
return EncryptedSharedPreferences.create( | ||
SECRET_SETTINGS, | ||
masterKey, | ||
context, | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
) | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun providesCredintialsStorage( | ||
@ApplicationContext context: Context, | ||
storage: SharedPreferences = providesEncriptedSharedPreferences(context), | ||
key: String = CREDENTIALS_KEY, | ||
gson: Json | ||
): CredentialsRepository = CredentialsRepositoryImpl(storage, key, gson) | ||
companion object { | ||
private const val SECRET_SETTINGS = "secret_shared_prefs" | ||
private const val CREDENTIALS_KEY = "secret_credentials" | ||
} | ||
} |
16 changes: 9 additions & 7 deletions
16
...hadvisor/authorization/di/DomainModule.kt → ...ation/di/AuthorizationInteractorModule.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,18 +1,20 @@ | ||
package app.cashadvisor.authorization.di | ||
|
||
|
||
import app.cashadvisor.authorization.domain.api.AccountInformationInteractor | ||
import app.cashadvisor.authorization.domain.api.CredentialsRepository | ||
import app.cashadvisor.authorization.domain.impl.AccountInformationInteractorImpl | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ViewModelComponent | ||
|
||
|
||
@Module | ||
@InstallIn(ViewModelComponent::class) | ||
abstract class DomainModule { | ||
@Binds | ||
abstract fun bindAccountInformationInteractor( | ||
accountInformationInteractorImpl: AccountInformationInteractorImpl | ||
): AccountInformationInteractor | ||
class AuthorizationInteractorModule { | ||
@Provides | ||
fun providesAccountInformationInteractor( | ||
storage: CredentialsRepository, | ||
): AccountInformationInteractor = AccountInformationInteractorImpl(storage) | ||
|
||
} |
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
15 changes: 15 additions & 0 deletions
15
app/src/main/java/app/cashadvisor/authorization/domain/api/CredentialsRepository.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,15 @@ | ||
package app.cashadvisor.authorization.domain.api | ||
|
||
import android.content.SharedPreferences | ||
import app.cashadvisor.authorization.data.dto.CredentialsDto | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface CredentialsRepository : SharedPreferences.OnSharedPreferenceChangeListener { | ||
|
||
suspend fun saveCredentials(credentials: CredentialsDto) | ||
suspend fun getCredentials(): CredentialsDto? | ||
fun getCredentialsFlow(): Flow<CredentialsDto?> | ||
suspend fun clearCredentials() | ||
suspend fun hasCredentials(): Boolean | ||
|
||
} |
28 changes: 16 additions & 12 deletions
28
...c/main/java/app/cashadvisor/authorization/domain/impl/AccountInformationInteractorImpl.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,26 +1,30 @@ | ||
package app.cashadvisor.authorization.domain.impl | ||
|
||
import app.cashadvisor.authorization.domain.api.AccountInformationInteractor | ||
import app.cashadvisor.authorization.domain.api.CredentialsRepository | ||
import app.cashadvisor.authorization.domain.models.AccountInformation | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import java.util.Date | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.map | ||
|
||
class AccountInformationInteractorImpl( | ||
private val storage: CredentialsRepository | ||
) : AccountInformationInteractor { | ||
override suspend fun getAccountInformation(): Flow<AccountInformation> = | ||
storage.getCredentialsFlow().map { credentials -> | ||
credentials?.let { | ||
AccountInformation.Authorized( | ||
accessToken = it.accessToken, | ||
refreshToken = it.refreshToken | ||
) | ||
} ?: AccountInformation.NotAuthorized | ||
} | ||
|
||
class AccountInformationInteractorImpl @Inject constructor() : AccountInformationInteractor { | ||
override fun getAccountInformation(): Flow<AccountInformation> = flow { | ||
// todo обновить класс с учетом реальных данных | ||
emit(MOCK_ACCOUNT_INFORMATION) | ||
} | ||
|
||
companion object { | ||
private val MOCK_ACCOUNT_INFORMATION = AccountInformation.Authorized( | ||
email = "test@test.test", | ||
token = "123", | ||
accessToken = "123", | ||
refreshToken = "123", | ||
isEmailVerified = true, | ||
tokenValidTill = Date() | ||
) | ||
} | ||
|
||
} |
10 changes: 2 additions & 8 deletions
10
app/src/main/java/app/cashadvisor/authorization/domain/models/AccountInformation.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,15 +1,9 @@ | ||
package app.cashadvisor.authorization.domain.models | ||
|
||
import java.util.Date | ||
|
||
sealed class AccountInformation { | ||
object NotAuthorized : AccountInformation() | ||
data class Authorized( | ||
// todo пока модель черновая, перепроверить что тут должно быть | ||
val email: String? = null, | ||
val token: String? = null, | ||
val refreshToken: String? = null, | ||
val isEmailVerified: Boolean = false, | ||
val tokenValidTill: Date? = null | ||
val accessToken: String, | ||
val refreshToken: String | ||
) : AccountInformation() | ||
} |
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,18 @@ | ||
package app.cashadvisor.common.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.json.Json | ||
import javax.inject.Singleton | ||
|
||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
class DataModule { | ||
@Provides | ||
@Singleton | ||
fun provideJson(): Json = Json { ignoreUnknownKeys = true } | ||
|
||
} |
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.