mirror of
https://github.com/baserow/baserow.git
synced 2026-12-05 00:38:35 +00:00
* fix: rotate refresh tokens and revoke the session when an old one is reused A refresh token could be replayed to get new access tokens until it expired, so a leaked token gave access for up to seven days. * test: mint the refresh token inside the frozen time The token was minted at the real time, so refreshing at an earlier frozen time failed once the clock passed it. * fix: document the rotated refresh token in the token refresh response The schema checked simplejwt's own rotation setting, which is off, so the API docs left out refresh_token. * fix: keep the user session valid across refresh token rotation The previous release crashed on the new cookie payload, and the cookie stopped working as soon as its refresh token rotated. * test: sign out through the API in the secure file serve test Signing out revokes the session, which is what the user session cookie checks now.
91 lines
2.9 KiB
JavaScript
91 lines
2.9 KiB
JavaScript
import { describe, test, expect, vi, beforeEach } from 'vitest'
|
|
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
|
|
|
|
import authenticationMiddleware from '@baserow/modules/core/middleware/authentication'
|
|
import {
|
|
getTokenIfEnoughTimeLeft,
|
|
setToken,
|
|
setUserSessionCookie,
|
|
} from '@baserow/modules/core/utils/auth'
|
|
|
|
const { store } = vi.hoisted(() => ({ store: { current: null } }))
|
|
// Nuxt itself also calls useNuxtApp, so only the store is swapped.
|
|
mockNuxtImport('useNuxtApp', (original) => () => {
|
|
const nuxtApp = original()
|
|
return store.current ? { ...nuxtApp, $store: store.current } : nuxtApp
|
|
})
|
|
|
|
vi.mock('@baserow/modules/core/utils/auth', async (importOriginal) => ({
|
|
...(await importOriginal()),
|
|
getTokenIfEnoughTimeLeft: vi.fn(),
|
|
setToken: vi.fn(),
|
|
setUserSessionCookie: vi.fn(),
|
|
}))
|
|
|
|
const encode = (value) =>
|
|
btoa(JSON.stringify(value))
|
|
.replace(/=+$/, '')
|
|
.replace(/\+/g, '-')
|
|
.replace(/\//g, '_')
|
|
|
|
const buildToken = (claims) =>
|
|
`${encode({ alg: 'HS256', typ: 'JWT' })}.${encode(claims)}.signature`
|
|
|
|
const buildStore = () => {
|
|
const dispatch = vi.fn().mockResolvedValue()
|
|
store.current = { getters: { 'auth/isAuthenticated': false }, dispatch }
|
|
return { dispatch }
|
|
}
|
|
|
|
describe('authentication middleware', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
test('uses the cookie token when the query token is of the same session', async () => {
|
|
const { dispatch } = buildStore()
|
|
const queryToken = buildToken({ jti: 'first' })
|
|
const cookieToken = buildToken({ jti: 'second', session_id: 'first' })
|
|
getTokenIfEnoughTimeLeft.mockResolvedValue(cookieToken)
|
|
|
|
await authenticationMiddleware({
|
|
query: { token: queryToken, user_session: 'signed' },
|
|
})
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('auth/refresh', cookieToken)
|
|
expect(setToken).not.toHaveBeenCalled()
|
|
expect(setUserSessionCookie).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('uses the query token when it is of another session', async () => {
|
|
const { dispatch } = buildStore()
|
|
const queryToken = buildToken({ jti: 'other' })
|
|
getTokenIfEnoughTimeLeft.mockResolvedValue(
|
|
buildToken({ jti: 'second', session_id: 'first' })
|
|
)
|
|
|
|
await authenticationMiddleware({
|
|
query: { token: queryToken, user_session: 'signed' },
|
|
})
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('auth/refresh', queryToken)
|
|
expect(setToken).toHaveBeenCalledWith(expect.anything(), queryToken)
|
|
expect(setUserSessionCookie).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
'signed'
|
|
)
|
|
})
|
|
|
|
test('uses the cookie token without a query token', async () => {
|
|
const { dispatch } = buildStore()
|
|
const cookieToken = buildToken({ jti: 'first' })
|
|
getTokenIfEnoughTimeLeft.mockResolvedValue(cookieToken)
|
|
|
|
await authenticationMiddleware({ query: {} })
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('auth/refresh', cookieToken)
|
|
expect(setToken).not.toHaveBeenCalled()
|
|
expect(setUserSessionCookie).not.toHaveBeenCalled()
|
|
})
|
|
})
|