Compare commits

...
2 changed files with 89 additions and 5 deletions

No files matched your search

@@ -61,6 +61,8 @@ export class AuthService implements AuthServiceInterface {
private mfaExpiryModalDismissed = false
private mfaExpiryModalId: string | null = null
private mfaExpiryBroadcastChannel: BroadcastChannel
private mfaExpiresAt: number | null = null
private mfaExpiryLogoutTimer: ReturnType<typeof setTimeout> | null = null
// number of seconds before an access token is to expire to raise the accessTokenExpiring event
private accessTokenExpiryThreshold = 10
@@ -256,6 +258,7 @@ export class AuthService implements AuthServiceInterface {
console.log('user unloaded…')
this.tokenTimerWorker?.resetTokenTimer()
this.mfaExpiryWorker?.resetMfaTimer()
this.clearMfaExpiryLogout()
this.resetStateAfterUserLogout()
if (this.userManager.unloadReason === 'authError') {
@@ -603,9 +606,7 @@ export class AuthService implements AuthServiceInterface {
}
const baseTime = this.clientService.lastSuccessfulRequestTime ?? Math.floor(Date.now() / 1000)
const expiresAt = baseTime + sessionDuration
this.mfaExpiryWorker.setMfaTimer({ expiresAt })
this.armMfaTimer(baseTime + sessionDuration)
}
private showMfaExpiryWarning() {
@@ -637,6 +638,28 @@ export class AuthService implements AuthServiceInterface {
this.mfaExpiryModalId = modal.id
}
// Single source of truth for the MFA deadline: stores it, re-arms the warning worker, and
// (re)schedules the hard logout for that same timestamp, regardless of modal interaction.
private armMfaTimer(expiresAt: number) {
this.mfaExpiresAt = expiresAt
this.mfaExpiryWorker?.setMfaTimer({ expiresAt })
this.clearMfaExpiryLogout()
const now = Math.floor(Date.now() / 1000)
const delaySeconds = Math.max(0, expiresAt - now)
this.mfaExpiryLogoutTimer = setTimeout(() => {
this.mfaExpiryLogoutTimer = null
this.logoutUser()
}, delaySeconds * 1000)
}
private clearMfaExpiryLogout() {
if (this.mfaExpiryLogoutTimer) {
clearTimeout(this.mfaExpiryLogoutTimer)
this.mfaExpiryLogoutTimer = null
}
}
private prolongMfaSession() {
const sessionDuration = this.capabilityStore.authMfaSessionDuration
if (!sessionDuration) {
@@ -644,8 +667,7 @@ export class AuthService implements AuthServiceInterface {
}
this.mfaExpiryModalDismissed = false
const expiresAt = Math.floor(Date.now() / 1000) + sessionDuration
this.mfaExpiryWorker?.setMfaTimer({ expiresAt })
this.armMfaTimer(Math.floor(Date.now() / 1000) + sessionDuration)
}
private initMfaExpiryBroadcastChannel() {
@@ -0,0 +1,62 @@
import { CapabilityStore } from '@ownclouders/web-pkg'
import { mock } from 'vitest-mock-extended'
import { AuthService } from '../../../../src/services/auth/authService'
import { createTestingPinia } from '@ownclouders/web-test-helpers'
import { useModals } from '@ownclouders/web-pkg'
// The MFA session must terminate once it actually expires, no matter how the user interacts
// with the expiry warning — dismissing it, or just leaving it open and ignoring it.
describe('AuthService MFA expiry', () => {
afterEach(() => {
vi.useRealTimers()
})
const setUp = (sessionDuration = 3600) => {
vi.useFakeTimers()
createTestingPinia({ stubActions: false })
const authService = new AuthService()
const capabilityStore = mock<CapabilityStore>({
vaultEnabled: true,
authMfaSessionDuration: sessionDuration
})
const language = { $gettext: (s: string) => s } as any
authService.initialize(null, null, null, null, language, null, null, capabilityStore, null)
const logoutSpy = vi.spyOn(authService, 'logoutUser')
// Mirrors what updateMfaExpiryTimer() does: store the deadline and arm the hard logout.
;(authService as any).armMfaTimer(Math.floor(Date.now() / 1000) + sessionDuration)
// Simulates the mfaExpiryWorker's onExpiring callback firing.
;(authService as any).showMfaExpiryWarning()
return { authService, logoutSpy, sessionDuration }
}
it('terminates the session once it expires, even if the expiry warning was dismissed', () => {
const { logoutSpy, sessionDuration } = setUp()
const modalStore = useModals()
const modal = modalStore.activeModal
expect(modal.title).toBe('Session expiring')
modal.onCancel() // user clicks "Dismiss"
modalStore.removeModal(modal.id)
expect(logoutSpy).not.toHaveBeenCalled()
vi.advanceTimersByTime(sessionDuration * 1000)
expect(logoutSpy).toHaveBeenCalled()
})
it('terminates the session once it expires, even if the expiry warning is left untouched', () => {
const { logoutSpy, sessionDuration } = setUp()
vi.advanceTimersByTime(sessionDuration * 1000)
expect(logoutSpy).toHaveBeenCalled()
})
})