-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat: extendSessionOnUse
to automatically renew Parse Sessions
#8505
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
04e9f9f
feat: `renewSessions` to automatically renew Parse Sessions
dblythy 05dfdc8
Update Auth.spec.js
dblythy a31b30b
Merge branch 'alpha' into token-expiry
mtrezza dea1334
renew sessions
dblythy 0b4bc98
tests
dblythy 373ce33
defs
dblythy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ import { isDeepStrictEqual } from 'util'; | |
import { getRequestObject, resolveError } from './triggers'; | ||
import Deprecator from './Deprecator/Deprecator'; | ||
import { logger } from './logger'; | ||
import RestQuery from './RestQuery'; | ||
import RestWrite from './RestWrite'; | ||
|
||
// An Auth object tells you who is requesting something and whether | ||
// the master key was used. | ||
|
@@ -66,6 +68,47 @@ function nobody(config) { | |
return new Auth({ config, isMaster: false }); | ||
} | ||
|
||
const throttle = {}; | ||
const renewSessionIfNeeded = async ({ config, session, sessionToken }) => { | ||
if (!config?.extendSessionOnUse) { | ||
return; | ||
} | ||
clearTimeout(throttle[sessionToken]); | ||
throttle[sessionToken] = setTimeout(async () => { | ||
try { | ||
if (!session) { | ||
const { results } = await new RestQuery( | ||
config, | ||
master(config), | ||
'_Session', | ||
{ sessionToken }, | ||
{ limit: 1 } | ||
).execute(); | ||
console.log({ results }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. console.log() here |
||
session = results[0]; | ||
} | ||
const lastUpdated = new Date(session?.updatedAt); | ||
const yesterday = new Date(); | ||
yesterday.setDate(yesterday.getDate() - 1); | ||
if (lastUpdated > yesterday || !session) { | ||
return; | ||
} | ||
const expiresAt = config.generateSessionExpiresAt(); | ||
await new RestWrite( | ||
config, | ||
master(config), | ||
'_Session', | ||
{ objectId: session.objectId }, | ||
{ expiresAt: Parse._encode(expiresAt) } | ||
).execute(); | ||
} catch (e) { | ||
if (e?.code !== Parse.Error.OBJECT_NOT_FOUND) { | ||
logger.error('Could not update session expiry: ', e); | ||
} | ||
} | ||
}, 500); | ||
}; | ||
|
||
// Returns a promise that resolves to an Auth object | ||
const getAuthForSessionToken = async function ({ | ||
config, | ||
|
@@ -78,6 +121,7 @@ const getAuthForSessionToken = async function ({ | |
const userJSON = await cacheController.user.get(sessionToken); | ||
if (userJSON) { | ||
const cachedUser = Parse.Object.fromJSON(userJSON); | ||
renewSessionIfNeeded({ config, sessionToken }); | ||
return Promise.resolve( | ||
new Auth({ | ||
config, | ||
|
@@ -112,18 +156,20 @@ const getAuthForSessionToken = async function ({ | |
if (results.length !== 1 || !results[0]['user']) { | ||
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'Invalid session token'); | ||
} | ||
const session = results[0]; | ||
const now = new Date(), | ||
expiresAt = results[0].expiresAt ? new Date(results[0].expiresAt.iso) : undefined; | ||
expiresAt = session.expiresAt ? new Date(session.expiresAt.iso) : undefined; | ||
if (expiresAt < now) { | ||
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'Session token is expired.'); | ||
} | ||
const obj = results[0]['user']; | ||
const obj = session.user; | ||
delete obj.password; | ||
obj['className'] = '_User'; | ||
obj['sessionToken'] = sessionToken; | ||
if (cacheController) { | ||
cacheController.user.put(sessionToken, obj); | ||
} | ||
renewSessionIfNeeded({ config, session, sessionToken }); | ||
const userObject = Parse.Object.fromJSON(obj); | ||
return new Auth({ | ||
config, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throttle seems to be a simple object never cleared, and it's not an LRU, memory is exposed to DDOS issues (since it seems that every token will be stored in memory), and the memory will grow infinitely
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked also clearTimeout function do not unset in the record on the throttle object. So the timeout instance is cancelled but still there in memory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I’ll open a new issue and fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked also clearTimeout function do not unset in the record on the throttle object. So the timeout instance is cancelled but still there in memory