Skip to content

Commit 1a678ee

Browse files
committed
moved to server side storage for oauth sessions
1 parent 50decf2 commit 1a678ee

3 files changed

Lines changed: 50 additions & 11 deletions

File tree

nuxt.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ export default defineNuxtConfig({
124124
driver: 'fsLite',
125125
base: './.cache/fetch',
126126
},
127+
'oauth-atproto-state': {
128+
driver: 'fsLite',
129+
base: './.cache/atproto-oauth/state',
130+
},
131+
'oauth-atproto-session': {
132+
driver: 'fsLite',
133+
base: './.cache/atproto-oauth/session',
134+
},
127135
},
128136
},
129137

server/api/auth/atproto.get.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,60 +41,89 @@ export default defineEventHandler(async event => {
4141
const agent = new Agent(authSession)
4242
event.context.agent = agent
4343

44+
//TODO prob do server side kv store here too?
4445
const session = await useSession(event, {
4546
password: process.env.NUXT_SESSION_PASSWORD as string,
4647
})
4748

4849
const response = await fetch(
4950
`https://slingshot.microcosm.blue/xrpc/com.bad-example.identity.resolveMiniDoc?identifier=${agent.did}`,
51+
{ headers: { 'User-Agent': 'npmx' } },
5052
)
5153
const miniDoc = (await response.json()) as { did: string; handle: string; pds: string }
5254

5355
await session.update({
5456
miniDoc,
5557
})
5658

57-
await sessionStore.del()
59+
// await sessionStore.del()
5860

5961
return sendRedirect(event, '/')
6062
})
6163

64+
/**
65+
* Storage key prefix for oauth state storage.
66+
*/
67+
export const OAUTH_STATE_CACHE_STORAGE_BASE = 'oauth-atproto-state'
68+
6269
export class StateStore implements NodeSavedStateStore {
63-
private readonly stateKey = 'oauth:bluesky:stat'
70+
private readonly cookieKey = 'oauth:atproto:state'
71+
private readonly storage = useStorage(OAUTH_STATE_CACHE_STORAGE_BASE)
6472

6573
constructor(private event: H3Event) {}
6674

6775
async get(): Promise<NodeSavedState | undefined> {
68-
const result = getCookie(this.event, this.stateKey)
76+
const stateKey = getCookie(this.event, this.cookieKey)
77+
if (!stateKey) return
78+
const result = await this.storage.getItem<NodeSavedState>(stateKey)
6979
if (!result) return
70-
return JSON.parse(atob(result))
80+
return result
7181
}
7282

7383
async set(key: string, val: NodeSavedState) {
74-
setCookie(this.event, this.stateKey, btoa(JSON.stringify(val)))
84+
setCookie(this.event, this.cookieKey, key)
85+
await this.storage.setItem<NodeSavedState>(key, val)
7586
}
7687

7788
async del() {
78-
deleteCookie(this.event, this.stateKey)
89+
let stateKey = getCookie(this.event, this.cookieKey)
90+
deleteCookie(this.event, this.cookieKey)
91+
if (stateKey) {
92+
await this.storage.del(stateKey)
93+
}
7994
}
8095
}
8196

97+
/**
98+
* Storage key prefix for oauth session storage.
99+
*/
100+
export const OAUTH_SESSION_CACHE_STORAGE_BASE = 'oauth-atproto-session'
101+
82102
export class SessionStore implements NodeSavedSessionStore {
83-
private readonly sessionKey = 'oauth:bluesky:session'
103+
//TODO not sure if we will support multi accounts, but if we do in the future will need to change this around
104+
private readonly cookieKey = 'oauth:atproto:session'
105+
private readonly storage = useStorage(OAUTH_SESSION_CACHE_STORAGE_BASE)
84106

85107
constructor(private event: H3Event) {}
86108

87109
async get(): Promise<NodeSavedSession | undefined> {
88-
const result = getCookie(this.event, this.sessionKey)
110+
const sessionKey = getCookie(this.event, this.cookieKey)
111+
if (!sessionKey) return
112+
let result = await this.storage.getItem<NodeSavedSession>(sessionKey)
89113
if (!result) return
90-
return JSON.parse(atob(result))
114+
return result
91115
}
92116

93117
async set(key: string, val: NodeSavedSession) {
94-
setCookie(this.event, this.sessionKey, btoa(JSON.stringify(val)))
118+
setCookie(this.event, this.cookieKey, key)
119+
await this.storage.setItem<NodeSavedSession>(key, val)
95120
}
96121

97122
async del() {
98-
deleteCookie(this.event, this.sessionKey)
123+
let sessionKey = getCookie(this.event, this.cookieKey)
124+
if (sessionKey) {
125+
await this.storage.del(sessionKey)
126+
}
127+
deleteCookie(this.event, this.cookieKey)
99128
}
100129
}

server/api/auth/session.delete.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export default defineEventHandler(async event => {
33
password: process.env.NUXT_SESSION_PASSWORD as string,
44
})
55

6+
//TODO clear out the oauth agent
7+
68
await session.clear()
79

810
return 'Session cleared'

0 commit comments

Comments
 (0)