From 2913ecacdce30c68cc96b4ec0c5585996d38d66b Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Sun, 6 Aug 2023 10:27:57 +0200 Subject: [PATCH] fix: refresh token on login and regularly --- backend/windmill-api/openapi.yaml | 84 +++++++++++++++++-- .../(logged)/user/(user)/login/+page.svelte | 2 +- frontend/src/routes/(root)/+layout.svelte | 22 ++++- 3 files changed, 98 insertions(+), 10 deletions(-) diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index 8504addd12..63d6274a26 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -536,6 +536,20 @@ paths: schema: type: string + /users/refresh_token: + get: + summary: refresh the current token + operationId: refreshUserToken + tags: + - user + responses: + "200": + description: free usage + content: + text/plain: + schema: + type: string + /users/usage: get: summary: get current usage outside of premium workspaces @@ -5672,7 +5686,19 @@ components: language: type: string enum: - [python3, deno, go, bash, postgresql, mysql, bigquery, snowflake, graphql, nativets, bun] + [ + python3, + deno, + go, + bash, + postgresql, + mysql, + bigquery, + snowflake, + graphql, + nativets, + bun, + ] kind: type: string enum: [script, failure, trigger, command, approval] @@ -5737,7 +5763,19 @@ components: language: type: string enum: - [python3, deno, go, bash, postgresql, mysql, bigquery, snowflake, graphql, nativets, bun] + [ + python3, + deno, + go, + bash, + postgresql, + mysql, + bigquery, + snowflake, + graphql, + nativets, + bun, + ] kind: type: string enum: [script, failure, trigger, command, approval] @@ -5907,7 +5945,19 @@ components: language: type: string enum: - [python3, deno, go, bash, postgresql, mysql, bigquery, snowflake, graphql, nativets, bun] + [ + python3, + deno, + go, + bash, + postgresql, + mysql, + bigquery, + snowflake, + graphql, + nativets, + bun, + ] email: type: string visible_to_owner: @@ -5997,7 +6047,19 @@ components: language: type: string enum: - [python3, deno, go, bash, postgresql, mysql, bigquery, snowflake, graphql, nativets, bun] + [ + python3, + deno, + go, + bash, + postgresql, + mysql, + bigquery, + snowflake, + graphql, + nativets, + bun, + ] is_skipped: type: boolean email: @@ -6426,7 +6488,19 @@ components: language: type: string enum: - [python3, deno, go, bash, postgresql, mysql, bigquery, snowflake, graphql, nativets, bun] + [ + python3, + deno, + go, + bash, + postgresql, + mysql, + bigquery, + snowflake, + graphql, + nativets, + bun, + ] tag: type: string kind: diff --git a/frontend/src/routes/(root)/(logged)/user/(user)/login/+page.svelte b/frontend/src/routes/(root)/(logged)/user/(user)/login/+page.svelte index f8f8731016..bbb9a08b2d 100644 --- a/frontend/src/routes/(root)/(logged)/user/(user)/login/+page.svelte +++ b/frontend/src/routes/(root)/(logged)/user/(user)/login/+page.svelte @@ -101,7 +101,7 @@ } catch {} } - const allWorkspaces = $usersWorkspaceStore?.workspaces + const allWorkspaces = $usersWorkspaceStore?.workspaces.filter((x) => x.id != 'admins') if (allWorkspaces?.length == 1) { $workspaceStore = allWorkspaces[0].id diff --git a/frontend/src/routes/(root)/+layout.svelte b/frontend/src/routes/(root)/+layout.svelte index 53dcba2aa5..10e52db2eb 100644 --- a/frontend/src/routes/(root)/+layout.svelte +++ b/frontend/src/routes/(root)/+layout.svelte @@ -124,11 +124,24 @@ console.error('Caught unhandled promise rejection without message', event.reason, event) } } + setLicense() + if ($page.url.pathname != '/user/login') { setUserWorkspaceStore() loadUser() - setLicense() - interval = setInterval(async () => { + UserService.refreshUserToken() + } + + let i = 0 + interval = setInterval(async () => { + if ($page.url.pathname != '/user/login') { + i += 1 + + // every 1 hour + if (i % 12 == 0) { + UserService.refreshUserToken() + } + try { const workspace = $workspaceStore const user = $userStore @@ -140,8 +153,9 @@ } catch (e) { console.error('Could not refresh user', e) } - }, 300000) - } + } + // every 5 minues + }, 300000) }) onDestroy(() => {