-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (105 loc) · 3.26 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const express = require('express');
const speakeasy = require('speakeasy');
const qrcode = require('qrcode');
const jwt = require('jsonwebtoken');
// Porta Server OTP
const PORT = 8085;
// Creazione di un'app Express
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Configurazione segreta per JWT
const jwtSecret = 'il_tuo_segreto';
// Genera la chiave segreta per l'utente
const secret = speakeasy.generateSecret({ length: 20 });
const secretBase32 = secret.base32;
app.get('/', (req, res) => {
// Genera l'URL per il codice QR
const otpUrl = speakeasy.otpauthURL({
secret: secretBase32,
encoding: 'base32',
label: 'JSON SERVER',
issuer: 'Vincenzo Montrone',
});
// Genera il codice QR e restituisci la pagina HTML con il codice QR
qrcode.toDataURL(otpUrl, (err, dataUrl) => {
if (err) {
console.error('Errore nella generazione del codice QR:', err);
res.send('Errore nella generazione del codice QR');
return;
}
const html = `
<html>
<body>
<img src="${dataUrl}" alt="Codice QR">
<form action="/verify" method="POST">
<input type="text" name="otp" placeholder="Inserisci il codice OTP">
<input type="submit" value="Verifica">
</form>
</body>
</html>
`;
res.send(html);
});
});
app.post('/verify', (req, res) => {
// const { token, code } = req.body;
const otp = req.body.otp;
// Verifica il codice OTP
const verified = speakeasy.totp.verify({
secret: secretBase32,
encoding: 'base32',
token: otp,
window: 1 // Controllo del codice valido per 1 step di tempo
});
if (verified) {
// Genera un JWT senza scadenza utilizzando il payload dell'utente
const payload = { id: 0, user: 'vmontro', password: '123' };
const authToken = jwt.sign(otp, jwtSecret);
const html = `
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OTP - JSON SERVER</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
</head>
<body>
<br>
<div class="container is-max-desktop">
<textarea class="textarea" value="${authToken}" disabled></textarea>
<br>
<button class="button is-link is-fullwidth">Copia</button>
</div>
</body>
</html>
`;
res.send(html);
// res.json({ authToken });
} else {
res.status(401).json({ error: 'Codice OTP non valido' });
}
});
// Middleware per verificare l'autenticazione JWT
function authenticateToken(req, res, next) {
const authToken = req.headers.authorization;
if (authToken) {
// Verifica l'autenticità del token JWT
jwt.verify(authToken, jwtSecret, (err, user) => {
if (err) {
res.status(403).json({ error: 'Token JWT non valido' });
} else {
next();
}
});
} else {
res.status(401).json({ error: 'Token JWT mancante' });
}
}
app.get('/protected-route', authenticateToken, (req, res) => {
res.json({ message: 'Hai accesso alla rotta protetta!' });
});
// Avvio del server
app.listen(PORT, () => {
console.log(`Server OTP in ascolto su porta ${PORT}`);
});