Skip to content

Commit

Permalink
reverted old changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ihsraham committed Aug 23, 2024
1 parent 19c1f8d commit 3e0b139
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) => {
Expand Down Expand Up @@ -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;
18 changes: 9 additions & 9 deletions single-factor-auth-web/sfa-web-ton-telegram-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -241,6 +235,7 @@ function App() {
</button>
</div>
</div>

<div id="console" style={{ whiteSpace: "pre-line" }}>
<p style={{ whiteSpace: "pre-line" }}></p>
</div>
Expand All @@ -249,13 +244,18 @@ function App() {

const logoutView = (
<button onClick={login} className="card">
Login with Telegram
Login
</button>
);

return (
<div className="container">
<h1 className="title">Web3Auth SFA React Telegram Example</h1>
<h1 className="title">
<a target="_blank" href="https://web3auth.io/docs/sdk/core-kit/sfa-web" rel="noreferrer">
Web3Auth
</a>{" "}
SFA React Telegram GitHub Example
</h1>

{isLoggingIn ? <Loading /> : <div className="grid">{web3authSfa ? (loggedIn ? loginView : logoutView) : null}</div>}

Expand Down

0 comments on commit 3e0b139

Please sign in to comment.