-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegramAuthHelpers.ts
57 lines (49 loc) · 1.55 KB
/
telegramAuthHelpers.ts
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
export async function isRecent(telegramInitData: string) {
const urlParams: URLSearchParams = new URLSearchParams(telegramInitData);
const auth_date = Number(urlParams.get("auth_date"));
const isRecent = Date.now() / 1000 - auth_date < 600;
return isRecent;
}
export async function verifyInitData(
telegramInitData: string,
botToken: string
) {
const urlParams: URLSearchParams = new URLSearchParams(telegramInitData);
const hash = urlParams.get("hash");
urlParams.delete("hash");
urlParams.sort();
let dataCheckString = "";
for (const [key, value] of urlParams.entries()) {
dataCheckString += `${key}=${value}\n`;
}
dataCheckString = dataCheckString.slice(0, -1);
const encoder = new TextEncoder();
const secretKey = await window.crypto.subtle.importKey(
"raw",
encoder.encode("WebAppData"),
{ name: "HMAC", hash: "SHA-256" },
false,
["sign"]
);
const botTokenKey = await window.crypto.subtle.sign(
"HMAC",
secretKey,
encoder.encode(botToken)
);
const calculatedHash = await window.crypto.subtle.sign(
"HMAC",
await window.crypto.subtle.importKey(
"raw",
botTokenKey,
{ name: "HMAC", hash: "SHA-256" },
false,
["sign"]
),
encoder.encode(dataCheckString)
);
const calculatedHashHex = Array.from(new Uint8Array(calculatedHash))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
const isVerified = hash === calculatedHashHex;
return isVerified;
}