Skip to content

Commit 67e5787

Browse files
committed
Should be state store as well
1 parent ef06c80 commit 67e5787

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

server/utils/atproto/oauth-state-store.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,35 @@ export class OAuthStateStore implements NodeSavedStateStore {
1111
this.serverSession = session
1212
}
1313

14-
private createAKey(did: string, sessionId: string) {
14+
private createStorageKey(did: string, sessionId: string) {
1515
return `state:${did}:${sessionId}`
1616
}
1717

18-
async get(): Promise<NodeSavedState | undefined> {
18+
async get(key: string): Promise<NodeSavedState | undefined> {
1919
const serverSessionData = this.serverSession.data
2020
if (!serverSessionData) return undefined
21-
return serverSessionData.oauthState
21+
if (!serverSessionData.oauthStateId) return undefined
22+
const state = await this.storage.getItem<NodeSavedState>(
23+
this.createStorageKey(key, serverSessionData.oauthStateId),
24+
)
25+
return state ?? undefined
2226
}
2327

2428
async set(key: string, val: NodeSavedState) {
25-
// We are ignoring the key since the mapping is already done in the session
29+
let stateId = crypto.randomUUID()
2630
await this.serverSession.update({
27-
oauthState: val,
31+
oauthStateId: stateId,
2832
})
33+
await this.storage.setItem<NodeSavedState>(this.createStorageKey(key, stateId), val)
2934
}
3035

31-
async del() {
36+
async del(key: string) {
37+
const serverSessionData = this.serverSession.data
38+
if (!serverSessionData) return undefined
39+
if (!serverSessionData.oauthStateId) return undefined
40+
await this.storage.removeItem(this.createStorageKey(key, serverSessionData.oauthStateId))
3241
await this.serverSession.update({
33-
oauthState: undefined,
42+
oauthStateId: undefined,
3443
})
3544
}
3645
}

shared/types/userSession.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { NodeSavedSession, NodeSavedState } from '@atproto/oauth-client-node'
2-
31
export interface UserServerSession {
42
public?:
53
| {
@@ -9,13 +7,7 @@ export interface UserServerSession {
97
avatar?: string
108
}
119
| undefined
12-
// Only to be used in the atproto session and state stores
13-
// Will need to change to Record<string, T> and add a current logged in user if we ever want to support
14-
// multiple did logins per server session
15-
oauthSession?: NodeSavedSession | undefined
16-
oauthState?: NodeSavedState | undefined
17-
// TODO: This todo is a place holder to rememebr to clean this up after this current oauth change
18-
//
19-
// Will most likely be crypto.randomUUID() and the did
10+
// These values are tied to the users browser session and used by atproto OAuth
2011
oauthSessionId?: string | undefined
12+
oauthStateId?: string | undefined
2113
}

0 commit comments

Comments
 (0)