DeviceKeyStore
Where a device keeps its own DeviceKey and the Grants that let it back into rooms.
A device is recognised when it comes back only if it comes back with the same key, so the key has to outlive the app: a closed and reopened app, a restarted phone. That is this store's whole job. The first deviceKey call makes the key and every later call returns it. Beside it the store keeps the device's grants, the newest from each host for each scope, so a returning device has a pass to show in every room it may come back to.
A grant is not a secret — it names this device's key and is useless to anyone who cannot sign with it — so a store keeps grants in plain view. The key is the secret, and how well it is kept is what tells one store from another. InMemoryDeviceKeyStore keeps both only as long as the process lives, which suits tests and an app that has chosen not to be recognised.
Every implementation must be safe to call from many coroutines at once, on a multi-threaded dispatcher: concurrent first calls to deviceKey agree on one key.
Samples
// One store per device. This one forgets with the process; a platform store keeps the key on disk.
val store: DeviceKeyStore = InMemoryDeviceKeyStore()
// The key is made on first use, and every call after returns the same one.
val me = store.deviceKey()
check(store.deviceKey().id == me.id)
// A host lets this device back in; the device keeps the newest pass per host and scope.
val host = DeviceKey.generate()
val now = 1_700_000_000_000L
val first = Grant.issue(host, me.id, GrantScope.AnyRoomOfIssuer, issuedAt = now, notAfter = now + 3_600_000)
val renewed = Grant.issue(host, me.id, GrantScope.AnyRoomOfIssuer, issuedAt = now + 1_800_000, notAfter = now + 5_400_000)
store.keepGrant(renewed)
store.keepGrant(first) // older than the one held from this host: ignored
check(store.grants() == listOf(renewed))Inheritors
Types
Where each platform's one-line default hangs: DeviceKeyStore.platform(appId, …), declared in that platform's own source set, since what a platform needs to find its storage differs — a directory on the desktop JVM, and nothing but the app id on iOS and macOS.
Properties
The scheme a room built on this store signs and checks every signature with: KeyScheme.P256, ECDSA through the platform's provider, unless the store was built for tests.
Functions
The grants this device holds: the newest per issuer and scope, but for a post-dated one a fresh pass replaced (keepFreshGrant). Not secret — useless without the key.
Keeps grant as keepGrant does, with one exception: a held grant from the same issuer for the same scope dated more than GrantTrust.CLOCK_SKEW_ALLOWANCE_MILLIS after grant gives way to it, although grant is older (identity Task 2.5, #3221).
Keeps grant, replacing the one held from the same Grant.issuer for the same Grant.scope when grant is newer by Grant.issuedAt. One host key can host many rooms, so a pass into Room("A") and one into Room("B") from the same issuer are both kept, and so is a grant from another issuer. A grant that is not newer — older, or issued at the same instant — is ignored, so a replayed or reordered copy can never displace the grant already held (Decided 21).