Skip to content

Commit

Permalink
[v10.4.x] Auth: Only call rotate token if we have a session expiry co…
Browse files Browse the repository at this point in the history
…okie (#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 4272483)

Co-authored-by: Karl Persson <kalle.persson@grafana.com>
  • Loading branch information
grafana-delivery-bot[bot] and kalleep committed Mar 13, 2024
1 parent af6316a commit 45e78b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
11 changes: 6 additions & 5 deletions public/app/core/services/backend_srv.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions public/app/core/specs/backend_srv.test.ts
Expand Up @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions public/app/core/utils/auth.ts
Expand Up @@ -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;
}

0 comments on commit 45e78b3

Please sign in to comment.