Last active
April 8, 2022 13:45
-
-
Save fipso/bca03a3bf2763727f5cc0f3dd6541b0f to your computer and use it in GitHub Desktop.
validate telegram bot login using nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Validate data | |
const pairs = Object.entries(tg) | |
.filter((e) => e[0] !== "hash") | |
.sort((a: any, b: any) => a[0].localeCompare(b[0])); | |
const tgData = pairs.map((p: any) => p.join("=")).join("\n"); | |
console.log(tgData); | |
const key = crypto | |
.createHash("sha256") | |
.update(process.env.TELEGRAM_BOT_TOKEN!) | |
.digest(); | |
console.log("key", key); | |
const dataHash = crypto | |
.createHmac("sha256", key) | |
.update(tgData) | |
.digest("hex"); | |
console.log("our hash", dataHash); | |
if (dataHash !== tg.hash) | |
return res.status(400).json({ error: "invalid hash" }); | |
console.log("hash ok"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment