@circle-fin/daa-react-native-sdk 0.0.0-stage → 0.4.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 +63 -0
- package/LICENSE +176 -0
- package/README.md +217 -2
- package/android/build.gradle +97 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaRegistrationExceptions.kt +47 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaRegistrationModule.kt +121 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaScaExceptions.kt +60 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaScaModule.kt +179 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyExceptions.kt +73 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyModule.kt +185 -0
- package/android/src/main/java/com/circle/daa/reactnative/InstallationIdentityStore.kt +73 -0
- package/android/src/main/java/com/circle/daa/reactnative/UnpaddedBase64Url.kt +45 -0
- package/build/DaaClient.d.ts +57 -0
- package/build/DaaClient.js +158 -0
- package/build/api/AccountLimitsClient.d.ts +79 -0
- package/build/api/AccountLimitsClient.js +142 -0
- package/build/api/AccountsClient.d.ts +95 -0
- package/build/api/AccountsClient.js +276 -0
- package/build/api/AchFiatAccountsClient.d.ts +92 -0
- package/build/api/AchFiatAccountsClient.js +142 -0
- package/build/api/AuthClient.d.ts +120 -0
- package/build/api/AuthClient.js +257 -0
- package/build/api/DaaUserClient.d.ts +124 -0
- package/build/api/DaaUserClient.js +245 -0
- package/build/api/DepositAddressesClient.d.ts +112 -0
- package/build/api/DepositAddressesClient.js +222 -0
- package/build/api/DepositsClient.d.ts +106 -0
- package/build/api/DepositsClient.js +183 -0
- package/build/api/FiatAccountsClient.d.ts +43 -0
- package/build/api/FiatAccountsClient.js +46 -0
- package/build/api/TransactionsClient.d.ts +64 -0
- package/build/api/TransactionsClient.js +120 -0
- package/build/api/TransfersClient.d.ts +104 -0
- package/build/api/TransfersClient.js +321 -0
- package/build/api/WireFiatAccountsClient.d.ts +99 -0
- package/build/api/WireFiatAccountsClient.js +156 -0
- package/build/api/WithdrawalsClient.d.ts +94 -0
- package/build/api/WithdrawalsClient.js +305 -0
- package/build/api/loginFailure.d.ts +43 -0
- package/build/api/loginFailure.js +64 -0
- package/build/api/pagination.d.ts +29 -0
- package/build/api/pagination.js +50 -0
- package/build/api/queryParameters.d.ts +23 -0
- package/build/api/queryParameters.js +77 -0
- package/build/api/resourceRequest.d.ts +60 -0
- package/build/api/resourceRequest.js +79 -0
- package/build/api/signedResource.d.ts +77 -0
- package/build/api/signedResource.js +168 -0
- package/build/auth/RegistrationFlowKey.d.ts +93 -0
- package/build/auth/RegistrationFlowKey.js +203 -0
- package/build/auth/daaRegistrationNative.d.ts +80 -0
- package/build/auth/daaRegistrationNative.js +184 -0
- package/build/auth/deviceAuthorization.d.ts +73 -0
- package/build/auth/deviceAuthorization.js +243 -0
- package/build/auth/ownerGrant.d.ts +29 -0
- package/build/auth/ownerGrant.js +90 -0
- package/build/auth/prepareReceipt.d.ts +150 -0
- package/build/auth/prepareReceipt.js +357 -0
- package/build/auth/registrationAuthorization.d.ts +48 -0
- package/build/auth/registrationAuthorization.js +243 -0
- package/build/auth/registrationCodeVerifier.d.ts +31 -0
- package/build/auth/registrationCodeVerifier.js +75 -0
- package/build/config.d.ts +79 -0
- package/build/config.js +116 -0
- package/build/crypto/base64url.d.ts +26 -0
- package/build/crypto/base64url.js +87 -0
- package/build/crypto/bytes.d.ts +27 -0
- package/build/crypto/bytes.js +32 -0
- package/build/crypto/es256Jws.d.ts +56 -0
- package/build/crypto/es256Jws.js +196 -0
- package/build/crypto/jwkThumbprint.d.ts +32 -0
- package/build/crypto/jwkThumbprint.js +49 -0
- package/build/crypto/random.d.ts +35 -0
- package/build/crypto/random.js +106 -0
- package/build/crypto/serialization.d.ts +26 -0
- package/build/crypto/serialization.js +43 -0
- package/build/crypto/sha256.d.ts +18 -0
- package/build/crypto/sha256.js +100 -0
- package/build/errors.d.ts +190 -0
- package/build/errors.js +252 -0
- package/build/index.d.ts +61 -0
- package/build/index.js +67 -0
- package/build/models/Account.d.ts +201 -0
- package/build/models/Account.js +147 -0
- package/build/models/AccountLimitsUsage.d.ts +169 -0
- package/build/models/AccountLimitsUsage.js +86 -0
- package/build/models/Ach.d.ts +135 -0
- package/build/models/Ach.js +184 -0
- package/build/models/AddressObject.d.ts +128 -0
- package/build/models/AddressObject.js +141 -0
- package/build/models/BusinessPii.d.ts +84 -0
- package/build/models/BusinessPii.js +142 -0
- package/build/models/CreateAccountRequest.d.ts +74 -0
- package/build/models/CreateAccountRequest.js +19 -0
- package/build/models/CreateTransferRequest.d.ts +91 -0
- package/build/models/CreateTransferRequest.js +19 -0
- package/build/models/CreateWithdrawalRequest.d.ts +99 -0
- package/build/models/CreateWithdrawalRequest.js +19 -0
- package/build/models/DaaUserContext.d.ts +96 -0
- package/build/models/DaaUserContext.js +133 -0
- package/build/models/Deposit.d.ts +241 -0
- package/build/models/Deposit.js +149 -0
- package/build/models/DepositSummary.d.ts +91 -0
- package/build/models/DepositSummary.js +114 -0
- package/build/models/FiatAccount.d.ts +99 -0
- package/build/models/FiatAccount.js +151 -0
- package/build/models/GenerateAddressRequest.d.ts +67 -0
- package/build/models/GenerateAddressRequest.js +68 -0
- package/build/models/Money.d.ts +76 -0
- package/build/models/Money.js +99 -0
- package/build/models/Page.d.ts +33 -0
- package/build/models/Page.js +57 -0
- package/build/models/PendingDeviceRegistration.d.ts +90 -0
- package/build/models/PendingDeviceRegistration.js +180 -0
- package/build/models/PreparedSocialLogin.d.ts +155 -0
- package/build/models/PreparedSocialLogin.js +235 -0
- package/build/models/Transaction.d.ts +187 -0
- package/build/models/Transaction.js +134 -0
- package/build/models/Transfer.d.ts +335 -0
- package/build/models/Transfer.js +240 -0
- package/build/models/Wire.d.ts +213 -0
- package/build/models/Wire.js +264 -0
- package/build/models/Withdrawal.d.ts +201 -0
- package/build/models/Withdrawal.js +156 -0
- package/build/models/redaction.d.ts +18 -0
- package/build/models/redaction.js +23 -0
- package/build/sca/ScaClient.d.ts +105 -0
- package/build/sca/ScaClient.js +193 -0
- package/build/securekey/InMemorySecureKey.d.ts +23 -0
- package/build/securekey/InMemorySecureKey.js +127 -0
- package/build/securekey/NativeSecureKey.d.ts +24 -0
- package/build/securekey/NativeSecureKey.js +178 -0
- package/build/securekey/NativeSecureKeyBridge.d.ts +83 -0
- package/build/securekey/NativeSecureKeyBridge.js +19 -0
- package/build/securekey/SecureKey.d.ts +137 -0
- package/build/securekey/SecureKey.js +19 -0
- package/build/securekey/alias.d.ts +88 -0
- package/build/securekey/alias.js +175 -0
- package/build/securekey/ownerAuthKeyResolver.d.ts +56 -0
- package/build/securekey/ownerAuthKeyResolver.js +63 -0
- package/build/securekey/resolveNativeBridge.d.ts +30 -0
- package/build/securekey/resolveNativeBridge.js +120 -0
- package/build/testing.d.ts +18 -0
- package/build/testing.js +34 -0
- package/build/transport/DaaTransport.d.ts +35 -0
- package/build/transport/DaaTransport.js +19 -0
- package/build/transport/headers.d.ts +30 -0
- package/build/transport/headers.js +45 -0
- package/build/validation/requestBody.d.ts +27 -0
- package/build/validation/requestBody.js +56 -0
- package/build/validation/uuid.d.ts +25 -0
- package/build/validation/uuid.js +33 -0
- package/build/wire/accountLimitsUsagePayload.d.ts +30 -0
- package/build/wire/accountLimitsUsagePayload.js +130 -0
- package/build/wire/accountPayload.d.ts +36 -0
- package/build/wire/accountPayload.js +100 -0
- package/build/wire/achPayload.d.ts +24 -0
- package/build/wire/achPayload.js +82 -0
- package/build/wire/addressObjectPayload.d.ts +44 -0
- package/build/wire/addressObjectPayload.js +64 -0
- package/build/wire/daaStatusCodes.d.ts +64 -0
- package/build/wire/daaStatusCodes.js +69 -0
- package/build/wire/dataEnvelope.d.ts +40 -0
- package/build/wire/dataEnvelope.js +79 -0
- package/build/wire/depositPayload.d.ts +42 -0
- package/build/wire/depositPayload.js +159 -0
- package/build/wire/deviceContextPayload.d.ts +33 -0
- package/build/wire/deviceContextPayload.js +57 -0
- package/build/wire/deviceRegistrationResultPayload.d.ts +32 -0
- package/build/wire/deviceRegistrationResultPayload.js +128 -0
- package/build/wire/errorEnvelope.d.ts +43 -0
- package/build/wire/errorEnvelope.js +85 -0
- package/build/wire/fiatAccountPayload.d.ts +37 -0
- package/build/wire/fiatAccountPayload.js +93 -0
- package/build/wire/instant.d.ts +27 -0
- package/build/wire/instant.js +91 -0
- package/build/wire/jsonNumber.d.ts +30 -0
- package/build/wire/jsonNumber.js +34 -0
- package/build/wire/moneyPayload.d.ts +30 -0
- package/build/wire/moneyPayload.js +56 -0
- package/build/wire/paginationLinks.d.ts +28 -0
- package/build/wire/paginationLinks.js +167 -0
- package/build/wire/payload.d.ts +60 -0
- package/build/wire/payload.js +121 -0
- package/build/wire/pendingRegistrationPayload.d.ts +32 -0
- package/build/wire/pendingRegistrationPayload.js +54 -0
- package/build/wire/socialLoginRequestPayload.d.ts +31 -0
- package/build/wire/socialLoginRequestPayload.js +72 -0
- package/build/wire/transactionPayload.d.ts +31 -0
- package/build/wire/transactionPayload.js +126 -0
- package/build/wire/transferPayload.d.ts +43 -0
- package/build/wire/transferPayload.js +218 -0
- package/build/wire/wireEnums.d.ts +22 -0
- package/build/wire/wireEnums.js +69 -0
- package/build/wire/wirePayload.d.ts +29 -0
- package/build/wire/wirePayload.js +138 -0
- package/build/wire/withdrawalPayload.d.ts +27 -0
- package/build/wire/withdrawalPayload.js +179 -0
- package/expo-module.config.json +14 -0
- package/ios/DaaRegistrationExceptions.swift +58 -0
- package/ios/DaaRegistrationModule.swift +143 -0
- package/ios/DaaScaExceptions.swift +96 -0
- package/ios/DaaScaModule.swift +125 -0
- package/ios/DaaSecureKeyExceptions.swift +77 -0
- package/ios/DaaSecureKeyModule.swift +162 -0
- package/ios/InstallationIdentityStore.swift +126 -0
- package/ios/UnpaddedBase64URL.swift +42 -0
- package/package.json +114 -4
- package/src/DaaClient.ts +171 -0
- package/src/api/AccountLimitsClient.ts +146 -0
- package/src/api/AccountsClient.ts +305 -0
- package/src/api/AchFiatAccountsClient.ts +147 -0
- package/src/api/AuthClient.ts +331 -0
- package/src/api/DaaUserClient.ts +270 -0
- package/src/api/DepositAddressesClient.ts +239 -0
- package/src/api/DepositsClient.ts +208 -0
- package/src/api/FiatAccountsClient.ts +50 -0
- package/src/api/TransactionsClient.ts +137 -0
- package/src/api/TransfersClient.ts +361 -0
- package/src/api/WireFiatAccountsClient.ts +172 -0
- package/src/api/WithdrawalsClient.ts +339 -0
- package/src/api/loginFailure.ts +75 -0
- package/src/api/pagination.ts +63 -0
- package/src/api/queryParameters.ts +82 -0
- package/src/api/resourceRequest.ts +135 -0
- package/src/api/signedResource.ts +267 -0
- package/src/auth/RegistrationFlowKey.ts +274 -0
- package/src/auth/daaRegistrationNative.ts +236 -0
- package/src/auth/deviceAuthorization.ts +302 -0
- package/src/auth/ownerGrant.ts +102 -0
- package/src/auth/prepareReceipt.ts +522 -0
- package/src/auth/registrationAuthorization.ts +306 -0
- package/src/auth/registrationCodeVerifier.ts +76 -0
- package/src/config.ts +156 -0
- package/src/crypto/base64url.ts +88 -0
- package/src/crypto/bytes.ts +30 -0
- package/src/crypto/es256Jws.ts +240 -0
- package/src/crypto/jwkThumbprint.ts +65 -0
- package/src/crypto/random.ts +113 -0
- package/src/crypto/serialization.ts +42 -0
- package/src/crypto/sha256.ts +105 -0
- package/src/errors.ts +319 -0
- package/src/index.ts +223 -0
- package/src/models/Account.ts +264 -0
- package/src/models/AccountLimitsUsage.ts +192 -0
- package/src/models/Ach.ts +269 -0
- package/src/models/AddressObject.ts +169 -0
- package/src/models/BusinessPii.ts +154 -0
- package/src/models/CreateAccountRequest.ts +109 -0
- package/src/models/CreateTransferRequest.ts +114 -0
- package/src/models/CreateWithdrawalRequest.ts +120 -0
- package/src/models/DaaUserContext.ts +172 -0
- package/src/models/Deposit.ts +307 -0
- package/src/models/DepositSummary.ts +142 -0
- package/src/models/FiatAccount.ts +195 -0
- package/src/models/GenerateAddressRequest.ts +101 -0
- package/src/models/Money.ts +91 -0
- package/src/models/Page.ts +48 -0
- package/src/models/PendingDeviceRegistration.ts +197 -0
- package/src/models/PreparedSocialLogin.ts +287 -0
- package/src/models/Transaction.ts +247 -0
- package/src/models/Transfer.ts +459 -0
- package/src/models/Wire.ts +405 -0
- package/src/models/Withdrawal.ts +283 -0
- package/src/models/redaction.ts +22 -0
- package/src/sca/ScaClient.ts +289 -0
- package/src/securekey/InMemorySecureKey.ts +138 -0
- package/src/securekey/NativeSecureKey.ts +179 -0
- package/src/securekey/NativeSecureKeyBridge.ts +109 -0
- package/src/securekey/SecureKey.ts +149 -0
- package/src/securekey/alias.ts +204 -0
- package/src/securekey/ownerAuthKeyResolver.ts +96 -0
- package/src/securekey/resolveNativeBridge.ts +185 -0
- package/src/testing.ts +32 -0
- package/src/transport/DaaTransport.ts +42 -0
- package/src/transport/headers.ts +46 -0
- package/src/validation/requestBody.ts +60 -0
- package/src/validation/uuid.ts +31 -0
- package/src/wire/accountLimitsUsagePayload.ts +182 -0
- package/src/wire/accountPayload.ts +108 -0
- package/src/wire/achPayload.ts +98 -0
- package/src/wire/addressObjectPayload.ts +64 -0
- package/src/wire/daaStatusCodes.ts +69 -0
- package/src/wire/dataEnvelope.ts +80 -0
- package/src/wire/depositPayload.ts +186 -0
- package/src/wire/deviceContextPayload.ts +58 -0
- package/src/wire/deviceRegistrationResultPayload.ts +153 -0
- package/src/wire/errorEnvelope.ts +87 -0
- package/src/wire/fiatAccountPayload.ts +112 -0
- package/src/wire/instant.ts +93 -0
- package/src/wire/jsonNumber.ts +33 -0
- package/src/wire/moneyPayload.ts +55 -0
- package/src/wire/paginationLinks.ts +204 -0
- package/src/wire/payload.ts +146 -0
- package/src/wire/pendingRegistrationPayload.ts +75 -0
- package/src/wire/socialLoginRequestPayload.ts +89 -0
- package/src/wire/transactionPayload.ts +145 -0
- package/src/wire/transferPayload.ts +249 -0
- package/src/wire/wireEnums.ts +74 -0
- package/src/wire/wirePayload.ts +160 -0
- package/src/wire/withdrawalPayload.ts +219 -0
- package/testing.d.ts +19 -0
- package/testing.js +25 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026, Circle Internet Group, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// The JS <-> native contract implemented by the platform secure-key module
|
|
20
|
+
// (iOS Keychain / Secure Enclave, Android Keystore).
|
|
21
|
+
//
|
|
22
|
+
// Keys are non-exportable and referenced by an opaque `alias`. Private key bytes
|
|
23
|
+
// never cross this boundary. All byte-valued arguments and results are unpadded
|
|
24
|
+
// Base64URL strings so the contract is uniform across the RN bridge.
|
|
25
|
+
//
|
|
26
|
+
// The Expo `DaaSecureKey` module under ios/ and android/ implements this
|
|
27
|
+
// contract by delegating to daa-ios-sdk / daa-android-sdk. Production apps
|
|
28
|
+
// resolve it via resolveNativeBridge(); tests inject a stand-in.
|
|
29
|
+
//
|
|
30
|
+
// Every method here takes the RN `alias`; resolveNativeBridge() parses it into
|
|
31
|
+
// the structured `{appId, environment}` pair the Expo module accepts.
|
|
32
|
+
import type { AuthKeyResolution } from './SecureKey'
|
|
33
|
+
|
|
34
|
+
// The bridge-side name for the public `AuthKeyResolution`.
|
|
35
|
+
export type NativeAuthKeyResolution = AuthKeyResolution
|
|
36
|
+
|
|
37
|
+
export interface NativeSecureKeyBridge {
|
|
38
|
+
/**
|
|
39
|
+
* Ensure a non-exportable P-256 key exists for `alias`, creating it only when
|
|
40
|
+
* absent. Idempotent.
|
|
41
|
+
*
|
|
42
|
+
* Returns the native SDKs' resolution discriminant, which is about
|
|
43
|
+
* REGISTRATION state, not merely whether this call minted the key:
|
|
44
|
+
* `'created'` when the key was just minted OR when an existing key carries no
|
|
45
|
+
* durable registration acknowledgement; `'existing'` only for a key Circle is
|
|
46
|
+
* durably recorded as having registered. `acknowledgeRegistration` is what
|
|
47
|
+
* moves a key from the former to the latter.
|
|
48
|
+
*/
|
|
49
|
+
resolveOrCreate(alias: string): Promise<NativeAuthKeyResolution>
|
|
50
|
+
|
|
51
|
+
/** Unpadded Base64URL DER SubjectPublicKeyInfo of the public key. */
|
|
52
|
+
publicKeySpkiBase64Url(alias: string): Promise<string>
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Persist that Circle registered the public key for `alias`. Idempotent —
|
|
56
|
+
* native no-ops when the key is already acknowledged. After it succeeds,
|
|
57
|
+
* later `resolveOrCreate` calls return `'existing'`.
|
|
58
|
+
*/
|
|
59
|
+
acknowledgeRegistration(alias: string): Promise<void>
|
|
60
|
+
|
|
61
|
+
// The RFC 7638 thumbprint is derived in TypeScript from the SPKI above (see
|
|
62
|
+
// crypto/jwkThumbprint), so the native module does not implement it — one
|
|
63
|
+
// canonicalization, no cross-platform drift. Native thumbprint() is not
|
|
64
|
+
// called at this seam.
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* ES256 signature over the exact message bytes.
|
|
68
|
+
*
|
|
69
|
+
* Production `resolveNativeBridge()` sends those bytes to Expo
|
|
70
|
+
* `signDeviceAuthorization` (native `DaaSecureKey.signDeviceAuthorization`
|
|
71
|
+
* + `DaaDeviceAuthorizationSigningInput`). There is no native generic
|
|
72
|
+
* `sign(bytes)` Expo method. Compact JWS construction lives in
|
|
73
|
+
* `auth/deviceAuthorization.ts` and signs through `OwnerAuthKey.signExactBytes`.
|
|
74
|
+
* Native validates the compact signing input and signs that same slice.
|
|
75
|
+
* The header carries the published `typ` as-is.
|
|
76
|
+
*
|
|
77
|
+
* Injectable test stand-ins may still implement this for NativeSecureKeyStore
|
|
78
|
+
* unit tests (including arbitrary bytes the native validator would reject).
|
|
79
|
+
*
|
|
80
|
+
* @param messageBase64Url unpadded Base64URL of the exact bytes to sign.
|
|
81
|
+
* @returns unpadded Base64URL of the 64-byte JOSE `R || S` signature.
|
|
82
|
+
*/
|
|
83
|
+
sign(alias: string, messageBase64Url: string): Promise<string>
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* ES256 signature over the exact Device Registration Complete device-proof
|
|
87
|
+
* signing input.
|
|
88
|
+
*
|
|
89
|
+
* A DISTINCT method from {@link sign}, not an overload or a shared code
|
|
90
|
+
* path: production `resolveNativeBridge()` sends these bytes to Expo
|
|
91
|
+
* `signRegistrationDeviceAuthorization` (native
|
|
92
|
+
* `DaaSecureKey.signRegistrationDeviceAuthorization` +
|
|
93
|
+
* `DaaRegistrationAuthorizationSigningInput`), which is a different native
|
|
94
|
+
* function with a different validator than `signDeviceAuthorization` +
|
|
95
|
+
* `DaaDeviceAuthorizationSigningInput` behind {@link sign}. The registration
|
|
96
|
+
* profile is built in `auth/registrationAuthorization.ts` and signs through
|
|
97
|
+
* `OwnerAuthKey.signRegistrationDeviceAuthorization`. Native validates the
|
|
98
|
+
* compact signing input and signs that same slice; it does not require a
|
|
99
|
+
* durably acknowledged key, because Complete runs before
|
|
100
|
+
* `acknowledgeRegistration`.
|
|
101
|
+
*
|
|
102
|
+
* Injectable test stand-ins may still implement this for NativeSecureKeyStore
|
|
103
|
+
* unit tests (including arbitrary bytes the native validator would reject).
|
|
104
|
+
*
|
|
105
|
+
* @param messageBase64Url unpadded Base64URL of the exact bytes to sign.
|
|
106
|
+
* @returns unpadded Base64URL of the 64-byte JOSE `R || S` signature.
|
|
107
|
+
*/
|
|
108
|
+
signRegistrationDeviceAuthorization(alias: string, messageBase64Url: string): Promise<string>
|
|
109
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026, Circle Internet Group, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { DaaEnvironment } from '../config'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The consumer's application-level identity: `appId` and `environment`, known
|
|
23
|
+
* at `DaaClient`/`AuthClient` construction, before any Owner is known.
|
|
24
|
+
*
|
|
25
|
+
* Combined with a per-Owner `clientEntityId` — available only after the
|
|
26
|
+
* Social Session response — to form the full {@link OwnerKeyIdentity} the
|
|
27
|
+
* secure-key store resolves. Kept as its own type so the two are
|
|
28
|
+
* never silently conflated: an app-level identity cannot select an AuthKey by
|
|
29
|
+
* itself, and code that only has one cannot pretend otherwise.
|
|
30
|
+
*/
|
|
31
|
+
export type AppKeyIdentity = Readonly<{
|
|
32
|
+
appId: string
|
|
33
|
+
environment: DaaEnvironment
|
|
34
|
+
}>
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Identity that namespaces the installation AuthKey: one non-exportable
|
|
38
|
+
* P-256/ES256 key per `{appId, environment, clientEntityId (Owner),
|
|
39
|
+
* installation}`.
|
|
40
|
+
*
|
|
41
|
+
* `clientEntityId` must be the SERVER-CONFIRMED value the Social Session
|
|
42
|
+
* response echoed back — never a caller-supplied value the SDK has not
|
|
43
|
+
* validated. See `models/PreparedSocialLogin.ts#completeSocialSession`.
|
|
44
|
+
*
|
|
45
|
+
* The raw value never reaches native or the alias in the clear: `alias.ts`
|
|
46
|
+
* only ever folds it into `deriveKeyScope`'s SHA-256 digest.
|
|
47
|
+
*/
|
|
48
|
+
export type OwnerKeyIdentity = AppKeyIdentity &
|
|
49
|
+
Readonly<{
|
|
50
|
+
clientEntityId: string
|
|
51
|
+
}>
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Result of resolving the installation AuthKey. Same name and same two cases as
|
|
55
|
+
* the native SDKs' `AuthKeyResolution` (`daa-ios-sdk`, `daa-android-sdk`), so
|
|
56
|
+
* the discriminant is not renamed or re-interpreted as it crosses the bridge.
|
|
57
|
+
*
|
|
58
|
+
* This is about REGISTRATION state, not about which call minted the key:
|
|
59
|
+
*
|
|
60
|
+
* - `created` — the key was just minted, OR a key already exists but carries no
|
|
61
|
+
* durable registration acknowledgement. Per the native SDK contract, passcode
|
|
62
|
+
* removal, Keychain reset, device restore, or a crash before acknowledgement
|
|
63
|
+
* all resolve to `created` rather than silently presenting an unregistered
|
|
64
|
+
* key as registered.
|
|
65
|
+
* - `existing` — returned only for a key with a durable acknowledgement, i.e.
|
|
66
|
+
* after {@link OwnerAuthKey.acknowledgeRegistration} has succeeded.
|
|
67
|
+
*/
|
|
68
|
+
export type AuthKeyResolution = 'created' | 'existing'
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The DAA Owner AuthKey.
|
|
72
|
+
*
|
|
73
|
+
* The private key never leaves the secure key handle. There is deliberately NO
|
|
74
|
+
* generic `sign(bytes)` at the SDK surface — signing is reachable only through
|
|
75
|
+
* this DAA-specific API, for defined DAA operations (login completion, protected
|
|
76
|
+
* writes, signed context refresh). See SECURITY.md.
|
|
77
|
+
*
|
|
78
|
+
* **Not the ephemeral registration-flow key.** The login contract has two
|
|
79
|
+
* P-256 keys, and this is the durable, namespaced, enclave-held one. The other —
|
|
80
|
+
* per login attempt, anonymous, process memory only, exported at Prepare — is
|
|
81
|
+
* `auth/RegistrationFlowKey.ts`, which never comes from this store and carries a
|
|
82
|
+
* comparison table for the difference.
|
|
83
|
+
*/
|
|
84
|
+
export interface OwnerAuthKey {
|
|
85
|
+
/** Unpadded Base64URL DER SubjectPublicKeyInfo of the public key. */
|
|
86
|
+
publicKeySpkiBase64Url(): Promise<string>
|
|
87
|
+
|
|
88
|
+
/** RFC 7638 JWK thumbprint (`jkt`) of the P-256 public key. */
|
|
89
|
+
thumbprint(): Promise<string>
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Registration state of this handle: `created` while Circle's acknowledgement
|
|
93
|
+
* is still outstanding, `existing` once it is durably recorded. Read from the
|
|
94
|
+
* value the store observed when it resolved the key, and refreshed locally by
|
|
95
|
+
* {@link acknowledgeRegistration}.
|
|
96
|
+
*/
|
|
97
|
+
resolution(): AuthKeyResolution
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Persist that Circle registered this public key, moving the handle from
|
|
101
|
+
* `created` to `existing`. Idempotent: a no-op once the handle is `existing`.
|
|
102
|
+
*
|
|
103
|
+
* Per the native SDK contract the caller invokes this only AFTER Circle has
|
|
104
|
+
* successfully registered the exported SPKI. Device Registration Complete
|
|
105
|
+
* calls it on an `ACTIVE` result. `RECOVERY_PENDING` must not.
|
|
106
|
+
*/
|
|
107
|
+
acknowledgeRegistration(): Promise<void>
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Produce an ES256 signature (JOSE `R || S`, 64 bytes) over the exact message
|
|
111
|
+
* bytes. The caller passes the already-serialized signing input; this method
|
|
112
|
+
* does not reserialize or canonicalize.
|
|
113
|
+
*
|
|
114
|
+
* This seam does NOT gate on {@link resolution}. Registered-device
|
|
115
|
+
* authorization (`auth/deviceAuthorization.ts`) requires `existing` before it
|
|
116
|
+
* calls this. Production `NativeSecureKeyStore` reaches native
|
|
117
|
+
* `signDeviceAuthorization`, which rejects with `authKeyRegistrationRequired`
|
|
118
|
+
* until acknowledgement succeeds (except native-allowed login completion).
|
|
119
|
+
* There is no public generic `sign(bytes)`.
|
|
120
|
+
*
|
|
121
|
+
* **Post-activation only.** Do not call this for the Device Registration
|
|
122
|
+
* Complete device proof — see {@link signRegistrationDeviceAuthorization}.
|
|
123
|
+
*/
|
|
124
|
+
signExactBytes(message: Uint8Array): Promise<Uint8Array>
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Produce an ES256 signature (JOSE `R || S`, 64 bytes) over the exact
|
|
128
|
+
* Device Registration Complete device-proof signing input
|
|
129
|
+
* (`auth/registrationAuthorization.ts`'s `X-Device-Authorization`, the
|
|
130
|
+
* registration profile's claim/header shape, not the post-activation one
|
|
131
|
+
* of the same header name).
|
|
132
|
+
*
|
|
133
|
+
* A DIFFERENT native seam from {@link signExactBytes}, on purpose: this key
|
|
134
|
+
* has typically not been acknowledged yet (Complete runs before
|
|
135
|
+
* `acknowledgeRegistration`), and the registration profile's claim set and
|
|
136
|
+
* embedded-JWK header are validated by native's registration validator, not
|
|
137
|
+
* `DaaDeviceAuthorizationSigningInput`. Production `NativeSecureKeyStore`
|
|
138
|
+
* reaches native `signRegistrationDeviceAuthorization`. Routing this proof
|
|
139
|
+
* through `signExactBytes` instead would sign valid bytes against the wrong
|
|
140
|
+
* native validator and fail on a real device even though it looks correct
|
|
141
|
+
* in this SDK's own JWS construction.
|
|
142
|
+
*/
|
|
143
|
+
signRegistrationDeviceAuthorization(message: Uint8Array): Promise<Uint8Array>
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface SecureKeyStore {
|
|
147
|
+
/** Resolve the installation AuthKey, creating it only when absent. */
|
|
148
|
+
resolveOrCreate(identity: OwnerKeyIdentity): Promise<OwnerAuthKey>
|
|
149
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026, Circle Internet Group, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { DAA_ENVIRONMENTS } from '../config'
|
|
20
|
+
import type { DaaEnvironment } from '../config'
|
|
21
|
+
import { toBase64Url } from '../crypto/base64url'
|
|
22
|
+
import { sha256 } from '../crypto/sha256'
|
|
23
|
+
import { contractViolation } from '../errors'
|
|
24
|
+
import { isUuid } from '../validation/uuid'
|
|
25
|
+
import type { OwnerKeyIdentity } from './SecureKey'
|
|
26
|
+
|
|
27
|
+
// The installation key namespace. This value is interpolated into an OS keystore
|
|
28
|
+
// identifier (iOS `kSecAttrApplicationTag`, Android KeyStore alias), so ALL
|
|
29
|
+
// THREE inputs are validated — it is a custody-key namespace, not free text,
|
|
30
|
+
// and a store can be constructed without going through DaaClient's checks.
|
|
31
|
+
//
|
|
32
|
+
// Validating `environment` against the allow-list is what keeps the alias
|
|
33
|
+
// injective on that axis: no allowed environment contains `.`, so splitting
|
|
34
|
+
// from the right always recovers it (see `parseSecureKeyAlias`).
|
|
35
|
+
//
|
|
36
|
+
// The per-Owner component is a SHA-256 digest, never the raw
|
|
37
|
+
// `clientEntityId` — see `deriveKeyScope`. Hashing also keeps a
|
|
38
|
+
// string-concatenation collision out of the `clientEntityId` axis: recovering
|
|
39
|
+
// two different `(appId, environment, clientEntityId)` triples from one alias
|
|
40
|
+
// would require a SHA-256 preimage or collision, not a parsing ambiguity.
|
|
41
|
+
const APP_ID_CHARSET = /^[A-Za-z0-9._-]+$/
|
|
42
|
+
|
|
43
|
+
// 128 bits of digest is enough collision resistance for a per-installation,
|
|
44
|
+
// per-Owner namespace while leaving room in native's 64-character appId slot
|
|
45
|
+
// for a real-world appId. Base64URL length is computed rather than hardcoded
|
|
46
|
+
// so it can never drift from KEY_SCOPE_DIGEST_BYTES.
|
|
47
|
+
const KEY_SCOPE_DIGEST_BYTES = 16
|
|
48
|
+
const KEY_SCOPE_LENGTH = Math.ceil((KEY_SCOPE_DIGEST_BYTES * 8) / 6) // 22
|
|
49
|
+
|
|
50
|
+
// Native's hard cap on the appId slot (`isValidNamespace` in daa-ios-sdk's
|
|
51
|
+
// DaaSecureKey.swift: `[A-Za-z0-9._-]{1,64}`). The value that actually reaches
|
|
52
|
+
// it is `${appId}.${keyScope}` (see `secureKeyAlias`), so this cap applies to
|
|
53
|
+
// the COMPOSED value, and only `parseSecureKeyAlias` — which recovers that
|
|
54
|
+
// composed value — validates against it.
|
|
55
|
+
const MAX_NATIVE_APP_ID_LENGTH = 64
|
|
56
|
+
|
|
57
|
+
// What a consumer may actually configure as `appId`: native's slot minus the
|
|
58
|
+
// per-Owner scope and its separator. Derived from the two constants above
|
|
59
|
+
// rather than written as 41, so the limit enforced at the door can never drift
|
|
60
|
+
// from the composed value's real budget.
|
|
61
|
+
//
|
|
62
|
+
// Enforcing this in `assertAppId` is what keeps the two checks from
|
|
63
|
+
// disagreeing: an over-long appId is rejected up front, naming the limit the
|
|
64
|
+
// consumer controls, instead of passing validation and failing later during
|
|
65
|
+
// key resolution with a budget the caller never saw.
|
|
66
|
+
const MAX_APP_ID_LENGTH = MAX_NATIVE_APP_ID_LENGTH - 1 - KEY_SCOPE_LENGTH // 41
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The per-Owner key-scope component: unpadded Base64URL of the
|
|
70
|
+
* first 128 bits of SHA-256 over `appId`, `environment`, and `clientEntityId`.
|
|
71
|
+
*
|
|
72
|
+
* The preimage deliberately includes `appId` and `environment`, not
|
|
73
|
+
* `clientEntityId` alone, so the digest cannot be correlated across
|
|
74
|
+
* applications or environments for the same Owner.
|
|
75
|
+
*
|
|
76
|
+
* `appId`, `environment`, and `clientEntityId` are joined with a NUL (`\0`)
|
|
77
|
+
* separator. NUL cannot appear in any of the three — `APP_ID_CHARSET` excludes
|
|
78
|
+
* control characters, `environment` is one of the fixed `DAA_ENVIRONMENTS`
|
|
79
|
+
* strings, and `clientEntityId` is validated as a hyphenated UUID — so the
|
|
80
|
+
* join cannot map two different triples onto the same preimage bytes.
|
|
81
|
+
*/
|
|
82
|
+
export function deriveKeyScope(identity: OwnerKeyIdentity): string {
|
|
83
|
+
const preimage = `${identity.appId}\u0000${identity.environment}\u0000${identity.clientEntityId}`
|
|
84
|
+
const digest = sha256(new TextEncoder().encode(preimage))
|
|
85
|
+
return toBase64Url(digest.subarray(0, KEY_SCOPE_DIGEST_BYTES))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* What actually reaches the native module's `appId` parameter once
|
|
90
|
+
* `parseSecureKeyAlias` recovers it: the consumer's configured `appId`
|
|
91
|
+
* composed with the derived per-Owner {@link deriveKeyScope} digest — NOT
|
|
92
|
+
* `OwnerKeyIdentity.appId` alone.
|
|
93
|
+
*
|
|
94
|
+
* Kept as its own type, rather than reusing or widening `OwnerKeyIdentity`,
|
|
95
|
+
* so the consumer's raw `appId` and the opaque scoped value native receives
|
|
96
|
+
* are never silently conflated. `environment` is unchanged, since
|
|
97
|
+
* it is not namespaced by Owner.
|
|
98
|
+
*/
|
|
99
|
+
export type NativeKeyIdentity = Readonly<{
|
|
100
|
+
appId: string
|
|
101
|
+
environment: DaaEnvironment
|
|
102
|
+
}>
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Build the RN secure-key alias: `{appId}.{keyScope}.{environment}`, where
|
|
106
|
+
* `keyScope` is {@link deriveKeyScope}'s digest of `identity`.
|
|
107
|
+
*
|
|
108
|
+
* `keyScope` is composed with `appId` in the slot native validates and stores
|
|
109
|
+
* against — the derived scope, not `identity.clientEntityId` and not
|
|
110
|
+
* `identity.appId` by itself, is what reaches the native module (see
|
|
111
|
+
* {@link NativeKeyIdentity}). Two different Owners (`clientEntityId`) on the
|
|
112
|
+
* same `{appId, environment}` therefore resolve to two different aliases, and
|
|
113
|
+
* so to two different native keys. A returning Owner reproduces the same
|
|
114
|
+
* digest and so the same alias, and therefore resolves the SAME native key.
|
|
115
|
+
*/
|
|
116
|
+
export function secureKeyAlias(identity: OwnerKeyIdentity): string {
|
|
117
|
+
assertAppId(identity.appId)
|
|
118
|
+
const environment = parseEnvironment(identity.environment)
|
|
119
|
+
assertClientEntityId(identity.clientEntityId)
|
|
120
|
+
const keyScope = deriveKeyScope(identity)
|
|
121
|
+
// No length check is needed here: `assertAppId` caps `appId` at
|
|
122
|
+
// MAX_APP_ID_LENGTH, which is defined as exactly the room left once the
|
|
123
|
+
// separator and the fixed-width `keyScope` are accounted for, so the
|
|
124
|
+
// composed value cannot exceed MAX_NATIVE_APP_ID_LENGTH.
|
|
125
|
+
const nativeAppId = `${identity.appId}.${keyScope}`
|
|
126
|
+
return `${nativeAppId}.${environment}`
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Inverse of `secureKeyAlias`'s environment split — NOT a full inverse of
|
|
131
|
+
* `secureKeyAlias`, because {@link deriveKeyScope} is one-way. Splits from the
|
|
132
|
+
* right so the composed `{appId}.{keyScope}` prefix may itself contain dots
|
|
133
|
+
* (an `appId` always could). The Expo module then constructs native
|
|
134
|
+
* `DaaAuthKeyIdentity` from these fields plus a per-install UUID the native
|
|
135
|
+
* module owns.
|
|
136
|
+
*
|
|
137
|
+
* Returns {@link NativeKeyIdentity}, not `OwnerKeyIdentity`: what this
|
|
138
|
+
* recovers is the value native's `appId` parameter needs — the composed
|
|
139
|
+
* scope — not the consumer's original `appId` and not the `clientEntityId`
|
|
140
|
+
* that fed into it. That information is exactly what the hash discards, by
|
|
141
|
+
* design.
|
|
142
|
+
*/
|
|
143
|
+
export function parseSecureKeyAlias(alias: string): NativeKeyIdentity {
|
|
144
|
+
const separator = alias.lastIndexOf('.')
|
|
145
|
+
if (separator <= 0 || separator === alias.length - 1) {
|
|
146
|
+
throw contractViolation(
|
|
147
|
+
`secure-key alias must be {appId}.{environment} (got a ${alias.length}-char value)`,
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
const appId = alias.slice(0, separator)
|
|
151
|
+
const environment = parseEnvironment(alias.slice(separator + 1))
|
|
152
|
+
assertNativeAppId(appId)
|
|
153
|
+
return { appId, environment }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Validates the consumer-supplied `appId`, before the per-Owner scope is
|
|
158
|
+
* folded in — so the cap is MAX_APP_ID_LENGTH, not native's slot size.
|
|
159
|
+
*
|
|
160
|
+
* Exported for `DaaClient`'s constructor, so an `appId` no key can be stored
|
|
161
|
+
* under is refused before any request is sent, not on the first key
|
|
162
|
+
* resolution — which comes after Social Session has already bound the end
|
|
163
|
+
* user. `secureKeyAlias` still checks it, because a store can be reached
|
|
164
|
+
* without a `DaaClient`.
|
|
165
|
+
*/
|
|
166
|
+
export function assertAppId(appId: string): void {
|
|
167
|
+
assertAppIdWithin(appId, MAX_APP_ID_LENGTH)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Validates the composed `{appId}.{keyScope}` recovered from an alias, which
|
|
172
|
+
* is what native's appId slot actually receives — so the cap here is native's
|
|
173
|
+
* full MAX_NATIVE_APP_ID_LENGTH. Applying assertAppId's tighter consumer cap
|
|
174
|
+
* would reject legitimate aliases, since the scope alone is 22 characters.
|
|
175
|
+
*/
|
|
176
|
+
function assertNativeAppId(nativeAppId: string): void {
|
|
177
|
+
assertAppIdWithin(nativeAppId, MAX_NATIVE_APP_ID_LENGTH)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function assertAppIdWithin(value: string, maxLength: number): void {
|
|
181
|
+
// Report only the length, never the (untrusted) value, so it cannot forge log
|
|
182
|
+
// lines when the error surfaces.
|
|
183
|
+
if (!APP_ID_CHARSET.test(value) || value.length > maxLength) {
|
|
184
|
+
throw contractViolation(
|
|
185
|
+
`appId must match ${String(APP_ID_CHARSET)} and be at most ${maxLength} ` +
|
|
186
|
+
`characters to namespace the secure key (got a ${value.length}-char value)`,
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function assertClientEntityId(clientEntityId: string): void {
|
|
192
|
+
if (!isUuid(clientEntityId)) {
|
|
193
|
+
throw contractViolation('clientEntityId must be a UUID to namespace the secure key')
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function parseEnvironment(value: string): DaaEnvironment {
|
|
198
|
+
for (const environment of DAA_ENVIRONMENTS) {
|
|
199
|
+
if (environment === value) {
|
|
200
|
+
return environment
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
throw contractViolation(`unknown environment; expected one of ${DAA_ENVIRONMENTS.join(', ')}`)
|
|
204
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026, Circle Internet Group, Inc. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// Resolving the installation AuthKey by alias, in one place.
|
|
20
|
+
//
|
|
21
|
+
// Two call sites need the same key: the login ceremony selects it after Social
|
|
22
|
+
// Session (`AuthClient`), and the authenticated operations surface signs writes
|
|
23
|
+
// with it (`DaaClient.forUser`). The key is not carried on `DaaUserContext` —
|
|
24
|
+
// that context is a decoded server result, not a handle to enclave-held key
|
|
25
|
+
// material — so `forUser` re-resolves by the same `{appId, environment}` alias
|
|
26
|
+
// login used, together with the SAME context's `clientEntityId`.
|
|
27
|
+
// `DeviceContext.authKeyId` supplies the JWS `kid` alongside it.
|
|
28
|
+
//
|
|
29
|
+
// The lazy `NativeSecureKeyStore` is why this is a factory rather than a free
|
|
30
|
+
// function. Constructing the store requires the Expo `DaaSecureKey` module, and
|
|
31
|
+
// `DaaClient` construction deliberately does not; the store is therefore built
|
|
32
|
+
// on first resolution and then reused, so login and every later signed write
|
|
33
|
+
// share one store — and so share its in-flight `resolveOrCreate` map rather
|
|
34
|
+
// than racing two independent handles for the same alias.
|
|
35
|
+
|
|
36
|
+
import { contractViolation } from '../errors'
|
|
37
|
+
import { NativeSecureKeyStore } from './NativeSecureKey'
|
|
38
|
+
import { resolveNativeBridge } from './resolveNativeBridge'
|
|
39
|
+
import type { AppKeyIdentity, OwnerAuthKey, SecureKeyStore } from './SecureKey'
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Resolves the installation AuthKey for one already-known Owner.
|
|
43
|
+
*
|
|
44
|
+
* Deliberately a zero-arg function rather than the store itself: a holder can
|
|
45
|
+
* obtain the one key its identity namespaces and nothing else — no alias of
|
|
46
|
+
* its choosing, and no store to resolve a different one from. This is the
|
|
47
|
+
* shape `DaaUserSigning.resolveAuthKey` (`api/signedResource.ts`) needs,
|
|
48
|
+
* because by the time `forUser` builds one it already knows which Owner it is
|
|
49
|
+
* signing for. It is NOT the shape {@link createOwnerAuthKeyResolver} returns
|
|
50
|
+
* — see {@link AppAuthKeyResolver}.
|
|
51
|
+
*/
|
|
52
|
+
export type OwnerAuthKeyResolver = () => Promise<OwnerAuthKey>
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Resolves the installation AuthKey for a given Owner, by `clientEntityId`.
|
|
56
|
+
*
|
|
57
|
+
* `clientEntityId` is a per-call parameter rather than baked into the
|
|
58
|
+
* resolver at construction: it is not known when `DaaClient`
|
|
59
|
+
* builds this resolver (only the Social Session response names an Owner), and
|
|
60
|
+
* one long-lived `DaaClient` may serve more than one Owner over its lifetime
|
|
61
|
+
* — sign-out, then sign back in as someone else. Each call site binds the
|
|
62
|
+
* `clientEntityId` it already has (Social Session's response for login;
|
|
63
|
+
* `DaaUserContext.clientEntityId` for `forUser`) and, for `forUser`, wraps the
|
|
64
|
+
* result down to an {@link OwnerAuthKeyResolver} before handing it to a
|
|
65
|
+
* resource client — a resource client has no Owner concept of its own.
|
|
66
|
+
*/
|
|
67
|
+
export type AppAuthKeyResolver = (clientEntityId: string) => Promise<OwnerAuthKey>
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Build the resolver for one application identity.
|
|
71
|
+
*
|
|
72
|
+
* @param identity App-level identity (`appId`, `environment`); `null` when
|
|
73
|
+
* the SDK has no application identity to select a key with, which fails
|
|
74
|
+
* closed at the first resolution rather than at construction —
|
|
75
|
+
* `AuthClient` is built for Prepare, and Prepare never touches an
|
|
76
|
+
* AuthKey.
|
|
77
|
+
* @param secureKeys Injected store, which tests substitute. Absent, the native
|
|
78
|
+
* store is constructed on first use and reused.
|
|
79
|
+
*/
|
|
80
|
+
export function createOwnerAuthKeyResolver(
|
|
81
|
+
identity: AppKeyIdentity | null,
|
|
82
|
+
secureKeys: SecureKeyStore | null,
|
|
83
|
+
): AppAuthKeyResolver {
|
|
84
|
+
// The mutable slot the lazily-constructed native store fills. A closure
|
|
85
|
+
// variable rather than a field because the clients holding this resolver are
|
|
86
|
+
// sealed with `Object.freeze`.
|
|
87
|
+
let nativeStore: NativeSecureKeyStore | null = null
|
|
88
|
+
|
|
89
|
+
return async (clientEntityId: string) => {
|
|
90
|
+
if (identity === null) {
|
|
91
|
+
throw contractViolation('AuthKey selection requires the DaaClient application identity')
|
|
92
|
+
}
|
|
93
|
+
const store = secureKeys ?? (nativeStore ??= new NativeSecureKeyStore(resolveNativeBridge()))
|
|
94
|
+
return store.resolveOrCreate({ ...identity, clientEntityId })
|
|
95
|
+
}
|
|
96
|
+
}
|