-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10955 from owncloud/feat/token-renewal-worker
feat: move access token timer to web worker
- Loading branch information
Showing
17 changed files
with
359 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './deleteWorker' | ||
export * from './tokenTimerWorker' |
1 change: 1 addition & 0 deletions
1
packages/web-pkg/src/composables/webWorkers/tokenTimerWorker/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useTokenTimerWorker' |
55 changes: 55 additions & 0 deletions
55
packages/web-pkg/src/composables/webWorkers/tokenTimerWorker/useTokenTimerWorker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ref, unref } from 'vue' | ||
import { ErrorTimeout } from 'oidc-client-ts' | ||
import { AuthServiceInterface } from '../../authContext' | ||
import { WebWorker, useWebWorkersStore } from '../../piniaStores/webWorkers' | ||
import TokenWorker from './worker?worker' | ||
|
||
export type TokenTimerWorkerTopic = 'set' | 'reset' | ||
|
||
export const useTokenTimerWorker = ({ authService }: { authService: AuthServiceInterface }) => { | ||
const { createWorker } = useWebWorkersStore() | ||
|
||
const worker = ref<WebWorker>() | ||
|
||
const startWorker = () => { | ||
worker.value = createWorker(TokenWorker as unknown as string) | ||
|
||
unref(unref(worker).worker).onmessage = () => { | ||
authService.signinSilent().catch((error) => { | ||
if (error instanceof ErrorTimeout) { | ||
console.warn('token renewal timed out, retrying in 5 seconds...') | ||
unref(worker).post(JSON.stringify({ topic: 'set', expiry: 5, expiryThreshold: 0 })) | ||
return | ||
} | ||
|
||
console.error('token renewal error:', error) | ||
}) | ||
} | ||
} | ||
|
||
const setTokenTimer = ({ | ||
expiry, | ||
expiryThreshold | ||
}: { | ||
expiry: number | ||
expiryThreshold: number | ||
}) => { | ||
if (!unref(worker)) { | ||
console.error('token timer worker is not running') | ||
return | ||
} | ||
|
||
unref(worker).post(JSON.stringify({ topic: 'set', expiry, expiryThreshold })) | ||
} | ||
|
||
const resetTokenTimer = () => { | ||
if (!unref(worker)) { | ||
console.error('token timer worker is not running') | ||
return | ||
} | ||
|
||
unref(worker).post(JSON.stringify({ topic: 'reset' })) | ||
} | ||
|
||
return { startWorker, setTokenTimer, resetTokenTimer } | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/web-pkg/src/composables/webWorkers/tokenTimerWorker/worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { TokenTimerWorkerTopic } from './useTokenTimerWorker' | ||
|
||
type Message = { | ||
topic: TokenTimerWorkerTopic | ||
expiry: number | ||
expiryThreshold: number | ||
} | ||
|
||
let timerId: ReturnType<typeof setTimeout> | ||
|
||
const resetTimer = () => { | ||
clearTimeout(timerId) | ||
timerId = undefined | ||
} | ||
|
||
self.onmessage = (e: MessageEvent) => { | ||
const { topic, expiry, expiryThreshold } = JSON.parse(e.data) as Message | ||
|
||
if (topic === 'reset') { | ||
resetTimer() | ||
return | ||
} | ||
|
||
let timerInSeconds = expiry - expiryThreshold | ||
if (timerInSeconds <= 0) { | ||
// timer can't be smaller or equal 0 | ||
timerInSeconds = 1 | ||
} | ||
|
||
resetTimer() | ||
|
||
timerId = setTimeout(() => { | ||
postMessage(true) | ||
}, timerInSeconds * 1000) | ||
} |
99 changes: 99 additions & 0 deletions
99
...es/web-pkg/tests/unit/composables/webWorkers/tokenTimerWorker/useTokenTimerWorker.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { unref } from 'vue' | ||
import { getComposableWrapper } from 'web-test-helpers' | ||
import { mock } from 'vitest-mock-extended' | ||
import { | ||
AuthServiceInterface, | ||
WebWorker, | ||
WebWorkersStore, | ||
useTokenTimerWorker, | ||
useWebWorkersStore | ||
} from '../../../../../src/composables' | ||
|
||
describe('useTokenTimerWorker', () => { | ||
describe('method "startWorker"', () => { | ||
it('creates a worker instance', () => { | ||
getWrapper({ | ||
setup: ({ startWorker }, { webWorkersStore }) => { | ||
startWorker() | ||
expect(vi.mocked(webWorkersStore.createWorker)).toHaveBeenCalled() | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
describe('method "setTokenTimer"', () => { | ||
it('posts an event to the worker when started', () => { | ||
getWrapper({ | ||
setup: ({ startWorker, setTokenTimer }, { workerMock }) => { | ||
startWorker() | ||
const expiryOptions = { expiry: 10, expiryThreshold: 1 } | ||
setTokenTimer(expiryOptions) | ||
|
||
expect(unref(workerMock).post).toHaveBeenCalledWith( | ||
JSON.stringify({ topic: 'set', ...expiryOptions }) | ||
) | ||
} | ||
}) | ||
}) | ||
it('does not post an event to the worker when not started', () => { | ||
const consoleSpy = vi.fn() | ||
vi.spyOn(console, 'error').mockImplementation(consoleSpy) | ||
|
||
getWrapper({ | ||
setup: ({ setTokenTimer }) => { | ||
const expiryOptions = { expiry: 10, expiryThreshold: 1 } | ||
setTokenTimer(expiryOptions) | ||
|
||
expect(consoleSpy).toHaveBeenCalled() | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
describe('method "resetTokenTimer"', () => { | ||
it('posts an event to the worker when started', () => { | ||
getWrapper({ | ||
setup: ({ startWorker, resetTokenTimer }, { workerMock }) => { | ||
startWorker() | ||
resetTokenTimer() | ||
|
||
expect(unref(workerMock).post).toHaveBeenCalledWith(JSON.stringify({ topic: 'reset' })) | ||
} | ||
}) | ||
}) | ||
it('does not post an event to the worker when not started', () => { | ||
const consoleSpy = vi.fn() | ||
vi.spyOn(console, 'error').mockImplementation(consoleSpy) | ||
|
||
getWrapper({ | ||
setup: ({ resetTokenTimer }) => { | ||
resetTokenTimer() | ||
|
||
expect(consoleSpy).toHaveBeenCalled() | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
function getWrapper({ | ||
setup | ||
}: { | ||
setup: ( | ||
instance: ReturnType<typeof useTokenTimerWorker>, | ||
{ webWorkersStore }: { webWorkersStore: WebWorkersStore; workerMock: WebWorker } | ||
) => void | ||
}) { | ||
return { | ||
wrapper: getComposableWrapper(() => { | ||
const instance = useTokenTimerWorker({ authService: mock<AuthServiceInterface>() }) | ||
|
||
const webWorkersStore = useWebWorkersStore() | ||
|
||
const workerMock = mock<WebWorker>() | ||
vi.mocked(webWorkersStore.createWorker).mockReturnValue(workerMock) | ||
|
||
setup(instance, { webWorkersStore, workerMock }) | ||
}) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/web-pkg/tests/unit/composables/webWorkers/tokenTimerWorker/worker.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { unref } from 'vue' | ||
import { useWebWorker } from '@vueuse/core' | ||
import TokenWorker from '../../../../../src/composables/webWorkers/tokenTimerWorker/worker?worker' | ||
|
||
describe('token timer worker', () => { | ||
let worker: ReturnType<typeof useWebWorker> | ||
|
||
beforeEach(() => { | ||
worker = useWebWorker(TokenWorker as unknown as string, { type: 'module' }) | ||
}) | ||
|
||
afterEach(() => { | ||
worker.terminate() | ||
}) | ||
|
||
it('should not post a message with "reset" topic', async () => { | ||
const messageSpy = vi.fn() | ||
unref(worker.worker).onmessage = messageSpy | ||
worker.post(JSON.stringify({ topic: 'reset' })) | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 1100)) | ||
|
||
expect(messageSpy).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should post a message with "set" topic', async () => { | ||
const messageSpy = vi.fn() | ||
unref(worker.worker).onmessage = messageSpy | ||
worker.post(JSON.stringify({ topic: 'set', expiry: 1, expiryThreshold: 1 })) | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 1100)) | ||
|
||
expect(messageSpy).toHaveBeenCalled() | ||
}) | ||
}) |
Oops, something went wrong.