-
-
Notifications
You must be signed in to change notification settings - Fork 272
refactor: moved the oauth to useSession #867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5da9972
19a9a5f
fb34be2
9ef9806
f04e9fc
1462eb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| export default eventHandlerWithOAuthSession(async (event, oAuthSession, serverSession) => { | ||
| await Promise.all([oAuthSession?.signOut(), serverSession.clear()]) | ||
|
|
||
| // Even tho the signOut also clears part of the server cache should be done in order | ||
| // to let the oAuth package do any other clean up it may need | ||
| await oAuthSession?.signOut() | ||
| await serverSession.clear() | ||
|
Comment on lines
+2
to
+5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure server session is cleared even when sign‑out fails. If Proposed fix- await oAuthSession?.signOut()
- await serverSession.clear()
+ try {
+ await oAuthSession?.signOut()
+ } finally {
+ await serverSession.clear()
+ } |
||
| return 'Session cleared' | ||
| }) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,8 @@ | ||||||||||||||||||||||||
| import { UserSessionSchema } from '#shared/schemas/userSession' | ||||||||||||||||||||||||
| import { PublicUserSessionSchema } from '#shared/schemas/publicUserSession' | ||||||||||||||||||||||||
| import { safeParse } from 'valibot' | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export default eventHandlerWithOAuthSession(async (event, oAuthSession, serverSession) => { | ||||||||||||||||||||||||
| const result = safeParse(UserSessionSchema, serverSession.data) | ||||||||||||||||||||||||
| const result = safeParse(PublicUserSessionSchema, serverSession.data.public) | ||||||||||||||||||||||||
|
Comment on lines
+1
to
+5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard against missing session data before validation.
Proposed fix- const result = safeParse(PublicUserSessionSchema, serverSession.data.public)
+ const result = safeParse(PublicUserSessionSchema, serverSession.data?.public)📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||
| if (!result.success) { | ||||||||||||||||||||||||
| return null | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,30 @@ | ||
| import type { NodeSavedSession, NodeSavedSessionStore } from '@atproto/oauth-client-node' | ||
| import type { H3Event } from 'h3' | ||
|
|
||
| /** | ||
| * Storage key prefix for oauth session storage. | ||
| */ | ||
| export const OAUTH_SESSION_CACHE_STORAGE_BASE = 'oauth-atproto-session' | ||
| import type { UserServerSession } from '#shared/types/userSession' | ||
| import type { SessionManager } from 'h3' | ||
|
|
||
| export class OAuthSessionStore implements NodeSavedSessionStore { | ||
| // TODO: not sure if we will support multi accounts, but if we do in the future will need to change this around | ||
| private readonly cookieKey = 'oauth:atproto:session' | ||
| private readonly storage = useStorage(OAUTH_SESSION_CACHE_STORAGE_BASE) | ||
| private readonly session: SessionManager<UserServerSession> | ||
|
|
||
| constructor(private event: H3Event) {} | ||
| constructor(session: SessionManager<UserServerSession>) { | ||
| this.session = session | ||
| } | ||
|
|
||
| async get(): Promise<NodeSavedSession | undefined> { | ||
| const sessionKey = getCookie(this.event, this.cookieKey) | ||
| if (!sessionKey) return | ||
| const result = await this.storage.getItem<NodeSavedSession>(sessionKey) | ||
| if (!result) return | ||
| return result | ||
| const sessionData = this.session.data | ||
| if (!sessionData) return undefined | ||
| return sessionData.oauthSession | ||
| } | ||
|
|
||
| async set(key: string, val: NodeSavedSession) { | ||
| setCookie(this.event, this.cookieKey, key, { | ||
| httpOnly: true, | ||
| secure: !import.meta.dev, | ||
| sameSite: 'lax', | ||
| async set(_key: string, val: NodeSavedSession) { | ||
| // We are ignoring the key since the mapping is already done in the session | ||
| await this.session.update({ | ||
| oauthSession: val, | ||
| }) | ||
| await this.storage.setItem<NodeSavedSession>(key, val) | ||
| } | ||
|
|
||
| async del() { | ||
| const sessionKey = getCookie(this.event, this.cookieKey) | ||
| if (sessionKey) { | ||
| await this.storage.del(sessionKey) | ||
| } | ||
| deleteCookie(this.event, this.cookieKey) | ||
| await this.session.update({ | ||
| oauthSession: undefined, | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,30 @@ | ||
| import type { NodeSavedState, NodeSavedStateStore } from '@atproto/oauth-client-node' | ||
| import type { H3Event } from 'h3' | ||
|
|
||
| /** | ||
| * Storage key prefix for oauth state storage. | ||
| */ | ||
| export const OAUTH_STATE_CACHE_STORAGE_BASE = 'oauth-atproto-state' | ||
| import type { UserServerSession } from '#shared/types/userSession' | ||
| import type { SessionManager } from 'h3' | ||
|
|
||
| export class OAuthStateStore implements NodeSavedStateStore { | ||
| private readonly cookieKey = 'oauth:atproto:state' | ||
| private readonly storage = useStorage(OAUTH_STATE_CACHE_STORAGE_BASE) | ||
| private readonly session: SessionManager<UserServerSession> | ||
|
|
||
| constructor(private event: H3Event) {} | ||
| constructor(session: SessionManager<UserServerSession>) { | ||
| this.session = session | ||
| } | ||
|
|
||
| async get(): Promise<NodeSavedState | undefined> { | ||
| const stateKey = getCookie(this.event, this.cookieKey) | ||
| if (!stateKey) return | ||
| const result = await this.storage.getItem<NodeSavedState>(stateKey) | ||
| if (!result) return | ||
| return result | ||
| const sessionData = this.session.data | ||
| if (!sessionData) return undefined | ||
| return sessionData.oauthState | ||
| } | ||
|
|
||
| async set(key: string, val: NodeSavedState) { | ||
| setCookie(this.event, this.cookieKey, key, { | ||
| httpOnly: true, | ||
| secure: !import.meta.dev, | ||
| sameSite: 'lax', | ||
| async set(_key: string, val: NodeSavedState) { | ||
| // We are ignoring the key since the mapping is already done in the session | ||
| await this.session.update({ | ||
| oauthState: val, | ||
| }) | ||
| await this.storage.setItem<NodeSavedState>(key, val) | ||
| } | ||
|
|
||
| async del() { | ||
| const stateKey = getCookie(this.event, this.cookieKey) | ||
| deleteCookie(this.event, this.cookieKey) | ||
| if (stateKey) { | ||
| await this.storage.del(stateKey) | ||
| } | ||
| await this.session.update({ | ||
| oauthState: undefined, | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import type { H3Event } from 'h3' | ||
| import type { SessionManager } from 'h3' | ||
| import { OAuthStateStore } from './oauth-state-store' | ||
| import { OAuthSessionStore } from './oauth-session-store' | ||
| import type { UserServerSession } from '#shared/types/userSession' | ||
|
|
||
| export const useOAuthStorage = (event: H3Event) => { | ||
| export const useOAuthStorage = (session: SessionManager<UserServerSession>) => { | ||
| return { | ||
| stateStore: new OAuthStateStore(event), | ||
| sessionStore: new OAuthSessionStore(event), | ||
| stateStore: new OAuthStateStore(session), | ||
| sessionStore: new OAuthSessionStore(session), | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // This is for getting the session on the npmx server and differs from the OAuthSession | ||
| import type { H3Event } from 'h3' | ||
| import type { UserServerSession } from '#shared/types/userSession' | ||
|
|
||
| /** | ||
| * Get's the user's session that is stored on the server | ||
| * @param event | ||
| * @returns | ||
| */ | ||
| export const useServerSession = async (event: H3Event) => { | ||
| const config = useRuntimeConfig(event) | ||
|
|
||
| if (!config.sessionPassword) { | ||
| throw new Error('Session password is not configured') | ||
| } | ||
|
|
||
| const serverSession = useSession<UserServerSession>(event, { | ||
| password: config.sessionPassword, | ||
| }) | ||
|
|
||
| return serverSession | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import { object, string, pipe, url } from 'valibot' | ||
| import type { InferOutput } from 'valibot' | ||
|
|
||
| export const UserSessionSchema = object({ | ||
| export const PublicUserSessionSchema = object({ | ||
| // Safe to pass to the frontend | ||
| did: string(), | ||
| handle: string(), | ||
| pds: pipe(string(), url()), | ||
| }) | ||
|
|
||
| export type UserSession = InferOutput<typeof UserSessionSchema> | ||
| export type PublicUserSession = InferOutput<typeof PublicUserSessionSchema> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import type { NodeSavedSession, NodeSavedState } from '@atproto/oauth-client-node' | ||
|
|
||
| export interface UserServerSession { | ||
| public: { | ||
| did: string | ||
| handle: string | ||
| pds: string | ||
| } | ||
| // Only to be used in the atproto session and state stores | ||
| // Will need to change to Record<string, T> and add a current logged in user if we ever want to support | ||
| // multiple did logins per server session | ||
| oauthSession: NodeSavedSession | undefined | ||
| oauthState: NodeSavedState | undefined | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.