-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
41 lines (35 loc) · 1.01 KB
/
index.js
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
const { getToken } = require("@sagi.io/workers-jwt");
module.exports = async function (request, credentials) {
const { headers } = request;
const { iss, kid, privateKeyPEM } = credentials;
const jwtPayload = {
iss,
iat: Math.floor(Date.now() / 1000),
};
const jwt = await getToken({
privateKeyPEM,
payload: jwtPayload,
alg: "ES256",
headerAdditions: {
kid,
},
});
const isDevelopment = headers.get("X-Apple-Device-Development") == "true";
const deviceToken = headers.get("X-Apple-Device-Token");
const environment = isDevelopment ? "api.development" : "api";
const response = await fetch(
`https://${environment}.devicecheck.apple.com/v1/validate_device_token`,
{
method: "POST",
body: JSON.stringify({
device_token: deviceToken,
transaction_id: `trns-${Date.now()}`,
timestamp: Math.floor(Date.now()),
}),
headers: {
Authorization: `Bearer ${jwt}`,
},
}
);
return response.status == 200;
};