-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (55 loc) · 2.13 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
const express = require('express');
const jwt = require('jsonwebtoken');
const app = express();
app.use(express.json());
// Endpoint to verify JWT value
app.post('/verifyToken', (req, res) => {
const { token, key, period } = req.body;
// Check if all fields are provided
if (!token || !key || period === undefined) {
return res.status(400).json({ error: 'Token, key, and period are required' });
}
try {
// Decode the JWT without verifying the signature
const decoded = jwt.decode(token);
// If the key doesn't exist in the decoded JWT
if (!decoded || decoded[key] === undefined) {
return res.status(404).json({ error: `Key "${key}" not found in token` });
}
// Ensure the period is a valid number, regardless of its input type
const periodValue = parseFloat(period);
if (isNaN(periodValue)) {
return res.status(400).json({ error: 'Invalid period value. Must be a valid number.' });
}
// Check if decoded[key] is a number (i.e., a timestamp)
const keyValue = parseFloat(decoded[key]);
if (isNaN(keyValue)) {
return res.status(400).json({ error: `Value for key "${key}" in token is not a valid number.` });
}
// Current time in seconds
const now = Math.floor(Date.now() / 1000);
// Compare the key's value with the current time + period
const comparisonValue = now + periodValue;
if (keyValue > comparisonValue) {
return res.json({ result: 'GREATER' });
} else if (keyValue === comparisonValue) {
return res.json({ result: 'EQUAL' });
} else {
return res.json({ result: 'LESSER' });
}
} catch (err) {
return res.status(400).json({ error: 'Invalid token' });
}
});
// Health check endpoint
app.get('/health', (req, res) => {
res.status(200).json({ status: 'UP' });
});
app.get('/', (req, res) => {
res.status(200).json({ status: 'UP' });
});
// Start the server
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});