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

Only call rotate token if we have a session expiry cookie
This commit is contained in:
Karl Persson 2024-03-11 14:10:03 +01:00 committed by GitHub
parent 0913324668
commit 4272483c54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 5 deletions

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(

View File

@ -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`

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;
}