@pithy-sh/auth 0.15.3 → 0.17.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 +49 -6
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +42 -7
- 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 +19 -1
- package/dist/data/tables.d.ts.map +1 -1
- package/dist/data/tables.js +6 -2
- package/dist/data/userLifecycle.d.ts +1 -0
- package/dist/data/userLifecycle.d.ts.map +1 -1
- package/dist/data/userLifecycle.js +3 -2
- package/dist/hooks/runtime.d.ts +32 -0
- package/dist/hooks/runtime.d.ts.map +1 -0
- package/dist/hooks/runtime.js +25 -0
- package/dist/http/adminRoutes.d.ts.map +1 -1
- package/dist/http/adminRoutes.js +4 -2
- package/dist/http/devLoginRoute.js +3 -3
- package/dist/http/resolve.d.ts.map +1 -1
- package/dist/http/resolve.js +3 -2
- package/dist/http/responses.d.ts +15 -0
- package/dist/http/responses.d.ts.map +1 -1
- package/dist/http/responses.js +13 -2
- 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/lifecycle/erasure.d.ts.map +1 -1
- package/dist/lifecycle/erasure.js +2 -1
- package/dist/lifecycle/hooks.d.ts +18 -10
- package/dist/lifecycle/hooks.d.ts.map +1 -1
- package/dist/lifecycle/request.d.ts +2 -1
- package/dist/lifecycle/request.d.ts.map +1 -1
- package/dist/lifecycle/request.js +4 -1
- package/dist/lifecycle/transitions.d.ts +24 -16
- package/dist/lifecycle/transitions.d.ts.map +1 -1
- package/dist/lifecycle/transitions.js +27 -16
- 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/0011_retained_basis_key.d.ts +10 -0
- package/dist/migrations/0011_retained_basis_key.d.ts.map +1 -0
- package/dist/migrations/0011_retained_basis_key.js +18 -0
- package/dist/migrations/set.d.ts +4 -1
- package/dist/migrations/set.d.ts.map +1 -1
- package/dist/migrations/set.js +8 -2
- package/dist/purge/run.d.ts +11 -11
- package/dist/purge/run.d.ts.map +1 -1
- package/dist/purge/run.js +9 -8
- package/dist/purge/workflow.d.ts +7 -1
- package/dist/purge/workflow.d.ts.map +1 -1
- package/dist/purge/workflow.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/account-lifecycle.md +46 -10
- 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 +85 -16
- 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/data/userLifecycle.ts +9 -2
- package/src/hooks/runtime.ts +41 -0
- package/src/http/adminRoutes.ts +14 -7
- package/src/http/resolve.ts +8 -4
- package/src/http/responses.ts +29 -1
- package/src/http/routes.ts +128 -38
- package/src/http/schemas.ts +24 -0
- package/src/instance/secrets.ts +88 -9
- package/src/lifecycle/erasure.ts +2 -1
- package/src/lifecycle/hooks.ts +22 -10
- package/src/lifecycle/request.ts +4 -1
- package/src/lifecycle/transitions.ts +47 -28
- package/src/migrations/0009_attestation_challenges.ts +28 -0
- package/src/migrations/0010_app_attest_keys.ts +27 -0
- package/src/migrations/0011_retained_basis_key.ts +19 -0
- package/src/migrations/set.ts +7 -1
- package/src/purge/run.ts +29 -17
- package/src/purge/workflow.ts +7 -3
- 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
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
/** The platforms a native app attestation can come from. */
|
|
5
|
+
export declare const AttestationPlatform: z.ZodEnum<{
|
|
6
|
+
android: "android";
|
|
7
|
+
ios: "ios";
|
|
8
|
+
}>;
|
|
9
|
+
export type AttestationPlatform = z.output<typeof AttestationPlatform>;
|
|
10
|
+
/** How an environment treats attestations. */
|
|
11
|
+
export declare const AttestationMode: z.ZodEnum<{
|
|
12
|
+
enforce: "enforce";
|
|
13
|
+
test: "test";
|
|
14
|
+
}>;
|
|
15
|
+
export type AttestationMode = z.output<typeof AttestationMode>;
|
|
16
|
+
/** An iOS app allowed to attest, as App Attest names it. */
|
|
17
|
+
export declare const AppleAppIdentity: z.ZodObject<{
|
|
18
|
+
teamId: z.ZodString;
|
|
19
|
+
bundleId: z.ZodString;
|
|
20
|
+
}, z.core.$strip>;
|
|
21
|
+
export type AppleAppIdentity = z.output<typeof AppleAppIdentity>;
|
|
22
|
+
/** An Android app allowed to attest, as Play Integrity names it. */
|
|
23
|
+
export declare const AndroidAppIdentity: z.ZodObject<{
|
|
24
|
+
packageName: z.ZodString;
|
|
25
|
+
}, z.core.$strip>;
|
|
26
|
+
export type AndroidAppIdentity = z.output<typeof AndroidAppIdentity>;
|
|
27
|
+
/** One environment's attestation settings. */
|
|
28
|
+
export declare const AttestationEnvironment: z.ZodObject<{
|
|
29
|
+
mode: z.ZodDefault<z.ZodEnum<{
|
|
30
|
+
enforce: "enforce";
|
|
31
|
+
test: "test";
|
|
32
|
+
}>>;
|
|
33
|
+
ios: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
34
|
+
teamId: z.ZodString;
|
|
35
|
+
bundleId: z.ZodString;
|
|
36
|
+
}, z.core.$strip>>>;
|
|
37
|
+
android: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
38
|
+
packageName: z.ZodString;
|
|
39
|
+
}, z.core.$strip>>>;
|
|
40
|
+
}, z.core.$strip>;
|
|
41
|
+
export type AttestationEnvironment = z.output<typeof AttestationEnvironment>;
|
|
42
|
+
/** Auth's `attestation` block. */
|
|
43
|
+
export declare const AttestationConfig: z.ZodObject<{
|
|
44
|
+
challengeExpiresIn: z.ZodDefault<z.ZodNumber>;
|
|
45
|
+
environments: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
46
|
+
mode: z.ZodDefault<z.ZodEnum<{
|
|
47
|
+
enforce: "enforce";
|
|
48
|
+
test: "test";
|
|
49
|
+
}>>;
|
|
50
|
+
ios: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
51
|
+
teamId: z.ZodString;
|
|
52
|
+
bundleId: z.ZodString;
|
|
53
|
+
}, z.core.$strip>>>;
|
|
54
|
+
android: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
55
|
+
packageName: z.ZodString;
|
|
56
|
+
}, z.core.$strip>>>;
|
|
57
|
+
}, z.core.$strip>>>;
|
|
58
|
+
}, z.core.$strip>;
|
|
59
|
+
export type AttestationConfig = z.output<typeof AttestationConfig>;
|
|
60
|
+
/** Whether any environment has an attestation block. Without one, nothing attestation adds is mounted. */
|
|
61
|
+
export declare function attestationIsConfigured(config: AttestationConfig): boolean;
|
|
62
|
+
/** The block for the environment this Worker is stamped with, or `undefined` for none and for an unstamped Worker. */
|
|
63
|
+
export declare function attestationFor(config: AttestationConfig, environment: string | null): AttestationEnvironment | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* The environments a Worker accepts the test assertion in, when their block is set to `test`: dev, staging and a
|
|
66
|
+
* feature deployment. An allowlist, because the Worker cannot read `seed.productionEnvironments`: a name a project
|
|
67
|
+
* declares, such as `live`, could be production, so the Worker holds it to `enforce` whatever its block says.
|
|
68
|
+
*/
|
|
69
|
+
export declare const TEST_ASSERTION_ENVIRONMENTS: readonly ["dev", "staging", "feature"];
|
|
70
|
+
/** Whether a Worker stamped with `environment` may accept the test assertion. An unstamped Worker may not. */
|
|
71
|
+
export declare function testAssertionAllowedIn(environment: string | null): boolean;
|
|
72
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/attestation/config.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,4DAA4D;AAC5D,eAAO,MAAM,mBAAmB;;;EAE0E,CAAC;AAC3G,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEvE,8CAA8C;AAC9C,eAAO,MAAM,eAAe;;;EAIzB,CAAC;AACJ,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,eAAe,CAAC,CAAC;AAE/D,4DAA4D;AAC5D,eAAO,MAAM,gBAAgB;;;iBAYkD,CAAC;AAChF,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAEjE,oEAAoE;AACpE,eAAO,MAAM,kBAAkB;;iBAOmC,CAAC;AACnE,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAErE,8CAA8C;AAC9C,eAAO,MAAM,sBAAsB;;;;;;;;;;;;iBAiBhC,CAAC;AACJ,MAAM,MAAM,sBAAsB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAE7E,kCAAkC;AAClC,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;iBAqB3B,CAAC;AACJ,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAEnE,0GAA0G;AAC1G,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAE1E;AAED,sHAAsH;AACtH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,iBAAiB,EACzB,WAAW,EAAE,MAAM,GAAG,IAAI,GACzB,sBAAsB,GAAG,SAAS,CAGpC;AAED;;;;GAIG;AACH,eAAO,MAAM,2BAA2B,mBAAuB,SAAS,YAA+B,CAAC;AAExG,8GAA8G;AAC9G,wBAAgB,sBAAsB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAE1E"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { FEATURE_ENVIRONMENT, LOCAL_ENVIRONMENT, isValidEnvironment } from "@pithy-sh/core/src/naming/environment";
|
|
5
|
+
//#region src/attestation/config.ts
|
|
6
|
+
/** The platforms a native app attestation can come from. */
|
|
7
|
+
const AttestationPlatform = z.enum(["ios", "android"]).describe("Where an attestation comes from: `ios`, Apple App Attest; `android`, Google Play Integrity.");
|
|
8
|
+
/** How an environment treats attestations. */
|
|
9
|
+
const AttestationMode = z.enum(["enforce", "test"]).describe("`enforce` accepts only a real attestation, judged by its platform's verifier; a platform with no verifier in this release answers `auth/attestation_config`. `test` also accepts the documented test assertion, so simulators, emulators and CI get through. The Worker accepts it only where its `ENVIRONMENT` is `dev`, `staging` or `feature`, whatever this says, and `pithy deploy` refuses a production environment set to `test`.");
|
|
10
|
+
/** An iOS app allowed to attest, as App Attest names it. */
|
|
11
|
+
const AppleAppIdentity = z.object({
|
|
12
|
+
teamId: z.string().regex(/^[A-Z0-9]{10}$/, "An Apple Team ID is ten uppercase letters or digits.").describe("The Apple Developer Team ID that signs the app: ten uppercase letters or digits."),
|
|
13
|
+
bundleId: z.string().min(1).max(255).describe("The app's bundle identifier, such as `com.example.app`. With the Team ID it is the App ID.")
|
|
14
|
+
}).describe("An iOS app allowed to attest: its Team ID and bundle identifier.");
|
|
15
|
+
/** An Android app allowed to attest, as Play Integrity names it. */
|
|
16
|
+
const AndroidAppIdentity = z.object({ packageName: z.string().regex(/^[A-Za-z][A-Za-z0-9_]*(\.[A-Za-z][A-Za-z0-9_]*)+$/, "An Android package name is a dotted Java name.").describe("The app's package name as Google Play knows it, such as `com.example.app`.") }).describe("An Android app allowed to attest: its package name.");
|
|
17
|
+
/** One environment's attestation settings. */
|
|
18
|
+
const AttestationEnvironment = z.object({
|
|
19
|
+
mode: AttestationMode.default("enforce"),
|
|
20
|
+
ios: z.array(AppleAppIdentity).default([]).describe("The iOS apps allowed to attest in this environment. Empty refuses every iOS attestation."),
|
|
21
|
+
android: z.array(AndroidAppIdentity).default([]).describe("The Android apps allowed to attest in this environment. Empty refuses every Android attestation.")
|
|
22
|
+
}).refine((block) => block.mode === "test" || block.ios.length + block.android.length > 0, { message: "An enforce block must allow at least one app, or no attestation could ever pass it." }).describe("One environment's attestation: the apps allowed to attest and the mode. With a block, the send routes accept an attestation in place of a Turnstile token, and require one when turnstile is not composed.");
|
|
23
|
+
/** Auth's `attestation` block. */
|
|
24
|
+
const AttestationConfig = z.object({
|
|
25
|
+
challengeExpiresIn: z.number().int().positive().max(600).default(300).describe("How long a challenge from the challenge route stays usable, in seconds. Defaults to 5 minutes."),
|
|
26
|
+
environments: z.record(z.string().refine(isValidEnvironment, "Not an environment name.").describe("An environment name."), AttestationEnvironment).default({}).describe("The attestation block per environment, keyed by the name the Worker is stamped with: `dev`, `staging`, `prod`, `feature` or one you declared. An environment with no block gates its send routes as before.")
|
|
27
|
+
}).describe("Native app attestation on the email send routes, per environment: Apple App Attest and Google Play Integrity in place of a Turnstile token.");
|
|
28
|
+
/** Whether any environment has an attestation block. Without one, nothing attestation adds is mounted. */
|
|
29
|
+
function attestationIsConfigured(config) {
|
|
30
|
+
return Object.keys(config.environments).length > 0;
|
|
31
|
+
}
|
|
32
|
+
/** The block for the environment this Worker is stamped with, or `undefined` for none and for an unstamped Worker. */
|
|
33
|
+
function attestationFor(config, environment) {
|
|
34
|
+
if (environment === null || !Object.hasOwn(config.environments, environment)) return void 0;
|
|
35
|
+
return config.environments[environment];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The environments a Worker accepts the test assertion in, when their block is set to `test`: dev, staging and a
|
|
39
|
+
* feature deployment. An allowlist, because the Worker cannot read `seed.productionEnvironments`: a name a project
|
|
40
|
+
* declares, such as `live`, could be production, so the Worker holds it to `enforce` whatever its block says.
|
|
41
|
+
*/
|
|
42
|
+
const TEST_ASSERTION_ENVIRONMENTS = [
|
|
43
|
+
LOCAL_ENVIRONMENT,
|
|
44
|
+
"staging",
|
|
45
|
+
FEATURE_ENVIRONMENT
|
|
46
|
+
];
|
|
47
|
+
/** Whether a Worker stamped with `environment` may accept the test assertion. An unstamped Worker may not. */
|
|
48
|
+
function testAssertionAllowedIn(environment) {
|
|
49
|
+
return environment !== null && TEST_ASSERTION_ENVIRONMENTS.includes(environment);
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { AndroidAppIdentity, AppleAppIdentity, AttestationConfig, AttestationEnvironment, AttestationMode, AttestationPlatform, TEST_ASSERTION_ENVIRONMENTS, attestationFor, attestationIsConfigured, testAssertionAllowedIn };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
4
|
+
import type { MessageParams } from "@pithy-sh/core/src/i18n/catalog";
|
|
5
|
+
import type { UnmarkedString } from "@pithy-sh/core/src/privacy/types";
|
|
6
|
+
/** What each refusal takes. `detail` says why, for the operator's log; it never reaches the client. */
|
|
7
|
+
interface AttestationErrorArgs {
|
|
8
|
+
/** Override the public, safe-to-expose message. */
|
|
9
|
+
message?: UnmarkedString;
|
|
10
|
+
/** The operator's remedy (CLI action line). */
|
|
11
|
+
action?: UnmarkedString;
|
|
12
|
+
/** Why it was refused, for logs. Never serialized to clients. */
|
|
13
|
+
detail?: UnmarkedString;
|
|
14
|
+
/** Values a translating client interpolates into its own wording for this code. */
|
|
15
|
+
params?: MessageParams;
|
|
16
|
+
}
|
|
17
|
+
/** `auth/attestation_missing` (400): attestation is this route's only gate, and the request carried none. */
|
|
18
|
+
export declare class AttestationMissingError extends PithyError {
|
|
19
|
+
constructor(args: AttestationErrorArgs);
|
|
20
|
+
}
|
|
21
|
+
/** `auth/attestation_failed` (403): the attestation did not verify. Never falls through to Turnstile. */
|
|
22
|
+
export declare class AttestationFailedError extends PithyError {
|
|
23
|
+
constructor(args: AttestationErrorArgs);
|
|
24
|
+
}
|
|
25
|
+
/** `auth/attestation_config` (500): the server's attestation setup cannot judge this request. */
|
|
26
|
+
export declare class AttestationConfigError extends PithyError {
|
|
27
|
+
constructor(args: AttestationErrorArgs);
|
|
28
|
+
}
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/attestation/errors.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,qCAAqC,CAAC;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAC;AAEvE,uGAAuG;AACvG,UAAU,oBAAoB;IAC5B,mDAAmD;IACnD,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,+CAA+C;IAC/C,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,iEAAiE;IACjE,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,mFAAmF;IACnF,MAAM,CAAC,EAAE,aAAa,CAAC;CACxB;AAED,6GAA6G;AAC7G,qBAAa,uBAAwB,SAAQ,UAAU;IACrD,YAAY,IAAI,EAAE,oBAAoB,EASrC;CACF;AAED,yGAAyG;AACzG,qBAAa,sBAAuB,SAAQ,UAAU;IACpD,YAAY,IAAI,EAAE,oBAAoB,EASrC;CACF;AAED,iGAAiG;AACjG,qBAAa,sBAAuB,SAAQ,UAAU;IACpD,YAAY,IAAI,EAAE,oBAAoB,EASrC;CACF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
4
|
+
//#region src/attestation/errors.ts
|
|
5
|
+
/** `auth/attestation_missing` (400): attestation is this route's only gate, and the request carried none. */
|
|
6
|
+
var AttestationMissingError = class extends PithyError {
|
|
7
|
+
constructor(args) {
|
|
8
|
+
super({
|
|
9
|
+
code: "auth/attestation_missing",
|
|
10
|
+
status: 400,
|
|
11
|
+
message: args.message ?? "This app must attest before it can sign in.",
|
|
12
|
+
action: args.action,
|
|
13
|
+
detail: args.detail,
|
|
14
|
+
params: args.params
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
/** `auth/attestation_failed` (403): the attestation did not verify. Never falls through to Turnstile. */
|
|
19
|
+
var AttestationFailedError = class extends PithyError {
|
|
20
|
+
constructor(args) {
|
|
21
|
+
super({
|
|
22
|
+
code: "auth/attestation_failed",
|
|
23
|
+
status: 403,
|
|
24
|
+
message: args.message ?? "The app's attestation did not verify. Ask for a new challenge and try again.",
|
|
25
|
+
action: args.action,
|
|
26
|
+
detail: args.detail,
|
|
27
|
+
params: args.params
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
/** `auth/attestation_config` (500): the server's attestation setup cannot judge this request. */
|
|
32
|
+
var AttestationConfigError = class extends PithyError {
|
|
33
|
+
constructor(args) {
|
|
34
|
+
super({
|
|
35
|
+
code: "auth/attestation_config",
|
|
36
|
+
status: 500,
|
|
37
|
+
message: args.message ?? "App attestation is not configured.",
|
|
38
|
+
action: args.action,
|
|
39
|
+
detail: args.detail,
|
|
40
|
+
params: args.params
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
//#endregion
|
|
45
|
+
export { AttestationConfigError, AttestationFailedError, AttestationMissingError };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { MiddlewareHandler } from "hono";
|
|
4
|
+
import { type AttestationConfig, type AttestationEnvironment } from "./config";
|
|
5
|
+
import { type AttestationVerifiers } from "./verifier";
|
|
6
|
+
/** What the send-route gate closes over. */
|
|
7
|
+
export interface SendGateOptions {
|
|
8
|
+
/** Auth's resolved `attestation` block. */
|
|
9
|
+
attestation: AttestationConfig;
|
|
10
|
+
/** The D1 binding auth's tables live in. */
|
|
11
|
+
database: string;
|
|
12
|
+
/** The verifier for each platform's real assertions. */
|
|
13
|
+
verifiers: AttestationVerifiers;
|
|
14
|
+
/** The Turnstile guard a request with no attestation goes to, when turnstile is composed. */
|
|
15
|
+
turnstile: MiddlewareHandler | undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The gate on auth's send routes once any environment has an attestation block.
|
|
19
|
+
*
|
|
20
|
+
* Either passes. A request carrying {@link ATTESTATION_HEADER} is judged by attestation alone, and a failure
|
|
21
|
+
* there is final. A request without one goes to Turnstile when it is composed; with no Turnstile, it is refused
|
|
22
|
+
* where this environment has a block, and passes where it has none, as it did before attestation existed.
|
|
23
|
+
*/
|
|
24
|
+
export declare function sendGate(options: SendGateOptions): MiddlewareHandler;
|
|
25
|
+
/** The challenge route's refusal where this environment has no block. Exported for the route. */
|
|
26
|
+
export declare function assertAttestationBlock(config: AttestationConfig, environment: string | null): AttestationEnvironment;
|
|
27
|
+
//# sourceMappingURL=gate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gate.d.ts","sourceRoot":"","sources":["../../src/attestation/gate.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAW,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAIvD,OAAO,EAAE,KAAK,iBAAiB,EAAE,KAAK,sBAAsB,EAAkB,MAAM,UAAU,CAAC;AAE/F,OAAO,EAAE,KAAK,oBAAoB,EAAgC,MAAM,YAAY,CAAC;AAGrF,4CAA4C;AAC5C,MAAM,WAAW,eAAe;IAC9B,2CAA2C;IAC3C,WAAW,EAAE,iBAAiB,CAAC;IAC/B,4CAA4C;IAC5C,QAAQ,EAAE,MAAM,CAAC;IACjB,wDAAwD;IACxD,SAAS,EAAE,oBAAoB,CAAC;IAChC,6FAA6F;IAC7F,SAAS,EAAE,iBAAiB,GAAG,SAAS,CAAC;CAC1C;AAiDD;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,iBAAiB,CAkBpE;AAED,iGAAiG;AACjG,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,sBAAsB,CAIpH"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { attestationFor } from "./config.js";
|
|
4
|
+
import { consumeChallenge } from "./challenge.js";
|
|
5
|
+
import { ATTESTATION_HEADER, decodeAttestation, requestBinding } from "./wire.js";
|
|
6
|
+
import { AttestationConfigError, AttestationFailedError, AttestationMissingError } from "./errors.js";
|
|
7
|
+
import { isTestAssertion, verifierFor } from "./verifier.js";
|
|
8
|
+
import { authDatabase } from "../data/tables.js";
|
|
9
|
+
import { resolveDb } from "../http/resolve.js";
|
|
10
|
+
import { workerIdentity } from "@pithy-sh/core/src/worker/identity";
|
|
11
|
+
//#region src/attestation/gate.ts
|
|
12
|
+
/** The refusal for an environment that has no block to judge an attestation by. */
|
|
13
|
+
function noBlock(environment) {
|
|
14
|
+
const where = environment === null ? "an unstamped Worker" : `the ${environment} environment`;
|
|
15
|
+
return new AttestationConfigError({
|
|
16
|
+
detail: `${where} has no attestation block in auth's config`,
|
|
17
|
+
action: `Add an \`attestation.environments.${environment ?? "<env>"}\` block to auth() in this Worker's pithy.config.ts, then redeploy.`
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/** Judge one presented attestation. Returns when it verified; throws the refusal otherwise. */
|
|
21
|
+
async function verifyAttestation(c, header, settings, environment, options) {
|
|
22
|
+
const decoded = decodeAttestation(header);
|
|
23
|
+
if (!decoded.ok) throw new AttestationFailedError({ detail: decoded.reason });
|
|
24
|
+
const presented = decoded.value;
|
|
25
|
+
const env = c.env;
|
|
26
|
+
const db = authDatabase(resolveDb(env, options.database));
|
|
27
|
+
const now = /* @__PURE__ */ new Date();
|
|
28
|
+
const use = await consumeChallenge(db, presented.challenge, now);
|
|
29
|
+
if (!use.ok) throw new AttestationFailedError({ detail: `challenge: ${use.reason}` });
|
|
30
|
+
if (!isTestAssertion(presented) && settings[presented.platform].length === 0) throw new AttestationFailedError({ detail: `no ${presented.platform} app is allowed to attest in ${environment ?? "an unstamped Worker"}` });
|
|
31
|
+
const verifier = verifierFor(presented, options.verifiers);
|
|
32
|
+
if (verifier === void 0) throw new AttestationConfigError({
|
|
33
|
+
detail: `no ${presented.platform} attestation verifier is available to this Worker`,
|
|
34
|
+
action: `Upgrade @pithy-sh/auth to a release that verifies ${presented.platform} attestations, or set this environment to test and send the test assertion.`
|
|
35
|
+
});
|
|
36
|
+
const body = new Uint8Array(await c.req.raw.clone().arrayBuffer());
|
|
37
|
+
const binding = await requestBinding(presented.challenge, body);
|
|
38
|
+
const verdict = await verifier.verify({
|
|
39
|
+
presented,
|
|
40
|
+
binding,
|
|
41
|
+
environment,
|
|
42
|
+
settings,
|
|
43
|
+
db,
|
|
44
|
+
env,
|
|
45
|
+
now
|
|
46
|
+
});
|
|
47
|
+
if (!verdict.ok) throw new AttestationFailedError({ detail: verdict.reason });
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The gate on auth's send routes once any environment has an attestation block.
|
|
51
|
+
*
|
|
52
|
+
* Either passes. A request carrying {@link ATTESTATION_HEADER} is judged by attestation alone, and a failure
|
|
53
|
+
* there is final. A request without one goes to Turnstile when it is composed; with no Turnstile, it is refused
|
|
54
|
+
* where this environment has a block, and passes where it has none, as it did before attestation existed.
|
|
55
|
+
*/
|
|
56
|
+
function sendGate(options) {
|
|
57
|
+
return async (c, next) => {
|
|
58
|
+
const environment = workerIdentity(c.env).environment;
|
|
59
|
+
const settings = attestationFor(options.attestation, environment);
|
|
60
|
+
const header = c.req.header(ATTESTATION_HEADER);
|
|
61
|
+
if (header !== void 0) {
|
|
62
|
+
if (settings === void 0) throw noBlock(environment);
|
|
63
|
+
await verifyAttestation(c, header, settings, environment, options);
|
|
64
|
+
return next();
|
|
65
|
+
}
|
|
66
|
+
if (options.turnstile !== void 0) return options.turnstile(c, next);
|
|
67
|
+
if (settings !== void 0) throw new AttestationMissingError({ detail: `no ${ATTESTATION_HEADER} header, and turnstile is not composed to take the request instead` });
|
|
68
|
+
return next();
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** The challenge route's refusal where this environment has no block. Exported for the route. */
|
|
72
|
+
function assertAttestationBlock(config, environment) {
|
|
73
|
+
const settings = attestationFor(config, environment);
|
|
74
|
+
if (settings === void 0) throw noBlock(environment);
|
|
75
|
+
return settings;
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { assertAttestationBlock, sendGate };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { type PlayIntegrityServiceAccount } from "../../instance/secrets";
|
|
4
|
+
/** The OAuth scope `decodeIntegrityToken` takes. */
|
|
5
|
+
export declare const PLAY_INTEGRITY_SCOPE = "https://www.googleapis.com/auth/playintegrity";
|
|
6
|
+
/** Google's token endpoint, where a service account's file names none. */
|
|
7
|
+
export declare const GOOGLE_TOKEN_URI = "https://oauth2.googleapis.com/token";
|
|
8
|
+
/** How long one call to Google may take, in milliseconds. */
|
|
9
|
+
export declare const GOOGLE_DEADLINE_MS = 10000;
|
|
10
|
+
/** The operator's remedy for a service account that is missing, malformed or refused. */
|
|
11
|
+
export declare const SERVICE_ACCOUNT_ACTION: string;
|
|
12
|
+
/** Sign the RS256 assertion a service account exchanges for an access token. */
|
|
13
|
+
export declare function signServiceAccountAssertion(account: PlayIntegrityServiceAccount, options: {
|
|
14
|
+
scope: string;
|
|
15
|
+
now: Date;
|
|
16
|
+
}): Promise<string>;
|
|
17
|
+
/** The access tokens a verifier holds, one per service account, each kept until shortly before it expires. */
|
|
18
|
+
export interface GoogleAccessTokens {
|
|
19
|
+
/** A live access token for `account`, minted when none is held. */
|
|
20
|
+
get(account: PlayIntegrityServiceAccount, now: Date): Promise<string>;
|
|
21
|
+
/** Forget `account`'s token, so the next {@link get} mints a new one. */
|
|
22
|
+
drop(account: PlayIntegrityServiceAccount): void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Access tokens for the Play Integrity scope. Google refusing the account is `auth/attestation_config`; Google
|
|
26
|
+
* failing, or answering something that is not a token, is `core/upstream_failed` or `core/upstream_timeout`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createGoogleAccessTokens(fetchImpl: typeof fetch): GoogleAccessTokens;
|
|
29
|
+
/** A token decoded, or refused by Google for this package. */
|
|
30
|
+
export type DecodedIntegrityToken = {
|
|
31
|
+
ok: true;
|
|
32
|
+
payload: Record<string, unknown>;
|
|
33
|
+
} | {
|
|
34
|
+
ok: false;
|
|
35
|
+
reason: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Call `decodeIntegrityToken` for `packageName`. A 400 is the token refused for this package; a 401, 403 or 404 is
|
|
39
|
+
* the server's credential or project setup, `auth/attestation_config`; anything else Google gets wrong is the
|
|
40
|
+
* upstream pair's.
|
|
41
|
+
*/
|
|
42
|
+
export declare function decodeIntegrityToken(fetchImpl: typeof fetch, input: {
|
|
43
|
+
accessToken: string;
|
|
44
|
+
packageName: string;
|
|
45
|
+
token: string;
|
|
46
|
+
}): Promise<DecodedIntegrityToken>;
|
|
47
|
+
//# sourceMappingURL=google.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../../../src/attestation/playIntegrity/google.ts"],"names":[],"mappings":"AAOA,OAAO,EAAuC,KAAK,2BAA2B,EAAE,MAAM,wBAAwB,CAAC;AAG/G,oDAAoD;AACpD,eAAO,MAAM,oBAAoB,kDAAkD,CAAC;AAEpF,0EAA0E;AAC1E,eAAO,MAAM,gBAAgB,wCAAwC,CAAC;AAEtE,6DAA6D;AAC7D,eAAO,MAAM,kBAAkB,QAAS,CAAC;AAUzC,yFAAyF;AACzF,eAAO,MAAM,sBAAsB,QAAkR,CAAC;AAqDtT,gFAAgF;AAChF,wBAAsB,2BAA2B,CAC/C,OAAO,EAAE,2BAA2B,EACpC,OAAO,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,IAAI,CAAA;CAAE,GACpC,OAAO,CAAC,MAAM,CAAC,CAcjB;AAiCD,8GAA8G;AAC9G,MAAM,WAAW,kBAAkB;IACjC,mEAAmE;IACnE,GAAG,CAAC,OAAO,EAAE,2BAA2B,EAAE,GAAG,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtE,yEAAyE;IACzE,IAAI,CAAC,OAAO,EAAE,2BAA2B,GAAG,IAAI,CAAC;CAClD;AAMD;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,OAAO,KAAK,GAAG,kBAAkB,CA2CpF;AAED,8DAA8D;AAC9D,MAAM,MAAM,qBAAqB,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnH;;;;GAIG;AACH,wBAAsB,oBAAoB,CACxC,SAAS,EAAE,OAAO,KAAK,EACvB,KAAK,EAAE;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACjE,OAAO,CAAC,qBAAqB,CAAC,CAkChC"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { AUTH_PLAY_INTEGRITY_SERVICE_ACCOUNT } from "../../instance/secrets.js";
|
|
4
|
+
import { AttestationConfigError } from "../errors.js";
|
|
5
|
+
import { UpstreamError, UpstreamTimeoutError } from "@pithy-sh/core/src/error/pithyError";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { base64UrlEncode } from "@pithy-sh/core/src/controlPlane/token/base64url";
|
|
8
|
+
import { pithyCommand } from "@pithy-sh/core/src/cli/command";
|
|
9
|
+
//#region src/attestation/playIntegrity/google.ts
|
|
10
|
+
/** The OAuth scope `decodeIntegrityToken` takes. */
|
|
11
|
+
const PLAY_INTEGRITY_SCOPE = "https://www.googleapis.com/auth/playintegrity";
|
|
12
|
+
/** Google's token endpoint, where a service account's file names none. */
|
|
13
|
+
const GOOGLE_TOKEN_URI = "https://oauth2.googleapis.com/token";
|
|
14
|
+
/** How long one call to Google may take, in milliseconds. */
|
|
15
|
+
const GOOGLE_DEADLINE_MS = 1e4;
|
|
16
|
+
/** The longest-lived assertion Google's token endpoint accepts, in seconds. */
|
|
17
|
+
const ASSERTION_LIFETIME_SECONDS = 3600;
|
|
18
|
+
/** How long before its stated expiry a cached access token is replaced, in milliseconds. */
|
|
19
|
+
const TOKEN_EARLY_REFRESH_MS = 6e4;
|
|
20
|
+
const PLAY_INTEGRITY_API = "https://playintegrity.googleapis.com/v1";
|
|
21
|
+
/** The operator's remedy for a service account that is missing, malformed or refused. */
|
|
22
|
+
const SERVICE_ACCOUNT_ACTION = `Store the Google Cloud service account's JSON key with \`${pithyCommand("secrets create", { args: [AUTH_PLAY_INTEGRITY_SERVICE_ACCOUNT] })}\` for this environment, and link its Google Cloud project to the app in Play Console. See the auth package's docs/attestation.md.`;
|
|
23
|
+
/** The token endpoint's answer, as far as auth reads it. */
|
|
24
|
+
const AccessTokenAnswer = z.object({
|
|
25
|
+
access_token: z.string().min(1).describe("The OAuth access token."),
|
|
26
|
+
expires_in: z.number().positive().describe("How long the token lives, in seconds.")
|
|
27
|
+
}).describe("Google's answer to a jwt-bearer grant.");
|
|
28
|
+
/** `decodeIntegrityToken`'s answer, as far as auth reads it. */
|
|
29
|
+
const DecodeAnswer = z.object({ tokenPayloadExternal: z.record(z.string(), z.unknown()).describe("The decoded token payload.") }).describe("Google's answer to decodeIntegrityToken.");
|
|
30
|
+
function decodePem(pem) {
|
|
31
|
+
const body = pem.replace(/-----BEGIN PRIVATE KEY-----/, "").replace(/-----END PRIVATE KEY-----/, "").replace(/\s+/g, "");
|
|
32
|
+
if (body.length === 0 || !/^[A-Za-z0-9+/]+={0,2}$/.test(body)) return void 0;
|
|
33
|
+
try {
|
|
34
|
+
return Uint8Array.from(atob(body), (char) => char.charCodeAt(0));
|
|
35
|
+
} catch {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function importPrivateKey(account) {
|
|
40
|
+
const der = decodePem(account.private_key);
|
|
41
|
+
try {
|
|
42
|
+
if (der === void 0) throw new TypeError("not a PEM");
|
|
43
|
+
return await crypto.subtle.importKey("pkcs8", der, {
|
|
44
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
45
|
+
hash: "SHA-256"
|
|
46
|
+
}, false, ["sign"]);
|
|
47
|
+
} catch {
|
|
48
|
+
throw new AttestationConfigError({
|
|
49
|
+
detail: `the Play Integrity service account ${account.client_email} has a private_key that is not an RSA PKCS#8 PEM`,
|
|
50
|
+
action: SERVICE_ACCOUNT_ACTION
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function encodeJson(value) {
|
|
55
|
+
return base64UrlEncode(new TextEncoder().encode(JSON.stringify(value)));
|
|
56
|
+
}
|
|
57
|
+
/** Sign the RS256 assertion a service account exchanges for an access token. */
|
|
58
|
+
async function signServiceAccountAssertion(account, options) {
|
|
59
|
+
const key = await importPrivateKey(account);
|
|
60
|
+
const iat = Math.floor(options.now.getTime() / 1e3);
|
|
61
|
+
const header = {
|
|
62
|
+
alg: "RS256",
|
|
63
|
+
typ: "JWT",
|
|
64
|
+
...account.private_key_id ? { kid: account.private_key_id } : {}
|
|
65
|
+
};
|
|
66
|
+
const claims = {
|
|
67
|
+
iss: account.client_email,
|
|
68
|
+
scope: options.scope,
|
|
69
|
+
aud: account.token_uri ?? "https://oauth2.googleapis.com/token",
|
|
70
|
+
iat,
|
|
71
|
+
exp: iat + ASSERTION_LIFETIME_SECONDS
|
|
72
|
+
};
|
|
73
|
+
const signingInput = `${encodeJson(header)}.${encodeJson(claims)}`;
|
|
74
|
+
const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", key, new TextEncoder().encode(signingInput));
|
|
75
|
+
return `${signingInput}.${base64UrlEncode(new Uint8Array(signature))}`;
|
|
76
|
+
}
|
|
77
|
+
/** One call to Google under {@link GOOGLE_DEADLINE_MS}. A call that never answers is the upstream pair's. */
|
|
78
|
+
async function callGoogle(fetchImpl, url, init, what) {
|
|
79
|
+
try {
|
|
80
|
+
return await fetchImpl(url, {
|
|
81
|
+
...init,
|
|
82
|
+
signal: AbortSignal.timeout(GOOGLE_DEADLINE_MS)
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if ((error instanceof Error || error instanceof DOMException ? error.name : "") === "TimeoutError") throw new UpstreamTimeoutError({ detail: `${what}: Google did not answer within ${GOOGLE_DEADLINE_MS}ms` });
|
|
86
|
+
throw new UpstreamError({ detail: `${what}: Google could not be reached: ${String(error)}` });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Google's own wording for a refusal, from an `{ error }` or `{ error: { message } }` body. */
|
|
90
|
+
async function googleReason(response) {
|
|
91
|
+
const text = await response.text().catch(() => "");
|
|
92
|
+
try {
|
|
93
|
+
const body = JSON.parse(text);
|
|
94
|
+
if (typeof body.error === "string") return typeof body.error_description === "string" ? `${body.error}: ${body.error_description}` : body.error;
|
|
95
|
+
if (typeof body.error === "object" && body.error !== null) {
|
|
96
|
+
const message = body.error.message;
|
|
97
|
+
if (typeof message === "string") return message;
|
|
98
|
+
}
|
|
99
|
+
} catch {}
|
|
100
|
+
return `HTTP ${response.status}`;
|
|
101
|
+
}
|
|
102
|
+
function cacheKey(account) {
|
|
103
|
+
return `${account.client_email}\n${account.private_key_id ?? ""}\n${account.token_uri ?? "https://oauth2.googleapis.com/token"}`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Access tokens for the Play Integrity scope. Google refusing the account is `auth/attestation_config`; Google
|
|
107
|
+
* failing, or answering something that is not a token, is `core/upstream_failed` or `core/upstream_timeout`.
|
|
108
|
+
*/
|
|
109
|
+
function createGoogleAccessTokens(fetchImpl) {
|
|
110
|
+
const held = /* @__PURE__ */ new Map();
|
|
111
|
+
return {
|
|
112
|
+
async get(account, now) {
|
|
113
|
+
const key = cacheKey(account);
|
|
114
|
+
const cached = held.get(key);
|
|
115
|
+
if (cached !== void 0 && now.getTime() < cached.expiresAt) return cached.token;
|
|
116
|
+
const assertion = await signServiceAccountAssertion(account, {
|
|
117
|
+
scope: PLAY_INTEGRITY_SCOPE,
|
|
118
|
+
now
|
|
119
|
+
});
|
|
120
|
+
const tokenUri = account.token_uri ?? "https://oauth2.googleapis.com/token";
|
|
121
|
+
const response = await callGoogle(fetchImpl, tokenUri, {
|
|
122
|
+
method: "POST",
|
|
123
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
124
|
+
body: new URLSearchParams({
|
|
125
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
126
|
+
assertion
|
|
127
|
+
})
|
|
128
|
+
}, "token");
|
|
129
|
+
if (response.status === 400 || response.status === 401 || response.status === 403) throw new AttestationConfigError({
|
|
130
|
+
detail: `Google refused the Play Integrity service account ${account.client_email} at ${tokenUri} (${response.status}): ${await googleReason(response)}`,
|
|
131
|
+
action: SERVICE_ACCOUNT_ACTION
|
|
132
|
+
});
|
|
133
|
+
if (!response.ok) throw new UpstreamError({ detail: `token: ${tokenUri} answered ${response.status}: ${await googleReason(response)}` });
|
|
134
|
+
const answer = AccessTokenAnswer.safeParse(await response.json().catch(() => void 0));
|
|
135
|
+
if (!answer.success) throw new UpstreamError({ detail: `token: ${tokenUri} answered 200 with no access token` });
|
|
136
|
+
held.set(key, {
|
|
137
|
+
token: answer.data.access_token,
|
|
138
|
+
expiresAt: now.getTime() + answer.data.expires_in * 1e3 - TOKEN_EARLY_REFRESH_MS
|
|
139
|
+
});
|
|
140
|
+
return answer.data.access_token;
|
|
141
|
+
},
|
|
142
|
+
drop(account) {
|
|
143
|
+
held.delete(cacheKey(account));
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Call `decodeIntegrityToken` for `packageName`. A 400 is the token refused for this package; a 401, 403 or 404 is
|
|
149
|
+
* the server's credential or project setup, `auth/attestation_config`; anything else Google gets wrong is the
|
|
150
|
+
* upstream pair's.
|
|
151
|
+
*/
|
|
152
|
+
async function decodeIntegrityToken(fetchImpl, input) {
|
|
153
|
+
const response = await callGoogle(fetchImpl, `${PLAY_INTEGRITY_API}/${encodeURIComponent(input.packageName)}:decodeIntegrityToken`, {
|
|
154
|
+
method: "POST",
|
|
155
|
+
headers: {
|
|
156
|
+
authorization: `Bearer ${input.accessToken}`,
|
|
157
|
+
"content-type": "application/json"
|
|
158
|
+
},
|
|
159
|
+
body: JSON.stringify({ integrity_token: input.token })
|
|
160
|
+
}, "decode");
|
|
161
|
+
if (response.status === 400) return {
|
|
162
|
+
ok: false,
|
|
163
|
+
reason: `Google would not decode it for ${input.packageName}: ${await googleReason(response)}`
|
|
164
|
+
};
|
|
165
|
+
if (response.status === 401 || response.status === 403 || response.status === 404) throw new AttestationConfigError({
|
|
166
|
+
detail: `decodeIntegrityToken for ${input.packageName} answered ${response.status}: ${await googleReason(response)}`,
|
|
167
|
+
action: `Enable the Play Integrity API in the service account's Google Cloud project, and link that project to ${input.packageName} in Play Console under Test and release > App integrity.`
|
|
168
|
+
});
|
|
169
|
+
if (!response.ok) throw new UpstreamError({ detail: `decode: decodeIntegrityToken for ${input.packageName} answered ${response.status}: ${await googleReason(response)}` });
|
|
170
|
+
const answer = DecodeAnswer.safeParse(await response.json().catch(() => void 0));
|
|
171
|
+
if (!answer.success) throw new UpstreamError({ detail: `decode: decodeIntegrityToken answered 200 with no tokenPayloadExternal` });
|
|
172
|
+
return {
|
|
173
|
+
ok: true,
|
|
174
|
+
payload: answer.data.tokenPayloadExternal
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
export { GOOGLE_DEADLINE_MS, GOOGLE_TOKEN_URI, PLAY_INTEGRITY_SCOPE, SERVICE_ACCOUNT_ACTION, createGoogleAccessTokens, decodeIntegrityToken, signServiceAccountAssertion };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import type { AttestationVerdict } from "../verifier";
|
|
5
|
+
/** How long after it was requested a token is still accepted, in seconds. */
|
|
6
|
+
export declare const PLAY_INTEGRITY_MAX_AGE_SECONDS = 300;
|
|
7
|
+
/** How far ahead of the Worker's clock a token's timestamp may be, in seconds. */
|
|
8
|
+
export declare const PLAY_INTEGRITY_MAX_SKEW_SECONDS = 60;
|
|
9
|
+
/** The parts of `tokenPayloadExternal` auth judges. Every other field is ignored. */
|
|
10
|
+
export declare const IntegrityTokenPayload: z.ZodObject<{
|
|
11
|
+
requestDetails: z.ZodObject<{
|
|
12
|
+
requestPackageName: z.ZodString;
|
|
13
|
+
requestHash: z.ZodOptional<z.ZodString>;
|
|
14
|
+
nonce: z.ZodOptional<z.ZodString>;
|
|
15
|
+
timestampMillis: z.ZodString;
|
|
16
|
+
}, z.core.$strip>;
|
|
17
|
+
appIntegrity: z.ZodOptional<z.ZodObject<{
|
|
18
|
+
appRecognitionVerdict: z.ZodOptional<z.ZodString>;
|
|
19
|
+
packageName: z.ZodOptional<z.ZodString>;
|
|
20
|
+
}, z.core.$strip>>;
|
|
21
|
+
deviceIntegrity: z.ZodOptional<z.ZodObject<{
|
|
22
|
+
deviceRecognitionVerdict: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
23
|
+
}, z.core.$strip>>;
|
|
24
|
+
}, z.core.$strip>;
|
|
25
|
+
export type IntegrityTokenPayload = z.output<typeof IntegrityTokenPayload>;
|
|
26
|
+
/** What a decoded payload is judged against. */
|
|
27
|
+
export interface IntegrityExpectation {
|
|
28
|
+
/** {@link import("../wire").requestBinding} for this request. */
|
|
29
|
+
binding: Uint8Array;
|
|
30
|
+
/** The package the token was decoded for, one this environment allows. */
|
|
31
|
+
packageName: string;
|
|
32
|
+
now: Date;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Judge a decoded Play Integrity payload. `reason` starts with the check that failed: `format`, `requestHash` or
|
|
36
|
+
* `nonce`, `requestPackageName`, `appRecognitionVerdict`, `appIntegrity` (the package Google attested is another
|
|
37
|
+
* app's), `deviceIntegrity` or `timestamp`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function judgeIntegrityVerdict(payload: unknown, expected: IntegrityExpectation): AttestationVerdict;
|
|
40
|
+
//# sourceMappingURL=verdict.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verdict.d.ts","sourceRoot":"","sources":["../../../src/attestation/playIntegrity/verdict.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtD,6EAA6E;AAC7E,eAAO,MAAM,8BAA8B,MAAM,CAAC;AAElD,kFAAkF;AAClF,eAAO,MAAM,+BAA+B,KAAK,CAAC;AAKlD,qFAAqF;AACrF,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;iBAsC4E,CAAC;AAC/G,MAAM,MAAM,qBAAqB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,qBAAqB,CAAC,CAAC;AAE3E,gDAAgD;AAChD,MAAM,WAAW,oBAAoB;IACnC,iEAAiE;IACjE,OAAO,EAAE,UAAU,CAAC;IACpB,0EAA0E;IAC1E,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,IAAI,CAAC;CACX;AAoBD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,oBAAoB,GAAG,kBAAkB,CA0D1G"}
|