1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-2660 Login | Tech | Seed phrase check (#1385)

This commit is contained in:
Konstantin Ivanov 2024-07-10 15:54:35 +02:00 committed by konstantiniiv
parent fa4bdf1bed
commit 136cbf3ed1
9 changed files with 84 additions and 9 deletions

View file

@ -9,7 +9,7 @@ interface AuthCache {
suspend fun updateAccount(account: AccountEntity)
suspend fun saveMnemonic(mnemonic: String)
suspend fun getMnemonic(): String
suspend fun getMnemonic(): String?
suspend fun getCurrentAccount(): AccountEntity
suspend fun getCurrentAccountId(): String

View file

@ -31,7 +31,7 @@ interface AuthDataStore {
suspend fun recoverWallet(path: String, mnemonic: String)
suspend fun convertWallet(entropy: String): String
suspend fun saveMnemonic(mnemonic: String)
suspend fun getMnemonic(): String
suspend fun getMnemonic(): String?
suspend fun logout(clearLocalRepositoryData: Boolean)
suspend fun getAccounts(): List<AccountEntity>

View file

@ -16,7 +16,8 @@ class CheckAuthorizationStatus @Inject constructor(
override suspend fun run(params: Unit) = safe {
repository.getAccounts().let { accounts ->
if (accounts.isNotEmpty())
val mnemonic= repository.getMnemonic()
if (accounts.isNotEmpty() && !mnemonic.isNullOrBlank())
AuthStatus.AUTHORIZED
else
AuthStatus.UNAUTHORIZED

View file

@ -13,6 +13,7 @@ class GetMnemonic(
override suspend fun run(params: Unit) = try {
repository.getMnemonic().let { mnemonic ->
if (mnemonic.isNullOrBlank()) throw IllegalStateException("Mnemonic is empty")
Either.Right(mnemonic)
}
} catch (e: Throwable) {

View file

@ -16,8 +16,10 @@ class LaunchWallet(
override suspend fun run(params: None) = try {
withTimeout(TIMEOUT_DURATION) {
val mnemonic = repository.getMnemonic()
if (mnemonic.isNullOrBlank()) throw IllegalStateException("Mnemonic is empty")
repository.recoverWallet(
mnemonic = repository.getMnemonic(),
mnemonic = mnemonic,
path = pathProvider.providePath()
).let {
Either.Right(it)

View file

@ -32,9 +32,11 @@ class ResumeAccount @Inject constructor(
version = metricsProvider.getVersion(),
platform = metricsProvider.getPlatform()
)
val mnemonic = repository.getMnemonic()
if (mnemonic.isNullOrBlank()) throw IllegalStateException("Mnemonic is empty")
repository.recoverWallet(
path = pathProvider.providePath(),
mnemonic = repository.getMnemonic()
mnemonic = mnemonic
)
val networkMode = repository.getNetworkMode()

View file

@ -43,7 +43,7 @@ interface AuthRepository {
suspend fun saveMnemonic(mnemonic: String)
suspend fun getMnemonic(): String
suspend fun getMnemonic(): String?
suspend fun logout(clearLocalRepositoryData: Boolean)

View file

@ -45,16 +45,81 @@ class CheckAuthorizationStatusTest {
onBlocking { getAccounts() } doReturn emptyList()
}
repo.stub {
onBlocking { getMnemonic() } doReturn "mnemonic"
}
val result = checkAuthorizationStatus.run(params = Unit)
assertTrue { result == Either.Right(AuthStatus.UNAUTHORIZED) }
verify(repo, times(1)).getAccounts()
verify(repo, times(1)).getMnemonic()
verifyNoMoreInteractions(repo)
}
@Test
fun `should return authorized status if account list is not empty`() = runBlocking {
fun `should return unauthorized status if phrase is null`() = runBlocking {
repo.stub {
onBlocking { getAccounts() } doReturn emptyList()
}
repo.stub {
onBlocking { getMnemonic() } doReturn null
}
val result = checkAuthorizationStatus.run(params = Unit)
assertTrue { result == Either.Right(AuthStatus.UNAUTHORIZED) }
verify(repo, times(1)).getAccounts()
verify(repo, times(1)).getMnemonic()
verifyNoMoreInteractions(repo)
}
@Test
fun `should return unauthorized status if phrase is empty`() = runBlocking {
repo.stub {
onBlocking { getAccounts() } doReturn emptyList()
}
repo.stub {
onBlocking { getMnemonic() } doReturn ""
}
val result = checkAuthorizationStatus.run(params = Unit)
assertTrue { result == Either.Right(AuthStatus.UNAUTHORIZED) }
verify(repo, times(1)).getAccounts()
verify(repo, times(1)).getMnemonic()
verifyNoMoreInteractions(repo)
}
@Test
fun `should return unauthorized status if phrase is blank`() = runBlocking {
repo.stub {
onBlocking { getAccounts() } doReturn emptyList()
}
repo.stub {
onBlocking { getMnemonic() } doReturn " "
}
val result = checkAuthorizationStatus.run(params = Unit)
assertTrue { result == Either.Right(AuthStatus.UNAUTHORIZED) }
verify(repo, times(1)).getAccounts()
verify(repo, times(1)).getMnemonic()
verifyNoMoreInteractions(repo)
}
@Test
fun `should return authorized status if account list is not empty and phrase is not empty`() = runBlocking {
val account = Account(
id = MockDataFactory.randomString()
@ -63,12 +128,16 @@ class CheckAuthorizationStatusTest {
repo.stub {
onBlocking { getAccounts() } doReturn listOf(account)
}
repo.stub {
onBlocking { getMnemonic() } doReturn "1"
}
val result = checkAuthorizationStatus.run(params = Unit)
assertTrue { result == Either.Right(AuthStatus.AUTHORIZED) }
verify(repo, times(1)).getAccounts()
verify(repo, times(1)).getMnemonic()
verifyNoMoreInteractions(repo)
}

View file

@ -55,7 +55,7 @@ class DefaultAuthCache(
/**
* N.B. Migrating sensitive data from default to encrypted prefs.
*/
override suspend fun getMnemonic(): String {
override suspend fun getMnemonic(): String? {
val nonEncryptedMnemonic = defaultPrefs.getString(MNEMONIC_KEY, null)
return if (nonEncryptedMnemonic != null) {
encryptedPrefs.edit().putString(MNEMONIC_KEY, nonEncryptedMnemonic).apply()
@ -63,7 +63,7 @@ class DefaultAuthCache(
nonEncryptedMnemonic
} else {
val encryptedMnemonic = encryptedPrefs.getString(MNEMONIC_KEY, null)
encryptedMnemonic ?: throw IllegalStateException("Recovery phrase is missing")
encryptedMnemonic
}
}