@circle-fin/daa-react-native-sdk 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DaaReactNativeSdk.podspec +14 -8
- package/README.md +498 -24
- package/android/build.gradle +12 -7
- package/android/src/main/java/com/circle/daa/reactnative/DaaRegistrationModule.kt +1 -18
- package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyExceptions.kt +38 -4
- package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyModule.kt +139 -6
- package/android/src/main/java/com/circle/daa/reactnative/InstallationIdentityStore.kt +50 -1
- package/build/DaaClient.d.ts +183 -3
- package/build/DaaClient.js +296 -12
- package/build/api/AccountsClient.d.ts +3 -0
- package/build/api/AccountsClient.js +3 -0
- package/build/api/AuthClient.d.ts +17 -42
- package/build/api/AuthClient.js +26 -62
- package/build/api/ContractExecutionsClient.d.ts +114 -0
- package/build/api/ContractExecutionsClient.js +462 -0
- package/build/api/DaaUserClient.d.ts +74 -11
- package/build/api/DaaUserClient.js +84 -17
- package/build/api/TransactionsClient.d.ts +1 -1
- package/build/api/TransactionsClient.js +1 -0
- package/build/api/TransfersClient.d.ts +3 -0
- package/build/api/TransfersClient.js +3 -0
- package/build/api/WithdrawalsClient.d.ts +3 -0
- package/build/api/WithdrawalsClient.js +3 -0
- package/build/api/deviceApproval.d.ts +48 -0
- package/build/api/deviceApproval.js +247 -0
- package/build/api/deviceStatus.d.ts +61 -0
- package/build/api/deviceStatus.js +216 -0
- package/build/api/loginFailure.d.ts +10 -2
- package/build/api/loginFailure.js +33 -2
- package/build/api/signedResource.d.ts +49 -2
- package/build/api/signedResource.js +135 -36
- package/build/auth/RegistrationFlowKey.js +1 -1
- package/build/auth/daaRegistrationNative.d.ts +0 -22
- package/build/auth/daaRegistrationNative.js +3 -29
- package/build/auth/deviceAuthorization.d.ts +17 -3
- package/build/auth/deviceAuthorization.js +60 -8
- package/build/auth/registrationAuthorization.d.ts +2 -2
- package/build/auth/registrationAuthorization.js +13 -10
- package/build/config.d.ts +27 -0
- package/build/config.js +15 -0
- package/build/crypto/matchingCode.d.ts +47 -0
- package/build/crypto/matchingCode.js +93 -0
- package/build/errors.d.ts +150 -33
- package/build/errors.js +208 -47
- package/build/index.d.ts +11 -3
- package/build/models/ContractExecution.d.ts +197 -0
- package/build/models/ContractExecution.js +130 -0
- package/build/models/CreateContractExecutionRequest.d.ts +81 -0
- package/build/models/CreateContractExecutionRequest.js +19 -0
- package/build/models/DaaUserContext.d.ts +53 -3
- package/build/models/DaaUserContext.js +33 -2
- package/build/models/DeviceStatus.d.ts +69 -0
- package/build/models/DeviceStatus.js +19 -0
- package/build/models/PendingDeviceApproval.d.ts +121 -0
- package/build/models/PendingDeviceApproval.js +208 -0
- package/build/models/PendingDeviceRegistration.d.ts +162 -23
- package/build/models/PendingDeviceRegistration.js +525 -69
- package/build/models/PreparedContractExecution.d.ts +70 -0
- package/build/models/PreparedContractExecution.js +77 -0
- package/build/models/PreparedSocialLogin.d.ts +19 -26
- package/build/models/PreparedSocialLogin.js +32 -15
- package/build/models/Transaction.d.ts +48 -2
- package/build/models/Transaction.js +52 -4
- package/build/models/codeSecrets.d.ts +100 -0
- package/build/models/codeSecrets.js +321 -0
- package/build/securekey/InMemorySecureKey.d.ts +3 -1
- package/build/securekey/InMemorySecureKey.js +47 -6
- package/build/securekey/NativeSecureKey.d.ts +3 -1
- package/build/securekey/NativeSecureKey.js +204 -27
- package/build/securekey/NativeSecureKeyBridge.d.ts +72 -4
- package/build/securekey/SecureKey.d.ts +128 -8
- package/build/securekey/alias.d.ts +9 -0
- package/build/securekey/alias.js +11 -2
- package/build/securekey/ownerAuthKeyResolver.d.ts +26 -9
- package/build/securekey/ownerAuthKeyResolver.js +35 -20
- package/build/securekey/registration.d.ts +19 -0
- package/build/securekey/registration.js +38 -0
- package/build/securekey/resolveNativeBridge.d.ts +3 -0
- package/build/securekey/resolveNativeBridge.js +88 -7
- package/build/transport/headers.d.ts +11 -0
- package/build/transport/headers.js +36 -0
- package/build/wire/contractExecutionPayload.d.ts +24 -0
- package/build/wire/contractExecutionPayload.js +130 -0
- package/build/wire/daaStatusCodes.d.ts +20 -3
- package/build/wire/daaStatusCodes.js +20 -3
- package/build/wire/deviceContextPayload.d.ts +9 -7
- package/build/wire/deviceContextPayload.js +9 -7
- package/build/wire/deviceRegistrationResultPayload.d.ts +55 -10
- package/build/wire/deviceRegistrationResultPayload.js +116 -82
- package/build/wire/deviceStatusPayload.d.ts +20 -0
- package/build/wire/deviceStatusPayload.js +66 -0
- package/build/wire/instant.js +4 -1
- package/build/wire/pendingRegistrationPayload.d.ts +16 -0
- package/build/wire/pendingRegistrationPayload.js +18 -0
- package/build/wire/socialLoginRequestPayload.d.ts +0 -5
- package/build/wire/socialLoginRequestPayload.js +2 -19
- package/build/wire/transactionPayload.d.ts +2 -2
- package/build/wire/transactionPayload.js +27 -4
- package/ios/DaaRegistrationModule.swift +1 -19
- package/ios/DaaSecureKeyExceptions.swift +53 -5
- package/ios/DaaSecureKeyModule.swift +149 -5
- package/ios/InstallationIdentityStore.swift +43 -7
- package/package.json +1 -1
- package/src/DaaClient.ts +353 -15
- package/src/api/AccountsClient.ts +3 -0
- package/src/api/AuthClient.ts +33 -98
- package/src/api/ContractExecutionsClient.ts +527 -0
- package/src/api/DaaUserClient.ts +92 -17
- package/src/api/TransactionsClient.ts +1 -0
- package/src/api/TransfersClient.ts +3 -0
- package/src/api/WithdrawalsClient.ts +3 -0
- package/src/api/deviceApproval.ts +306 -0
- package/src/api/deviceStatus.ts +238 -0
- package/src/api/loginFailure.ts +39 -2
- package/src/api/signedResource.ts +206 -46
- package/src/auth/RegistrationFlowKey.ts +1 -1
- package/src/auth/daaRegistrationNative.ts +4 -49
- package/src/auth/deviceAuthorization.ts +74 -11
- package/src/auth/registrationAuthorization.ts +12 -9
- package/src/config.ts +36 -0
- package/src/crypto/matchingCode.ts +100 -0
- package/src/errors.ts +309 -52
- package/src/index.ts +36 -1
- package/src/models/ContractExecution.ts +250 -0
- package/src/models/CreateContractExecutionRequest.ts +100 -0
- package/src/models/DaaUserContext.ts +59 -3
- package/src/models/DeviceStatus.ts +50 -0
- package/src/models/PendingDeviceApproval.ts +229 -0
- package/src/models/PendingDeviceRegistration.ts +673 -79
- package/src/models/PreparedContractExecution.ts +128 -0
- package/src/models/PreparedSocialLogin.ts +41 -31
- package/src/models/Transaction.ts +77 -2
- package/src/models/codeSecrets.ts +360 -0
- package/src/securekey/InMemorySecureKey.ts +61 -7
- package/src/securekey/NativeSecureKey.ts +234 -25
- package/src/securekey/NativeSecureKeyBridge.ts +84 -4
- package/src/securekey/SecureKey.ts +138 -8
- package/src/securekey/alias.ts +11 -3
- package/src/securekey/ownerAuthKeyResolver.ts +63 -24
- package/src/securekey/registration.ts +40 -0
- package/src/securekey/resolveNativeBridge.ts +167 -9
- package/src/transport/headers.ts +47 -0
- package/src/wire/contractExecutionPayload.ts +152 -0
- package/src/wire/daaStatusCodes.ts +20 -3
- package/src/wire/deviceContextPayload.ts +9 -7
- package/src/wire/deviceRegistrationResultPayload.ts +156 -98
- package/src/wire/deviceStatusPayload.ts +68 -0
- package/src/wire/instant.ts +4 -1
- package/src/wire/pendingRegistrationPayload.ts +44 -1
- package/src/wire/socialLoginRequestPayload.ts +2 -25
- package/src/wire/transactionPayload.ts +40 -4
- package/build/auth/prepareReceipt.d.ts +0 -150
- package/build/auth/prepareReceipt.js +0 -357
- package/build/crypto/es256Jws.d.ts +0 -56
- package/build/crypto/es256Jws.js +0 -196
- package/src/auth/prepareReceipt.ts +0 -522
- package/src/crypto/es256Jws.ts +0 -240
|
@@ -16,12 +16,23 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
import { isRecord } from '../config'
|
|
19
20
|
import { fromBase64Url, toBase64Url } from '../crypto/base64url'
|
|
20
21
|
import { p256JwkThumbprint } from '../crypto/jwkThumbprint'
|
|
21
22
|
import { contractViolation, mapNativeError } from '../errors'
|
|
23
|
+
import { isUuid } from '../validation/uuid'
|
|
22
24
|
import { secureKeyAlias } from './alias'
|
|
23
25
|
import type { NativeSecureKeyBridge } from './NativeSecureKeyBridge'
|
|
24
|
-
import
|
|
26
|
+
import { assertRegistration } from './registration'
|
|
27
|
+
import type {
|
|
28
|
+
AuthKeyRegistration,
|
|
29
|
+
AuthKeyResolution,
|
|
30
|
+
DeviceApprovalSigning,
|
|
31
|
+
OwnerAuthKey,
|
|
32
|
+
OwnerKeyIdentity,
|
|
33
|
+
RegisteredOwnerAuthKey,
|
|
34
|
+
SecureKeyStore,
|
|
35
|
+
} from './SecureKey'
|
|
25
36
|
|
|
26
37
|
// Production DAA Owner-key store backed by the platform secure-key module. It
|
|
27
38
|
// holds no key material itself — every operation delegates to the native bridge,
|
|
@@ -81,9 +92,30 @@ function parseNativeResolution(value: unknown): AuthKeyResolution {
|
|
|
81
92
|
throw contractViolation('native resolveOrCreate must return created or existing')
|
|
82
93
|
}
|
|
83
94
|
|
|
95
|
+
// The record resolveExisting returned, validated at the store seam for the same
|
|
96
|
+
// reason as parseNativeResolution: an injected stand-in is untyped at runtime,
|
|
97
|
+
// and these identifiers become a JWS `kid` and an Owner-bound context.
|
|
98
|
+
function parseNativeRegistration(value: unknown): Omit<RegisteredOwnerAuthKey, 'authKey'> {
|
|
99
|
+
if (!isRecord(value)) {
|
|
100
|
+
throw contractViolation('native resolveExisting must return a registration record or null')
|
|
101
|
+
}
|
|
102
|
+
const { authKeyId, ownerBindingId } = value
|
|
103
|
+
if (
|
|
104
|
+
typeof authKeyId !== 'string' ||
|
|
105
|
+
typeof ownerBindingId !== 'string' ||
|
|
106
|
+
!isUuid(authKeyId) ||
|
|
107
|
+
!isUuid(ownerBindingId)
|
|
108
|
+
) {
|
|
109
|
+
throw contractViolation('native resolveExisting returned malformed registration identifiers')
|
|
110
|
+
}
|
|
111
|
+
return { authKeyId, ownerBindingId }
|
|
112
|
+
}
|
|
113
|
+
|
|
84
114
|
class NativeOwnerAuthKey implements OwnerAuthKey {
|
|
85
115
|
#resolution: AuthKeyResolution
|
|
86
|
-
|
|
116
|
+
// The most recently queued acknowledgement. Every acknowledgement chains
|
|
117
|
+
// behind it, so upserts reach native in call order.
|
|
118
|
+
#ackTail: { key: string; done: Promise<void> } | null = null
|
|
87
119
|
readonly #bridge: NativeSecureKeyBridge
|
|
88
120
|
readonly #alias: string
|
|
89
121
|
|
|
@@ -93,32 +125,59 @@ class NativeOwnerAuthKey implements OwnerAuthKey {
|
|
|
93
125
|
this.#resolution = resolution
|
|
94
126
|
}
|
|
95
127
|
|
|
128
|
+
/**
|
|
129
|
+
* @internal Native has just confirmed this key's registration
|
|
130
|
+
* (`resolveExisting`). A handle cached as `created` before the key was
|
|
131
|
+
* acknowledged, for example by another `DaaClient`'s store, would otherwise
|
|
132
|
+
* stay `created` and be refused for registered-device signing. Moves it to
|
|
133
|
+
* `existing` and keeps its acknowledgement queue.
|
|
134
|
+
*/
|
|
135
|
+
markRegistered(): void {
|
|
136
|
+
this.#resolution = 'existing'
|
|
137
|
+
}
|
|
138
|
+
|
|
96
139
|
resolution(): AuthKeyResolution {
|
|
97
140
|
return this.#resolution
|
|
98
141
|
}
|
|
99
142
|
|
|
100
|
-
async acknowledgeRegistration(): Promise<void> {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
143
|
+
async acknowledgeRegistration(registration: AuthKeyRegistration): Promise<void> {
|
|
144
|
+
const { authKeyId, ownerBindingId } = assertRegistration(registration)
|
|
145
|
+
// No early return once `existing`: native always upserts, which is what
|
|
146
|
+
// lets a re-registration that assigned new identifiers replace the stored
|
|
147
|
+
// ones.
|
|
148
|
+
//
|
|
149
|
+
// Serialized per handle: each acknowledgement waits for the one queued
|
|
150
|
+
// before it (whatever its outcome), so when two calls carry different
|
|
151
|
+
// identifiers the later call's identifiers are the ones native keeps. A
|
|
152
|
+
// call recording the SAME identifiers as the most recently queued one
|
|
153
|
+
// shares it instead of queuing a duplicate round trip — it would write the
|
|
154
|
+
// same record in the same position.
|
|
155
|
+
//
|
|
156
|
+
// The tail is cleared once it settles either way: on success so a later
|
|
157
|
+
// call upserts again rather than trusting an earlier write, and on failure
|
|
158
|
+
// so a transient native error stays retryable instead of being memoized.
|
|
159
|
+
const key = `${authKeyId}\n${ownerBindingId}`
|
|
160
|
+
const tail = this.#ackTail
|
|
161
|
+
if (tail !== null && tail.key === key) {
|
|
162
|
+
return tail.done
|
|
120
163
|
}
|
|
121
|
-
|
|
164
|
+
const previous = tail === null ? Promise.resolve() : tail.done.catch(() => undefined)
|
|
165
|
+
const done: Promise<void> = previous
|
|
166
|
+
.then(() =>
|
|
167
|
+
guardNative(() =>
|
|
168
|
+
this.#bridge.acknowledgeRegistration(this.#alias, authKeyId, ownerBindingId),
|
|
169
|
+
),
|
|
170
|
+
)
|
|
171
|
+
.then(() => {
|
|
172
|
+
this.#resolution = 'existing'
|
|
173
|
+
})
|
|
174
|
+
.finally(() => {
|
|
175
|
+
if (this.#ackTail?.done === done) {
|
|
176
|
+
this.#ackTail = null
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
this.#ackTail = { key, done }
|
|
180
|
+
return done
|
|
122
181
|
}
|
|
123
182
|
|
|
124
183
|
async publicKeySpkiBase64Url(): Promise<string> {
|
|
@@ -150,10 +209,56 @@ class NativeOwnerAuthKey implements OwnerAuthKey {
|
|
|
150
209
|
)
|
|
151
210
|
return decodeExact(raw, 64, 'registration signature (JOSE R||S; convert DER ECDSA to R||S)')
|
|
152
211
|
}
|
|
212
|
+
|
|
213
|
+
async signDeviceApproval(
|
|
214
|
+
message: Uint8Array,
|
|
215
|
+
approval: DeviceApprovalSigning,
|
|
216
|
+
): Promise<Uint8Array> {
|
|
217
|
+
const signDeviceApproval = this.#bridge.signDeviceApproval
|
|
218
|
+
if (typeof signDeviceApproval !== 'function') {
|
|
219
|
+
throw contractViolation('native DaaSecureKey.signDeviceApproval is required')
|
|
220
|
+
}
|
|
221
|
+
const raw = await guardNative(() =>
|
|
222
|
+
signDeviceApproval.call(
|
|
223
|
+
this.#bridge,
|
|
224
|
+
this.#alias,
|
|
225
|
+
toBase64Url(message),
|
|
226
|
+
toBase64Url(approval.requestBody),
|
|
227
|
+
toBase64Url(approval.codeSecret),
|
|
228
|
+
approval.enteredCode,
|
|
229
|
+
),
|
|
230
|
+
)
|
|
231
|
+
return decodeExact(raw, 64, 'approval signature (JOSE R||S; convert DER ECDSA to R||S)')
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async codeSecret(): Promise<Uint8Array> {
|
|
235
|
+
const codeSecret = this.#bridge.codeSecret
|
|
236
|
+
if (typeof codeSecret !== 'function') {
|
|
237
|
+
throw contractViolation('native DaaSecureKey.codeSecret is required')
|
|
238
|
+
}
|
|
239
|
+
const raw = await guardNative(() => codeSecret.call(this.#bridge, this.#alias))
|
|
240
|
+
return decodeExact(raw, 32, 'code secret')
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async deleteCodeSecret(secret: Uint8Array): Promise<void> {
|
|
244
|
+
const deleteCodeSecret = this.#bridge.deleteCodeSecret
|
|
245
|
+
if (typeof deleteCodeSecret !== 'function') {
|
|
246
|
+
throw contractViolation('native DaaSecureKey.deleteCodeSecret is required')
|
|
247
|
+
}
|
|
248
|
+
await guardNative(() => deleteCodeSecret.call(this.#bridge, this.#alias, toBase64Url(secret)))
|
|
249
|
+
}
|
|
153
250
|
}
|
|
154
251
|
|
|
155
252
|
export class NativeSecureKeyStore implements SecureKeyStore {
|
|
156
|
-
readonly #keys = new Map<string, Promise<
|
|
253
|
+
readonly #keys = new Map<string, Promise<NativeOwnerAuthKey>>()
|
|
254
|
+
// Bumped by `forget`, per alias. A `resolveExisting` read that started under
|
|
255
|
+
// an older generation raced a forget, and must not cache or return a handle
|
|
256
|
+
// to the key that forget deleted.
|
|
257
|
+
readonly #generations = new Map<string, number>()
|
|
258
|
+
// How many `forget` calls are running for each alias. A read that settles
|
|
259
|
+
// while a delete is still in progress fails closed even when the generation
|
|
260
|
+
// still matches: it may have read the record just before native deleted it.
|
|
261
|
+
readonly #forgetting = new Map<string, number>()
|
|
157
262
|
readonly #bridge: NativeSecureKeyBridge
|
|
158
263
|
|
|
159
264
|
constructor(bridge: NativeSecureKeyBridge) {
|
|
@@ -169,11 +274,115 @@ export class NativeSecureKeyStore implements SecureKeyStore {
|
|
|
169
274
|
existing = guardNative(() => this.#bridge.resolveOrCreate(alias))
|
|
170
275
|
.then((raw) => new NativeOwnerAuthKey(this.#bridge, alias, parseNativeResolution(raw)))
|
|
171
276
|
.catch((err: unknown) => {
|
|
172
|
-
this
|
|
277
|
+
// Only this call's own entry: `forget` may have evicted it and a
|
|
278
|
+
// newer resolveOrCreate installed its own in the meantime.
|
|
279
|
+
if (this.#keys.get(alias) === existing) {
|
|
280
|
+
this.#keys.delete(alias)
|
|
281
|
+
}
|
|
173
282
|
throw err
|
|
174
283
|
})
|
|
175
284
|
this.#keys.set(alias, existing)
|
|
176
285
|
}
|
|
177
286
|
return existing
|
|
178
287
|
}
|
|
288
|
+
|
|
289
|
+
async resolveExisting(identity: OwnerKeyIdentity): Promise<RegisteredOwnerAuthKey | null> {
|
|
290
|
+
const alias = secureKeyAlias(identity)
|
|
291
|
+
const generation = this.#generations.get(alias) ?? 0
|
|
292
|
+
// Never bridge.resolveOrCreate: this is the read-only path. Every call
|
|
293
|
+
// reads native, never joining a read already in flight: an acknowledgement
|
|
294
|
+
// can settle while that read is outstanding, and a caller arriving after it
|
|
295
|
+
// must see the identifiers it recorded.
|
|
296
|
+
const raw = await guardNative(() => this.#bridge.resolveExisting(alias))
|
|
297
|
+
if (raw === null) {
|
|
298
|
+
return null
|
|
299
|
+
}
|
|
300
|
+
const registration = parseNativeRegistration(raw)
|
|
301
|
+
// Checked before a handle is cached (inside `#registeredHandle`, after each
|
|
302
|
+
// wait), and again before it is returned, since a forget can start after.
|
|
303
|
+
this.#assertNotForgotten(alias, generation)
|
|
304
|
+
const authKey = await this.#registeredHandle(alias, generation)
|
|
305
|
+
this.#assertNotForgotten(alias, generation)
|
|
306
|
+
return Object.freeze({ authKey, ...registration })
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* `forget` ran, or is still running, while a read was in flight: the record
|
|
311
|
+
* may describe a key that no longer exists. Fail closed rather than hand a
|
|
312
|
+
* restore a handle it would try to sign with, or put that handle back into
|
|
313
|
+
* the cache for a later resolveOrCreate to reuse.
|
|
314
|
+
*/
|
|
315
|
+
#assertNotForgotten(alias: string, generation: number): void {
|
|
316
|
+
if (
|
|
317
|
+
(this.#generations.get(alias) ?? 0) !== generation ||
|
|
318
|
+
(this.#forgetting.get(alias) ?? 0) > 0
|
|
319
|
+
) {
|
|
320
|
+
throw contractViolation('the local AuthKey was forgotten while it was being read')
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* The one handle for `alias` that native has just confirmed is registered.
|
|
326
|
+
*
|
|
327
|
+
* One handle per alias, never two: each handle serializes its own
|
|
328
|
+
* acknowledgements, so a second handle for the same key would let upserts
|
|
329
|
+
* reach native out of call order. A cached handle, including a
|
|
330
|
+
* `resolveOrCreate` still in flight, is reused and marked `existing`,
|
|
331
|
+
* since native has just confirmed the registration. Only when nothing usable is
|
|
332
|
+
* cached is a new `existing` handle stored, so a later `resolveOrCreate`
|
|
333
|
+
* (`DaaClient.forUser`) reuses it instead of calling native again.
|
|
334
|
+
*
|
|
335
|
+
* A forget can finish while this waits on a cached resolution. So after
|
|
336
|
+
* every wait, and before touching the cache or a handle, it re-checks
|
|
337
|
+
* `generation`: otherwise it could cache an `existing` handle to the deleted
|
|
338
|
+
* key, or mark a key minted after the forget as `existing`.
|
|
339
|
+
*/
|
|
340
|
+
async #registeredHandle(alias: string, generation: number): Promise<OwnerAuthKey> {
|
|
341
|
+
let cached = this.#keys.get(alias)
|
|
342
|
+
while (cached !== undefined) {
|
|
343
|
+
let handle: NativeOwnerAuthKey | undefined
|
|
344
|
+
try {
|
|
345
|
+
handle = await cached
|
|
346
|
+
} catch {
|
|
347
|
+
// That resolveOrCreate failed and has evicted itself. Native has just
|
|
348
|
+
// returned this key's registration, so a failed resolution must not
|
|
349
|
+
// fail this read: reuse whatever replaced it, else mint a fresh handle.
|
|
350
|
+
}
|
|
351
|
+
this.#assertNotForgotten(alias, generation)
|
|
352
|
+
if (handle !== undefined) {
|
|
353
|
+
handle.markRegistered()
|
|
354
|
+
return handle
|
|
355
|
+
}
|
|
356
|
+
const next = this.#keys.get(alias)
|
|
357
|
+
cached = next === cached ? undefined : next
|
|
358
|
+
}
|
|
359
|
+
const authKey = new NativeOwnerAuthKey(this.#bridge, alias, 'existing')
|
|
360
|
+
this.#keys.set(alias, Promise.resolve(authKey))
|
|
361
|
+
return authKey
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
async forget(identity: OwnerKeyIdentity): Promise<void> {
|
|
365
|
+
const alias = secureKeyAlias(identity)
|
|
366
|
+
// Invalidate any resolveExisting already in flight.
|
|
367
|
+
this.#generations.set(alias, (this.#generations.get(alias) ?? 0) + 1)
|
|
368
|
+
this.#forgetting.set(alias, (this.#forgetting.get(alias) ?? 0) + 1)
|
|
369
|
+
// Evicted before and after: a handle cached here must not outlive the
|
|
370
|
+
// native key it names, even when the native delete fails part-way.
|
|
371
|
+
this.#keys.delete(alias)
|
|
372
|
+
try {
|
|
373
|
+
await guardNative(() => this.#bridge.forgetLocalKey(alias))
|
|
374
|
+
} finally {
|
|
375
|
+
// A read that starts while the delete runs fails closed either way: if
|
|
376
|
+
// it settles before the delete does, `#forgetting` is still non-zero;
|
|
377
|
+
// if it settles after, this second bump has changed its generation.
|
|
378
|
+
const pending = (this.#forgetting.get(alias) ?? 1) - 1
|
|
379
|
+
if (pending > 0) {
|
|
380
|
+
this.#forgetting.set(alias, pending)
|
|
381
|
+
} else {
|
|
382
|
+
this.#forgetting.delete(alias)
|
|
383
|
+
}
|
|
384
|
+
this.#generations.set(alias, (this.#generations.get(alias) ?? 0) + 1)
|
|
385
|
+
this.#keys.delete(alias)
|
|
386
|
+
}
|
|
387
|
+
}
|
|
179
388
|
}
|
|
@@ -34,6 +34,18 @@ import type { AuthKeyResolution } from './SecureKey'
|
|
|
34
34
|
// The bridge-side name for the public `AuthKeyResolution`.
|
|
35
35
|
export type NativeAuthKeyResolution = AuthKeyResolution
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* The registration record native keeps beside a registered key, as the bridge
|
|
39
|
+
* returns it from {@link NativeSecureKeyBridge.resolveExisting}. Untrusted at
|
|
40
|
+
* runtime until `NativeSecureKeyStore` has validated it.
|
|
41
|
+
*/
|
|
42
|
+
export type NativeRegisteredAuthKey = Readonly<{
|
|
43
|
+
/** Server-assigned AuthKey id (UUID), exactly as acknowledged. */
|
|
44
|
+
authKeyId: string
|
|
45
|
+
/** Server-assigned Owner binding id (UUID), exactly as acknowledged. */
|
|
46
|
+
ownerBindingId: string
|
|
47
|
+
}>
|
|
48
|
+
|
|
37
49
|
export interface NativeSecureKeyBridge {
|
|
38
50
|
/**
|
|
39
51
|
* Ensure a non-exportable P-256 key exists for `alias`, creating it only when
|
|
@@ -52,11 +64,31 @@ export interface NativeSecureKeyBridge {
|
|
|
52
64
|
publicKeySpkiBase64Url(alias: string): Promise<string>
|
|
53
65
|
|
|
54
66
|
/**
|
|
55
|
-
* Persist that Circle registered the public key for `alias
|
|
56
|
-
*
|
|
57
|
-
*
|
|
67
|
+
* Persist that Circle registered the public key for `alias` under
|
|
68
|
+
* `authKeyId` and `ownerBindingId`. Always upserts the stored identifiers,
|
|
69
|
+
* even when the key is already acknowledged, so a re-registration that
|
|
70
|
+
* assigns new identifiers replaces the old ones. After it succeeds, later
|
|
71
|
+
* `resolveOrCreate` calls return `'existing'` and `resolveExisting` returns
|
|
72
|
+
* these identifiers.
|
|
73
|
+
*/
|
|
74
|
+
acknowledgeRegistration(alias: string, authKeyId: string, ownerBindingId: string): Promise<void>
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Read the registration record for `alias` without creating a key and
|
|
78
|
+
* without a user-authentication prompt.
|
|
79
|
+
*
|
|
80
|
+
* Returns `null` when no key exists or the key has no recorded identifiers.
|
|
81
|
+
* A non-null result counts as native's explicit resolution of the key, so
|
|
82
|
+
* `publicKeySpkiBase64Url` and `sign` work afterwards without
|
|
83
|
+
* `resolveOrCreate`.
|
|
58
84
|
*/
|
|
59
|
-
|
|
85
|
+
resolveExisting(alias: string): Promise<NativeRegisteredAuthKey | null>
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Delete the key for `alias` and its local metadata. Local only — it does not
|
|
89
|
+
* revoke the key with Circle. Idempotent: succeeds when no key exists.
|
|
90
|
+
*/
|
|
91
|
+
forgetLocalKey(alias: string): Promise<void>
|
|
60
92
|
|
|
61
93
|
// The RFC 7638 thumbprint is derived in TypeScript from the SPKI above (see
|
|
62
94
|
// crypto/jwkThumbprint), so the native module does not implement it — one
|
|
@@ -106,4 +138,52 @@ export interface NativeSecureKeyBridge {
|
|
|
106
138
|
* @returns unpadded Base64URL of the 64-byte JOSE `R || S` signature.
|
|
107
139
|
*/
|
|
108
140
|
signRegistrationDeviceAuthorization(alias: string, messageBase64Url: string): Promise<string>
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* ES256 signature over the exact `approve_device` signing input.
|
|
144
|
+
*
|
|
145
|
+
* Production `resolveNativeBridge()` sends it to Expo `signDeviceApproval`
|
|
146
|
+
* (native `DaaDeviceAuthorizationSigningInput` approval initializer on iOS,
|
|
147
|
+
* `forDeviceApproval` on Android), which checks the body, the code secret
|
|
148
|
+
* and the typed code before any biometric prompt, and shows the code in it.
|
|
149
|
+
* A wrong code rejects with `MATCHING_CODE_MISMATCH`.
|
|
150
|
+
*
|
|
151
|
+
* Optional so an injected stand-in written before device approval still
|
|
152
|
+
* type-checks; `NativeSecureKeyStore` fails closed without it.
|
|
153
|
+
*
|
|
154
|
+
* @param messageBase64Url unpadded Base64URL of the exact signing input.
|
|
155
|
+
* @param requestBodyBase64Url unpadded Base64URL of the exact approval body.
|
|
156
|
+
* @param codeSecretBase64Url unpadded Base64URL of the 32-byte code secret.
|
|
157
|
+
* @param enteredCode the code the user typed.
|
|
158
|
+
* @returns unpadded Base64URL of the 64-byte JOSE `R || S` signature.
|
|
159
|
+
*/
|
|
160
|
+
signDeviceApproval?(
|
|
161
|
+
alias: string,
|
|
162
|
+
messageBase64Url: string,
|
|
163
|
+
requestBodyBase64Url: string,
|
|
164
|
+
codeSecretBase64Url: string,
|
|
165
|
+
enteredCode: string,
|
|
166
|
+
): Promise<string>
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Native `DaaSecureKey.codeSecret()`: the key's stored add-a-device code
|
|
170
|
+
* secret, created on first use, kept across restarts. Needs the key
|
|
171
|
+
* resolved (`resolveOrCreate`, or a non-null `resolveExisting`). Never
|
|
172
|
+
* prompts.
|
|
173
|
+
*
|
|
174
|
+
* Optional so an injected stand-in written before it still type-checks;
|
|
175
|
+
* `NativeSecureKeyStore` fails closed without it.
|
|
176
|
+
*
|
|
177
|
+
* @returns unpadded Base64URL of the 32-byte secret.
|
|
178
|
+
*/
|
|
179
|
+
codeSecret?(alias: string): Promise<string>
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Native `DaaSecureKey.deleteCodeSecret(_:)`: deletes the stored code
|
|
183
|
+
* secret only when it equals the given bytes. Succeeds when nothing
|
|
184
|
+
* matches or no key exists. Never prompts and never mints anything.
|
|
185
|
+
*
|
|
186
|
+
* @param secretBase64Url unpadded Base64URL of the 32-byte secret.
|
|
187
|
+
*/
|
|
188
|
+
deleteCodeSecret?(alias: string, secretBase64Url: string): Promise<void>
|
|
109
189
|
}
|
|
@@ -38,9 +38,12 @@ export type AppKeyIdentity = Readonly<{
|
|
|
38
38
|
* P-256/ES256 key per `{appId, environment, clientEntityId (Owner),
|
|
39
39
|
* installation}`.
|
|
40
40
|
*
|
|
41
|
-
* `clientEntityId` must be the SERVER-CONFIRMED value
|
|
42
|
-
* response echoed back — never a caller-supplied value the
|
|
43
|
-
* validated. See `models/PreparedSocialLogin.ts#completeSocialSession`.
|
|
41
|
+
* When a key is CREATED, `clientEntityId` must be the SERVER-CONFIRMED value
|
|
42
|
+
* the Social Session response echoed back — never a caller-supplied value the
|
|
43
|
+
* SDK has not validated. See `models/PreparedSocialLogin.ts#completeSocialSession`.
|
|
44
|
+
*
|
|
45
|
+
* {@link SecureKeyStore.resolveExisting} takes the same identity but never
|
|
46
|
+
* creates a key: it only finds one already registered under the derived alias.
|
|
44
47
|
*
|
|
45
48
|
* The raw value never reaches native or the alias in the clear: `alias.ts`
|
|
46
49
|
* only ever folds it into `deriveKeyScope`'s SHA-256 digest.
|
|
@@ -63,10 +66,58 @@ export type OwnerKeyIdentity = AppKeyIdentity &
|
|
|
63
66
|
* all resolve to `created` rather than silently presenting an unregistered
|
|
64
67
|
* key as registered.
|
|
65
68
|
* - `existing` — returned only for a key with a durable acknowledgement, i.e.
|
|
66
|
-
* after {@link OwnerAuthKey.acknowledgeRegistration} has succeeded
|
|
69
|
+
* after {@link OwnerAuthKey.acknowledgeRegistration} has succeeded, or for a
|
|
70
|
+
* key {@link SecureKeyStore.resolveExisting} found registered.
|
|
67
71
|
*/
|
|
68
72
|
export type AuthKeyResolution = 'created' | 'existing'
|
|
69
73
|
|
|
74
|
+
/**
|
|
75
|
+
* The server-assigned identifiers Circle returned when it registered an
|
|
76
|
+
* AuthKey: `DeviceContext.authKeyId` (the JWS `kid` the service looks the key
|
|
77
|
+
* up by) and `DeviceContext.ownerBindingId`. Both are UUIDs.
|
|
78
|
+
*
|
|
79
|
+
* Neither is secret, but both identify the Owner, so keep them out of logs.
|
|
80
|
+
*/
|
|
81
|
+
export type AuthKeyRegistration = Readonly<{
|
|
82
|
+
authKeyId: string
|
|
83
|
+
ownerBindingId: string
|
|
84
|
+
}>
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A registered AuthKey found on this device, with the identifiers recorded
|
|
88
|
+
* when Circle's registration was acknowledged.
|
|
89
|
+
*
|
|
90
|
+
* Returned by {@link SecureKeyStore.resolveExisting}. `authKey` is already
|
|
91
|
+
* `existing`, so it can sign a registered-device authorization straight away
|
|
92
|
+
* with `authKeyId` as the `kid`.
|
|
93
|
+
*/
|
|
94
|
+
export type RegisteredOwnerAuthKey = Readonly<{
|
|
95
|
+
authKey: OwnerAuthKey
|
|
96
|
+
/** The server-assigned AuthKey id, exactly as it was acknowledged. */
|
|
97
|
+
authKeyId: string
|
|
98
|
+
/** The server-assigned Owner binding id, exactly as it was acknowledged. */
|
|
99
|
+
ownerBindingId: string
|
|
100
|
+
}>
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* What the approving device's key needs, beyond the signing input, to sign an
|
|
104
|
+
* `approve_device` authorization: the new device's 32-byte code secret and the
|
|
105
|
+
* code the user typed from its screen.
|
|
106
|
+
*/
|
|
107
|
+
export type DeviceApprovalCheck = Readonly<{
|
|
108
|
+
codeSecret: Uint8Array
|
|
109
|
+
enteredCode: string
|
|
110
|
+
}>
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* {@link DeviceApprovalCheck} plus the exact approval body the signing input's
|
|
114
|
+
* `body_sha256` commits to.
|
|
115
|
+
*/
|
|
116
|
+
export type DeviceApprovalSigning = DeviceApprovalCheck &
|
|
117
|
+
Readonly<{
|
|
118
|
+
requestBody: Uint8Array
|
|
119
|
+
}>
|
|
120
|
+
|
|
70
121
|
/**
|
|
71
122
|
* The DAA Owner AuthKey.
|
|
72
123
|
*
|
|
@@ -97,14 +148,23 @@ export interface OwnerAuthKey {
|
|
|
97
148
|
resolution(): AuthKeyResolution
|
|
98
149
|
|
|
99
150
|
/**
|
|
100
|
-
* Persist that Circle registered this public key
|
|
101
|
-
*
|
|
151
|
+
* Persist that Circle registered this public key under `registration`'s
|
|
152
|
+
* identifiers, moving the handle from `created` to `existing`.
|
|
153
|
+
*
|
|
154
|
+
* Always an upsert, never a no-op: when Circle registers the same key again
|
|
155
|
+
* and assigns new identifiers, the stored ones are replaced, so a later
|
|
156
|
+
* {@link SecureKeyStore.resolveExisting} never returns a stale `authKeyId`
|
|
157
|
+
* the service would no longer accept as a `kid`. Calls on one handle reach
|
|
158
|
+
* native in call order, so the last call's identifiers are the ones kept.
|
|
159
|
+
* The ordering is per handle: the built-in stores hand out one handle per
|
|
160
|
+
* key, and a custom {@link SecureKeyStore} must do the same to keep it.
|
|
102
161
|
*
|
|
103
162
|
* Per the native SDK contract the caller invokes this only AFTER Circle has
|
|
104
163
|
* successfully registered the exported SPKI. Device Registration Complete
|
|
105
|
-
* calls it on an `ACTIVE` result
|
|
164
|
+
* calls it on an `ACTIVE` result with that result's `DeviceContext`
|
|
165
|
+
* identifiers. `RECOVERY_PENDING` must not.
|
|
106
166
|
*/
|
|
107
|
-
acknowledgeRegistration(): Promise<void>
|
|
167
|
+
acknowledgeRegistration(registration: AuthKeyRegistration): Promise<void>
|
|
108
168
|
|
|
109
169
|
/**
|
|
110
170
|
* Produce an ES256 signature (JOSE `R || S`, 64 bytes) over the exact message
|
|
@@ -141,9 +201,79 @@ export interface OwnerAuthKey {
|
|
|
141
201
|
* in this SDK's own JWS construction.
|
|
142
202
|
*/
|
|
143
203
|
signRegistrationDeviceAuthorization(message: Uint8Array): Promise<Uint8Array>
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Produce an ES256 signature (JOSE `R || S`, 64 bytes) over the exact
|
|
207
|
+
* `approve_device` signing input, after checking that `approval.requestBody`
|
|
208
|
+
* is the body the input commits to, that `approval.codeSecret` opens the
|
|
209
|
+
* body's `codeSeal`, and that `approval.enteredCode` is the matching code
|
|
210
|
+
* they produce (spaces and dashes ignored).
|
|
211
|
+
*
|
|
212
|
+
* A third native seam: production `NativeSecureKeyStore` reaches native
|
|
213
|
+
* `DaaDeviceAuthorizationSigningInput`'s approval initializer, which refuses
|
|
214
|
+
* a wrong code before any biometric prompt and shows the code in it.
|
|
215
|
+
* `signExactBytes` refuses an `approve_device` input.
|
|
216
|
+
*
|
|
217
|
+
* Optional so a custom {@link SecureKeyStore} written before device approval
|
|
218
|
+
* keeps compiling; `DaaUserClient.approveDevice` fails closed with
|
|
219
|
+
* `contract_violation` on a key without it.
|
|
220
|
+
*/
|
|
221
|
+
signDeviceApproval?(message: Uint8Array, approval: DeviceApprovalSigning): Promise<Uint8Array>
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* This key's add-a-device code secret: 32 random bytes, created on the first
|
|
225
|
+
* call and returned unchanged by later calls, across app restarts, until
|
|
226
|
+
* {@link deleteCodeSecret} deletes it, the key is forgotten, or a new key
|
|
227
|
+
* replaces it. Device Registration Complete seals it into `codeSeal`, and
|
|
228
|
+
* DAA keeps a waiting request's first seal, so a restart must not lose it.
|
|
229
|
+
* No user-authentication prompt. Each call returns a new array; never log it.
|
|
230
|
+
*
|
|
231
|
+
* Production `NativeSecureKeyStore` reaches native `codeSecret()` (Keychain
|
|
232
|
+
* on iOS, Keystore-encrypted storage on Android), which needs the key
|
|
233
|
+
* resolved first. Optional, with {@link deleteCodeSecret}, so a custom
|
|
234
|
+
* {@link SecureKeyStore} written before it keeps compiling: without them the
|
|
235
|
+
* secret lives in process memory only, and after a restart a waiting
|
|
236
|
+
* request's code is unavailable (`matching_code_unavailable`). Implement
|
|
237
|
+
* both or neither: with only one, `complete()` fails with
|
|
238
|
+
* `contract_violation` before it sends anything.
|
|
239
|
+
*/
|
|
240
|
+
codeSecret?(): Promise<Uint8Array>
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Delete the stored code secret only when its bytes equal `secret`, so an
|
|
244
|
+
* answer to an older registration can't delete a newer secret. Succeeds when
|
|
245
|
+
* nothing is stored, the stored secret differs, or the key is gone.
|
|
246
|
+
*/
|
|
247
|
+
deleteCodeSecret?(secret: Uint8Array): Promise<void>
|
|
144
248
|
}
|
|
145
249
|
|
|
146
250
|
export interface SecureKeyStore {
|
|
147
251
|
/** Resolve the installation AuthKey, creating it only when absent. */
|
|
148
252
|
resolveOrCreate(identity: OwnerKeyIdentity): Promise<OwnerAuthKey>
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Find the AuthKey Circle registered for `identity` on this device, with the
|
|
256
|
+
* identifiers recorded when that registration was acknowledged.
|
|
257
|
+
*
|
|
258
|
+
* Read-only: it never creates a key and never shows a user-authentication
|
|
259
|
+
* prompt. Returns `null` when there is no key, when the key that exists
|
|
260
|
+
* has no recorded registration identifiers (it was never acknowledged, or
|
|
261
|
+
* was acknowledged before identifiers were recorded), or when native does
|
|
262
|
+
* not trust the recorded metadata (malformed, an unknown version, or
|
|
263
|
+
* written for a different key); `resolveOrCreate` reports that metadata
|
|
264
|
+
* problem as an error. A failure to read storage throws; it is never
|
|
265
|
+
* `null`. Each call reads native, so a call made after an acknowledgement
|
|
266
|
+
* settles sees the identifiers it recorded.
|
|
267
|
+
*/
|
|
268
|
+
resolveExisting(identity: OwnerKeyIdentity): Promise<RegisteredOwnerAuthKey | null>
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Delete this installation's AuthKey for `identity`, and its local
|
|
272
|
+
* registration record.
|
|
273
|
+
*
|
|
274
|
+
* Local only: it does NOT revoke the key with Circle, which still holds the
|
|
275
|
+
* registered public key. Idempotent: succeeds when no key exists. The next
|
|
276
|
+
* {@link resolveOrCreate} mints a new key that must be registered again.
|
|
277
|
+
*/
|
|
278
|
+
forget(identity: OwnerKeyIdentity): Promise<void>
|
|
149
279
|
}
|
package/src/securekey/alias.ts
CHANGED
|
@@ -188,9 +188,17 @@ function assertAppIdWithin(value: string, maxLength: number): void {
|
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
191
|
+
/**
|
|
192
|
+
* The one rule for a `clientEntityId` that names a key, on enrollment and
|
|
193
|
+
* session restore alike. {@link deriveKeyScope} hashes the exact string, so
|
|
194
|
+
* the same UUID in another letter case would name a different key. DAA
|
|
195
|
+
* serializes UUIDs in lowercase, so only that form is accepted: a
|
|
196
|
+
* differently cased value fails here, loudly, instead of enrolling under one
|
|
197
|
+
* alias and later restoring from another.
|
|
198
|
+
*/
|
|
199
|
+
export function assertClientEntityId(clientEntityId: string): void {
|
|
200
|
+
if (!isUuid(clientEntityId) || clientEntityId !== clientEntityId.toLowerCase()) {
|
|
201
|
+
throw contractViolation('clientEntityId must be a lowercase UUID to namespace the secure key')
|
|
194
202
|
}
|
|
195
203
|
}
|
|
196
204
|
|