@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
|
@@ -1,522 +0,0 @@
|
|
|
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 DAA-signed Prepare receipt.
|
|
20
|
-
//
|
|
21
|
-
// When a trust anchor is pinned for the environment, the SDK verifies the
|
|
22
|
-
// returned Prepare receipt before starting provider login and fails closed if
|
|
23
|
-
// the receipt is missing or does not verify. With no anchor pinned, the SDK
|
|
24
|
-
// ignores the receipt (see below).
|
|
25
|
-
//
|
|
26
|
-
// **What it defends against.** The SDK reaches DAA through the application's
|
|
27
|
-
// own backend, which the trust model treats as untrusted. Without a verified
|
|
28
|
-
// receipt that relay can substitute a different login request — another
|
|
29
|
-
// attempt's `loginRequestId` and nonce — and the SDK would hand that nonce to
|
|
30
|
-
// provider login. The receipt is DAA-signed, so the relay cannot mint one for
|
|
31
|
-
// substituted values. What makes the signature an answer is binding its claims
|
|
32
|
-
// to values the relay cannot choose: the registration code challenge and the
|
|
33
|
-
// registration-flow key thumbprint the SDK generated itself, before Prepare
|
|
34
|
-
// was sent. Those two are the load-bearing checks. Tenant scope alone would not
|
|
35
|
-
// be: two attempts under one customer app carry identical tenant claims.
|
|
36
|
-
//
|
|
37
|
-
// **Claims.** The claim set DAA signs under `typ: daa-prepare-receipt+jws`:
|
|
38
|
-
// `iss`, `aud`, `iat`, `exp`, `jti`, `environment`, `customerAppId`,
|
|
39
|
-
// `parentEntityIdDigest`, `provider`, `loginRequestId`, `nonce`,
|
|
40
|
-
// `registrationCodeChallenge`, `registrationFlowKeyThumbprint`. The
|
|
41
|
-
// `nonce` claim carries the value the Prepare response calls `oidcNonce`.
|
|
42
|
-
// There is no state claim: `oidcState` is not bound by the receipt.
|
|
43
|
-
//
|
|
44
|
-
// **Tenant scope is not checked on the login path.** Comparing
|
|
45
|
-
// `customerAppId` / `parentEntityIdDigest` needs the parent entity the SDK
|
|
46
|
-
// expects, and the SDK defines no mapping from `DaaSdkConfiguration.appId` to
|
|
47
|
-
// DAA's parent entity. {@link requirePrepareReceiptTenantScope} is the
|
|
48
|
-
// comparison; the login path does not call it.
|
|
49
|
-
//
|
|
50
|
-
// **No trust anchor is pinned in this release.** The anchor (issuer, audience,
|
|
51
|
-
// signing keys, clock allowance) ships inside the SDK build, and
|
|
52
|
-
// {@link pinnedPrepareReceiptTrustAnchor} returns `null` for every environment.
|
|
53
|
-
// {@link checkPrepareReceipt} defines what login does in each case: with no
|
|
54
|
-
// anchor, the SDK ignores a receipt rather than failing on it. Ignoring loses
|
|
55
|
-
// nothing, because an SDK with no anchor could not verify the receipt anyway.
|
|
56
|
-
// It does mean `oidcNonce` and `oidcState` are not authenticated by the SDK:
|
|
57
|
-
// integrators bind the provider redirect with their own OAuth `state`.
|
|
58
|
-
//
|
|
59
|
-
// **Errors.** Every failure to verify a receipt the SDK needs is
|
|
60
|
-
// `prepare_receipt_invalid`. Faults in the SDK's own inputs — a
|
|
61
|
-
// malformed pinned anchor, an incomplete expectation, a bad test clock, a
|
|
62
|
-
// parent entity id that is not a canonical UUID — are `contract_violation`,
|
|
63
|
-
// because they describe this build, not the receipt.
|
|
64
|
-
|
|
65
|
-
import type { DaaEnvironment } from '../config'
|
|
66
|
-
import { fromBase64Url, toBase64Url } from '../crypto/base64url'
|
|
67
|
-
import { ownMember, verifyCompactEs256, type PinnedEs256Key } from '../crypto/es256Jws'
|
|
68
|
-
import type { P256PublicJwk } from '../crypto/jwkThumbprint'
|
|
69
|
-
import { sha256 } from '../crypto/sha256'
|
|
70
|
-
import { contractViolation, DaaError, isDaaError, prepareReceiptInvalid } from '../errors'
|
|
71
|
-
import { isUuid } from '../validation/uuid'
|
|
72
|
-
|
|
73
|
-
/** Contract bounds on `SocialLoginRequest.prepareReceiptJws`. */
|
|
74
|
-
export const PREPARE_RECEIPT_MIN_LENGTH = 64
|
|
75
|
-
export const PREPARE_RECEIPT_MAX_LENGTH = 8192
|
|
76
|
-
|
|
77
|
-
/** The JWS `typ` DAA signs the receipt with. */
|
|
78
|
-
export const PREPARE_RECEIPT_TYP = 'daa-prepare-receipt+jws'
|
|
79
|
-
|
|
80
|
-
/** Widest clock skew an anchor may allow, so a configuration typo cannot disable expiry. */
|
|
81
|
-
export const PREPARE_RECEIPT_MAX_CLOCK_SKEW_SECONDS = 300
|
|
82
|
-
|
|
83
|
-
/** Compact JWS serialization: three Base64URL segments separated by dots. */
|
|
84
|
-
const COMPACT_JWS = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/
|
|
85
|
-
|
|
86
|
-
/** One pinned receipt-signing key, addressed by the `kid` DAA puts in the header. */
|
|
87
|
-
export type PrepareReceiptVerificationKey = Readonly<{ kid: string; jwk: P256PublicJwk }>
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Everything a trust anchor pins, in one value.
|
|
91
|
-
*
|
|
92
|
-
* `verificationKeys` is pinned by the build and never derived from a receipt.
|
|
93
|
-
* More than one entry allows a rotation overlap; the receipt's `kid` selects
|
|
94
|
-
* among them and nothing else.
|
|
95
|
-
*/
|
|
96
|
-
export type PrepareReceiptTrustAnchor = Readonly<{
|
|
97
|
-
/** Exact expected `iss`. */
|
|
98
|
-
issuer: string
|
|
99
|
-
/** Exact expected `aud`, or one member of an `aud` array. */
|
|
100
|
-
audience: string
|
|
101
|
-
verificationKeys: readonly PrepareReceiptVerificationKey[]
|
|
102
|
-
/** Allowance on `exp` / `iat` / `nbf`, in seconds. */
|
|
103
|
-
maxClockSkewSeconds: number
|
|
104
|
-
}>
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* The build-time pinned anchor for an environment. **`null` for every
|
|
108
|
-
* environment in this release.**
|
|
109
|
-
*
|
|
110
|
-
* A function rather than a `const`, so that its declared return type does not
|
|
111
|
-
* narrow the rest of this module to unreachable code.
|
|
112
|
-
*/
|
|
113
|
-
export function pinnedPrepareReceiptTrustAnchor(
|
|
114
|
-
environment: DaaEnvironment,
|
|
115
|
-
): PrepareReceiptTrustAnchor | null {
|
|
116
|
-
void environment
|
|
117
|
-
return null
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* The values this attempt's receipt must name. The first two come from the
|
|
122
|
-
* Prepare response; the rest are the SDK's own.
|
|
123
|
-
*/
|
|
124
|
-
export type PrepareReceiptExpectation = Readonly<{
|
|
125
|
-
loginRequestId: string
|
|
126
|
-
/** The Prepare response's `oidcNonce`; the receipt's `nonce` claim. */
|
|
127
|
-
nonce: string
|
|
128
|
-
provider: string
|
|
129
|
-
environment: DaaEnvironment
|
|
130
|
-
registrationCodeChallenge: string
|
|
131
|
-
registrationFlowKeyThumbprint: string
|
|
132
|
-
}>
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Test-only clock, following `auth/deviceAuthorization.ts`: production code
|
|
136
|
-
* always uses the system clock, and no production caller passes one.
|
|
137
|
-
*/
|
|
138
|
-
export type PrepareReceiptClock = Readonly<{ nowEpochSeconds: () => number }>
|
|
139
|
-
|
|
140
|
-
const SYSTEM_CLOCK: PrepareReceiptClock = {
|
|
141
|
-
nowEpochSeconds: () => Math.floor(Date.now() / 1000),
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/** What a verified receipt attested. Only ever returned after every check passed. */
|
|
145
|
-
export type VerifiedPrepareReceipt = Readonly<{
|
|
146
|
-
/** The receipt exactly as received. */
|
|
147
|
-
compactJws: string
|
|
148
|
-
loginRequestId: string
|
|
149
|
-
nonce: string
|
|
150
|
-
expiresAtEpochSeconds: number
|
|
151
|
-
/** Signed but not compared on the login path; see {@link requirePrepareReceiptTenantScope}. */
|
|
152
|
-
tenantScope: Readonly<{ customerAppId: unknown; parentEntityIdDigest: unknown }>
|
|
153
|
-
}>
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Whether a value is shaped like the compact JWS the contract describes and sits
|
|
157
|
-
* within its declared length bounds. Structural only: shape is not trust.
|
|
158
|
-
*/
|
|
159
|
-
export function isPrepareReceiptShape(value: string): boolean {
|
|
160
|
-
return (
|
|
161
|
-
value.length >= PREPARE_RECEIPT_MIN_LENGTH &&
|
|
162
|
-
value.length <= PREPARE_RECEIPT_MAX_LENGTH &&
|
|
163
|
-
COMPACT_JWS.test(value)
|
|
164
|
-
)
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* What login does with the receipt, given the anchor for this environment.
|
|
169
|
-
*
|
|
170
|
-
* | Anchor | Receipt | Result |
|
|
171
|
-
* | ------ | ------- | ----------------------------------------------------------- |
|
|
172
|
-
* | unset | absent | `null`: nothing to verify, login proceeds |
|
|
173
|
-
* | unset | present | `null`: the receipt is ignored, login proceeds as if absent |
|
|
174
|
-
* | set | absent | `prepare_receipt_invalid`: the receipt is required |
|
|
175
|
-
* | set | present | {@link verifyPrepareReceipt}; any failure throws |
|
|
176
|
-
*
|
|
177
|
-
* `prepareReceiptJws` is the raw response member: `undefined` or `null` is
|
|
178
|
-
* absent, and anything else is present, whatever its type. In the first two
|
|
179
|
-
* rows it is not read at all, and `expected` is a thunk so they compute
|
|
180
|
-
* nothing: with no anchor, the login path does no receipt work.
|
|
181
|
-
*
|
|
182
|
-
* In the second row, an SDK without the anchor ignores the receipt: it could
|
|
183
|
-
* not verify it, so ignoring gives up no protection. An ignored receipt is
|
|
184
|
-
* never passed on as if it were checked: this returns `null` for it exactly as
|
|
185
|
-
* for no receipt, so the login path hands the application no receipt at all.
|
|
186
|
-
*/
|
|
187
|
-
export async function checkPrepareReceipt(
|
|
188
|
-
prepareReceiptJws: unknown,
|
|
189
|
-
anchor: PrepareReceiptTrustAnchor | null,
|
|
190
|
-
expected: () => PrepareReceiptExpectation,
|
|
191
|
-
clock: PrepareReceiptClock = SYSTEM_CLOCK,
|
|
192
|
-
): Promise<VerifiedPrepareReceipt | null> {
|
|
193
|
-
if (anchor === null) {
|
|
194
|
-
return null
|
|
195
|
-
}
|
|
196
|
-
if (prepareReceiptJws === undefined || prepareReceiptJws === null) {
|
|
197
|
-
throw prepareReceiptInvalid(
|
|
198
|
-
'the Prepare response carries no receipt, and this environment requires one',
|
|
199
|
-
)
|
|
200
|
-
}
|
|
201
|
-
return verifyPrepareReceipt(prepareReceiptJws, anchor, expected(), clock)
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Verify the receipt against a trust anchor and this attempt's expectation.
|
|
206
|
-
*
|
|
207
|
-
* Order: the anchor and the expectation first, because a fault in either is a
|
|
208
|
-
* configuration error; then the receipt's shape; then its header and signature;
|
|
209
|
-
* and only then the claims, because a claim from an unverified token is not
|
|
210
|
-
* evidence of anything.
|
|
211
|
-
*
|
|
212
|
-
* A fault in the anchor, the expectation, or the clock is `contract_violation`;
|
|
213
|
-
* every rejection of the receipt itself, including a platform verifier that is
|
|
214
|
-
* missing or fails, is `prepare_receipt_invalid`.
|
|
215
|
-
*
|
|
216
|
-
* The receipt is never interpolated into an error: it is unverified
|
|
217
|
-
* server-supplied text, and a consumer is likely to log the message verbatim.
|
|
218
|
-
*/
|
|
219
|
-
export async function verifyPrepareReceipt(
|
|
220
|
-
prepareReceiptJws: unknown,
|
|
221
|
-
anchor: PrepareReceiptTrustAnchor,
|
|
222
|
-
expected: PrepareReceiptExpectation,
|
|
223
|
-
clock: PrepareReceiptClock = SYSTEM_CLOCK,
|
|
224
|
-
): Promise<VerifiedPrepareReceipt> {
|
|
225
|
-
const keys = pinnedKeys(anchor)
|
|
226
|
-
requireExpectation(expected)
|
|
227
|
-
const now = clock.nowEpochSeconds()
|
|
228
|
-
if (!Number.isInteger(now) || now <= 0) {
|
|
229
|
-
throw contractViolation('the Prepare receipt clock must yield a positive Unix timestamp')
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (typeof prepareReceiptJws !== 'string' || !isPrepareReceiptShape(prepareReceiptJws)) {
|
|
233
|
-
throw prepareReceiptInvalid(
|
|
234
|
-
'the Prepare receipt is not a compact JWS within the contract length bounds',
|
|
235
|
-
)
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const result = await platformVerify(prepareReceiptJws, keys)
|
|
239
|
-
if (!result.verified) {
|
|
240
|
-
throw prepareReceiptInvalid(`the Prepare receipt did not verify (${result.reason})`)
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const claims = result.payload
|
|
244
|
-
requireClaimEquals(claims, 'iss', anchor.issuer)
|
|
245
|
-
requireAudience(claims, anchor.audience)
|
|
246
|
-
requireNonEmptyClaim(claims, 'jti')
|
|
247
|
-
requireClaimEquals(claims, 'environment', expected.environment)
|
|
248
|
-
requireClaimEquals(claims, 'provider', expected.provider)
|
|
249
|
-
requireClaimEquals(claims, 'loginRequestId', expected.loginRequestId)
|
|
250
|
-
requireClaimEquals(claims, 'nonce', expected.nonce)
|
|
251
|
-
requireClaimEquals(claims, 'registrationCodeChallenge', expected.registrationCodeChallenge)
|
|
252
|
-
requireClaimEquals(
|
|
253
|
-
claims,
|
|
254
|
-
'registrationFlowKeyThumbprint',
|
|
255
|
-
expected.registrationFlowKeyThumbprint,
|
|
256
|
-
)
|
|
257
|
-
const expiresAtEpochSeconds = requireTimeClaims(claims, now, anchor.maxClockSkewSeconds)
|
|
258
|
-
|
|
259
|
-
return {
|
|
260
|
-
compactJws: prepareReceiptJws,
|
|
261
|
-
loginRequestId: expected.loginRequestId,
|
|
262
|
-
nonce: expected.nonce,
|
|
263
|
-
expiresAtEpochSeconds,
|
|
264
|
-
tenantScope: {
|
|
265
|
-
customerAppId: ownMember(claims, 'customerAppId'),
|
|
266
|
-
parentEntityIdDigest: ownMember(claims, 'parentEntityIdDigest'),
|
|
267
|
-
},
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* The signature check, with a platform verifier that is missing or fails
|
|
273
|
-
* reported as the receipt failing to verify.
|
|
274
|
-
*
|
|
275
|
-
* `pinnedKeys` has already validated everything {@link verifyCompactEs256}
|
|
276
|
-
* would reject as a configuration error, so the only `DaaError` left to come
|
|
277
|
-
* out of it is the platform's: no `DaaRegistration` module, one without the
|
|
278
|
-
* verify function, or a native rejection. A native rejection also covers a
|
|
279
|
-
* pinned point the platform does not accept as on the curve — the one anchor
|
|
280
|
-
* fault only the platform can see, and indistinguishable here from the rest.
|
|
281
|
-
* The SDK's own message and the original error (as `cause`) are kept.
|
|
282
|
-
*/
|
|
283
|
-
async function platformVerify(
|
|
284
|
-
jws: string,
|
|
285
|
-
keys: readonly PinnedEs256Key[],
|
|
286
|
-
): Promise<Awaited<ReturnType<typeof verifyCompactEs256>>> {
|
|
287
|
-
try {
|
|
288
|
-
return await verifyCompactEs256(jws, { keys, expectedTyp: PREPARE_RECEIPT_TYP })
|
|
289
|
-
} catch (err) {
|
|
290
|
-
if (isDaaError(err) && err.detail.kind === 'contract_violation') {
|
|
291
|
-
throw new DaaError({ kind: 'prepare_receipt_invalid' }, err.message, err)
|
|
292
|
-
}
|
|
293
|
-
throw err
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Domain separator for `parentEntityIdDigest`, byte-for-byte the one DAA
|
|
299
|
-
* signs with. The trailing NUL is part of it.
|
|
300
|
-
*/
|
|
301
|
-
export const PARENT_ENTITY_ID_DIGEST_DOMAIN = 'daa-prepare-receipt-parent-entity-digest-v1\0'
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* `base64url_nopad(SHA-256(utf8(domain) || utf8(parentEntityId)))`, where the
|
|
305
|
-
* id is the canonical lower-case hyphenated UUID — DAA's pinned formula.
|
|
306
|
-
*/
|
|
307
|
-
export function parentEntityIdDigest(parentEntityId: string): string {
|
|
308
|
-
if (
|
|
309
|
-
typeof parentEntityId !== 'string' ||
|
|
310
|
-
!isUuid(parentEntityId) ||
|
|
311
|
-
parentEntityId !== parentEntityId.toLowerCase()
|
|
312
|
-
) {
|
|
313
|
-
throw contractViolation('parentEntityId must be a canonical lower-case UUID')
|
|
314
|
-
}
|
|
315
|
-
const encoder = new TextEncoder()
|
|
316
|
-
const domain = encoder.encode(PARENT_ENTITY_ID_DIGEST_DOMAIN)
|
|
317
|
-
const id = encoder.encode(parentEntityId)
|
|
318
|
-
const input = new Uint8Array(domain.length + id.length)
|
|
319
|
-
input.set(domain)
|
|
320
|
-
input.set(id, domain.length)
|
|
321
|
-
return toBase64Url(sha256(input))
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Tenant-scope comparison for a verified receipt. **Not called on the login
|
|
326
|
-
* path:** the SDK has no defined way to derive the expected parent entity from
|
|
327
|
-
* its configuration. A caller that has the expected values calls this after
|
|
328
|
-
* {@link verifyPrepareReceipt}.
|
|
329
|
-
*/
|
|
330
|
-
export function requirePrepareReceiptTenantScope(
|
|
331
|
-
receipt: VerifiedPrepareReceipt,
|
|
332
|
-
expected: Readonly<{ customerAppId: string; parentEntityId: string }>,
|
|
333
|
-
): void {
|
|
334
|
-
const { customerAppId, parentEntityIdDigest: digest } = receipt.tenantScope
|
|
335
|
-
if (typeof customerAppId !== 'string' || customerAppId !== expected.customerAppId) {
|
|
336
|
-
throw prepareReceiptInvalid('the Prepare receipt does not bind the expected customerAppId')
|
|
337
|
-
}
|
|
338
|
-
if (typeof digest !== 'string' || digest !== parentEntityIdDigest(expected.parentEntityId)) {
|
|
339
|
-
throw prepareReceiptInvalid(
|
|
340
|
-
'the Prepare receipt does not bind the expected parentEntityIdDigest',
|
|
341
|
-
)
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Validate the anchor and convert its keys to the points the platform verifies
|
|
347
|
-
* against. A malformed anchor is a configuration error, not a rejected receipt.
|
|
348
|
-
* Whether each point is on the curve is the platform's check.
|
|
349
|
-
*/
|
|
350
|
-
function pinnedKeys(anchor: PrepareReceiptTrustAnchor): readonly PinnedEs256Key[] {
|
|
351
|
-
if (typeof anchor !== 'object' || anchor === null) {
|
|
352
|
-
throw contractViolation('the Prepare receipt trust anchor must be an object')
|
|
353
|
-
}
|
|
354
|
-
if (typeof anchor.issuer !== 'string' || anchor.issuer.length === 0) {
|
|
355
|
-
throw contractViolation('the Prepare receipt trust anchor must name an issuer')
|
|
356
|
-
}
|
|
357
|
-
if (typeof anchor.audience !== 'string' || anchor.audience.length === 0) {
|
|
358
|
-
throw contractViolation('the Prepare receipt trust anchor must name an audience')
|
|
359
|
-
}
|
|
360
|
-
if (
|
|
361
|
-
!Number.isInteger(anchor.maxClockSkewSeconds) ||
|
|
362
|
-
anchor.maxClockSkewSeconds < 0 ||
|
|
363
|
-
anchor.maxClockSkewSeconds > PREPARE_RECEIPT_MAX_CLOCK_SKEW_SECONDS
|
|
364
|
-
) {
|
|
365
|
-
throw contractViolation(
|
|
366
|
-
'the Prepare receipt trust anchor clock skew must be between 0 and ' +
|
|
367
|
-
`${PREPARE_RECEIPT_MAX_CLOCK_SKEW_SECONDS} seconds`,
|
|
368
|
-
)
|
|
369
|
-
}
|
|
370
|
-
if (!Array.isArray(anchor.verificationKeys) || anchor.verificationKeys.length === 0) {
|
|
371
|
-
throw contractViolation('the Prepare receipt trust anchor must pin at least one P-256 key')
|
|
372
|
-
}
|
|
373
|
-
const kids = new Set<string>()
|
|
374
|
-
for (const key of anchor.verificationKeys) {
|
|
375
|
-
if (typeof key !== 'object' || key === null) {
|
|
376
|
-
throw contractViolation('every Prepare receipt verification key must be an object')
|
|
377
|
-
}
|
|
378
|
-
if (typeof key.kid !== 'string' || key.kid.length === 0) {
|
|
379
|
-
throw contractViolation('every Prepare receipt verification key must have a non-empty kid')
|
|
380
|
-
}
|
|
381
|
-
if (kids.has(key.kid)) {
|
|
382
|
-
throw contractViolation('Prepare receipt verification keys must have distinct kids')
|
|
383
|
-
}
|
|
384
|
-
kids.add(key.kid)
|
|
385
|
-
}
|
|
386
|
-
return anchor.verificationKeys.map((key) => ({
|
|
387
|
-
kid: key.kid,
|
|
388
|
-
publicKeyX963: x963FromJwk(key.jwk),
|
|
389
|
-
}))
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
function x963FromJwk(jwk: P256PublicJwk): Uint8Array {
|
|
393
|
-
if (typeof jwk !== 'object' || jwk === null || jwk.kty !== 'EC' || jwk.crv !== 'P-256') {
|
|
394
|
-
throw contractViolation('a Prepare receipt verification key must be an EC P-256 JWK')
|
|
395
|
-
}
|
|
396
|
-
let x: Uint8Array
|
|
397
|
-
let y: Uint8Array
|
|
398
|
-
try {
|
|
399
|
-
x = fromBase64Url(jwk.x)
|
|
400
|
-
y = fromBase64Url(jwk.y)
|
|
401
|
-
} catch {
|
|
402
|
-
throw contractViolation('a Prepare receipt verification key has a non-Base64URL coordinate')
|
|
403
|
-
}
|
|
404
|
-
if (x.length !== 32 || y.length !== 32) {
|
|
405
|
-
throw contractViolation(
|
|
406
|
-
'a Prepare receipt verification key must have 32-byte x and y coordinates',
|
|
407
|
-
)
|
|
408
|
-
}
|
|
409
|
-
const point = new Uint8Array(65)
|
|
410
|
-
point[0] = 0x04
|
|
411
|
-
point.set(x, 1)
|
|
412
|
-
point.set(y, 33)
|
|
413
|
-
return point
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
function requireExpectation(expected: PrepareReceiptExpectation): void {
|
|
417
|
-
if (typeof expected !== 'object' || expected === null) {
|
|
418
|
-
throw contractViolation(
|
|
419
|
-
'verifying a Prepare receipt requires the expected values for this attempt',
|
|
420
|
-
)
|
|
421
|
-
}
|
|
422
|
-
for (const name of [
|
|
423
|
-
'loginRequestId',
|
|
424
|
-
'nonce',
|
|
425
|
-
'provider',
|
|
426
|
-
'environment',
|
|
427
|
-
'registrationCodeChallenge',
|
|
428
|
-
'registrationFlowKeyThumbprint',
|
|
429
|
-
] as const) {
|
|
430
|
-
const value = expected[name]
|
|
431
|
-
if (typeof value !== 'string' || value.length === 0) {
|
|
432
|
-
throw contractViolation(`verifying a Prepare receipt requires the expected ${name}`)
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* Exact string equality on one claim. Not constant-time, and does not need to
|
|
439
|
-
* be: every value compared is one the SDK sent, generated, or relayed to the
|
|
440
|
-
* application; none is a secret. The claim name goes in the message — it is
|
|
441
|
-
* one of this module's literals — and the value never does.
|
|
442
|
-
*/
|
|
443
|
-
function requireClaimEquals(
|
|
444
|
-
claims: Readonly<Record<string, unknown>>,
|
|
445
|
-
name: string,
|
|
446
|
-
expected: string,
|
|
447
|
-
): void {
|
|
448
|
-
const actual = ownMember(claims, name)
|
|
449
|
-
if (typeof actual !== 'string' || actual !== expected) {
|
|
450
|
-
throw prepareReceiptInvalid(`the Prepare receipt does not bind the expected ${name}`)
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
function requireNonEmptyClaim(claims: Readonly<Record<string, unknown>>, name: string): void {
|
|
455
|
-
const actual = ownMember(claims, name)
|
|
456
|
-
if (typeof actual !== 'string' || actual.length === 0) {
|
|
457
|
-
throw prepareReceiptInvalid(`the Prepare receipt does not carry a ${name}`)
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
/** RFC 7519 §4.1.3: `aud` is a string or an array of strings that must contain ours. */
|
|
462
|
-
function requireAudience(claims: Readonly<Record<string, unknown>>, audience: string): void {
|
|
463
|
-
const actual = ownMember(claims, 'aud')
|
|
464
|
-
if (typeof actual === 'string' && actual === audience) {
|
|
465
|
-
return
|
|
466
|
-
}
|
|
467
|
-
if (Array.isArray(actual) && actual.some((value) => value === audience)) {
|
|
468
|
-
return
|
|
469
|
-
}
|
|
470
|
-
throw prepareReceiptInvalid('the Prepare receipt does not bind the expected aud')
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
/**
|
|
474
|
-
* `exp` and `iat` are required (DAA always sets both); `nbf` is checked when
|
|
475
|
-
* present. All are RFC 7519 NumericDates in seconds — deliberately not the
|
|
476
|
-
* epoch-millis tolerance `wire/instant.ts` carries for the Prepare body, since
|
|
477
|
-
* DAA's signer emits seconds. A millisecond value fails the upper bound below
|
|
478
|
-
* instead of passing as an expiry thousands of years out.
|
|
479
|
-
*/
|
|
480
|
-
function requireTimeClaims(
|
|
481
|
-
claims: Readonly<Record<string, unknown>>,
|
|
482
|
-
nowEpochSeconds: number,
|
|
483
|
-
maxClockSkewSeconds: number,
|
|
484
|
-
): number {
|
|
485
|
-
const exp = requireNumericDate(claims, 'exp')
|
|
486
|
-
const iat = requireNumericDate(claims, 'iat')
|
|
487
|
-
if (exp + maxClockSkewSeconds < nowEpochSeconds) {
|
|
488
|
-
throw prepareReceiptInvalid('the Prepare receipt has expired')
|
|
489
|
-
}
|
|
490
|
-
if (iat - maxClockSkewSeconds > nowEpochSeconds) {
|
|
491
|
-
throw prepareReceiptInvalid('the Prepare receipt is not valid yet (iat is in the future)')
|
|
492
|
-
}
|
|
493
|
-
if (iat > exp) {
|
|
494
|
-
throw prepareReceiptInvalid('the Prepare receipt expires before it was issued')
|
|
495
|
-
}
|
|
496
|
-
if (ownMember(claims, 'nbf') !== undefined) {
|
|
497
|
-
const nbf = requireNumericDate(claims, 'nbf')
|
|
498
|
-
if (nbf - maxClockSkewSeconds > nowEpochSeconds) {
|
|
499
|
-
throw prepareReceiptInvalid('the Prepare receipt is not valid yet (nbf is in the future)')
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
return exp
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
/** Past year 9999 is not a clock; it is a unit mistake or a receipt that never expires. */
|
|
506
|
-
const MAX_NUMERIC_DATE = 253402300799
|
|
507
|
-
|
|
508
|
-
function requireNumericDate(claims: Readonly<Record<string, unknown>>, name: string): number {
|
|
509
|
-
const value = ownMember(claims, name)
|
|
510
|
-
if (value === undefined) {
|
|
511
|
-
throw prepareReceiptInvalid(`the Prepare receipt does not carry an ${name}`)
|
|
512
|
-
}
|
|
513
|
-
if (
|
|
514
|
-
typeof value !== 'number' ||
|
|
515
|
-
!Number.isInteger(value) ||
|
|
516
|
-
value <= 0 ||
|
|
517
|
-
value > MAX_NUMERIC_DATE
|
|
518
|
-
) {
|
|
519
|
-
throw prepareReceiptInvalid(`the Prepare receipt carries a malformed ${name}`)
|
|
520
|
-
}
|
|
521
|
-
return value
|
|
522
|
-
}
|