Skip to content

Commit

Permalink
Fix to have hardcoded issuer work without server.
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Sorotokin <[email protected]>
  • Loading branch information
sorotokin committed Sep 26, 2024
1 parent ba2fab4 commit afa81ee
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.android.identity_credential.wallet.R
import com.android.identity_credential.wallet.SettingsModel
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import kotlinx.coroutines.runBlocking
import kotlinx.io.bytestring.ByteString
import java.io.ByteArrayOutputStream
import java.nio.charset.StandardCharsets
Expand All @@ -27,12 +28,12 @@ import kotlin.reflect.cast
* This implementation of [FlowEnvironment] can be used to run wallet server locally in the app,
* which is useful for development, but should never be done in production.
*/
class LocalDevelopmentEnvironment(
internal class LocalDevelopmentEnvironment(
context: Context,
settingsModel: SettingsModel,
private val secureArea: SecureArea,
private val notifications: FlowNotifications,
private val applicationSupport: ApplicationSupport
private val applicationSupportSupplier: WalletServerProvider.ApplicationSupportSupplier
) : FlowEnvironment {
private var configuration = ConfigurationImpl(context, settingsModel)
private val storage = StorageImpl(context, "dev_local_data")
Expand All @@ -58,7 +59,13 @@ class LocalDevelopmentEnvironment(
FlowNotifications::class -> notifications
HttpClient::class -> httpClient
SecureArea::class -> secureArea
ApplicationSupport::class -> applicationSupport
ApplicationSupport::class -> runBlocking {
// We do not want to attempt to obtain applicationSupport ahead of time
// as there may be connection problems and we want to deal with them only
// if we have to, thus runBlocking is used. But this code is only used for
// "dev:" Wallet Server.
applicationSupportSupplier.getApplicationSupport()
}
else -> return null
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.android.identity.issuance.authenticationMessage
import com.android.identity.issuance.extractAttestationSequence
import com.android.identity.issuance.wallet.WalletServerState
import com.android.identity.securearea.KeyInfo
import com.android.identity.storage.StorageEngine
import com.android.identity.util.Logger
import com.android.identity_credential.wallet.SettingsModel
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -66,13 +65,12 @@ class WalletServerProvider(
private val context: Context,
private val secureArea: AndroidKeystoreSecureArea,
private val settingsModel: SettingsModel,
private val storageEngine: StorageEngine,
private val getWalletApplicationCapabilities: suspend () -> WalletApplicationCapabilities
) {
private val instanceLock = Mutex()
private var instance: WalletServer? = null
private val issuingAuthorityMap = mutableMapOf<String, IssuingAuthority>()
private var applicationSupport: ApplicationSupport? = null
private var applicationSupportSupplier: ApplicationSupportSupplier? = null

private var notificationsJob: Job? = null
private var resetListeners = mutableListOf<()->Unit>()
Expand Down Expand Up @@ -120,13 +118,13 @@ class WalletServerProvider(
for (issuingAuthority in issuingAuthorityMap.values) {
issuingAuthority.complete()
}
applicationSupport?.complete()
applicationSupportSupplier?.release()
instance?.complete()
} catch (err: Exception) {
Logger.e(TAG, "Error shutting down Wallet Server connection", err)
}
issuingAuthorityMap.clear()
applicationSupport = null
applicationSupportSupplier = null
instance = null
notificationsJob?.cancel()
notificationsJob = null
Expand Down Expand Up @@ -158,7 +156,9 @@ class WalletServerProvider(
instanceLock.withLock {
if (instance == null) {
Logger.i(TAG, "Creating new WalletServer instance: $baseUrl")
instance = getWalletServerUnlocked(baseUrl)
val server = getWalletServerUnlocked(baseUrl)
instance = server.first
applicationSupportSupplier = server.second
Logger.i(TAG, "Created new WalletServer instance: $baseUrl")
} else {
Logger.i(TAG, "Reusing existing WalletServer instance: $baseUrl")
Expand Down Expand Up @@ -207,25 +207,27 @@ class WalletServerProvider(
*/
suspend fun getApplicationSupport(): ApplicationSupport {
getWalletServer()
return applicationSupport!!
return applicationSupportSupplier!!.getApplicationSupport()
}

private suspend fun getWalletServerUnlocked(baseUrl: String): WalletServer {
private suspend fun getWalletServerUnlocked(
baseUrl: String
): Pair<WalletServer, ApplicationSupportSupplier> {
val dispatcher: FlowDispatcher
val notifier: FlowNotifier
val exceptionMapBuilder = FlowExceptionMap.Builder()
var applicationSupportSupplier: ApplicationSupportSupplier? = null
WalletServerState.registerExceptions(exceptionMapBuilder)
if (baseUrl == "dev:") {
val builder = FlowDispatcherLocal.Builder()
WalletServerState.registerAll(builder)
notifier = FlowNotificationsLocal(noopCipher)
if (applicationSupport == null) {
// this will initialize applicationSupport object
getWalletServerUnlocked(settingsModel.minServerUrl.value!!)
check(applicationSupport != null)
applicationSupportSupplier = ApplicationSupportSupplier() {
val minServer = getWalletServerUnlocked(settingsModel.minServerUrl.value!!)
minServer.second.getApplicationSupport()
}
val environment = LocalDevelopmentEnvironment(
context, settingsModel, secureArea, notifier, applicationSupport!!)
context, settingsModel, secureArea, notifier, applicationSupportSupplier)
dispatcher = WrapperFlowDispatcher(builder.build(
environment,
noopCipher,
Expand Down Expand Up @@ -288,11 +290,13 @@ class WalletServerProvider(
))
authentication.complete()

if (baseUrl != "dev:") {
applicationSupport = walletServer.applicationSupport()
if (applicationSupportSupplier == null) {
applicationSupportSupplier = ApplicationSupportSupplier {
walletServer.applicationSupport()
}
}

return walletServer
return Pair(walletServer, applicationSupportSupplier)
}

/**
Expand Down Expand Up @@ -324,4 +328,19 @@ class WalletServerProvider(
return (durationNano/1000000).toString().padEnd(4, ' ')
}
}

internal class ApplicationSupportSupplier(val factory: suspend () -> ApplicationSupport) {
private var applicationSupport: ApplicationSupport? = null

suspend fun getApplicationSupport(): ApplicationSupport {
if (applicationSupport == null) {
applicationSupport = factory()
}
return applicationSupport!!
}

suspend fun release() {
applicationSupport?.complete()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ class WalletApplication : Application() {
this,
this.
androidKeystoreSecureArea,
settingsModel,
storageEngine,
{ getWalletApplicationInformation() }
)
settingsModel
) {
getWalletApplicationInformation()
}

// init TrustManagers
readerTrustManager.addTrustPoint(
Expand Down

0 comments on commit afa81ee

Please sign in to comment.