Lançamento da versão 1.0.10
This commit is contained in:
@@ -402,6 +402,54 @@
|
||||
return new Date(value);
|
||||
}
|
||||
|
||||
function decodeJwtPayload(token) {
|
||||
const normalizedToken = cleanString(token);
|
||||
|
||||
if (
|
||||
!/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(
|
||||
normalizedToken
|
||||
)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const payloadPart = normalizedToken.split(".")[1];
|
||||
const base64 = payloadPart.replace(/-/g, "+").replace(/_/g, "/");
|
||||
const paddedBase64 = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
||||
const json = decodeURIComponent(
|
||||
Array.from(atob(paddedBase64), (char) => {
|
||||
return `%${char.charCodeAt(0).toString(16).padStart(2, "0")}`;
|
||||
}).join("")
|
||||
);
|
||||
|
||||
return JSON.parse(json);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function getJwtExpiresAt(token) {
|
||||
const payload = decodeJwtPayload(token);
|
||||
const expiresAtSeconds = Number(payload?.exp);
|
||||
|
||||
return Number.isFinite(expiresAtSeconds) && expiresAtSeconds > 0
|
||||
? expiresAtSeconds * 1000
|
||||
: 0;
|
||||
}
|
||||
|
||||
function isJwtExpired(token, skewSeconds = 60) {
|
||||
const expiresAt = getJwtExpiresAt(token);
|
||||
|
||||
if (!expiresAt) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const skewMilliseconds = Math.max(Number(skewSeconds) || 0, 0) * 1000;
|
||||
|
||||
return expiresAt <= Date.now() + skewMilliseconds;
|
||||
}
|
||||
|
||||
function getErrorMessage(error) {
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
@@ -456,6 +504,9 @@
|
||||
getOperationalWeekInfo,
|
||||
getCalendarRowWeekDate,
|
||||
parseDatePreservingDateOnly,
|
||||
decodeJwtPayload,
|
||||
getJwtExpiresAt,
|
||||
isJwtExpired,
|
||||
getErrorMessage,
|
||||
escapeHtml,
|
||||
cssEscape,
|
||||
|
||||
Reference in New Issue
Block a user