mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 16:28:31 +00:00
* fix: sign users out of existing sessions when an admin changes their password * fix: keep admins signed in after changing their own password in the admin panel * fix: send other tabs to the login page after a password change instead of crashing * chore: add changelog entry for the password change tab crash fix * fix: send users with two-factor auth to the login page after changing their own password
221 lines
6.7 KiB
JavaScript
221 lines
6.7 KiB
JavaScript
import { isSecureURL } from '@baserow/modules/core/utils/string'
|
|
import { pageFinished } from '@baserow/modules/core/utils/routing'
|
|
import { getCookieName } from '@baserow/modules/core/utils/cookie'
|
|
import { jwtDecode } from 'jwt-decode'
|
|
import tldjs from 'tldjs'
|
|
import { useCookie, useRuntimeConfig, nextTick } from '#imports'
|
|
import {
|
|
USER_SOURCE_COOKIE_TOKEN_NAME,
|
|
REFRESH_TOKEN_MAX_AGE,
|
|
getRefreshTokenCookieOptions,
|
|
} from '@baserow/modules/core/utils/userSourceAuth'
|
|
|
|
const cookieTokenName = 'jwt_token'
|
|
export const userSourceCookieTokenName = USER_SOURCE_COOKIE_TOKEN_NAME
|
|
export const userSessionCookieName = 'user_session'
|
|
|
|
export const setToken = (
|
|
appOrContext,
|
|
token,
|
|
key = cookieTokenName,
|
|
configuration = { sameSite: null }
|
|
) => {
|
|
const { runWithContext } = appOrContext
|
|
return runWithContext(() => {
|
|
const config = useRuntimeConfig()
|
|
const secure =
|
|
configuration.secure ??
|
|
isSecureURL(configuration.cookieUrl || config.public.publicWebFrontendUrl)
|
|
const cookie = useCookie(
|
|
getCookieName(config, key),
|
|
getRefreshTokenCookieOptions({
|
|
path: configuration.path || '/',
|
|
sameSite:
|
|
configuration.sameSite || config.public.baserowFrontendSameSiteCookie,
|
|
secure,
|
|
})
|
|
)
|
|
cookie.value = token
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Sets a session cookie in the browser to store the user's signed session payload upon
|
|
* login. This cookie facilitates backend authentication for GET requests, such as
|
|
* downloading files with the secure_file_serve feature, when the Authorization header
|
|
* is unavailable. The payload includes a token hash to invalidate the cookie upon
|
|
* logout.
|
|
*
|
|
* @param {*} app: the nuxt app instance
|
|
* @param {*} signedUserSession: the signed user session payload to be stored in the
|
|
* cookie
|
|
* @param {*} key: the cookie name
|
|
* @param {*} configuration: the configuration object with the sameSite key
|
|
* @returns
|
|
*/
|
|
export const setUserSessionCookie = (
|
|
appOrContext,
|
|
signedUserSession,
|
|
key = userSessionCookieName,
|
|
configuration = { sameSite: null }
|
|
) => {
|
|
const { runWithContext } = appOrContext
|
|
return runWithContext(() => {
|
|
const config = useRuntimeConfig()
|
|
const secure = isSecureURL(config.public.publicWebFrontendUrl)
|
|
|
|
// To make the cookie available to all subdomains, set the domain to the top-level
|
|
// domain. This is necessary for the secure_file_serve feature to work across
|
|
// subdomains, as when the backend serves files from a different subdomain from the
|
|
// frontend. The top-level domain is extracted from the backend URL.
|
|
// NOTE: For security reasons, it's not possible to set a cookie for a different
|
|
// domain, so this won't work if the frontend and backend are on different domains.
|
|
const topLevelDomain = tldjs.getDomain(config.public.publicBackendUrl)
|
|
|
|
const cookie = useCookie(getCookieName(config, key), {
|
|
path: '/',
|
|
maxAge: REFRESH_TOKEN_MAX_AGE,
|
|
sameSite:
|
|
configuration.sameSite || config.public.baserowFrontendSameSiteCookie,
|
|
secure,
|
|
domain: topLevelDomain,
|
|
})
|
|
cookie.value = signedUserSession
|
|
})
|
|
}
|
|
|
|
export const unsetToken = (
|
|
appOrContext,
|
|
key = cookieTokenName,
|
|
configuration = {}
|
|
) => {
|
|
const { runWithContext } = appOrContext
|
|
return runWithContext(() => {
|
|
const config = useRuntimeConfig()
|
|
const cookie = useCookie(getCookieName(config, key), configuration)
|
|
cookie.value = null
|
|
})
|
|
}
|
|
|
|
export const unsetUserSessionCookie = (
|
|
appOrContext,
|
|
key = userSessionCookieName
|
|
) => {
|
|
const { runWithContext } = appOrContext
|
|
runWithContext(() => {
|
|
const config = useRuntimeConfig()
|
|
const cookie = useCookie(getCookieName(config, key))
|
|
cookie.value = null
|
|
})
|
|
}
|
|
|
|
export const getToken = async (
|
|
appOrContext,
|
|
key = cookieTokenName,
|
|
configuration = {}
|
|
) => {
|
|
const { runWithContext } = appOrContext
|
|
return await runWithContext(() => {
|
|
const config = useRuntimeConfig()
|
|
const cookie = useCookie(getCookieName(config, key), configuration)
|
|
return cookie.value
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the id of the login session a refresh token belongs to. A rotated refresh
|
|
* token carries it in the `session_id` claim, the first one of a login uses its own
|
|
* id.
|
|
*/
|
|
export const getRefreshTokenSessionId = (token) => {
|
|
try {
|
|
const payload = jwtDecode(token)
|
|
return payload.session_id || payload.jti || null
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export const getTokenIfEnoughTimeLeft = async (
|
|
appOrContext,
|
|
key = cookieTokenName,
|
|
configuration = {}
|
|
) => {
|
|
const token = await getToken(appOrContext, key, configuration)
|
|
const now = Math.ceil(new Date().getTime() / 1000)
|
|
|
|
let data
|
|
try {
|
|
data = jwtDecode(token)
|
|
} catch (error) {
|
|
return null
|
|
}
|
|
// Return the token if it is still valid for more of the 10% of the lifespan.
|
|
return data && (data.exp - now) / (data.exp - data.iat) > 0.1 ? token : null
|
|
}
|
|
|
|
/**
|
|
* Signs the current user back in after they changed their own password, which
|
|
* ends all of their sessions. When the account has two-factor authentication,
|
|
* signing in can't finish without the second factor, so the user is sent to the
|
|
* login page with a message instead.
|
|
*
|
|
* @param {object} router The app router, used for the redirect to the login page.
|
|
* @param {object} store The Vuex store.
|
|
* @param {string} password The new password.
|
|
* @returns {Promise<boolean>} Whether the user is still signed in.
|
|
*/
|
|
export const loginAgainAfterPasswordChange = async (
|
|
router,
|
|
store,
|
|
password
|
|
) => {
|
|
const result = await store.dispatch('auth/login', {
|
|
email: store.getters['auth/getUsername'],
|
|
password,
|
|
})
|
|
if (result?.two_factor_auth) {
|
|
await logoutAndRedirectToLogin(router, store, false, true)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
export const logoutAndRedirectToLogin = async (
|
|
router,
|
|
store,
|
|
showSessionExpiredToast = false,
|
|
showPasswordChangedToast = false,
|
|
invalidateToken = false
|
|
) => {
|
|
if (showPasswordChangedToast) {
|
|
await store.dispatch('auth/forceLogoff')
|
|
} else {
|
|
await store.dispatch('auth/logoff', { invalidateToken })
|
|
}
|
|
|
|
await router.push({ name: 'login', query: { noredirect: null } })
|
|
await pageFinished(store.app)
|
|
await nextTick()
|
|
|
|
if (showSessionExpiredToast) {
|
|
store.dispatch('toast/setUserSessionExpired', true)
|
|
} else if (showPasswordChangedToast) {
|
|
store.dispatch('toast/setUserPasswordChanged', true)
|
|
}
|
|
|
|
await store.dispatch('auth/clearAllStoreUserData')
|
|
}
|
|
|
|
/**
|
|
* Remembers who is signed in now. The returned function tells whether that user
|
|
* is still signed in, so that an answer that comes after a logout, or after a
|
|
* login as someone else, can be left out.
|
|
*/
|
|
export const signedInUserCheck = (store) => {
|
|
const userId = store.getters['auth/getUserId']
|
|
return () =>
|
|
store.getters['auth/isAuthenticated'] &&
|
|
store.getters['auth/getUserId'] === userId
|
|
}
|