@@ -3,12 +3,18 @@ import type { UserServerSession } from '#shared/types/userSession'
33import type { SessionManager } from 'h3'
44import { OAUTH_CACHE_STORAGE_BASE } from '#server/utils/atproto/storage'
55
6+ // Refresh tokens from a confidential client should last for 180 days, each new refresh of access token resets
7+ // the expiration with the new refresh token. Shorting to 179 days to keep it a bit simpler since we rely on redis to clear sessions
8+ // Note: This expiration only lasts this long in production. Local dev is 2 weeks
9+ const SESSION_EXPIRATION = CACHE_MAX_AGE_ONE_DAY * 179
10+
611export class OAuthSessionStore implements NodeSavedSessionStore {
712 private readonly serverSession : SessionManager < UserServerSession >
8- private readonly storage = useStorage ( OAUTH_CACHE_STORAGE_BASE )
13+ private readonly cache : CacheAdapter
914
1015 constructor ( session : SessionManager < UserServerSession > ) {
1116 this . serverSession = session
17+ this . cache = getCacheAdapter ( OAUTH_CACHE_STORAGE_BASE )
1218 }
1319
1420 private createStorageKey ( did : string , sessionId : string ) {
@@ -23,7 +29,7 @@ export class OAuthSessionStore implements NodeSavedSessionStore {
2329 return undefined
2430 }
2531
26- let session = await this . storage . getItem < NodeSavedSession > (
32+ let session = await this . cache . get < NodeSavedSession > (
2733 this . createStorageKey ( key , serverSessionData . oauthSessionId ) ,
2834 )
2935 return session ?? undefined
@@ -41,7 +47,14 @@ export class OAuthSessionStore implements NodeSavedSessionStore {
4147 sessionId = serverSessionData . oauthSessionId
4248 }
4349 try {
44- await this . storage . setItem < NodeSavedSession > ( this . createStorageKey ( key , sessionId ) , val )
50+ await this . cache . set < NodeSavedSession > (
51+ this . createStorageKey ( key , sessionId ) ,
52+ val ,
53+ SESSION_EXPIRATION ,
54+ )
55+ await this . serverSession . update ( {
56+ lastUpdatedAt : new Date ( ) ,
57+ } )
4558 } catch ( error ) {
4659 // Not sure if this has been happening. But helps with debugging
4760 console . error (
@@ -59,7 +72,7 @@ export class OAuthSessionStore implements NodeSavedSessionStore {
5972 console . warn ( '[oauth session store] No oauthSessionId found in session data' )
6073 return undefined
6174 }
62- await this . storage . removeItem ( this . createStorageKey ( key , serverSessionData . oauthSessionId ) )
75+ await this . cache . delete ( this . createStorageKey ( key , serverSessionData . oauthSessionId ) )
6376 await this . serverSession . update ( {
6477 oauthSessionId : undefined ,
6578 } )
0 commit comments