tsvesync
Version:
A TypeScript library for interacting with VeSync smart home devices
26 lines (25 loc) • 930 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeJwtTimestamps = decodeJwtTimestamps;
/**
* Decode a JWT token payload to extract iat/exp without verifying the signature.
*/
function decodeJwtTimestamps(token) {
try {
const parts = token.split('.');
if (parts.length !== 3)
return null;
const payload = JSON.parse(Buffer.from(parts[1].replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8'));
let iat = typeof payload.iat === 'number' ? payload.iat : undefined;
let exp = typeof payload.exp === 'number' ? payload.exp : undefined;
// Normalize to seconds if values are in milliseconds
if (iat && iat > 1e11)
iat = Math.floor(iat / 1000);
if (exp && exp > 1e11)
exp = Math.floor(exp / 1000);
return { iat, exp };
}
catch (_a) {
return null;
}
}