Skip to content

Commit 860e8d7

Browse files
committed
Some changes to hopefully help with oauth session
1 parent ee4d95a commit 860e8d7

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

server/api/auth/atproto.get.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useOAuthStorage } from '#server/utils/atproto/storage'
66
import { SLINGSHOT_HOST } from '#shared/utils/constants'
77
import { useServerSession } from '#server/utils/server-session'
88
import type { PublicUserSession } from '#shared/schemas/publicUserSession'
9+
import { handleResolver } from '#server/utils/atproto/oauth'
910

1011
interface ProfileRecord {
1112
avatar?: {
@@ -35,6 +36,7 @@ export default defineEventHandler(async event => {
3536
sessionStore,
3637
clientMetadata,
3738
requestLock: getOAuthLock(),
39+
handleResolver,
3840
})
3941

4042
if (!query.code) {
@@ -98,6 +100,16 @@ export default defineEventHandler(async event => {
98100
avatar,
99101
},
100102
})
103+
} else {
104+
//If slingshot fails we still want to set some key info we need.
105+
const pdsBase = (await authSession.getTokenInfo()).aud
106+
await session.update({
107+
public: {
108+
did: authSession.did,
109+
handle: 'Not available',
110+
pds: pdsBase,
111+
},
112+
})
101113
}
102114

103115
return sendRedirect(event, '/')

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class OAuthSessionStore implements NodeSavedSessionStore {
1717

1818
async set(_key: string, val: NodeSavedSession) {
1919
// We are ignoring the key since the mapping is already done in the session
20+
console.log('oauth session set', val)
2021
await this.session.update({
2122
oauthSession: val,
2223
})

server/utils/atproto/oauth.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OAuthClientMetadataInput, OAuthSession } from '@atproto/oauth-client-node'
22
import type { EventHandlerRequest, H3Event, SessionManager } from 'h3'
3-
import { NodeOAuthClient } from '@atproto/oauth-client-node'
3+
import { NodeOAuthClient, AtprotoDohHandleResolver } from '@atproto/oauth-client-node'
44
import { parse } from 'valibot'
55
import { getOAuthLock } from '#server/utils/atproto/lock'
66
import { useOAuthStorage } from '#server/utils/atproto/storage'
@@ -11,6 +11,13 @@ import { clientUri } from '#oauth/config'
1111
// TODO: If you add writing a new record you will need to add a scope for it
1212
export const scope = `atproto ${LIKES_SCOPE}`
1313

14+
/**
15+
* Resolves a did to a handle via DoH or via the http website calls
16+
*/
17+
export const handleResolver = new AtprotoDohHandleResolver({
18+
dohEndpoint: 'https://cloudflare-dns.com/dns-query',
19+
})
20+
1421
export function getOauthClientMetadata() {
1522
const dev = import.meta.dev
1623

@@ -42,24 +49,32 @@ type EventHandlerWithOAuthSession<T extends EventHandlerRequest, D> = (
4249
serverSession: SessionManager,
4350
) => Promise<D>
4451

45-
async function getOAuthSession(event: H3Event): Promise<OAuthSession | undefined> {
52+
async function getOAuthSession(
53+
event: H3Event,
54+
): Promise<{ oauthSession: OAuthSession | undefined; serverSession: SessionManager }> {
55+
const serverSession = await useServerSession(event)
56+
4657
try {
4758
const clientMetadata = getOauthClientMetadata()
48-
const serverSession = await useServerSession(event)
4959
const { stateStore, sessionStore } = useOAuthStorage(serverSession)
5060

5161
const client = new NodeOAuthClient({
5262
stateStore,
5363
sessionStore,
5464
clientMetadata,
5565
requestLock: getOAuthLock(),
66+
handleResolver,
5667
})
5768

58-
const currentSession = await sessionStore.get()
59-
if (!currentSession) return undefined
69+
const currentSession = serverSession.data
70+
if (!currentSession) {
71+
console.log('oauth session not found')
72+
return { oauthSession: undefined, serverSession }
73+
}
6074

6175
// restore using the subject
62-
return await client.restore(currentSession.tokenSet.sub)
76+
const oauthSession = await client.restore(currentSession.public.did)
77+
return { oauthSession, serverSession }
6378
} catch (error) {
6479
// Log error safely without using util.inspect on potentially problematic objects
6580
// The @atproto library creates error objects with getters that crash Node's util.inspect
@@ -68,7 +83,7 @@ async function getOAuthSession(event: H3Event): Promise<OAuthSession | undefined
6883
'[oauth] Failed to get session:',
6984
error instanceof Error ? error.message : 'Unknown error',
7085
)
71-
return undefined
86+
return { oauthSession: undefined, serverSession }
7287
}
7388
}
7489

@@ -93,9 +108,7 @@ export function eventHandlerWithOAuthSession<T extends EventHandlerRequest, D>(
93108
handler: EventHandlerWithOAuthSession<T, D>,
94109
) {
95110
return defineEventHandler(async event => {
96-
const serverSession = await useServerSession(event)
97-
98-
const oAuthSession = await getOAuthSession(event)
99-
return await handler(event, oAuthSession, serverSession)
111+
const { oauthSession, serverSession } = await getOAuthSession(event)
112+
return await handler(event, oauthSession, serverSession)
100113
})
101114
}

0 commit comments

Comments
 (0)