Skip to content
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

feat: OAuth session expiry #222

Merged
merged 12 commits into from
Sep 26, 2023
Merged

feat: OAuth session expiry #222

merged 12 commits into from
Sep 26, 2023

Conversation

iuioiua
Copy link
Contributor

@iuioiua iuioiua commented Sep 21, 2023

This change adds OAuth session expirations to Deno KV. This will automatically keep the oauth-session KV space clean by expiring entries after 10 minutes, on par with cookie expiration.

Supercedes #170
Closes #148

Comment on lines 48 to 57
Deno.test("setTokens() applies key expiry", async () => {
const sessionId = crypto.randomUUID();
const oauthSession = randomOAuthSession();
await setOAuthSession(sessionId, oauthSession, { expireIn: 1_000 });

assertEquals(await getOAuthSession(sessionId), oauthSession);
await delay(10_000);
assertEquals(await getOAuthSession(sessionId), null);
});

Copy link
Contributor Author

@iuioiua iuioiua Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised this simple test is failing. The same thing happens when using fake time instead of a delay. I've deliberately exaggerated the expiry and delay durations to highlight the issue. Am I missing something, @kt3k and @losfair?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it have to do with this?

Copy link
Contributor

@mitchwadair mitchwadair Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the deno runtime tests, they manually stop/start the DB to trigger immediate cleanup of expired keys. Not sure if that's really viable to do here or not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, @mitchwadair! Yes, database stop/start is not ideal in our case. We run our tests in-memory to avoid the need for cleanup. Though, I guess it wouldn't be too hard to move away from that... @losfair, is in-memory expiration supported?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iuioiua Yes, expiration is handled in the same way for in-memory and persistent databases - but since the cleanup interval is long (~1 minute) it isn't practical to depend on periodic cleanup in a test.

Currently I think the best approach is to open a persistent database and re-open it to trigger an immediate cleanup...

Copy link
Contributor Author

@iuioiua iuioiua Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opted not to test OAuth session expiration as it'd interfere too much with the current KV setup of this module. I've added a small note detailing the reasoning.

@iuioiua iuioiua marked this pull request as ready for review September 26, 2023 02:49
@iuioiua iuioiua merged commit 64e8980 into main Sep 26, 2023
@iuioiua iuioiua deleted the oauth-session-expiry-2 branch September 26, 2023 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose methods to allow running cronjobs against session's expiry time
3 participants