Skip to content

fix: Parse Server option extendSessionOnUse not working for session lengths < 24 hours #9113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 55 additions & 0 deletions spec/Auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,58 @@ describe('Auth', () => {
});
});
});

describe('extendSessionOnUse', () => {
const tests = [
{
title: 'Updated more than 24 hrs ago',
sessionLength: 86460,
sessionUpdatedAt: 86410,
result: true,
},
{
title: 'Updated less than 24 hrs ago',
sessionLength: 86460,
sessionUpdatedAt: 86390,
result: false,
},
{
title: 'Update more than an hour ago',
sessionLength: 3660,
sessionUpdatedAt: 3610,
result: true,
},
{
title: 'Updated less than an hour ago',
sessionLength: 3660,
sessionUpdatedAt: 3590,
result: false,
},
{
title: 'Updated more than a minute ago.',
sessionLength: 120,
sessionUpdatedAt: 70,
result: true,
},
{
title: 'Updated less than a minute ago.',
sessionLength: 120,
sessionUpdatedAt: 50,
result: false,
},
];

tests.forEach(({ title, sessionLength, sessionUpdatedAt, result }) => {
it(`shouldUpdateSessionExpiry() when ${title}`, async () => {
const { shouldUpdateSessionExpiry } = require('../lib/Auth');
const update = new Date();
update.setTime(update.getTime() - sessionUpdatedAt * 1000);
const res = shouldUpdateSessionExpiry(
{ sessionLength: sessionLength },
{ updatedAt: update }
);

expect(res).toBe(result);
});
});
});
25 changes: 21 additions & 4 deletions src/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ function nobody(config) {
return new Auth({ config, isMaster: false });
}

/**
* Checks whether session should be updated based on last update time & session length.
*/
function shouldUpdateSessionExpiry(config, session) {
let resetAfter = 60;
if (config.sessionLength > 86400) {
resetAfter = 86400; // Every 24 hours
} else if (config.sessionLength > 3600) {
resetAfter = 3600; // 1 hour
} else {
resetAfter = 60; // Every minute
}

const lastUpdated = new Date(session?.updatedAt);
const skipRange = new Date();
skipRange.setTime(skipRange.getTime() - resetAfter * 1000);
return lastUpdated <= skipRange;
}

const throttle = {};
const renewSessionIfNeeded = async ({ config, session, sessionToken }) => {
if (!config?.extendSessionOnUse) {
Expand All @@ -88,10 +107,7 @@ const renewSessionIfNeeded = async ({ config, session, sessionToken }) => {
const { results } = await query.execute();
session = results[0];
}
const lastUpdated = new Date(session?.updatedAt);
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
if (lastUpdated > yesterday || !session) {
if (!shouldUpdateSessionExpiry(config, session) || !session) {
return;
}
const expiresAt = config.generateSessionExpiresAt();
Expand Down Expand Up @@ -579,6 +595,7 @@ module.exports = {
maintenance,
nobody,
readOnly,
shouldUpdateSessionExpiry,
getAuthForSessionToken,
getAuthForLegacySessionToken,
findUsersWithAuthData,
Expand Down
Loading