Skip to content

Commit

Permalink
Merge branch 'main' into EXT-1569-preserve-modules
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
#	src/session/refreshAppSession/refreshAppSession.ts
#	src/storyblok-auth-api/handle-requests/handleCallback/spaceIdFromUrl.ts
#	src/storyblok-auth-api/refreshToken.ts
#	src/storyblok-auth-api/user-info/Role/index.ts
#	src/storyblok-auth-api/user-info/UserInfo/UserInfo.ts
#	src/storyblok-auth-api/user-info/UserInfo/isUserInfo.test.ts
#	src/storyblok-auth-api/user-info/UserInfo/isUserInfo.ts
#	tsconfig.json
#	yarn.lock
  • Loading branch information
johannes-lindgren committed May 18, 2023
2 parents 28f405c + d85150f commit b1920d6
Show file tree
Hide file tree
Showing 26 changed files with 753 additions and 1,307 deletions.
23 changes: 9 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,25 @@
"test": "jest"
},
"engines": {
"yarn": "^3.2.4"
"yarn": "^3.2.4",
"node": ">=14.21.3"
},
"packageManager": "[email protected]",
"dependencies": {
"jsonwebtoken": "^8.5.1",
"openid-client": "^5.4.2"
},
"devDependencies": {
"@jest-mock/express": "^1.4.5",
"@rollup/plugin-commonjs": "21.0.1",
"@rollup/plugin-json": "4.1.0",
"@rollup/plugin-node-resolve": "13.1.3",
"@rollup/plugin-typescript": "8.3.2",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.1.1",
"@types/cookie": "^0.5.1",
"@types/cookies": "^0.7.7",
"@types/jest": "^27.4.1",
"@types/jsonwebtoken": "^8.5.8",
"@types/node": "18.16.10",
"@types/node": "14.18.47",
"@typescript-eslint/eslint-plugin": "^5.36.2",
"@typescript-eslint/parser": "^5.36.2",
"dotenv": "^16.0.0",
Expand All @@ -60,26 +63,18 @@
"eslint-plugin-prettier": "^4.2.1",
"jest": "^27.5.1",
"jest-express": "^1.12.0",
"jsonwebtoken": "^9.0.0",
"node-mocks-http": "1.12.1",
"openid-client": "^5.4.2",
"prettier": "^2.7.1",
"require": "^2.4.20",
"rollup": "^2.78.0",
"rollup-plugin-dts": "4.2.1",
"rollup-plugin-peer-deps-external": "2.2.4",
"rollup-plugin-rename": "^1.0.1",
"rollup-plugin-summary": "^1.4.3",
"rollup-plugin-typescript2": "^0.31.1",
"rollup-plugin-visualizer": "^5.6.0",
"ts-jest": "^27.1.4",
"tsutils": "^3.21.0",
"typescript": "^4.8.3",
"vite": "4.2.1",
"vite-plugin-dts": "1.7.1"
},
"peerDependencies": {
"jsonwebtoken": "^9.0.0",
"node": "^18.0.0",
"openid-client": "^5.4.2"
"typescript": "^4.8.3"
}
}
51 changes: 51 additions & 0 deletions src/session/crud/AppSessionCookiePayload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
AppSessionCookiePayload,
isAppSessionCookiePayload,
} from './AppSessionCookiePayload'

const stub: AppSessionCookiePayload = {
sessions: [
{
spaceId: 123,
userId: 12345,
appClientId: '094uif90e',
expiresAt: Date.now(),
region: 'eu',
accessToken: 'abctoken123',
roles: ['admin'],
userName: 'Johannes Lindgren',
spaceName: 'My Space',
refreshToken: '98rejf984ej89crejf98je',
},
],
}

describe('isAppSessionCookiePayload', () => {
describe('validation', () => {
it('is an object with a property "session"', () => {
expect(isAppSessionCookiePayload({})).toEqual(false)
expect(isAppSessionCookiePayload(stub)).toEqual(true)
})
it('The property "session" is an array', () => {
expect(
isAppSessionCookiePayload({
sessions: {},
}),
).toEqual(false)
expect(isAppSessionCookiePayload(stub)).toEqual(true)
})
it('The property "session" is an array of AppSession', () => {
expect(
isAppSessionCookiePayload({
sessions: [],
}),
).toEqual(true)
expect(isAppSessionCookiePayload(stub)).toEqual(true)
expect(
isAppSessionCookiePayload({
sessions: [{ not: 'aSession' }],
}),
).toEqual(false)
})
})
})
13 changes: 13 additions & 0 deletions src/session/crud/AppSessionCookiePayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { hasKey } from '../../utils'
import { AppSession, isAppSession } from '../types'

export type AppSessionCookiePayload = {
sessions: AppSession[]
}

export const isAppSessionCookiePayload = (
data: unknown,
): data is AppSessionCookiePayload =>
hasKey(data, 'sessions') &&
Array.isArray(data.sessions) &&
data.sessions.every(isAppSession)
18 changes: 9 additions & 9 deletions src/session/crud/getAllSessions.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { AuthHandlerParams } from '../../storyblok-auth-api'
import { AppSession } from '../types'
import { getSignedCookie, GetCookie } from '../../utils'
import { GetCookie, getSignedCookie } from '../../utils'
import { authCookieName } from '../authCookieName'
import { isAppSessionCookiePayload } from './AppSessionCookiePayload'

export type AppSessionCookiePayload =
| {
sessions: AppSession[]
}
| undefined
export type GetAllSessionsParams = Pick<
AuthHandlerParams,
'clientSecret' | 'cookieName' | 'clientId'
>

export type GetAllSessions = (
params: GetAllSessionsParams,
getCookie: GetCookie,
) => AppSession[]

export const getAllSessions: GetAllSessions = (params, getCookie) => {
const signedCookie = getSignedCookie(
params.clientSecret,
getCookie,
authCookieName(params),
) as AppSessionCookiePayload
// TODO validate at runtime
return signedCookie?.sessions ?? []
)
if (!isAppSessionCookiePayload(signedCookie)) {
return []
}
return signedCookie.sessions
}
7 changes: 5 additions & 2 deletions src/session/refreshAppSession/refreshAppSession.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { refreshAppSession, RefreshToken } from './refreshAppSession'
import { AppSession } from '../types'
import { RefreshToken } from '../../storyblok-auth-api/refreshToken'
import { refreshAppSession } from './refreshAppSession'

const validRefreshToken = 'abc123abc123'
const invalidRefreshToken = 'oo0o0o0o0o0o0o0o0oo'
Expand All @@ -9,7 +10,7 @@ const refresh: RefreshToken = async (refreshToken) =>
access_token: 'new-access-token',
expires_in: 899, // seconds
}
: undefined
: new Error('Invalid refresh token')

const oldValidSession: AppSession = {
refreshToken: validRefreshToken,
Expand All @@ -21,6 +22,7 @@ const oldValidSession: AppSession = {
appClientId: '123',
userName: 'Johannes',
accessToken: 'old-token',
region: 'eu',
}

const invalidSession: AppSession = {
Expand All @@ -33,6 +35,7 @@ const invalidSession: AppSession = {
appClientId: '123',
userName: 'Johannes',
accessToken: 'old-token',
region: 'us',
}

describe('refreshAppSession', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/session/refreshAppSession/refreshAppSession.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppSession } from '../types'
import { RefreshToken } from '@src/storyblok-auth-api'
import { RefreshToken } from '../../storyblok-auth-api/refreshToken'

/**
* Returns a new session that is refreshed
Expand Down
1 change: 1 addition & 0 deletions src/session/shouldRefresh/shouldRefresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const session: Omit<AppSession, 'expiresAt'> = {
roles: [],
userName: 'admin',
spaceName: 'MySpace',
region: 'eu',
}

describe('shouldRefresh', () => {
Expand Down
Loading

0 comments on commit b1920d6

Please sign in to comment.