mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +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.
51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
import {
|
|
getRefreshTokenSessionId,
|
|
getTokenIfEnoughTimeLeft,
|
|
setToken,
|
|
setUserSessionCookie,
|
|
} from '@baserow/modules/core/utils/auth'
|
|
|
|
export default defineNuxtRouteMiddleware(async (to) => {
|
|
const nuxtApp = useNuxtApp()
|
|
const store = nuxtApp.$store
|
|
const event = import.meta.server ? useRequestEvent() : null
|
|
|
|
// If nuxt generate or already authenticated, pass this middleware
|
|
if ((import.meta.server && !event) || store.getters['auth/isAuthenticated']) {
|
|
return
|
|
}
|
|
|
|
// token can be in the query string (SSO) or in the cookies (previous session)
|
|
const cookieToken = await getTokenIfEnoughTimeLeft(nuxtApp)
|
|
let refreshToken = to.query.token
|
|
const sessionId = getRefreshTokenSessionId(refreshToken)
|
|
if (
|
|
sessionId &&
|
|
cookieToken &&
|
|
sessionId === getRefreshTokenSessionId(cookieToken)
|
|
) {
|
|
// Reloading a page that still has the token in its URL. It has been
|
|
// rotated since, and using it again would sign the session off.
|
|
refreshToken = cookieToken
|
|
} else if (refreshToken) {
|
|
await setToken(nuxtApp, refreshToken)
|
|
if (to.query.user_session) {
|
|
await setUserSessionCookie(nuxtApp, to.query.user_session)
|
|
}
|
|
} else {
|
|
refreshToken = cookieToken
|
|
}
|
|
|
|
if (refreshToken) {
|
|
try {
|
|
await store.dispatch('auth/refresh', refreshToken)
|
|
} catch (error) {
|
|
if (error.response?.status === 401) {
|
|
return nuxtApp.runWithContext(() =>
|
|
navigateTo({ name: 'login' }, { external: true })
|
|
) // force browser 302 redirect to get rid of the jwt cookie in the request headers
|
|
}
|
|
}
|
|
}
|
|
})
|