@pithy-sh/auth 0.15.2 → 0.16.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/README.md +4 -0
- package/dist/attestation/appAttest/assertion.d.ts +32 -0
- package/dist/attestation/appAttest/assertion.d.ts.map +1 -0
- package/dist/attestation/appAttest/assertion.js +59 -0
- package/dist/attestation/appAttest/attestation.d.ts +49 -0
- package/dist/attestation/appAttest/attestation.d.ts.map +1 -0
- package/dist/attestation/appAttest/attestation.js +111 -0
- package/dist/attestation/appAttest/authData.d.ts +24 -0
- package/dist/attestation/appAttest/authData.d.ts.map +1 -0
- package/dist/attestation/appAttest/authData.js +32 -0
- package/dist/attestation/appAttest/base64.d.ts +8 -0
- package/dist/attestation/appAttest/base64.d.ts.map +1 -0
- package/dist/attestation/appAttest/base64.js +27 -0
- package/dist/attestation/appAttest/bytes.d.ts +7 -0
- package/dist/attestation/appAttest/bytes.d.ts.map +1 -0
- package/dist/attestation/appAttest/bytes.js +19 -0
- package/dist/attestation/appAttest/cbor.d.ts +20 -0
- package/dist/attestation/appAttest/cbor.d.ts.map +1 -0
- package/dist/attestation/appAttest/cbor.js +96 -0
- package/dist/attestation/appAttest/der.d.ts +38 -0
- package/dist/attestation/appAttest/der.d.ts.map +1 -0
- package/dist/attestation/appAttest/der.js +106 -0
- package/dist/attestation/appAttest/keys.d.ts +15 -0
- package/dist/attestation/appAttest/keys.d.ts.map +1 -0
- package/dist/attestation/appAttest/keys.js +37 -0
- package/dist/attestation/appAttest/root.d.ts +9 -0
- package/dist/attestation/appAttest/root.d.ts.map +1 -0
- package/dist/attestation/appAttest/root.js +12 -0
- package/dist/attestation/appAttest/verifier.d.ts +39 -0
- package/dist/attestation/appAttest/verifier.d.ts.map +1 -0
- package/dist/attestation/appAttest/verifier.js +121 -0
- package/dist/attestation/appAttest/x509.d.ts +50 -0
- package/dist/attestation/appAttest/x509.d.ts.map +1 -0
- package/dist/attestation/appAttest/x509.js +163 -0
- package/dist/attestation/challenge.d.ts +26 -0
- package/dist/attestation/challenge.d.ts.map +1 -0
- package/dist/attestation/challenge.js +47 -0
- package/dist/attestation/config.d.ts +72 -0
- package/dist/attestation/config.d.ts.map +1 -0
- package/dist/attestation/config.js +52 -0
- package/dist/attestation/errors.d.ts +30 -0
- package/dist/attestation/errors.d.ts.map +1 -0
- package/dist/attestation/errors.js +45 -0
- package/dist/attestation/gate.d.ts +27 -0
- package/dist/attestation/gate.d.ts.map +1 -0
- package/dist/attestation/gate.js +78 -0
- package/dist/attestation/playIntegrity/google.d.ts +47 -0
- package/dist/attestation/playIntegrity/google.d.ts.map +1 -0
- package/dist/attestation/playIntegrity/google.js +178 -0
- package/dist/attestation/playIntegrity/verdict.d.ts +40 -0
- package/dist/attestation/playIntegrity/verdict.d.ts.map +1 -0
- package/dist/attestation/playIntegrity/verdict.js +93 -0
- package/dist/attestation/playIntegrity/verifier.d.ts +22 -0
- package/dist/attestation/playIntegrity/verifier.d.ts.map +1 -0
- package/dist/attestation/playIntegrity/verifier.js +64 -0
- package/dist/attestation/verifier.d.ts +60 -0
- package/dist/attestation/verifier.d.ts.map +1 -0
- package/dist/attestation/verifier.js +46 -0
- package/dist/attestation/wire.d.ts +42 -0
- package/dist/attestation/wire.d.ts.map +1 -0
- package/dist/attestation/wire.js +72 -0
- package/dist/capability.d.ts +40 -0
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +38 -5
- package/dist/client/api.d.ts +37 -24
- package/dist/client/api.d.ts.map +1 -1
- package/dist/client/api.js +52 -41
- package/dist/client/attestation.d.ts +148 -0
- package/dist/client/attestation.d.ts.map +1 -0
- package/dist/client/attestation.js +321 -0
- package/dist/client/projection.d.ts +29 -4
- package/dist/client/projection.d.ts.map +1 -1
- package/dist/data/appAttestKey.d.ts +30 -0
- package/dist/data/appAttestKey.d.ts.map +1 -0
- package/dist/data/appAttestKey.js +25 -0
- package/dist/data/attestationChallenge.d.ts +17 -0
- package/dist/data/attestationChallenge.d.ts.map +1 -0
- package/dist/data/attestationChallenge.js +19 -0
- package/dist/data/tables.d.ts +18 -1
- package/dist/data/tables.d.ts.map +1 -1
- package/dist/data/tables.js +6 -2
- package/dist/http/devLoginRoute.js +3 -3
- package/dist/http/resolve.js +1 -1
- package/dist/http/responses.d.ts +12 -0
- package/dist/http/responses.d.ts.map +1 -1
- package/dist/http/responses.js +11 -1
- package/dist/http/routes.d.ts +5 -1
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +92 -25
- package/dist/http/schemas.d.ts +7 -0
- package/dist/http/schemas.d.ts.map +1 -1
- package/dist/http/schemas.js +7 -1
- package/dist/index.js +1 -1
- package/dist/instance/secrets.d.ts +86 -7
- package/dist/instance/secrets.d.ts.map +1 -1
- package/dist/instance/secrets.js +54 -8
- package/dist/migrations/0009_attestation_challenges.d.ts +9 -0
- package/dist/migrations/0009_attestation_challenges.d.ts.map +1 -0
- package/dist/migrations/0009_attestation_challenges.js +21 -0
- package/dist/migrations/0010_app_attest_keys.d.ts +9 -0
- package/dist/migrations/0010_app_attest_keys.d.ts.map +1 -0
- package/dist/migrations/0010_app_attest_keys.js +17 -0
- package/dist/migrations/set.d.ts +3 -1
- package/dist/migrations/set.d.ts.map +1 -1
- package/dist/migrations/set.js +6 -2
- package/dist/seeds/devSession.js +1 -1
- package/dist/test-utils/appAttestDevice.d.ts +92 -0
- package/dist/test-utils/appAttestDevice.d.ts.map +1 -0
- package/dist/test-utils/appAttestDevice.js +282 -0
- package/dist/test-utils/appAttestSample.d.ts +22 -0
- package/dist/test-utils/appAttestSample.d.ts.map +1 -0
- package/dist/test-utils/appAttestSample.js +24 -0
- package/dist/test-utils/googlePlay.d.ts +47 -0
- package/dist/test-utils/googlePlay.d.ts.map +1 -0
- package/dist/test-utils/googlePlay.js +117 -0
- package/dist/test-utils/liveApp.d.ts +76 -36
- package/dist/test-utils/liveApp.d.ts.map +1 -1
- package/dist/test-utils/liveApp.js +16 -5
- package/dist/test-utils/rollbackThrough.d.ts +11 -0
- package/dist/test-utils/rollbackThrough.d.ts.map +1 -0
- package/dist/test-utils/rollbackThrough.js +16 -0
- package/dist/test-utils/scopedBackend.d.ts +25 -0
- package/dist/test-utils/scopedBackend.d.ts.map +1 -1
- package/dist/test-utils/scopedBackend.js +1 -1
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/docs/attestation.md +173 -0
- package/package.json +5 -5
- package/pithy.manifest.json +14 -0
- package/src/attestation/appAttest/assertion.ts +65 -0
- package/src/attestation/appAttest/attestation.ts +159 -0
- package/src/attestation/appAttest/authData.ts +45 -0
- package/src/attestation/appAttest/base64.ts +29 -0
- package/src/attestation/appAttest/bytes.ts +18 -0
- package/src/attestation/appAttest/cbor.ts +116 -0
- package/src/attestation/appAttest/der.ts +126 -0
- package/src/attestation/appAttest/keys.ts +57 -0
- package/src/attestation/appAttest/root.ts +13 -0
- package/src/attestation/appAttest/verifier.ts +113 -0
- package/src/attestation/appAttest/x509.ts +209 -0
- package/src/attestation/challenge.ts +56 -0
- package/src/attestation/config.ts +118 -0
- package/src/attestation/errors.ts +60 -0
- package/src/attestation/gate.ts +105 -0
- package/src/attestation/playIntegrity/google.ts +239 -0
- package/src/attestation/playIntegrity/verdict.ts +149 -0
- package/src/attestation/playIntegrity/verifier.ts +77 -0
- package/src/attestation/verifier.ts +87 -0
- package/src/attestation/wire.ts +79 -0
- package/src/capability.ts +64 -8
- package/src/client/api.ts +73 -40
- package/src/client/attestation.ts +404 -0
- package/src/client/projection.ts +29 -4
- package/src/data/appAttestKey.ts +45 -0
- package/src/data/attestationChallenge.ts +23 -0
- package/src/data/tables.ts +5 -1
- package/src/http/responses.ts +22 -0
- package/src/http/routes.ts +128 -38
- package/src/http/schemas.ts +24 -0
- package/src/instance/secrets.ts +88 -9
- package/src/migrations/0009_attestation_challenges.ts +28 -0
- package/src/migrations/0010_app_attest_keys.ts +27 -0
- package/src/migrations/set.ts +5 -1
- package/src/test-utils/appAttestDevice.ts +399 -0
- package/src/test-utils/appAttestSample.ts +20 -0
- package/src/test-utils/googlePlay.ts +144 -0
- package/src/test-utils/liveApp.ts +31 -5
- package/src/test-utils/rollbackThrough.ts +24 -0
- package/src/version.generated.ts +1 -1
package/src/client/api.ts
CHANGED
|
@@ -6,13 +6,9 @@
|
|
|
6
6
|
* same-origin, cookie-bearing request to this Worker, and the calls made through it — the six a
|
|
7
7
|
* scaffolded screen makes, and {@link updateUser}, which an adopter's own screen calls.
|
|
8
8
|
*
|
|
9
|
-
* **
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* files is frozen there at the moment `pithy init` ran, and a later fix to the base path, the cookie
|
|
13
|
-
* mode or the failure directions reaches none of them. Six such requests existed — `/get-session`,
|
|
14
|
-
* `/sign-out`, the two OTP routes, the magic link, and the social handoff — each spelling out the same
|
|
15
|
-
* transport by hand. They call this now, and this upgrades with a minor release (#370).
|
|
9
|
+
* **It lives in the package because the screens do not.** `pithy ui add` copies the auth screens into the
|
|
10
|
+
* adopter's repository once, and a request written out in one of them could never be fixed by a release. The
|
|
11
|
+
* screens call this, and this upgrades with a minor release.
|
|
16
12
|
*
|
|
17
13
|
* **Two transports, chosen by one option.** With no `origin`, every request is a rooted path on the
|
|
18
14
|
* page's own origin carrying `credentials: "include"` and nothing else: no token in `localStorage` or
|
|
@@ -104,6 +100,22 @@ export type AuthGate = (body: Record<string, unknown>) => {
|
|
|
104
100
|
headers: Record<string, string>;
|
|
105
101
|
};
|
|
106
102
|
|
|
103
|
+
/**
|
|
104
|
+
* A native app's attestation, for the two send routes: the headers that carry one over exactly `body`.
|
|
105
|
+
*
|
|
106
|
+
* `./attestation` builds it from a challenge and the platform's assertion. When a call carries one, the send is
|
|
107
|
+
* judged by it alone and the humanity check is not applied.
|
|
108
|
+
*/
|
|
109
|
+
export interface AuthAttestation {
|
|
110
|
+
/** The headers to send with `body`, or the failure that stopped the attestation. */
|
|
111
|
+
attest(body: string): Promise<AuthResult<Record<string, string>>>;
|
|
112
|
+
/**
|
|
113
|
+
* The Worker refused the attestation in `headers`, the very object {@link AuthAttestation.attest} answered for the
|
|
114
|
+
* refused send, with `auth/attestation_failed`, so a key it named is not used again.
|
|
115
|
+
*/
|
|
116
|
+
refused?(failure: AuthFailure, headers: Readonly<Record<string, string>>): void | Promise<void>;
|
|
117
|
+
}
|
|
118
|
+
|
|
107
119
|
/** What every call takes: where the routes are, and the seams a test replaces. */
|
|
108
120
|
export interface AuthClientOptions {
|
|
109
121
|
/**
|
|
@@ -121,6 +133,8 @@ export interface AuthClientOptions {
|
|
|
121
133
|
global?: AuthGlobal;
|
|
122
134
|
/** The humanity check to satisfy, on the two routes that are gated by one. */
|
|
123
135
|
gate?: AuthGate;
|
|
136
|
+
/** A native app's attestation, sent on those two routes in place of the humanity check. */
|
|
137
|
+
attestation?: AuthAttestation;
|
|
124
138
|
}
|
|
125
139
|
|
|
126
140
|
/** A refusal a screen can render: the namespaced code, the public message, and what to do next. */
|
|
@@ -164,6 +178,16 @@ export const AUTH_CROSS_ORIGIN: AuthFailure = {
|
|
|
164
178
|
"Check the auth `basePath` in pithy.config.ts, which is a path on the worker and not a URL, and the client's `origin`, which is a scheme and host with no path.",
|
|
165
179
|
};
|
|
166
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Whether a refusal is the send gate's: Turnstile's `turnstile/*` or attestation's `auth/attestation_*`.
|
|
183
|
+
*
|
|
184
|
+
* The gate refuses before the address is looked at, so a screen may name it without telling a registered address
|
|
185
|
+
* from another. Every other refusal of a send is answered the same way whatever the address.
|
|
186
|
+
*/
|
|
187
|
+
export function isGateRefusal(failure: AuthFailure): boolean {
|
|
188
|
+
return failure.code.startsWith("turnstile/") || failure.code.startsWith("auth/attestation_");
|
|
189
|
+
}
|
|
190
|
+
|
|
167
191
|
/** Whether a value is a plain record — the first step of every guard below. */
|
|
168
192
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
169
193
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -223,14 +247,10 @@ export function withoutCookies(init: AuthRequestInit | undefined): AuthRequestIn
|
|
|
223
247
|
* route Better Auth owns — the one-time code, the magic link, sign-out, `get-session`, the social
|
|
224
248
|
* handoff, `update-user` — answers its own flat `{ message, code }`, because better-call renders an
|
|
225
249
|
* endpoint's `APIError` into a Response inside `instance.handler` and nothing on our side of that ever
|
|
226
|
-
* sees it
|
|
227
|
-
*
|
|
228
|
-
* Reading only the envelope meant every one of those refusals arrived as {@link AUTH_UNREADABLE}, so a
|
|
229
|
-
* reader who mistyped their one-time code was told the app was broken rather than that the code was
|
|
230
|
-
* wrong — in a screen `pithy ui add` copies into an adopter's repository, where it can never be fixed.
|
|
250
|
+
* sees it. Both are read, so a mistyped one-time code reads as a wrong code rather than as a broken app.
|
|
231
251
|
*
|
|
232
|
-
* **The flat shape is read rather than rewritten on the server
|
|
233
|
-
*
|
|
252
|
+
* **The flat shape is read rather than rewritten on the server.** `packages/auth/README.md` documents
|
|
253
|
+
* `createAuthClient` from `better-auth/client` as a first-class client surface, and
|
|
234
254
|
* `@better-fetch/fetch` builds its error as `{ ...parsedBody, status }` — so an adopter on that path
|
|
235
255
|
* reads `error.code === "INVALID_OTP"` today. Re-homing the body into our envelope would make that
|
|
236
256
|
* `undefined` for every one of them. The wire is Better Auth's contract; this side learns to read it.
|
|
@@ -263,11 +283,8 @@ function readFailure(body: unknown): AuthFailure {
|
|
|
263
283
|
* renders. A body that will not parse is the generic failure rather than a crash — a corporate proxy's
|
|
264
284
|
* HTML page reaches a browser far more often than anyone expects.
|
|
265
285
|
*
|
|
266
|
-
* **Exported,
|
|
267
|
-
*
|
|
268
|
-
* adopter's repository, where Pithy can never fix them. Every browser program that asks this Worker an
|
|
269
|
-
* auth question calls this, and `sameOrigin.test.ts` fails the build on any module that grows its own
|
|
270
|
-
* (#370).
|
|
286
|
+
* **Exported, so no screen writes a transport of its own.** Every browser program that asks this Worker an
|
|
287
|
+
* auth question calls this, and `sameOrigin.test.ts` fails the build on any module that grows its own.
|
|
271
288
|
*
|
|
272
289
|
* **Zod-free, like everything on this side of the wire.** The answer is narrowed by the hand-written
|
|
273
290
|
* `guard` a caller passes, never by a schema: this compiles into an adopter's browser bundle, and
|
|
@@ -312,12 +329,7 @@ export async function callAuth<T>(
|
|
|
312
329
|
return guard(body) ? { ok: true, value: body } : { ok: false, failure: AUTH_UNREADABLE };
|
|
313
330
|
}
|
|
314
331
|
|
|
315
|
-
/**
|
|
316
|
-
* A JSON POST, with the humanity check's contribution already folded in.
|
|
317
|
-
*
|
|
318
|
-
* The gate is applied here rather than at each call site for the reason the cookie mode is: a screen
|
|
319
|
-
* that forgets it sends an ungated request to a gated route and gets a 403 it cannot explain.
|
|
320
|
-
*/
|
|
332
|
+
/** A JSON POST, with the humanity check's contribution folded in, so no call site can forget it. */
|
|
321
333
|
function jsonPost(body: Record<string, unknown>, options: AuthClientOptions | undefined): AuthRequestInit {
|
|
322
334
|
const gated = options?.gate ? options.gate(body) : { body, headers: {} };
|
|
323
335
|
return {
|
|
@@ -327,6 +339,31 @@ function jsonPost(body: Record<string, unknown>, options: AuthClientOptions | un
|
|
|
327
339
|
};
|
|
328
340
|
}
|
|
329
341
|
|
|
342
|
+
/**
|
|
343
|
+
* A send to a gated route: attested when `options.attestation` is set, and otherwise through {@link jsonPost}'s
|
|
344
|
+
* humanity check. A refused attestation is reported back to it, so a key the Worker refused is not used again.
|
|
345
|
+
*/
|
|
346
|
+
async function gatedSend(
|
|
347
|
+
path: string,
|
|
348
|
+
body: Record<string, unknown>,
|
|
349
|
+
options: AuthClientOptions | undefined,
|
|
350
|
+
): Promise<AuthResult<Record<string, unknown>>> {
|
|
351
|
+
const attestation = options?.attestation;
|
|
352
|
+
if (!attestation) return callAuth(path, jsonPost(body, options), options, isObject);
|
|
353
|
+
const serialized = JSON.stringify(body);
|
|
354
|
+
const attested = await attestation.attest(serialized);
|
|
355
|
+
if (!attested.ok) return attested;
|
|
356
|
+
const result = await callAuth(
|
|
357
|
+
path,
|
|
358
|
+
{ method: "POST", headers: { "content-type": "application/json", ...attested.value }, body: serialized },
|
|
359
|
+
options,
|
|
360
|
+
isObject,
|
|
361
|
+
);
|
|
362
|
+
if (!result.ok && result.failure.code === "auth/attestation_failed")
|
|
363
|
+
await attestation.refused?.(result.failure, attested.value);
|
|
364
|
+
return result;
|
|
365
|
+
}
|
|
366
|
+
|
|
330
367
|
/** The signed-in user, as far as a browser is concerned. */
|
|
331
368
|
export interface AuthUser {
|
|
332
369
|
/** The user's id. */
|
|
@@ -374,12 +411,9 @@ export function getSession(options?: AuthClientOptions): Promise<AuthResult<Auth
|
|
|
374
411
|
/**
|
|
375
412
|
* End the session server-side.
|
|
376
413
|
*
|
|
377
|
-
* **The content type is
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
* which looks exactly like a successful sign-out until the back button restores a live session.
|
|
381
|
-
* `clientRoundTrip.test.ts` is what found it, and is what would find it again: a request written into a
|
|
382
|
-
* file Pithy may never rewrite is a request nobody ever tested against the route it names (#370).
|
|
414
|
+
* **The content type is required.** The route refuses a POST without `application/json` with a 415, and a
|
|
415
|
+
* screen that ignores the answer and navigates on would look signed out while the session lives.
|
|
416
|
+
* `clientRoundTrip.test.ts` holds this call against the real route.
|
|
383
417
|
*
|
|
384
418
|
* The body is an empty object rather than nothing, because a declared JSON content type with no body is
|
|
385
419
|
* the other half of the same 415.
|
|
@@ -432,7 +466,7 @@ export function signOut(options?: AuthClientOptions): Promise<AuthResult<Record<
|
|
|
432
466
|
* function.** Standing up a second Better Auth client with `inferAdditionalFields` duplicates
|
|
433
467
|
* configuration `@pithy-sh/auth` already owns. Reaching for {@link callAuth} instead means an adopter
|
|
434
468
|
* spelling out the path, the content type and the guard themselves, one deep import below the surface
|
|
435
|
-
* every other auth call in their app goes through
|
|
469
|
+
* every other auth call in their app goes through, in a file a release can never fix.
|
|
436
470
|
*
|
|
437
471
|
* **The answer carries no user, whatever the route's own OpenAPI block advertises.** Better Auth's
|
|
438
472
|
* `/update-user` ends `ctx.json({ status: true })`, so `value` is `{ status: true }` and
|
|
@@ -471,14 +505,14 @@ export function signInWithOtp(
|
|
|
471
505
|
/**
|
|
472
506
|
* Mail a one-time code.
|
|
473
507
|
*
|
|
474
|
-
* Gated
|
|
475
|
-
* reads it
|
|
508
|
+
* Gated: `options.attestation` attests over the request, and otherwise `options.gate` puts the humanity check's
|
|
509
|
+
* token where the middleware reads it. A gate's refusal comes back with its code; see {@link isGateRefusal}.
|
|
476
510
|
*/
|
|
477
511
|
export function sendOtp(
|
|
478
512
|
input: { email: string; type: "sign-in" },
|
|
479
513
|
options?: AuthClientOptions,
|
|
480
514
|
): Promise<AuthResult<Record<string, unknown>>> {
|
|
481
|
-
return
|
|
515
|
+
return gatedSend("/email-otp/send-verification-otp", { ...input }, options);
|
|
482
516
|
}
|
|
483
517
|
|
|
484
518
|
/**
|
|
@@ -491,7 +525,7 @@ export function sendMagicLink(
|
|
|
491
525
|
input: { email: string; callbackURL: string },
|
|
492
526
|
options?: AuthClientOptions,
|
|
493
527
|
): Promise<AuthResult<Record<string, unknown>>> {
|
|
494
|
-
return
|
|
528
|
+
return gatedSend("/sign-in/magic-link", { ...input }, options);
|
|
495
529
|
}
|
|
496
530
|
|
|
497
531
|
/**
|
|
@@ -542,10 +576,9 @@ function isAuthorization(value: unknown): value is { url: string } {
|
|
|
542
576
|
* refused callback to `${baseURL}${basePath}/error`, which in production redirects on to the adopter's
|
|
543
577
|
* home route carrying a query string no template reads — so the user lands on a page that says nothing
|
|
544
578
|
* about what just happened. Pass the sign-in screen's own path and the refusal returns to the control
|
|
545
|
-
* that started it, as `?error=<code
|
|
546
|
-
* (pithy-sh/pithy#554); without it the sentence exists and is unreachable.
|
|
579
|
+
* that started it, as `?error=<code>`, which is how a provider's refusal copy reaches the screen.
|
|
547
580
|
*
|
|
548
|
-
* **The value has a shape, and the Worker refuses one that does not fit
|
|
581
|
+
* **The value has a shape, and the Worker refuses one that does not fit.** An absolute URL on a
|
|
549
582
|
* trusted origin, or a root-relative path — with **no fragment** and no `error`/`error_description`
|
|
550
583
|
* parameter already on it. Better Auth appends `?error=<code>` to this string without parsing it, so a
|
|
551
584
|
* `#` anywhere in it swallows the code the refusal screen is waiting for, and an `error` already there
|
|
@@ -559,7 +592,7 @@ export async function startSocialSignIn(
|
|
|
559
592
|
): Promise<SocialSignIn> {
|
|
560
593
|
// The gate is dropped rather than passed through, so a screen holding one client options object for
|
|
561
594
|
// the whole page cannot accidentally gate this route.
|
|
562
|
-
const ungated: AuthClientOptions | undefined = options && { ...options, gate: undefined };
|
|
595
|
+
const ungated: AuthClientOptions | undefined = options && { ...options, gate: undefined, attestation: undefined };
|
|
563
596
|
const result = await callAuth("/sign-in/social", jsonPost({ ...input }, undefined), ungated, isObject);
|
|
564
597
|
if (!result.ok) return { kind: "refused", failure: result.failure };
|
|
565
598
|
return isAuthorization(result.value) ? { kind: "authorize", url: result.value.url } : { kind: "unconfigured" };
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
AUTH_UNREADABLE,
|
|
6
|
+
type AuthAttestation,
|
|
7
|
+
type AuthClientOptions,
|
|
8
|
+
type AuthFailure,
|
|
9
|
+
type AuthResult,
|
|
10
|
+
callAuth,
|
|
11
|
+
} from "./api";
|
|
12
|
+
import type { SecureStore } from "./bearer";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A native app's attestation on the two send routes: a challenge from the Worker, the platform's assertion over
|
|
16
|
+
* that challenge and the send's exact body, and the one header that carries both.
|
|
17
|
+
*
|
|
18
|
+
* **The platform is an injected adapter.** {@link appAttestAttester} and {@link playIntegrityAttester} take
|
|
19
|
+
* `@expo/app-integrity`'s functions as a structural argument, so this package never depends on it: the scaffolded
|
|
20
|
+
* React Native app installs it and hands it in, and a web-only or server-only project installs nothing for this.
|
|
21
|
+
* {@link testAttester} sends the documented test assertion, for simulators, emulators and CI.
|
|
22
|
+
*
|
|
23
|
+
* Nothing here throws. A platform that will not attest, and a Worker that will not issue a challenge, are each an
|
|
24
|
+
* {@link AuthFailure}.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** The request header the attestation travels in. The Worker's own name for it is in `../attestation/wire`. */
|
|
28
|
+
export const ATTESTATION_HEADER = "x-pithy-attestation";
|
|
29
|
+
|
|
30
|
+
/** The assertion a `test` environment accepts in place of a real one. */
|
|
31
|
+
export const ATTESTATION_TEST_ASSERTION = "pithy-test-attestation";
|
|
32
|
+
|
|
33
|
+
/** Where an App Attest key id is kept between launches, by default. */
|
|
34
|
+
export const APP_ATTEST_KEY_STORAGE_KEY = "pithy.attestation.appAttestKey";
|
|
35
|
+
|
|
36
|
+
/** The platform would not attest: no App Attest on this device, Play services missing, or a key it refused. */
|
|
37
|
+
export const ATTESTATION_UNAVAILABLE: AuthFailure = {
|
|
38
|
+
code: "client/attestation_unavailable",
|
|
39
|
+
message: "This device couldn't prove it is running the app.",
|
|
40
|
+
action: "Try again. If it keeps happening, update the app or sign in on another device.",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** What an attester is asked to attest over. */
|
|
44
|
+
export interface AttestationRequest {
|
|
45
|
+
/** The challenge the Worker issued. */
|
|
46
|
+
readonly challenge: string;
|
|
47
|
+
/** The send's body, exactly as it goes on the wire. */
|
|
48
|
+
readonly body: string;
|
|
49
|
+
/** SHA-256 of the challenge's UTF-8 followed by the body's: what the Worker checks the assertion against. */
|
|
50
|
+
readonly binding: Uint8Array;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** What an attester produced. */
|
|
54
|
+
export interface AttestationProof {
|
|
55
|
+
/** The App Attest assertion, the Play Integrity token, or the test assertion. */
|
|
56
|
+
readonly assertion: string;
|
|
57
|
+
/** The App Attest key id, on iOS. */
|
|
58
|
+
readonly keyId?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** What an attester may ask of the Worker besides the send it attests: the calls a key's registration makes. */
|
|
62
|
+
export interface AttestationTransport {
|
|
63
|
+
/** A fresh single-use challenge from `POST {basePath}/attestation/challenge`. */
|
|
64
|
+
challenge(): Promise<AuthResult<string>>;
|
|
65
|
+
/** Register an App Attest key with Apple's attestation of it, at `POST {basePath}/attestation/ios/keys`. */
|
|
66
|
+
registerAppAttestKey(input: { challenge: string; keyId: string; attestation: string }): Promise<AuthResult<void>>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The Worker refused a call an attester made through its {@link AttestationTransport}. Thrown by an attester and
|
|
71
|
+
* answered by the send as {@link AttestationRefusedError.failure}, so a refused registration reaches the screen with
|
|
72
|
+
* the Worker's code rather than as {@link ATTESTATION_UNAVAILABLE}.
|
|
73
|
+
*/
|
|
74
|
+
export class AttestationRefusedError extends Error {
|
|
75
|
+
readonly failure: AuthFailure;
|
|
76
|
+
constructor(failure: AuthFailure) {
|
|
77
|
+
super(failure.message);
|
|
78
|
+
this.name = "AttestationRefusedError";
|
|
79
|
+
this.failure = failure;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** One platform's attestation, injected. */
|
|
84
|
+
export interface Attester {
|
|
85
|
+
/** The platform the Worker judges the assertion as. */
|
|
86
|
+
readonly platform: "ios" | "android";
|
|
87
|
+
/** Whether this device can attest at all. Absent means it can. A screen offers Turnstile where it cannot. */
|
|
88
|
+
readonly available?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* The assertion over `request`. `transport` reaches the Worker for anything the assertion needs first, such as
|
|
91
|
+
* registering a key. May throw: the caller answers an {@link AttestationRefusedError} with its failure and any
|
|
92
|
+
* other throw with {@link ATTESTATION_UNAVAILABLE}.
|
|
93
|
+
*/
|
|
94
|
+
attest(request: AttestationRequest, transport: AttestationTransport): Promise<AttestationProof>;
|
|
95
|
+
/**
|
|
96
|
+
* The Worker refused `proof`, which this produced for one send. An attester drops the key that proof used, and
|
|
97
|
+
* only that key, so the next send starts over without losing a newer key another send registered.
|
|
98
|
+
*/
|
|
99
|
+
refused?(proof: AttestationProof): Promise<void>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The test assertion from `platform`. A `test` environment accepts it; nothing else does. */
|
|
103
|
+
export function testAttester(platform: "ios" | "android"): Attester {
|
|
104
|
+
return { platform, attest: async () => ({ assertion: ATTESTATION_TEST_ASSERTION }) };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The App Attest half of `@expo/app-integrity`, as this module calls it. */
|
|
108
|
+
export interface AppAttestModule {
|
|
109
|
+
/** Whether this device provides App Attest. */
|
|
110
|
+
readonly isSupported: boolean;
|
|
111
|
+
/** A new key in the Secure Enclave, by its id. */
|
|
112
|
+
generateKeyAsync(): Promise<string>;
|
|
113
|
+
/** Apple's attestation of the key, over SHA-256 of `challenge`'s UTF-8. */
|
|
114
|
+
attestKeyAsync(keyId: string, challenge: string): Promise<string>;
|
|
115
|
+
/** An assertion by the key, over SHA-256 of `challenge`'s UTF-8. */
|
|
116
|
+
generateAssertionAsync(keyId: string, challenge: string): Promise<string>;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A stored key id read back, or `null` when the value is not one this module wrote. */
|
|
120
|
+
function readKeyId(raw: string | null): string | null {
|
|
121
|
+
if (raw === null) return null;
|
|
122
|
+
try {
|
|
123
|
+
const parsed: unknown = JSON.parse(raw);
|
|
124
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
125
|
+
const { keyId } = parsed as Record<string, unknown>;
|
|
126
|
+
return typeof keyId === "string" && keyId !== "" ? keyId : null;
|
|
127
|
+
} catch {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Apple App Attest.
|
|
134
|
+
*
|
|
135
|
+
* **A key is registered once, then asserts.** With no registered key, a send generates one, asks the Worker for a
|
|
136
|
+
* challenge, has Apple attest the key over it, and posts the attestation object to `/attestation/ios/keys`. The key
|
|
137
|
+
* is kept in `store`, the Keychain on a phone, only when that route answers success; a key it refused, or that never
|
|
138
|
+
* reached it, is never asserted with, and the next send registers another. Then that send, and every later one,
|
|
139
|
+
* asserts with the key over its own challenge and exact body.
|
|
140
|
+
*
|
|
141
|
+
* **A key that stops working is replaced.** A key the device can no longer assert with, which is what a Keychain
|
|
142
|
+
* entry outliving a reinstall or a restore leaves, is dropped and a new one registered on the same send, unless
|
|
143
|
+
* another send has registered one meanwhile, which this send then asserts with. A send the Worker refuses with
|
|
144
|
+
* `auth/attestation_failed` drops the key that send used, through {@link Attester.refused}, so the next send
|
|
145
|
+
* registers a new one; a refusal arriving after a newer key replaced it leaves the newer key alone.
|
|
146
|
+
*
|
|
147
|
+
* **Sends share their work.** The stored key id is read once per launch however many sends start together, and
|
|
148
|
+
* one registration at a time serves every send waiting on a key.
|
|
149
|
+
*
|
|
150
|
+
* **What the module is handed is what the Worker hashes.** `@expo/app-integrity` hashes the string it is handed into
|
|
151
|
+
* `clientDataHash`. Registration hands it the challenge alone, whose SHA-256 is the binding over the challenge and an
|
|
152
|
+
* empty body; an assertion hands it the challenge followed by the send's body, whose SHA-256 is the send's binding.
|
|
153
|
+
*/
|
|
154
|
+
export function appAttestAttester(input: {
|
|
155
|
+
module: AppAttestModule;
|
|
156
|
+
store: SecureStore;
|
|
157
|
+
storageKey?: string;
|
|
158
|
+
}): Attester {
|
|
159
|
+
const { module, store } = input;
|
|
160
|
+
const key = input.storageKey ?? APP_ATTEST_KEY_STORAGE_KEY;
|
|
161
|
+
let held: string | null | undefined;
|
|
162
|
+
let loading: Promise<string | null> | null = null;
|
|
163
|
+
let registering: Promise<string> | null = null;
|
|
164
|
+
|
|
165
|
+
/** The key id in hand, read from `store` once. A key kept or forgotten while the read ran wins over it. */
|
|
166
|
+
function load(): Promise<string | null> {
|
|
167
|
+
if (held !== undefined) return Promise.resolve(held);
|
|
168
|
+
loading ??= (async () => {
|
|
169
|
+
let read: string | null;
|
|
170
|
+
try {
|
|
171
|
+
read = readKeyId(await store.getItem(key));
|
|
172
|
+
} catch {
|
|
173
|
+
read = null;
|
|
174
|
+
}
|
|
175
|
+
if (held === undefined) held = read;
|
|
176
|
+
return held;
|
|
177
|
+
})();
|
|
178
|
+
return loading;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function keep(keyId: string): Promise<void> {
|
|
182
|
+
held = keyId;
|
|
183
|
+
try {
|
|
184
|
+
await store.setItem(key, JSON.stringify({ keyId }));
|
|
185
|
+
} catch {
|
|
186
|
+
// Kept for this launch. The next launch registers a new key.
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function forget(): Promise<void> {
|
|
191
|
+
held = null;
|
|
192
|
+
try {
|
|
193
|
+
await store.deleteItem(key);
|
|
194
|
+
} catch {
|
|
195
|
+
// Nothing left to try. A key the store kept is refused again, and dropped again.
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Generate a key, have Apple attest it over a fresh challenge, and register it. Kept only once registered. */
|
|
200
|
+
async function register(transport: AttestationTransport): Promise<string> {
|
|
201
|
+
const keyId = await module.generateKeyAsync();
|
|
202
|
+
const issued = await transport.challenge();
|
|
203
|
+
if (!issued.ok) throw new AttestationRefusedError(issued.failure);
|
|
204
|
+
const attestation = await module.attestKeyAsync(keyId, issued.value);
|
|
205
|
+
const registered = await transport.registerAppAttestKey({ challenge: issued.value, keyId, attestation });
|
|
206
|
+
if (!registered.ok) throw new AttestationRefusedError(registered.failure);
|
|
207
|
+
await keep(keyId);
|
|
208
|
+
return keyId;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** One registration at a time, so two sends at once register one key between them. */
|
|
212
|
+
function registerOnce(transport: AttestationTransport): Promise<string> {
|
|
213
|
+
registering ??= register(transport).finally(() => {
|
|
214
|
+
registering = null;
|
|
215
|
+
});
|
|
216
|
+
return registering;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
platform: "ios",
|
|
221
|
+
available: module.isSupported,
|
|
222
|
+
async attest(request, transport) {
|
|
223
|
+
const message = `${request.challenge}${request.body}`;
|
|
224
|
+
const tried = new Set<string>();
|
|
225
|
+
let current = await load();
|
|
226
|
+
while (current !== null && !tried.has(current)) {
|
|
227
|
+
tried.add(current);
|
|
228
|
+
try {
|
|
229
|
+
return { assertion: await module.generateAssertionAsync(current, message), keyId: current };
|
|
230
|
+
} catch {
|
|
231
|
+
// The Keychain outlives a reinstall, a restore and a migration; the key itself does not. Drop it, unless
|
|
232
|
+
// another send has already replaced it, then assert with whatever key is held now.
|
|
233
|
+
if (held === current) await forget();
|
|
234
|
+
}
|
|
235
|
+
current = held ?? null;
|
|
236
|
+
}
|
|
237
|
+
const keyId = await registerOnce(transport);
|
|
238
|
+
return { assertion: await module.generateAssertionAsync(keyId, message), keyId };
|
|
239
|
+
},
|
|
240
|
+
async refused(proof) {
|
|
241
|
+
if (proof.keyId !== undefined && proof.keyId === held) await forget();
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** The Play Integrity half of `@expo/app-integrity`, as this module calls it. */
|
|
247
|
+
export interface PlayIntegrityModule {
|
|
248
|
+
/** Warm up the token provider for a Google Cloud project, by its number. */
|
|
249
|
+
prepareIntegrityTokenProviderAsync(cloudProjectNumber: string): Promise<void>;
|
|
250
|
+
/** A Play Integrity token bound to `requestHash`. */
|
|
251
|
+
requestIntegrityCheckAsync(requestHash: string): Promise<string>;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Google Play Integrity, standard requests. The provider is prepared for the Google Cloud project the app is linked
|
|
256
|
+
* to in Play Console, and prepared again after any failure, since the module does not prepare an invalid provider
|
|
257
|
+
* again by itself; each send asks for a token whose `requestHash` is the binding's base64url. With no project
|
|
258
|
+
* number there is nothing to prepare, and {@link Attester.available} is `false`.
|
|
259
|
+
*/
|
|
260
|
+
export function playIntegrityAttester(input: {
|
|
261
|
+
module: PlayIntegrityModule;
|
|
262
|
+
cloudProjectNumber: string | null;
|
|
263
|
+
}): Attester {
|
|
264
|
+
const { module, cloudProjectNumber } = input;
|
|
265
|
+
let prepared: Promise<void> | null = null;
|
|
266
|
+
const prepare = (): Promise<void> => {
|
|
267
|
+
prepared ??= module.prepareIntegrityTokenProviderAsync(cloudProjectNumber ?? "").catch((failure: unknown) => {
|
|
268
|
+
prepared = null;
|
|
269
|
+
throw failure;
|
|
270
|
+
});
|
|
271
|
+
return prepared;
|
|
272
|
+
};
|
|
273
|
+
return {
|
|
274
|
+
platform: "android",
|
|
275
|
+
available: cloudProjectNumber !== null && cloudProjectNumber !== "",
|
|
276
|
+
async attest(request) {
|
|
277
|
+
await prepare();
|
|
278
|
+
try {
|
|
279
|
+
return { assertion: await module.requestIntegrityCheckAsync(base64Url(request.binding)) };
|
|
280
|
+
} catch (failure) {
|
|
281
|
+
prepared = null;
|
|
282
|
+
throw failure;
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Unpadded base64url, the encoding the attestation header and Play Integrity's `requestHash` take. */
|
|
289
|
+
export function base64Url(bytes: Uint8Array): string {
|
|
290
|
+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
291
|
+
let out = "";
|
|
292
|
+
for (let at = 0; at < bytes.length; at += 3) {
|
|
293
|
+
const a = bytes[at] ?? 0;
|
|
294
|
+
const b = bytes[at + 1] ?? 0;
|
|
295
|
+
const c = bytes[at + 2] ?? 0;
|
|
296
|
+
const triple = (a << 16) | (b << 8) | c;
|
|
297
|
+
const chars = Math.min(4, Math.ceil(((bytes.length - at) * 8) / 6));
|
|
298
|
+
for (let index = 0; index < chars; index += 1) out += alphabet[(triple >> (18 - index * 6)) & 0x3f];
|
|
299
|
+
}
|
|
300
|
+
return out;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** SHA-256 over Web Crypto, where the platform has it. React Native passes `expo-crypto`'s instead. */
|
|
304
|
+
async function webDigest(bytes: Uint8Array): Promise<Uint8Array> {
|
|
305
|
+
return new Uint8Array(await crypto.subtle.digest("SHA-256", bytes as Uint8Array<ArrayBuffer>));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Whether a value is the challenge route's answer. */
|
|
309
|
+
function isChallenge(value: unknown): value is { challenge: string; expiresAt: string } {
|
|
310
|
+
if (typeof value !== "object" || value === null) return false;
|
|
311
|
+
const { challenge } = value as Record<string, unknown>;
|
|
312
|
+
return typeof challenge === "string" && challenge !== "";
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Whether a value is the key registration route's answer. */
|
|
316
|
+
function isRegistration(value: unknown): value is { keyId: string; registeredAt: string } {
|
|
317
|
+
if (typeof value !== "object" || value === null) return false;
|
|
318
|
+
const { keyId } = value as Record<string, unknown>;
|
|
319
|
+
return typeof keyId === "string" && keyId !== "";
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** The Worker calls an attester makes, over `options`. */
|
|
323
|
+
function transportOver(options: AuthClientOptions): AttestationTransport {
|
|
324
|
+
const post = (body: unknown) => ({
|
|
325
|
+
method: "POST",
|
|
326
|
+
headers: { "content-type": "application/json" },
|
|
327
|
+
body: JSON.stringify(body),
|
|
328
|
+
});
|
|
329
|
+
return {
|
|
330
|
+
async challenge() {
|
|
331
|
+
const issued = await callAuth("/attestation/challenge", post({}), options, isChallenge);
|
|
332
|
+
return issued.ok ? { ok: true, value: issued.value.challenge } : issued;
|
|
333
|
+
},
|
|
334
|
+
async registerAppAttestKey(input) {
|
|
335
|
+
const registered = await callAuth("/attestation/ios/keys", post(input), options, isRegistration);
|
|
336
|
+
if (!registered.ok) return registered;
|
|
337
|
+
// The route echoes the key id it registered. Any other answer is not a registration of this key.
|
|
338
|
+
return registered.value.keyId === input.keyId
|
|
339
|
+
? { ok: true, value: undefined }
|
|
340
|
+
: { ok: false, failure: AUTH_UNREADABLE };
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* The attestation a gated send takes as `options.attestation`: one challenge per send, the attester's assertion
|
|
347
|
+
* over it and the body, and the header that carries them. `options` reaches the challenge route the way every
|
|
348
|
+
* other auth call does, so on a phone it is the bearer session's.
|
|
349
|
+
*/
|
|
350
|
+
export function createAttestation(
|
|
351
|
+
input: AuthClientOptions & {
|
|
352
|
+
attester: Attester;
|
|
353
|
+
/** SHA-256. Web Crypto's by default. */
|
|
354
|
+
digest?: (bytes: Uint8Array) => Promise<Uint8Array>;
|
|
355
|
+
},
|
|
356
|
+
): AuthAttestation {
|
|
357
|
+
const { attester, digest: _digest, attestation: _attestation, gate: _gate, ...options } = input;
|
|
358
|
+
const digest = input.digest ?? webDigest;
|
|
359
|
+
const transport = transportOver(options);
|
|
360
|
+
// Each header object this answered, to the proof in it, so a refusal reaches the attester with the proof of the
|
|
361
|
+
// send the Worker refused rather than whatever proof is newest.
|
|
362
|
+
const proofs = new WeakMap<object, AttestationProof>();
|
|
363
|
+
return {
|
|
364
|
+
async attest(body): Promise<AuthResult<Record<string, string>>> {
|
|
365
|
+
const issued = await transport.challenge();
|
|
366
|
+
if (!issued.ok) return issued;
|
|
367
|
+
const challenge = issued.value;
|
|
368
|
+
const encoder = new TextEncoder();
|
|
369
|
+
const prefix = encoder.encode(challenge);
|
|
370
|
+
const bytes = encoder.encode(body);
|
|
371
|
+
const message = new Uint8Array(prefix.length + bytes.length);
|
|
372
|
+
message.set(prefix, 0);
|
|
373
|
+
message.set(bytes, prefix.length);
|
|
374
|
+
let proof: AttestationProof;
|
|
375
|
+
try {
|
|
376
|
+
proof = await attester.attest({ challenge, body, binding: await digest(message) }, transport);
|
|
377
|
+
} catch (failure) {
|
|
378
|
+
if (failure instanceof AttestationRefusedError) return { ok: false, failure: failure.failure };
|
|
379
|
+
return { ok: false, failure: ATTESTATION_UNAVAILABLE };
|
|
380
|
+
}
|
|
381
|
+
if (typeof proof.assertion !== "string" || proof.assertion === "") {
|
|
382
|
+
return { ok: false, failure: AUTH_UNREADABLE };
|
|
383
|
+
}
|
|
384
|
+
const presented = {
|
|
385
|
+
platform: attester.platform,
|
|
386
|
+
challenge,
|
|
387
|
+
assertion: proof.assertion,
|
|
388
|
+
...(proof.keyId === undefined ? {} : { keyId: proof.keyId }),
|
|
389
|
+
};
|
|
390
|
+
const headers = { [ATTESTATION_HEADER]: base64Url(encoder.encode(JSON.stringify(presented))) };
|
|
391
|
+
proofs.set(headers, proof);
|
|
392
|
+
return { ok: true, value: headers };
|
|
393
|
+
},
|
|
394
|
+
async refused(_failure, headers) {
|
|
395
|
+
const proof = proofs.get(headers);
|
|
396
|
+
if (proof === undefined) return;
|
|
397
|
+
try {
|
|
398
|
+
await attester.refused?.(proof);
|
|
399
|
+
} catch {
|
|
400
|
+
// The refusal is the caller's answer either way.
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
};
|
|
404
|
+
}
|