-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: moving cookie related logic to cookieAdapter.ts (#42)
* feat: moving cookie related logic to cookieAdapter.ts * feat: forcing appSession type upon adapter * feat: add error handling in case public adapter does not handle errors * feat: typo fixes * feat: add PR change request * feat: remove comment * feat: remove yarn * feat: auto install peers * feat: splitting types * feat: fix inferSessionQuery * feat: better test cases * feat: fix expires * feat: change cookieAdapter const to createCookieAdapter function to be able to pass custom sessionKey - minor function extraction for better readability * feat: reorder private functions * bump version --------- Co-authored-by: Eunjae Lee <[email protected]>
- Loading branch information
1 parent
a2aced7
commit 2e0885b
Showing
16 changed files
with
3,034 additions
and
4,429 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@storyblok/app-extension-auth", | ||
"description": "A typed JavaScript library for handling authentication with Storyblok apps.", | ||
"version": "2.0.0-beta.1", | ||
"version": "2.0.0-beta.2", | ||
"author": { | ||
"name": "Johannes Lindgren", | ||
"email": "[email protected]" | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { expireCookie, getCookie, setCookie, verifyData } from '../utils' | ||
import jwt from 'jsonwebtoken' | ||
import { Adapter } from './publicAdapter' | ||
import { isAppSession } from '../session' | ||
|
||
const clientSecret = process.env['CLIENT_SECRET'] || '' | ||
const defaultSessionKey = 'sb.auth' | ||
|
||
type CreateCookieAdapter = (params?: { | ||
sessionKey?: string | undefined | ||
}) => Adapter | ||
|
||
export const createCookieAdapter: CreateCookieAdapter = (params) => { | ||
const key = params?.sessionKey ?? defaultSessionKey | ||
|
||
const adapter: Adapter = { | ||
getSession: ({ req, spaceId, userId }) => { | ||
const cookie = getCookie(req, createScopedKey({ spaceId, userId, key })) | ||
|
||
if (!cookie) { | ||
return undefined | ||
} | ||
|
||
const verifiedData = verifyData(clientSecret, cookie) | ||
|
||
if (!isAppSession(verifiedData)) { | ||
return undefined | ||
} | ||
|
||
return verifiedData | ||
}, | ||
|
||
setSession: ({ res, spaceId, userId, session }) => { | ||
const expires = createExpirationDate(7) | ||
|
||
const signedData = jwt.sign({ data: session }, clientSecret) | ||
|
||
setCookie( | ||
res, | ||
createScopedKey({ spaceId, userId, key }), | ||
signedData, | ||
expires, | ||
) | ||
return true | ||
}, | ||
|
||
hasSession: (params) => { | ||
const session = adapter.getSession(params) | ||
return session !== undefined | ||
}, | ||
|
||
removeSession: ({ res, spaceId, userId }) => { | ||
expireCookie(res, createScopedKey({ spaceId, userId, key })) | ||
return true | ||
}, | ||
} | ||
|
||
return adapter | ||
} | ||
|
||
// We do not use `clientId` in cookie adapter, | ||
// because different plugins will have different domain names, | ||
// and it's enough to differentiate these cookie values. | ||
const createScopedKey = ({ | ||
spaceId, | ||
userId, | ||
key, | ||
}: { | ||
spaceId: string | ||
userId: string | ||
key: string | ||
}) => { | ||
return `${spaceId}:${userId}:${key}` | ||
} | ||
|
||
//TODO: extract to util | ||
const createExpirationDate = (days: number): Date => { | ||
const expires = new Date() | ||
expires.setDate(expires.getDate() + days) | ||
return expires | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './cookieAdapter' | ||
export * from './createCookieAdapter' | ||
export * from './publicAdapter' | ||
export * from './internalAdapter' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,33 @@ | ||
import { IncomingMessage, ServerResponse } from 'node:http' | ||
import { AppSession } from '../session' | ||
|
||
export type MaybePromise<T> = T | Promise<T> | ||
|
||
export type Adapter = { | ||
getItem: (params: { | ||
req: IncomingMessage | ||
res: ServerResponse | ||
clientId: string | ||
spaceId: string | ||
userId: string | ||
key: string | ||
}) => MaybePromise<string | undefined> | ||
getSession: GetSession | ||
setSession: SetSession | ||
removeSession: RemoveSession | ||
hasSession: HasSession | ||
} | ||
|
||
setItem: (params: { | ||
req: IncomingMessage | ||
res: ServerResponse | ||
clientId: string | ||
spaceId: string | ||
userId: string | ||
key: string | ||
value: string | ||
}) => MaybePromise<boolean> | ||
type BaseSessionParams = { | ||
req: IncomingMessage | ||
res: ServerResponse | ||
clientId: string | ||
spaceId: string | ||
userId: string | ||
} | ||
|
||
removeItem: (params: { | ||
req: IncomingMessage | ||
res: ServerResponse | ||
clientId: string | ||
spaceId: string | ||
userId: string | ||
key: string | ||
}) => MaybePromise<boolean> | ||
type GetSession = ( | ||
params: BaseSessionParams, | ||
) => MaybePromise<AppSession | undefined> | ||
|
||
hasItem: (params: { | ||
req: IncomingMessage | ||
res: ServerResponse | ||
clientId: string | ||
spaceId: string | ||
userId: string | ||
key: string | ||
}) => MaybePromise<boolean> | ||
} | ||
type SetSession = ( | ||
params: BaseSessionParams & { | ||
session: AppSession | ||
}, | ||
) => MaybePromise<boolean> | ||
|
||
type RemoveSession = (params: BaseSessionParams) => MaybePromise<boolean> | ||
|
||
type HasSession = (params: BaseSessionParams) => MaybePromise<boolean> |
Oops, something went wrong.