Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}