* fix(web): [OCISDEV-1518] show correct message when Safe access is denied for lacking permission * chore: changelog
787 lines
29 KiB
TypeScript
787 lines
29 KiB
TypeScript
import {
|
|
CapabilityStore,
|
|
ConfigStore,
|
|
useAuthStore,
|
|
useConfigStore,
|
|
useMessages
|
|
} from '@ownclouders/web-pkg'
|
|
import { mock } from 'vitest-mock-extended'
|
|
import { Router } from 'vue-router'
|
|
import { Ability } from '@ownclouders/web-client'
|
|
import { AuthService } from '../../../../src/services/auth/authService'
|
|
import { vaultStepUpFailedKey } from '../../../../src/helpers/vaultStepUp'
|
|
import { UserManager } from '../../../../src/services/auth/userManager'
|
|
import { RouteLocation, createRouter, createTestingPinia } from '@ownclouders/web-test-helpers'
|
|
import { User } from 'oidc-client-ts'
|
|
import { Language } from 'vue3-gettext'
|
|
|
|
const mockUpdateContext = vi.fn()
|
|
console.debug = vi.fn()
|
|
|
|
vi.mock('../../../../src/services/auth/userManager')
|
|
|
|
const initAuthService = ({
|
|
authService,
|
|
configStore = null,
|
|
router = null
|
|
}: {
|
|
authService: AuthService
|
|
configStore?: ConfigStore
|
|
router?: Router
|
|
}) => {
|
|
createTestingPinia()
|
|
const authStore = useAuthStore()
|
|
configStore = configStore || useConfigStore()
|
|
|
|
authService.initialize(configStore, null, router, null, null, null, authStore, null, null)
|
|
}
|
|
|
|
const stepUpRequest = (target: string) => ({
|
|
acr_values: 'advanced',
|
|
state: { mfaStepUpTarget: target }
|
|
})
|
|
|
|
describe('AuthService', () => {
|
|
describe('signInCallback', () => {
|
|
it.each([
|
|
['/', '/', {}],
|
|
['/?details=sharing', '/', { details: 'sharing' }],
|
|
[
|
|
'/external?contextRouteName=files-spaces-personal&fileId=0f897576',
|
|
'/external',
|
|
{
|
|
contextRouteName: 'files-spaces-personal',
|
|
fileId: '0f897576'
|
|
}
|
|
]
|
|
])(
|
|
'parses query params and passes them explicitly to router.replace: %s => %s %s',
|
|
async (url, path, query: Record<string, string>) => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: {
|
|
signinRedirectCallback: vi.fn(),
|
|
getUser: vi.fn().mockResolvedValue(null),
|
|
getAndClearPostLoginRedirectUrl: () => url
|
|
}
|
|
})
|
|
|
|
const router = createRouter()
|
|
const replaceSpy = vi.spyOn(router, 'replace')
|
|
|
|
initAuthService({ authService, router })
|
|
await authService.signInCallback()
|
|
|
|
expect(replaceSpy).toHaveBeenCalledWith({
|
|
path,
|
|
query
|
|
})
|
|
}
|
|
)
|
|
})
|
|
|
|
describe('initializeContext', () => {
|
|
it('when embed mode is disabled and access_token is present, should call updateContext', async () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getAccessToken: vi.fn().mockResolvedValue('access-token'),
|
|
getUser: vi.fn().mockResolvedValue(mock<User>({ expires_in: 3600 })),
|
|
updateContext: mockUpdateContext
|
|
})
|
|
})
|
|
|
|
initAuthService({ authService })
|
|
|
|
await authService.initializeContext(mock<RouteLocation>({}))
|
|
|
|
expect(mockUpdateContext).toHaveBeenCalledWith('access-token', true)
|
|
})
|
|
|
|
it('when embed mode is disabled and access_token is not present, should not call updateContext', async () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getAccessToken: vi.fn().mockResolvedValue(null),
|
|
updateContext: mockUpdateContext
|
|
})
|
|
})
|
|
|
|
initAuthService({ authService })
|
|
|
|
await authService.initializeContext(mock<RouteLocation>({}))
|
|
|
|
expect(mockUpdateContext).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('when embed mode is enabled, access_token is present but auth is not delegated, should call updateContext', async () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getAccessToken: vi.fn().mockResolvedValue('access-token'),
|
|
getUser: vi.fn().mockResolvedValue(mock<User>({ expires_in: 3600 })),
|
|
updateContext: mockUpdateContext
|
|
})
|
|
})
|
|
|
|
initAuthService({ authService })
|
|
|
|
await authService.initializeContext(mock<RouteLocation>({}))
|
|
|
|
expect(mockUpdateContext).toHaveBeenCalledWith('access-token', true)
|
|
})
|
|
|
|
it('when embed mode is enabled, access_token is present and auth is delegated, should not call updateContext', async () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getAccessToken: vi.fn().mockResolvedValue('access-token'),
|
|
updateContext: mockUpdateContext
|
|
})
|
|
})
|
|
|
|
const configStore = useConfigStore()
|
|
configStore.options = { embed: { enabled: true, delegateAuthentication: true } }
|
|
initAuthService({ authService, configStore })
|
|
|
|
await authService.initializeContext(mock<RouteLocation>({}))
|
|
|
|
expect(mockUpdateContext).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('when embed mode is disabled, access_token is present and auth is delegated, should call updateContext', async () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getAccessToken: vi.fn().mockResolvedValue('access-token'),
|
|
getUser: vi.fn().mockResolvedValue(mock<User>({ expires_in: 3600 })),
|
|
updateContext: mockUpdateContext
|
|
})
|
|
})
|
|
|
|
initAuthService({ authService })
|
|
|
|
await authService.initializeContext(mock<RouteLocation>({}))
|
|
|
|
expect(mockUpdateContext).toHaveBeenCalledWith('access-token', true)
|
|
})
|
|
})
|
|
|
|
describe('handleDelegatedTokenUpdate', () => {
|
|
const buildMessageEvent = (origin: string, accessToken = 'attacker-token') =>
|
|
mock<MessageEvent>({
|
|
origin,
|
|
data: { name: 'owncloud-embed:update-token', data: { access_token: accessToken } }
|
|
})
|
|
|
|
it('when delegateAuthenticationOrigin is not configured, should reject the message regardless of its origin', () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({ updateContext: mockUpdateContext })
|
|
})
|
|
|
|
const configStore = useConfigStore()
|
|
configStore.options = { embed: { enabled: true, delegateAuthentication: true } }
|
|
initAuthService({ authService, configStore })
|
|
;(authService as any).handleDelegatedTokenUpdate(
|
|
buildMessageEvent('https://attacker.example')
|
|
)
|
|
|
|
expect(mockUpdateContext).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('when the message origin does not match the configured delegateAuthenticationOrigin, should reject the message', () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({ updateContext: mockUpdateContext })
|
|
})
|
|
|
|
const configStore = useConfigStore()
|
|
configStore.options = {
|
|
embed: {
|
|
enabled: true,
|
|
delegateAuthentication: true,
|
|
delegateAuthenticationOrigin: 'https://trusted.example'
|
|
}
|
|
}
|
|
initAuthService({ authService, configStore })
|
|
;(authService as any).handleDelegatedTokenUpdate(
|
|
buildMessageEvent('https://attacker.example')
|
|
)
|
|
|
|
expect(mockUpdateContext).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('when the message origin matches the configured delegateAuthenticationOrigin, should update the context', () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({ updateContext: mockUpdateContext })
|
|
})
|
|
|
|
const configStore = useConfigStore()
|
|
configStore.options = {
|
|
embed: {
|
|
enabled: true,
|
|
delegateAuthentication: true,
|
|
delegateAuthenticationOrigin: 'https://trusted.example'
|
|
}
|
|
}
|
|
initAuthService({ authService, configStore })
|
|
;(authService as any).handleDelegatedTokenUpdate(
|
|
buildMessageEvent('https://trusted.example', 'renewed-token')
|
|
)
|
|
|
|
expect(mockUpdateContext).toHaveBeenCalledWith('renewed-token', false)
|
|
})
|
|
|
|
describe('when dispatched through the window message listener', () => {
|
|
let authService: AuthService
|
|
|
|
const signInDelegated = async () => {
|
|
authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getUser: vi.fn().mockResolvedValue(null),
|
|
getAndClearPostLoginRedirectUrl: vi.fn().mockReturnValue('/'),
|
|
updateContext: mockUpdateContext
|
|
})
|
|
})
|
|
|
|
const configStore = useConfigStore()
|
|
configStore.options = {
|
|
embed: {
|
|
enabled: true,
|
|
delegateAuthentication: true,
|
|
delegateAuthenticationOrigin: 'https://host.example.org'
|
|
}
|
|
}
|
|
initAuthService({ authService, configStore, router: createRouter() })
|
|
|
|
await authService.signInCallback('initial-token')
|
|
mockUpdateContext.mockClear()
|
|
}
|
|
|
|
afterEach(() => {
|
|
window.removeEventListener(
|
|
'message',
|
|
(authService as any).handleDelegatedTokenUpdate as EventListener
|
|
)
|
|
})
|
|
|
|
it('ignores "owncloud-embed:update-token" messages from unexpected origins', async () => {
|
|
await signInDelegated()
|
|
|
|
window.dispatchEvent(
|
|
new MessageEvent('message', {
|
|
data: { name: 'owncloud-embed:update-token', data: { access_token: 'renewed-token' } },
|
|
origin: 'https://attacker.example.org'
|
|
})
|
|
)
|
|
|
|
expect(mockUpdateContext).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('ignores "owncloud-embed:update-token" messages without an access token', async () => {
|
|
await signInDelegated()
|
|
|
|
window.dispatchEvent(
|
|
new MessageEvent('message', {
|
|
data: { name: 'owncloud-embed:update-token', data: {} },
|
|
origin: 'https://host.example.org'
|
|
})
|
|
)
|
|
|
|
expect(mockUpdateContext).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('updates the user context with the access token from "owncloud-embed:update-token" messages', async () => {
|
|
await signInDelegated()
|
|
|
|
window.dispatchEvent(
|
|
new MessageEvent('message', {
|
|
data: { name: 'owncloud-embed:update-token', data: { access_token: 'renewed-token' } },
|
|
origin: 'https://host.example.org'
|
|
})
|
|
)
|
|
|
|
expect(mockUpdateContext).toHaveBeenCalledWith('renewed-token', false)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('acr', () => {
|
|
const mockSignInRedirect = vi.fn()
|
|
|
|
beforeEach(() => {
|
|
sessionStorage.clear()
|
|
})
|
|
|
|
it('when user is not authenticated, should redirect to login page', async () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getUser: vi.fn().mockResolvedValue(null),
|
|
signinRedirect: mockSignInRedirect
|
|
})
|
|
})
|
|
|
|
await authService.requireAcr('advanced', '/')
|
|
expect(mockSignInRedirect).toHaveBeenCalledWith(
|
|
expect.objectContaining({ acr_values: 'advanced' })
|
|
)
|
|
})
|
|
|
|
it('when user is authenticated and acr is not the one required, should redirect to login page', async () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getUser: vi
|
|
.fn()
|
|
.mockResolvedValue(mock<User>({ profile: { acr: 'regular' }, expired: false })),
|
|
signinRedirect: mockSignInRedirect
|
|
})
|
|
})
|
|
|
|
await authService.requireAcr('advanced', '/')
|
|
expect(mockSignInRedirect).toHaveBeenCalledWith(
|
|
expect.objectContaining({ acr_values: 'advanced' })
|
|
)
|
|
})
|
|
|
|
it('when user is authenticated and acr is the one required but access token is expired, should redirect to login page', async () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getUser: vi
|
|
.fn()
|
|
.mockResolvedValue(mock<User>({ profile: { acr: 'advanced' }, expired: true })),
|
|
signinRedirect: mockSignInRedirect
|
|
})
|
|
})
|
|
|
|
await authService.requireAcr('advanced', '/')
|
|
expect(mockSignInRedirect).toHaveBeenCalledWith(
|
|
expect.objectContaining({ acr_values: 'advanced' })
|
|
)
|
|
})
|
|
|
|
it('when user is authenticated and acr is the one required, should not redirect to login page', async () => {
|
|
const authService = new AuthService()
|
|
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getUser: vi
|
|
.fn()
|
|
.mockResolvedValue(mock<User>({ profile: { acr: 'advanced' }, expired: false })),
|
|
signinRedirect: mockSignInRedirect
|
|
})
|
|
})
|
|
|
|
await authService.requireAcr('advanced', '/')
|
|
expect(mockSignInRedirect).not.toHaveBeenCalled()
|
|
})
|
|
|
|
describe('MFA step-up loop protection', () => {
|
|
const adminRoute = '/admin-settings/users'
|
|
|
|
const setupRequireAcr = (user: User) => {
|
|
const signinRedirect = vi.fn()
|
|
const authService = new AuthService()
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getUser: vi.fn().mockResolvedValue(user),
|
|
signinRedirect,
|
|
setPostLoginRedirectUrl: vi.fn()
|
|
})
|
|
})
|
|
Object.defineProperty(authService, 'language', {
|
|
value: {
|
|
$gettext: (msg: string) => msg,
|
|
$pgettext: (_context: string, msg: string) => msg
|
|
}
|
|
})
|
|
return { authService, signinRedirect }
|
|
}
|
|
|
|
// the route guard runs `initializeContext` before `requireAcr`, which marks the
|
|
// navigation right after the sign-in callback as the IdP's answer for `target`
|
|
const answeredStepUpFor = (authService: AuthService, target: string) => {
|
|
;(authService as any).stepUpReturnTarget = target
|
|
}
|
|
|
|
const regularUser = () => mock<User>({ profile: { acr: 'regular' }, expired: false })
|
|
|
|
it('requests the step-up with the target as OIDC state', async () => {
|
|
const { authService, signinRedirect } = setupRequireAcr(regularUser())
|
|
|
|
const result = await authService.requireAcr('advanced', adminRoute)
|
|
|
|
expect(result).toBe(true)
|
|
expect(signinRedirect).toHaveBeenCalledWith(stepUpRequest(adminRoute))
|
|
})
|
|
|
|
it('denies access when the IdP answered the step-up without the required acr', async () => {
|
|
const { authService, signinRedirect } = setupRequireAcr(regularUser())
|
|
answeredStepUpFor(authService, adminRoute)
|
|
|
|
const result = await authService.requireAcr('advanced', adminRoute)
|
|
|
|
expect(result).toBe(false)
|
|
expect(signinRedirect).not.toHaveBeenCalled()
|
|
expect(useMessages().showErrorMessage).toHaveBeenCalled()
|
|
})
|
|
|
|
it('redirects again when the user came back without the IdP answering (e.g. browser back)', async () => {
|
|
const { authService, signinRedirect } = setupRequireAcr(regularUser())
|
|
|
|
await authService.requireAcr('advanced', adminRoute)
|
|
const result = await authService.requireAcr('advanced', adminRoute)
|
|
|
|
expect(result).toBe(true)
|
|
expect(signinRedirect).toHaveBeenCalledTimes(2)
|
|
expect(useMessages().showErrorMessage).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('redirects when the IdP answered a step-up for another target', async () => {
|
|
const { authService, signinRedirect } = setupRequireAcr(regularUser())
|
|
answeredStepUpFor(authService, '/vault')
|
|
|
|
const result = await authService.requireAcr('advanced', adminRoute)
|
|
|
|
expect(result).toBe(true)
|
|
expect(signinRedirect).toHaveBeenCalledWith(stepUpRequest(adminRoute))
|
|
})
|
|
|
|
it('redirects an expired user even when the IdP just answered', async () => {
|
|
const { authService, signinRedirect } = setupRequireAcr(
|
|
mock<User>({ profile: { acr: 'regular' }, expired: true })
|
|
)
|
|
answeredStepUpFor(authService, adminRoute)
|
|
|
|
const result = await authService.requireAcr('advanced', adminRoute)
|
|
|
|
expect(result).toBe(true)
|
|
expect(signinRedirect).toHaveBeenCalledWith(stepUpRequest(adminRoute))
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('vault scope', () => {
|
|
const vaultRoute = {
|
|
params: { scope: 'vault' },
|
|
fullPath: '/vault'
|
|
} as unknown as RouteLocation
|
|
|
|
const setupVaultAuthService = ({
|
|
canAccessVault,
|
|
userContextReady,
|
|
getUser
|
|
}: {
|
|
canAccessVault: boolean
|
|
userContextReady: boolean
|
|
getUser: () => Promise<User>
|
|
}) => {
|
|
const authService = new AuthService()
|
|
const mockSignInRedirect = vi.fn()
|
|
|
|
const mockLoadUserAbilities = vi.fn().mockResolvedValue(undefined)
|
|
const mockUpdateContext = vi.fn().mockResolvedValue(undefined)
|
|
Object.defineProperty(authService, 'userManager', {
|
|
value: mock<UserManager>({
|
|
getUser: vi.fn().mockImplementation(getUser),
|
|
getAccessToken: vi.fn().mockResolvedValue('access-token'),
|
|
signinRedirect: mockSignInRedirect,
|
|
setPostLoginRedirectUrl: vi.fn(),
|
|
loadUserAbilities: mockLoadUserAbilities,
|
|
updateContext: mockUpdateContext
|
|
})
|
|
})
|
|
|
|
const ability = { can: vi.fn().mockReturnValue(canAccessVault) } as unknown as Ability
|
|
const capabilityStore = mock<CapabilityStore>({
|
|
isInitialized: true,
|
|
authMfaRequiredLevelname: 'advanced'
|
|
})
|
|
const router = createRouter()
|
|
const pushSpy = vi.spyOn(router, 'push').mockResolvedValue(undefined)
|
|
|
|
createTestingPinia()
|
|
const authStore = useAuthStore()
|
|
authStore.userContextReady = userContextReady
|
|
const configStore = useConfigStore()
|
|
authService.initialize(
|
|
configStore,
|
|
null,
|
|
router,
|
|
ability,
|
|
{
|
|
$gettext: (msg: string) => msg,
|
|
$pgettext: (_context: string, msg: string) => msg
|
|
} as unknown as Language,
|
|
null,
|
|
authStore,
|
|
capabilityStore,
|
|
null
|
|
)
|
|
|
|
return { authService, pushSpy, mockSignInRedirect, mockLoadUserAbilities, mockUpdateContext }
|
|
}
|
|
|
|
beforeEach(() => {
|
|
sessionStorage.clear()
|
|
})
|
|
|
|
it('when the user lacks the vault ability, denies (accessDenied) instead of the IdP', async () => {
|
|
const { authService, mockSignInRedirect } = setupVaultAuthService({
|
|
canAccessVault: false,
|
|
userContextReady: true,
|
|
getUser: () => Promise.resolve(mock<User>({ profile: { acr: 'advanced' }, expired: false }))
|
|
})
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
// initializeContext returns the redirect target; the guard (setupAuthGuard) returns it
|
|
// so vue-router cancels the navigation to the vault route.
|
|
expect(result).toEqual({ name: 'accessDenied', query: { reason: 'forbidden' } })
|
|
expect(mockSignInRedirect).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('when the user holds the vault ability, does not deny and enforces MFA', async () => {
|
|
const { authService, mockSignInRedirect } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: true,
|
|
getUser: () => Promise.resolve(null)
|
|
})
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
expect(result).toBeUndefined()
|
|
expect(mockSignInRedirect).toHaveBeenCalledWith(
|
|
expect.objectContaining({ acr_values: 'advanced' })
|
|
)
|
|
})
|
|
|
|
it('when there is no authenticated user (cold load), does not deny and hands off to the IdP for login', async () => {
|
|
const { authService, mockSignInRedirect, mockLoadUserAbilities } = setupVaultAuthService({
|
|
canAccessVault: false,
|
|
userContextReady: false,
|
|
getUser: () => Promise.resolve(null)
|
|
})
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
expect(mockLoadUserAbilities).not.toHaveBeenCalled()
|
|
expect(result).toBeUndefined()
|
|
expect(mockSignInRedirect).toHaveBeenCalledWith(
|
|
expect.objectContaining({ acr_values: 'advanced' })
|
|
)
|
|
})
|
|
|
|
it('on a cold load, denies an authenticated but unentitled user before the IdP hand-off', async () => {
|
|
const { authService, mockSignInRedirect, mockLoadUserAbilities } = setupVaultAuthService({
|
|
canAccessVault: false,
|
|
userContextReady: false,
|
|
getUser: () => Promise.resolve(mock<User>({ profile: { acr: 'regular' }, expired: false }))
|
|
})
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
expect(mockLoadUserAbilities).toHaveBeenCalled()
|
|
expect(result).toEqual({ name: 'accessDenied', query: { reason: 'forbidden' } })
|
|
expect(mockSignInRedirect).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('on a cold load, fails closed (denies) when loading abilities throws', async () => {
|
|
const { authService, mockSignInRedirect, mockLoadUserAbilities } = setupVaultAuthService({
|
|
canAccessVault: true, // even if the ability would allow, a load failure must deny
|
|
userContextReady: false,
|
|
getUser: () => Promise.resolve(mock<User>({ profile: { acr: 'regular' }, expired: false }))
|
|
})
|
|
mockLoadUserAbilities.mockRejectedValueOnce(new Error('network error'))
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
expect(mockLoadUserAbilities).toHaveBeenCalled()
|
|
expect(result).toEqual({ name: 'accessDenied' })
|
|
expect(mockSignInRedirect).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does not report a load failure as a confirmed permission denial', async () => {
|
|
const { authService, mockLoadUserAbilities } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: false,
|
|
getUser: () => Promise.resolve(mock<User>({ profile: { acr: 'regular' }, expired: false }))
|
|
})
|
|
mockLoadUserAbilities.mockRejectedValueOnce(new Error('network error'))
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
expect(mockLoadUserAbilities).toHaveBeenCalled()
|
|
// reason: 'forbidden' tells accessDenied.vue the ability check ran and failed;
|
|
// here it never ran, so claiming that is false regardless of the (correct) denial.
|
|
expect((result as { query?: { reason?: string } })?.query?.reason).not.toBe('forbidden')
|
|
})
|
|
|
|
it('on a cold load with a valid MFA session, initializes the user context instead of bouncing to login', async () => {
|
|
const { authService, mockSignInRedirect, mockUpdateContext } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: false,
|
|
getUser: () => Promise.resolve(mock<User>({ profile: { acr: 'advanced' }, expired: false }))
|
|
})
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
// acr already satisfies MFA: no step-up, and the context must still be initialized
|
|
// (loadUserAbilities pre-set the token, which must not short-circuit updateContext).
|
|
expect(mockSignInRedirect).not.toHaveBeenCalled()
|
|
expect(mockUpdateContext).toHaveBeenCalledWith('access-token', true)
|
|
expect(result).toBeUndefined()
|
|
})
|
|
|
|
it('on a cold load, lets an authenticated entitled user through to the MFA step-up', async () => {
|
|
const { authService, mockSignInRedirect, mockLoadUserAbilities } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: false,
|
|
getUser: () => Promise.resolve(mock<User>({ profile: { acr: 'regular' }, expired: false }))
|
|
})
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
expect(mockLoadUserAbilities).toHaveBeenCalled()
|
|
expect(result).toBeUndefined()
|
|
expect(mockSignInRedirect).toHaveBeenCalledWith(
|
|
expect.objectContaining({ acr_values: 'advanced' })
|
|
)
|
|
})
|
|
|
|
describe('MFA step-up loop protection', () => {
|
|
const regularUser = () =>
|
|
Promise.resolve(mock<User>({ profile: { acr: 'regular' }, expired: false }))
|
|
|
|
// Runs the real sign-in callback for a step-up the IdP answered for `target`.
|
|
const returnFromIdp = async (authService: AuthService, target: string) => {
|
|
const userManager = (authService as any).userManager
|
|
userManager.signinRedirectCallback.mockResolvedValue(
|
|
mock<User>({ state: { mfaStepUpTarget: target } })
|
|
)
|
|
userManager.getAndClearPostLoginRedirectUrl.mockReturnValue(target)
|
|
vi.spyOn((authService as any).router, 'replace').mockResolvedValue(undefined)
|
|
await authService.signInCallback()
|
|
}
|
|
|
|
const spyNavigateOutsideVault = (authService: AuthService) =>
|
|
vi.spyOn(authService as any, 'navigateOutsideVault').mockImplementation(() => undefined)
|
|
|
|
it('requests the step-up with the target as OIDC state', async () => {
|
|
const { authService, mockSignInRedirect } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: true,
|
|
getUser: regularUser
|
|
})
|
|
|
|
await authService.initializeContext(vaultRoute)
|
|
|
|
expect(mockSignInRedirect).toHaveBeenCalledWith(stepUpRequest('/vault'))
|
|
})
|
|
|
|
it('leaves the vault with a full page load when the IdP answered without the required acr', async () => {
|
|
const { authService, mockSignInRedirect } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: true,
|
|
getUser: regularUser
|
|
})
|
|
const navigateSpy = spyNavigateOutsideVault(authService)
|
|
await returnFromIdp(authService, '/vault')
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
// navigation is cancelled, no in-app redirect that would keep the vault clients
|
|
expect(result).toBe(false)
|
|
expect(navigateSpy).toHaveBeenCalledTimes(1)
|
|
expect(navigateSpy.mock.calls[0][0]).not.toMatch(/vault/)
|
|
expect(mockSignInRedirect).not.toHaveBeenCalled()
|
|
expect(sessionStorage.getItem(vaultStepUpFailedKey)).toBe('true')
|
|
})
|
|
|
|
it('redirects again when the user came back without the IdP answering (e.g. browser back)', async () => {
|
|
const { authService, mockSignInRedirect } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: true,
|
|
getUser: regularUser
|
|
})
|
|
const navigateSpy = spyNavigateOutsideVault(authService)
|
|
|
|
await authService.initializeContext(vaultRoute)
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
expect(result).toBeUndefined()
|
|
expect(mockSignInRedirect).toHaveBeenCalledTimes(2)
|
|
expect(navigateSpy).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('only treats the navigation right after the callback as the answer', async () => {
|
|
const { authService, mockSignInRedirect } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: true,
|
|
getUser: regularUser
|
|
})
|
|
const navigateSpy = spyNavigateOutsideVault(authService)
|
|
await returnFromIdp(authService, '/vault')
|
|
|
|
// the callback lands somewhere else first, a later vault navigation must ask the IdP again
|
|
await authService.initializeContext({ params: {}, fullPath: '/files' } as any)
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
expect(result).toBeUndefined()
|
|
expect(navigateSpy).not.toHaveBeenCalled()
|
|
expect(mockSignInRedirect).toHaveBeenCalledWith(stepUpRequest('/vault'))
|
|
})
|
|
|
|
it('redirects an expired user to the IdP even when the IdP just answered', async () => {
|
|
const { authService, mockSignInRedirect } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: true,
|
|
getUser: () => Promise.resolve(mock<User>({ profile: { acr: 'regular' }, expired: true }))
|
|
})
|
|
const navigateSpy = spyNavigateOutsideVault(authService)
|
|
await returnFromIdp(authService, '/vault')
|
|
|
|
const result = await authService.initializeContext(vaultRoute)
|
|
|
|
expect(result).toBeUndefined()
|
|
expect(navigateSpy).not.toHaveBeenCalled()
|
|
expect(mockSignInRedirect).toHaveBeenCalledWith(stepUpRequest('/vault'))
|
|
})
|
|
|
|
it('shows the step-up failure message after the reload and only once', async () => {
|
|
const { authService } = setupVaultAuthService({
|
|
canAccessVault: true,
|
|
userContextReady: true,
|
|
getUser: () =>
|
|
Promise.resolve(mock<User>({ profile: { acr: 'advanced' }, expired: false }))
|
|
})
|
|
sessionStorage.setItem(vaultStepUpFailedKey, 'true')
|
|
|
|
await authService.initializeContext(vaultRoute)
|
|
await authService.initializeContext(vaultRoute)
|
|
|
|
expect(useMessages().showErrorMessage).toHaveBeenCalledTimes(1)
|
|
expect(sessionStorage.getItem(vaultStepUpFailedKey)).toBeNull()
|
|
})
|
|
})
|
|
})
|
|
})
|