Repository navigation
Expand file tree
/
Copy pathverifier.ts
More file actions
211 lines (194 loc) · 6.95 KB
/
Copy pathverifier.ts
File metadata and controls
211 lines (194 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { UpstreamUnavailableError } from "@/shared/server/common/errors";
import axios from "axios";
import { GoogleAuth } from "google-auth-library";
const PLAY_INTEGRITY_SCOPE = "https://www.googleapis.com/auth/playintegrity";
interface AccessTokenClient {
getAccessToken(): Promise<
string | { token?: string | null } | null | undefined
>;
}
interface IntegrityHttpClient {
post(
url: string,
body: { integrity_token: string },
options: { headers: Record<string, string>; timeout?: number },
): Promise<{ data: unknown }>;
}
interface DecodedIntegrityToken {
tokenPayloadExternal?: {
requestDetails?: {
requestHash?: string;
requestPackageName?: string;
};
appIntegrity?: { appRecognitionVerdict?: string | null };
deviceIntegrity?: { deviceRecognitionVerdict?: unknown };
accountDetails?: { appLicensingVerdict?: string | null };
};
}
interface VerifyInput {
integrityToken: string;
expectedRequestHash?: string;
expectedPackageName?: string;
}
interface ServiceOptions {
packageName?: string;
requireLicensedApp?: boolean;
authClient?: AccessTokenClient;
httpClient?: IntegrityHttpClient;
timeoutMs?: number;
}
/** Provider/authentication failure with a stable reason code for API handling. */
class PlayIntegrityUnavailableError extends UpstreamUnavailableError {
reasonCode: string;
constructor(reasonCode: string, cause: unknown) {
super(reasonCode, cause);
this.name = "PlayIntegrityUnavailableError";
this.reasonCode = reasonCode;
this.cause = cause;
}
}
/**
* Creates a Play Integrity verifier with injectable Google auth and HTTP clients.
*
* @param options - Package name, license policy, adapters and request timeout.
* @returns A `verify` operation for one integrity token.
*/
function createPlayIntegrityService(options: ServiceOptions) {
const {
packageName,
requireLicensedApp = false,
authClient = new GoogleAuth({ scopes: [PLAY_INTEGRITY_SCOPE] }),
httpClient = axios,
timeoutMs,
} = options;
/**
* Decodes Google's response and evaluates it against the expected app/request.
*
* @param input - Integrity token and expected request hash/package name.
* @returns Allow/deny decision, stable reason codes and safe verdict fields.
* @throws `PlayIntegrityUnavailableError` when Google auth or decode is unavailable.
*/
async function verify(input: VerifyInput) {
// Google requires a service access token before its decode endpoint can be called.
const accessToken = await getAccessToken(authClient);
// Decode the opaque client token server-side, then evaluate only the returned verdict fields.
const decoded = await decodeIntegrityToken({
httpClient,
packageName: input.expectedPackageName || packageName,
integrityToken: input.integrityToken,
accessToken,
timeoutMs,
});
return evaluateVerdict({
decoded,
expectedRequestHash: input.expectedRequestHash,
expectedPackageName: input.expectedPackageName || packageName,
requireLicensedApp,
});
}
return { verify };
}
/** @param authClient - Google service auth adapter. @returns Google access token. @throws `PlayIntegrityUnavailableError` when no token is available. */
async function getAccessToken(authClient: AccessTokenClient) {
try {
// GoogleAuth may return either the token string or an object containing it.
const credential = await authClient.getAccessToken();
const token =
typeof credential === "string" ? credential : credential?.token;
if (!token) throw new Error("access token unavailable");
return token;
} catch (error: unknown) {
throw new PlayIntegrityUnavailableError(
"GOOGLE_CREDENTIALS_UNAVAILABLE",
error,
);
}
}
/** @param options - Google client, package, token, access token and timeout. @returns Decoded verdict payload. @throws `PlayIntegrityUnavailableError` for timeout or decode failure. */
async function decodeIntegrityToken({
httpClient,
packageName,
integrityToken,
accessToken,
timeoutMs,
}: {
httpClient: IntegrityHttpClient;
packageName?: string;
integrityToken: string;
accessToken: string;
timeoutMs?: number;
}): Promise<DecodedIntegrityToken> {
try {
// Send the integrity token to Google's package-specific decode endpoint.
const response = await httpClient.post(
`https://playintegrity.googleapis.com/v1/${packageName}:decodeIntegrityToken`,
{ integrity_token: integrityToken },
{
headers: { Authorization: `Bearer ${accessToken}` },
timeout: timeoutMs,
},
);
return response.data as DecodedIntegrityToken;
} catch (error: unknown) {
const reasonCode = ["ECONNABORTED", "ETIMEDOUT"].includes(errorCode(error))
? "GOOGLE_DECODE_TIMEOUT"
: "GOOGLE_DECODE_FAILED";
throw new PlayIntegrityUnavailableError(reasonCode, error);
}
}
/** @param error - Thrown HTTP/network value. @returns Its string `code`, or an empty string. */
function errorCode(error: unknown): string {
return error && typeof error === "object" && "code" in error
? String(error.code)
: "";
}
/** @param input - Decoded token and expected request/app policy. @returns Decision, reason codes, match result and safe verdict values. */
function evaluateVerdict({
decoded,
expectedRequestHash,
expectedPackageName,
requireLicensedApp,
}: {
decoded: DecodedIntegrityToken;
expectedRequestHash?: string;
expectedPackageName?: string;
requireLicensedApp: boolean;
}) {
// Each failed comparison adds a stable reason code for the API response and logs.
const tokenPayload = decoded?.tokenPayloadExternal;
const requestDetails = tokenPayload?.requestDetails;
const requestHash = requestDetails?.requestHash;
const requestPackageName = requestDetails?.requestPackageName;
const appRecognition =
tokenPayload?.appIntegrity?.appRecognitionVerdict ?? null;
const deviceIntegrity = Array.isArray(
tokenPayload?.deviceIntegrity?.deviceRecognitionVerdict,
)
? tokenPayload.deviceIntegrity.deviceRecognitionVerdict
: [];
const appLicensing =
tokenPayload?.accountDetails?.appLicensingVerdict ?? null;
const requestHashMatched = requestHash === expectedRequestHash;
const packageNameMatched = requestPackageName === expectedPackageName;
const reasonCodes = [];
if (!requestHashMatched) reasonCodes.push("REQUEST_HASH_MISMATCH");
if (!packageNameMatched) reasonCodes.push("PACKAGE_NAME_MISMATCH");
if (appRecognition !== "PLAY_RECOGNIZED")
reasonCodes.push("APP_NOT_RECOGNIZED");
if (!deviceIntegrity.includes("MEETS_DEVICE_INTEGRITY")) {
reasonCodes.push("DEVICE_INTEGRITY_NOT_MET");
}
if (requireLicensedApp && appLicensing !== "LICENSED")
reasonCodes.push("APP_NOT_LICENSED");
return {
decision: reasonCodes.length === 0 ? "allow" : "deny",
reasonCodes,
requestHashMatched,
verdict: {
appRecognition,
deviceIntegrity,
appLicensing,
},
};
}
export { createPlayIntegrityService, PlayIntegrityUnavailableError };