[v10.4.x] Auth: Only call rotate token if we have a session expiry cookie (#84181)

Auth: Only call rotate token if we have a session expiry cookie (#84169)

Only call rotate token if we have a session expiry cookie

(cherry picked from commit 4272483c54)

Co-authored-by: Karl Persson <kalle.persson@grafana.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-03-13 14:04:46 +01:00
committed by GitHub
co-authored by Karl Persson
parent af6316a8f6
commit 45e78b36bf
3 changed files with 15 additions and 5 deletions
+6 -5
View File
@@ -19,7 +19,7 @@ import { AppEvents, DataQueryErrorType } from '@grafana/data';
import { BackendSrv as BackendService, BackendSrvRequest, config, FetchError, FetchResponse } from '@grafana/runtime';
import appEvents from 'app/core/app_events';
import { getConfig } from 'app/core/config';
import { getSessionExpiry } from 'app/core/utils/auth';
import { getSessionExpiry, hasSessionExpiry } from 'app/core/utils/auth';
import { loadUrlToken } from 'app/core/utils/urlToken';
import { DashboardModel } from 'app/features/dashboard/state';
import { DashboardSearchItem } from 'app/features/search/types';
@@ -390,10 +390,11 @@ export class BackendSrv implements BackendService {
}
let authChecker = this.loginPing();
const expired = getSessionExpiry() * 1000 < Date.now();
if (expired) {
authChecker = this.rotateToken();
if (hasSessionExpiry()) {
const expired = getSessionExpiry() * 1000 < Date.now();
if (expired) {
authChecker = this.rotateToken();
}
}
return from(authChecker).pipe(
@@ -86,6 +86,11 @@ const getTestContext = (overides?: object, mockFromFetch = true) => {
};
};
jest.mock('app/core/utils/auth', () => ({
getSessionExpiry: () => 1,
hasSessionExpiry: () => true,
}));
describe('backendSrv', () => {
describe('parseRequestOptions', () => {
it.each`
+4
View File
@@ -11,3 +11,7 @@ export function getSessionExpiry() {
return parseInt(expiresStr, 10);
}
export function hasSessionExpiry() {
return document.cookie.split('; ').findIndex((row) => row.startsWith('grafana_session_expiry=')) > -1;
}