From 3e0b1392b3b0aec3f6fdef8dc6239786694d1441 Mon Sep 17 00:00:00 2001 From: Maharshi Mishra Date: Fri, 23 Aug 2024 09:09:50 +0530 Subject: [PATCH] reverted old changes --- .../server/api/index.js | 50 ------------------- .../sfa-web-ton-telegram-example/src/App.tsx | 18 +++---- 2 files changed, 9 insertions(+), 59 deletions(-) diff --git a/single-factor-auth-web/sfa-web-ton-telegram-example/server/api/index.js b/single-factor-auth-web/sfa-web-ton-telegram-example/server/api/index.js index dae39dd9..ffd8d10b 100644 --- a/single-factor-auth-web/sfa-web-ton-telegram-example/server/api/index.js +++ b/single-factor-auth-web/sfa-web-ton-telegram-example/server/api/index.js @@ -5,7 +5,6 @@ const dotenv = require("dotenv"); const path = require("path"); const { AuthDataValidator } = require("@telegram-auth/server"); const { objectToAuthDataMap } = require("@telegram-auth/server/utils"); -const crypto = require('crypto'); dotenv.config(); @@ -31,33 +30,6 @@ const generateJwtToken = (userData) => { return jwt.sign(payload, privateKey, { algorithm: "RS256", keyid: JWT_KEY_ID }); }; -function validateTelegramWebAppData(telegramInitData) { - // Parse the received init data - const initData = new URLSearchParams(telegramInitData); - - // Extract the hash and remove it from the data - const hash = initData.get('hash'); - initData.delete('hash'); - - // Sort the data alphabetically - const sortedInitData = new URLSearchParams([...initData.entries()].sort()); - - // Create a data check string - const dataCheckString = sortedInitData.toString(); - - // Create a secret key from the bot token - const secretKey = crypto.createHmac('sha256', 'WebAppData').update(process.env.TELEGRAM_BOT_TOKEN).digest(); - - // Calculate the hmac-sha256 signature - const calculatedHash = crypto - .createHmac('sha256', secretKey) - .update(dataCheckString) - .digest('hex'); - - // Compare the calculated hash with the received hash - return calculatedHash === hash; -} - app.get("/", (req, res) => res.send("Express on Vercel for Telegram Login to be used with Web3Auth")); app.get("/.well-known/jwks.json", (req, res) => { @@ -119,28 +91,6 @@ app.get("/callback", async (req, res) => { } }); -app.post("/auth", (req, res) => { - const { telegramInitData, user } = req.body; - - if (!validateTelegramWebAppData(telegramInitData)) { - return res.status(401).json({ error: 'Invalid Telegram data' }); - } - - // If validation passes, generate a JWT token - const token = jwt.sign( - { - telegramId: user.id, - username: user.username, - firstName: user.first_name, - lastName: user.last_name - }, - process.env.JWT_SECRET, - { expiresIn: '1h' } - ); - - res.json({ token }); -}); - app.listen(3000, () => console.log("Server ready on port 3000.")); module.exports = app; \ No newline at end of file diff --git a/single-factor-auth-web/sfa-web-ton-telegram-example/src/App.tsx b/single-factor-auth-web/sfa-web-ton-telegram-example/src/App.tsx index bb061882..03de71c6 100644 --- a/single-factor-auth-web/sfa-web-ton-telegram-example/src/App.tsx +++ b/single-factor-auth-web/sfa-web-ton-telegram-example/src/App.tsx @@ -69,14 +69,8 @@ function App() { headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ - telegramInitData: WebApp.initData, - user: userData, - }), + body: JSON.stringify(userData), }); - if (!response.ok) { - throw new Error("Authentication failed"); - } const { token } = await response.json(); await loginWithWeb3Auth(token, web3auth); } @@ -241,6 +235,7 @@ function App() { +

@@ -249,13 +244,18 @@ function App() { const logoutView = ( ); return (
-

Web3Auth SFA React Telegram Example

+

+ + Web3Auth + {" "} + SFA React Telegram GitHub Example +

{isLoggingIn ? :
{web3authSfa ? (loggedIn ? loginView : logoutView) : null}
}