Skip to content

Commit 2827048

Browse files
authored
Merge pull request #464 from code-payments/chore/account-login-tests
chore: add account request attempts
2 parents a0864fd + a86bfe9 commit 2827048

2 files changed

Lines changed: 41 additions & 33 deletions

File tree

app/src/main/java/com/getcode/util/AccountAuthenticator.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.getcode.util
33
import android.accounts.*
44
import android.content.Context
55
import android.os.Bundle
6+
import androidx.core.os.bundleOf
67
import com.getcode.utils.trace
78

89

@@ -66,13 +67,21 @@ class AccountAuthenticator(
6667
return Bundle()
6768
}
6869

69-
override fun getAuthTokenLabel(arg0: String): String? = null
70+
override fun getAuthTokenLabel(arg0: String): String? {
71+
return "entropy"
72+
}
7073

7174
@Throws(NetworkErrorException::class)
7275
override fun hasFeatures(
7376
arg0: AccountAuthenticatorResponse, arg1: Account,
7477
arg2: Array<String>
75-
): Bundle? = null
78+
): Bundle {
79+
// This call is used to query whether the Authenticator supports
80+
// specific features. We don't expect to get called, so we always
81+
// return false (no) for any queries.
82+
val result = bundleOf(AccountManager.KEY_BOOLEAN_RESULT to false)
83+
return result
84+
}
7685

7786
@Throws(NetworkErrorException::class)
7887
override fun updateCredentials(

app/src/main/java/com/getcode/util/AccountUtils.kt

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package com.getcode.util
33
import android.accounts.Account
44
import android.accounts.AccountManager
55
import android.accounts.AuthenticatorException
6-
import android.app.Activity
76
import android.content.Context
8-
import android.os.Bundle
97
import android.os.Handler
108
import android.os.HandlerThread
9+
import androidx.core.os.bundleOf
1110
import com.getcode.BuildConfig
1211
import com.getcode.utils.TraceType
1312
import com.getcode.utils.trace
@@ -23,14 +22,20 @@ import kotlin.coroutines.resume
2322

2423

2524
object AccountUtils {
26-
private const val acctType = BuildConfig.APPLICATION_ID
27-
28-
fun addAccount(context: Context, name: String, password: String, token: String) {
29-
val am: AccountManager = AccountManager.get(context)
30-
val a = Account(name, acctType)
31-
32-
am.addAccountExplicitly(a, password, Bundle())
33-
am.setAuthToken(a, acctType, token)
25+
private const val ACCOUNT_TYPE = BuildConfig.APPLICATION_ID
26+
27+
fun addAccount(
28+
context: Context,
29+
name: String,
30+
password: String,
31+
token: String
32+
) {
33+
val accountManager: AccountManager = AccountManager.get(context)
34+
val account = Account(name, ACCOUNT_TYPE)
35+
36+
val data = bundleOf(AccountManager.KEY_AUTH_TOKEN_LABEL to "entropy")
37+
accountManager.addAccountExplicitly(account, password, data)
38+
accountManager.setAuthToken(account, ACCOUNT_TYPE, token)
3439
}
3540

3641
suspend fun removeAccounts(context: Context): @NonNull Single<Boolean> {
@@ -63,54 +68,48 @@ object AccountUtils {
6368

6469
private suspend fun getAccountNoActivity(
6570
context: Context
66-
) : Pair<String?, Account?>? = suspendCancellableCoroutine { cont ->
71+
): Pair<String?, Account?>? = suspendCancellableCoroutine { cont ->
6772
trace("getAuthToken", type = TraceType.Silent)
6873
val am: AccountManager = AccountManager.get(context)
69-
val accountthing = am.accounts.getOrNull(0)
70-
if (accountthing == null) {
74+
val account = am.accounts.getOrNull(0)
75+
if (account == null) {
7176
trace("no associated account found", type = TraceType.Error)
7277
cont.resume(null to null)
7378
return@suspendCancellableCoroutine
7479
}
7580
val start = Clock.System.now()
7681
am.getAuthToken(
77-
accountthing, acctType, null, false,
82+
account, ACCOUNT_TYPE, null, false,
7883
{ future ->
7984
try {
8085
val bundle = future?.result
8186
val authToken = bundle?.getString(AccountManager.KEY_AUTHTOKEN)
82-
val accountName = bundle?.getString(AccountManager.KEY_ACCOUNT_NAME)
83-
val account: Account? = getAccount(context, accountName)
8487

8588
val end = Clock.System.now()
8689
trace("auth token fetch took ${end.toEpochMilliseconds() - start.toEpochMilliseconds()} ms")
8790

88-
cont.resume(authToken.orEmpty() to account)
89-
90-
if (null == account && authToken != null) {
91-
addAccount(context, accountName.orEmpty(), "", authToken)
92-
}
91+
cont.resume(authToken.orEmpty() to account)
9392
} catch (e: AuthenticatorException) {
93+
e.printStackTrace()
9494
trace(message = "failed to read account", error = e, type = TraceType.Error)
9595
cont.resume(null to null)
9696
}
9797
}, handler
9898
)
9999
}
100100

101-
102101
suspend fun getToken(context: Context): String? {
103-
return getAccountNoActivity(context)?.first
104-
}
105-
106-
private fun getAccount(context: Context?, accountName: String?): Account? {
107102
val accountManager = AccountManager.get(context)
108-
val accounts = accountManager.getAccountsByType(acctType)
109-
for (account in accounts) {
110-
if (account.name.equals(accountName, ignoreCase = true)) {
111-
return account
103+
val account = accountManager.accounts.firstOrNull()
104+
if (account != null) {
105+
val token = runCatching { accountManager.peekAuthToken(account, ACCOUNT_TYPE) }
106+
.getOrNull()?.takeIf { it.isNotEmpty() }
107+
108+
if (token != null) {
109+
return token
112110
}
113111
}
114-
return null
112+
113+
return getAccountNoActivity(context)?.first
115114
}
116115
}

0 commit comments

Comments
 (0)