mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
api/jwt: fix timestamp to match the spec
This commit is contained in:
parent
580ca042f3
commit
9592e59f76
2 changed files with 4 additions and 4 deletions
|
@ -12,7 +12,7 @@ const makeHmac = (header, payload) =>
|
||||||
.digest("base64url");
|
.digest("base64url");
|
||||||
|
|
||||||
export const generate = () => {
|
export const generate = () => {
|
||||||
const exp = new Date().getTime() + env.jwtLifetime * 1000;
|
const exp = Math.floor(new Date().getTime() / 1000) + env.jwtLifetime;
|
||||||
|
|
||||||
const header = toBase64URL(JSON.stringify({
|
const header = toBase64URL(JSON.stringify({
|
||||||
alg: "HS256",
|
alg: "HS256",
|
||||||
|
@ -20,7 +20,7 @@ export const generate = () => {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const payload = toBase64URL(JSON.stringify({
|
const payload = toBase64URL(JSON.stringify({
|
||||||
jti: nanoid(3),
|
jti: nanoid(8),
|
||||||
exp,
|
exp,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ export const generate = () => {
|
||||||
|
|
||||||
export const verify = (jwt) => {
|
export const verify = (jwt) => {
|
||||||
const [header, payload, signature] = jwt.split(".", 3);
|
const [header, payload, signature] = jwt.split(".", 3);
|
||||||
const timestamp = new Date().getTime();
|
const timestamp = Math.floor(new Date().getTime() / 1000);
|
||||||
|
|
||||||
if ([header, payload, signature].join('.') !== jwt) {
|
if ([header, payload, signature].join('.') !== jwt) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -42,7 +42,7 @@ export const requestSession = async() => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getSession = async () => {
|
export const getSession = async () => {
|
||||||
const currentTime = new Date().getTime();
|
const currentTime = Math.floor(new Date().getTime() / 1000);
|
||||||
const cache = get(cachedSession);
|
const cache = get(cachedSession);
|
||||||
|
|
||||||
if (cache?.token && cache?.exp > currentTime) {
|
if (cache?.token && cache?.exp > currentTime) {
|
||||||
|
|
Loading…
Reference in a new issue