Skip to content

chore: scope authenticator type to namespace #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ dependencies {

implementation(Libs.grpc_okhttp)
implementation(Libs.grpc_kotlin)
implementation(Libs.androidx_lifecycle_runtime)
implementation(Libs.androidx_room_runtime)
implementation(Libs.androidx_room_ktx)
implementation(Libs.androidx_room_rxjava3)
Expand Down
4 changes: 4 additions & 0 deletions api/src/main/java/com/getcode/manager/MnemonicManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ class MnemonicManager @Inject constructor(
return mnemonicPhrase.getBase64EncodedEntropy(context)
}

fun getEncodedBase58(mnemonicPhrase: MnemonicPhrase): String {
return mnemonicPhrase.getBase58EncodedEntropy(context)
}

val mnemonicCode: MnemonicCode = context.resources.let(::MnemonicCode)
}
4 changes: 1 addition & 3 deletions api/src/main/java/com/getcode/network/TipController.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.getcode.network

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.getcode.manager.SessionManager
import com.getcode.model.CodePayload
import com.getcode.model.TipMetadata
import com.getcode.model.PrefsBool
import com.getcode.model.PrefsString
import com.getcode.model.TipMetadata
import com.getcode.model.TwitterUser
import com.getcode.network.client.Client
import com.getcode.network.client.fetchTwitterUser
Expand Down
53 changes: 17 additions & 36 deletions api/src/main/java/com/getcode/network/client/Client.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package com.getcode.network.client

import android.content.Context
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.getcode.analytics.AnalyticsService
import com.getcode.manager.MnemonicManager
import com.getcode.manager.SessionManager
import com.getcode.network.BalanceController
import com.getcode.network.HistoryController
import com.getcode.network.exchange.Exchange
import com.getcode.network.repository.AccountRepository
import com.getcode.network.repository.IdentityRepository
import com.getcode.network.repository.MessagingRepository
import com.getcode.network.repository.PrefRepository
import com.getcode.network.repository.TransactionRepository
import com.getcode.utils.network.NetworkConnectivityListener
import com.getcode.network.service.ChatService
import com.getcode.network.service.DeviceService
import com.getcode.utils.ErrorUtils
import dagger.hilt.android.qualifiers.ApplicationContext
import com.getcode.utils.network.NetworkConnectivityListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import timber.log.Timber
import java.util.Timer
Expand All @@ -36,8 +33,6 @@ internal const val TAG = "Client"

@Singleton
class Client @Inject constructor(
@ApplicationContext
internal val context: Context,
internal val identityRepository: IdentityRepository,
internal val transactionRepository: TransactionRepository,
internal val messagingRepository: MessagingRepository,
Expand All @@ -51,9 +46,9 @@ class Client @Inject constructor(
internal val networkObserver: NetworkConnectivityListener,
internal val chatService: ChatService,
internal val deviceService: DeviceService,
) : LifecycleEventObserver {
internal val mnemonicManager: MnemonicManager,
) {

private val TAG = "PollTimer"
private val scope = CoroutineScope(Dispatchers.IO)

private var pollTimer: Timer? = null
Expand All @@ -62,13 +57,15 @@ class Client @Inject constructor(
private fun startPollTimerWhenAuthenticated() {
Timber.tag(TAG).i("Creating poll timer")
scope.launch {
SessionManager.authState.collect {
if (it.isAuthenticated == true) {
SessionManager.authState
.map { it.isAuthenticated }
.filterNotNull()
.filter { it }
.onEach {
Timber.tag(TAG).i("User Authenticated - starting timer")
startPollTimer()
this.cancel()
}
}
}.launchIn(this)
}
}

Expand Down Expand Up @@ -101,28 +98,12 @@ class Client @Inject constructor(
}
}

private fun startTimer() {
fun startTimer() {
startPollTimerWhenAuthenticated()
}

fun pollOnce(delay: Long = 2_000L) {
scope.launch {
delay(delay)
Timber.tag(TAG).i("Poll Once")
poll()
}
}

private fun stopTimer() {
fun stopTimer() {
Timber.tag(TAG).i("Cancelling Poller")
pollTimer?.cancel()
}

override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event) {
Lifecycle.Event.ON_RESUME -> startTimer()
Lifecycle.Event.ON_STOP -> stopTimer()
else -> Unit
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fun Client.transferWithResultSingle(
return getTransferPreflightAction(amount.kin)
.andThen(Single.defer {
transactionRepository.transfer(
context, amount, fee, additionalFees, organizer, rendezvousKey, destination, isWithdrawal, tipMetadata
amount, fee, additionalFees, organizer, rendezvousKey, destination, isWithdrawal, tipMetadata
)
})
.map {
Expand Down Expand Up @@ -141,7 +141,6 @@ fun Client.sendRemotely(
getTransferPreflightAction(truncatedAmount.kin)
.andThen(
sendRemotely(
context = context,
amount = truncatedAmount,
organizer = organizer,
rendezvousKey = rendezvousKey,
Expand All @@ -150,7 +149,7 @@ fun Client.sendRemotely(
.doOnComplete {
val giftCardItem = GiftCard(
key = giftCard.cluster.vaultPublicKey.base58(),
entropy = giftCard.mnemonicPhrase.getBase58EncodedEntropy(context),
entropy = mnemonicManager.getEncodedBase58(giftCard.mnemonicPhrase),
amount = truncatedAmount.kin.quarks,
date = System.currentTimeMillis()
)
Expand Down Expand Up @@ -367,15 +366,14 @@ private fun Client.withdraw(
}

fun Client.sendRemotely(
context: Context,
amount: KinAmount,
organizer: Organizer,
rendezvousKey: PublicKey,
giftCard: GiftCardAccount
): Completable {
return Completable.defer {
transactionRepository.sendRemotely(
context, amount, organizer, rendezvousKey, giftCard
amount, organizer, rendezvousKey, giftCard
)
.map {
if (it is IntentRemoteSend) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ class TransactionRepository @Inject constructor(
}

fun transfer(
context: Context,
amount: KinAmount,
fee: Kin,
additionalFees: List<Fee>,
Expand Down Expand Up @@ -254,7 +253,6 @@ class TransactionRepository @Inject constructor(
}

fun sendRemotely(
context: Context,
amount: KinAmount,
organizer: Organizer,
rendezvousKey: PublicKey,
Expand Down
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ android {
buildToolsVersion = Android.buildToolsVersion
testInstrumentationRunner = Android.testInstrumentationRunner

resValue("string", "applicationId", Android.namespace)

buildConfigField("String", "MIXPANEL_API_KEY", "\"${tryReadProperty(rootProject.rootDir, "MIXPANEL_API_KEY")}\"")
buildConfigField("String", "KADO_API_KEY", "\"${tryReadProperty(rootProject.rootDir, "KADO_API_KEY")}\"")
buildConfigField("Boolean", "NOTIFY_ERRORS", "false")
Expand Down
33 changes: 2 additions & 31 deletions app/src/main/java/com/getcode/CodeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,36 +200,7 @@ internal data object MainRoot : Screen {
Box(
modifier = Modifier
.fillMaxSize()
.background(CodeTheme.colors.background),
contentAlignment = Alignment.Center
) {
var show by remember {
mutableStateOf(false)
}

AnimatedContent(show, transitionSpec = { fadeIn() togetherWith fadeOut() }) {
if (it) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(CodeTheme.dimens.inset)
) {
Image(
painter = painterResource(R.drawable.ic_code_logo_near_white),
contentDescription = "",
modifier = Modifier
.fillMaxWidth(0.65f)
.fillMaxHeight(0.65f)
)

CodeCircularProgressIndicator()
}
}
}

LaunchedEffect(Unit) {
delay(1_000)
show = true
}
}
.background(CodeTheme.colors.background)
)
}
}
17 changes: 8 additions & 9 deletions app/src/main/java/com/getcode/inject/ApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import com.getcode.BuildConfig
import com.getcode.R
import com.getcode.analytics.AnalyticsService
import com.getcode.manager.MnemonicManager
import com.getcode.model.Currency
import com.getcode.network.BalanceController
import com.getcode.network.PrivacyMigration
Expand Down Expand Up @@ -73,12 +74,8 @@ object ApiModule {
val DEV_URL = "api.codeinfra.dev"
val PROD_URL = "api.codeinfra.net"

return AndroidChannelBuilder.usingBuilder(
OkHttpChannelBuilderForcedTls12.forAddress(
PROD_URL,
TLS_PORT
)
)
return AndroidChannelBuilder
.usingBuilder(OkHttpChannelBuilderForcedTls12.forAddress(PROD_URL, TLS_PORT))
.context(context)
.userAgent("Code/Android/${BuildConfig.VERSION_NAME}")
.keepAliveTime(4, TimeUnit.MINUTES)
Expand All @@ -87,7 +84,9 @@ object ApiModule {

@Singleton
@Provides
fun provideAccountAuthenticator(@ApplicationContext context: Context): AccountAuthenticator {
fun provideAccountAuthenticator(
@ApplicationContext context: Context,
): AccountAuthenticator {
return AccountAuthenticator(context)
}

Expand Down Expand Up @@ -157,7 +156,6 @@ object ApiModule {
@Singleton
@Provides
fun provideClient(
@ApplicationContext context: Context,
identityRepository: IdentityRepository,
transactionRepository: TransactionRepository,
messagingRepository: MessagingRepository,
Expand All @@ -171,9 +169,9 @@ object ApiModule {
networkObserver: NetworkConnectivityListener,
chatService: ChatService,
deviceService: DeviceService,
mnemonicManager: MnemonicManager,
): Client {
return Client(
context,
identityRepository,
transactionRepository,
messagingRepository,
Expand All @@ -187,6 +185,7 @@ object ApiModule {
networkObserver,
chatService,
deviceService,
mnemonicManager
)
}

Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/com/getcode/util/AccountAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package com.getcode.util
import android.accounts.*
import android.content.Context
import android.os.Bundle
import com.getcode.model.PrefsString
import com.getcode.network.repository.PrefRepository
import com.getcode.utils.startupLog


class AccountAuthenticator(private val mContext: Context) : AbstractAccountAuthenticator(mContext) {
class AccountAuthenticator(
private val context: Context,
) : AbstractAccountAuthenticator(context) {
@Throws(NetworkErrorException::class)
override fun addAccount(
response: AccountAuthenticatorResponse,
Expand All @@ -32,9 +36,9 @@ class AccountAuthenticator(private val mContext: Context) : AbstractAccountAuthe
options: Bundle
): Bundle {
// Extract the username and password from the Account Manager, then, generate token
val am = AccountManager.get(mContext)
val am = AccountManager.get(context)
var authToken = am.peekAuthToken(account, authTokenType)
startupLog("authenticator: authToken ${authToken != null}")
startupLog("authenticator: authToken ${authToken != null}, $authTokenType")
// Lets give another try to authenticate the user
if (null != authToken) {
if (authToken.isEmpty()) {
Expand All @@ -56,7 +60,10 @@ class AccountAuthenticator(private val mContext: Context) : AbstractAccountAuthe
}
}

startupLog("authenticator failure", Throwable("Failed to retrieve authToken from AccountManager"))
startupLog(
"authenticator failure",
Throwable("Failed to retrieve authToken from AccountManager")
)
// If we get here, then we couldn't access the user's password
return Bundle()
}
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/getcode/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.getcode.LocalExchange
import com.getcode.LocalNetworkObserver
import com.getcode.LocalPhoneFormatter
import com.getcode.analytics.AnalyticsService
import com.getcode.network.client.Client
import com.getcode.network.exchange.Exchange
import com.getcode.ui.utils.handleUncaughtException
import com.getcode.util.CurrencyUtils
Expand All @@ -30,6 +31,9 @@ import javax.inject.Inject
@AndroidEntryPoint
class MainActivity : FragmentActivity() {

@Inject
lateinit var client: Client

@Inject
lateinit var analyticsManager: AnalyticsService

Expand Down Expand Up @@ -96,5 +100,15 @@ class MainActivity : FragmentActivity() {
private fun setFullscreen() {
enableEdgeToEdge()
}

override fun onResume() {
super.onResume()
client.startTimer()
}

override fun onStop() {
super.onStop()
client.stopTimer()
}
}

2 changes: 1 addition & 1 deletion app/src/main/res/xml/authenticator.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.getcode"
android:accountType="@string/applicationId"
android:icon="@mipmap/ic_launcher_round"
android:smallIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"/>
Loading
Loading