Skip to content

Commit 3ad466c

Browse files
committed
chore: protect oauthClient usages
1 parent 3104958 commit 3ad466c

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

server/api/auth/atproto.get.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ export default defineEventHandler(async event => {
8080
// Handle callback
8181
try {
8282
const params = new URLSearchParams(query as Record<string, string>)
83-
const result = await event.context.oauthClient.callback(params)
83+
const result = await event.context.oauthClient?.callback(params)
84+
if (!result) {
85+
return handleApiError('Failed to initiate authentication', {
86+
statusCode: 401,
87+
statusMessage: 'Unauthorized',
88+
message: `Failed to initiate authentication. Please login and try again.`,
89+
})
90+
}
8491
try {
8592
const state = decodeOAuthState(event, result.state)
8693
const profile = await getMiniProfile(result.session)

server/plugins/oauth-client.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import type { NodeOAuthClient } from '@atproto/oauth-client-node'
44
* Creates a long living instance of the NodeOAuthClient.
55
*/
66
export default defineNitroPlugin(async nitroApp => {
7-
const oauthClient = await getNodeOAuthClient()
7+
try {
8+
const oauthClient = await getNodeOAuthClient()
89

9-
// Attach to event context for access in composables via useRequestEvent()
10-
nitroApp.hooks.hook('request', event => {
11-
event.context.oauthClient = oauthClient
12-
})
10+
// Attach to event context for access in composables via useRequestEvent()
11+
nitroApp.hooks.hook('request', event => {
12+
event.context.oauthClient = oauthClient
13+
})
14+
} catch (e) {
15+
if (!import.meta.test) {
16+
throw e
17+
}
18+
}
1319
})
1420

1521
// Extend the H3EventContext type

server/utils/atproto/oauth.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export async function loadJWKs(): Promise<Keyset | undefined> {
9090
// under a new variable and update here
9191
const jwkOne = useRuntimeConfig().oauthJwkOne
9292
if (!jwkOne) {
93-
if (import.meta.test) {
93+
if (!import.meta.test) {
9494
// eslint-disable-next-line no-console
9595
console.error('Failed to load JWKs (not set).')
9696
}
@@ -100,14 +100,8 @@ export async function loadJWKs(): Promise<Keyset | undefined> {
100100
// For multiple keys if we need to rotate
101101
// const keys = await Promise.all([JoseKey.fromImportable(jwkOne)])
102102

103-
try {
104-
const keys = await JoseKey.fromImportable(jwkOne)
105-
return new Keyset([keys])
106-
} catch (e) {
107-
// eslint-disable-next-line no-console
108-
console.error('Failed to load JWKs.', e)
109-
return undefined
110-
}
103+
const keys = await JoseKey.fromImportable(jwkOne)
104+
return new Keyset([keys])
111105
}
112106

113107
async function getOAuthSession(event: H3Event): Promise<{
@@ -123,7 +117,7 @@ async function getOAuthSession(event: H3Event): Promise<{
123117
return { oauthSession: undefined, serverSession }
124118
}
125119

126-
const oauthSession = await event.context.oauthClient.restore(currentSession.public.did)
120+
const oauthSession = await event.context.oauthClient?.restore(currentSession.public.did)
127121
return { oauthSession, serverSession }
128122
} catch (error) {
129123
// Log error safely without using util.inspect on potentially problematic objects

0 commit comments

Comments
 (0)