Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions backend/src/middleware/jwtAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const jwt = require('jsonwebtoken');

/**
* JWT authentication middleware.
* Validates the Authorization: Bearer <token> header using the secret
* from the JWT_SECRET environment variable.
* On success attaches the decoded payload (wallet, role, exp, etc.) to req.user.
*/
function jwtAuthMiddleware(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Missing Authorization header' });
}

const token = authHeader.slice(7).trim();
const secret = process.env.JWT_SECRET;
if (!secret) {
// Configuration error – treat as server error but do not expose secret
return res.status(500).json({ error: 'JWT secret not configured' });
}

try {
const decoded = jwt.verify(token, secret);
// Attach only relevant fields (wallet, role, exp) to req.user
const { wallet, role, exp } = decoded;
req.user = { wallet, role, exp };
next();
} catch (err) {
// jwt.verify throws for invalid signature, expired token, etc.
return res.status(401).json({ error: 'Invalid or expired token' });
}
}

module.exports = jwtAuthMiddleware;
5 changes: 5 additions & 0 deletions backend/src/routes/vaccination.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ router.post(
result: 'success',
meta: { token_id: result.tokenId, vaccine_name, date_administered, dose_number, dose_series },
});
// Invalidate verification cache for this wallet
const verifyRouter = require('../routes/verify');
if (verifyRouter.verifyCache) {
verifyRouter.verifyCache.delete(patient_address);
}

res.json({
success: true,
Expand Down
11 changes: 11 additions & 0 deletions backend/src/routes/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const router = express.Router();

const verifyCache = new Map();
const CACHE_TTL = 60 * 1000; // 60 seconds
const MAX_CACHE_SIZE = 1000; // maximum entries to bound memory

/**
* Try JWT first; if no Authorization header, fall through to API key auth.
Expand Down Expand Up @@ -108,6 +109,11 @@ router.get(
records,
timestamp: now
});
// Evict oldest entry if cache exceeds max size
if (verifyCache.size > MAX_CACHE_SIZE) {
const oldestKey = verifyCache.keys().next().value;
verifyCache.delete(oldestKey);
}

audit({
actor: ip,
Expand Down Expand Up @@ -217,6 +223,11 @@ router.get(
records,
timestamp: now
});
// Evict oldest entry if cache exceeds max size
if (verifyCache.size > MAX_CACHE_SIZE) {
const oldestKey = verifyCache.keys().next().value;
verifyCache.delete(oldestKey);
}

audit({
actor,
Expand Down