Auth: Allow soft token revocation (#31601)

* Add revoked_at field to user auth token to allow soft revokes

* Allow soft token revocations

* Update token revocations and tests

* Return error info on revokedTokenErr

* Override session cookie only when no revokedErr nor API request

* Display modal on revoked token error

* Feedback: Refactor TokenRevokedModal to FC

* Add GetUserRevokedTokens into UserTokenService

* Backendsrv: adds tests and refactors soft token path

* Apply feedback

* Write redirect cookie on token revoked error

* Update TokenRevokedModal style

* Return meaningful error info

* Some UI changes

* Update backend_srv tests

* Minor style fix on backend_srv tests

* Replace deprecated method usage to publish events

* Fix backend_srv tests

* Apply suggestions from code review

Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>
Co-authored-by: Hugo Häggmark <hugo.haggmark@gmail.com>

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Hugo Häggmark <hugo.haggmark@gmail.com>

* Minor style fix after PR suggestion commit

* Apply suggestions from code review

Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com>

* Prettier fixes

Co-authored-by: Hugo Häggmark <hugo.haggmark@gmail.com>
Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>
Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com>
This commit is contained in:
Joan López de la Franca Beltran
2021-03-16 17:44:02 +01:00
committed by GitHub
parent a1c7e0630d
commit 610999cfa2
13 changed files with 286 additions and 30 deletions

View File

@@ -7,6 +7,8 @@ import { BackendSrv } from '../services/backend_srv';
import { ContextSrv, User } from '../services/context_srv';
import { describe, expect } from '../../../test/lib/common';
import { BackendSrvRequest, FetchError } from '@grafana/runtime';
import { TokenRevokedModal } from '../../features/users/TokenRevokedModal';
import { ShowModalReactEvent } from '../../types/events';
const getTestContext = (overides?: object) => {
const defaults = {
@@ -37,6 +39,7 @@ const getTestContext = (overides?: object) => {
const appEventsMock: EventBusExtended = ({
emit: jest.fn(),
publish: jest.fn(),
} as any) as EventBusExtended;
const user: User = ({
@@ -185,6 +188,36 @@ describe('backendSrv', () => {
});
});
describe('when making an unsuccessful call because of soft token revocation', () => {
it('then it should dispatch show Token Revoked modal event', async () => {
const url = '/api/dashboard/';
const { backendSrv, appEventsMock, logoutMock, expectRequestCallChain } = getTestContext({
ok: false,
status: 401,
statusText: 'UnAuthorized',
data: { message: 'Token revoked', error: { id: 'ERR_TOKEN_REVOKED', maxConcurrentSessions: 3 } },
url,
});
backendSrv.loginPing = jest.fn();
await backendSrv.request({ url, method: 'GET', retry: 0 }).catch(() => {
expect(appEventsMock.publish).toHaveBeenCalledTimes(1);
expect(appEventsMock.publish).toHaveBeenCalledWith(
new ShowModalReactEvent({
component: TokenRevokedModal,
props: {
maxConcurrentSessions: 3,
},
})
);
expect(backendSrv.loginPing).not.toHaveBeenCalled();
expect(logoutMock).not.toHaveBeenCalled();
expectRequestCallChain({ url, method: 'GET', retry: 0 });
});
});
});
describe('when making an unsuccessful call and conditions for retry are favorable and retry throws', () => {
it('then it throw error', async () => {
jest.useFakeTimers();
@@ -394,6 +427,36 @@ describe('backendSrv', () => {
});
});
describe('when making an unsuccessful call because of soft token revocation', () => {
it('then it should dispatch show Token Revoked modal event', async () => {
const { backendSrv, logoutMock, appEventsMock, expectRequestCallChain } = getTestContext({
ok: false,
status: 401,
statusText: 'UnAuthorized',
data: { message: 'Token revoked', error: { id: 'ERR_TOKEN_REVOKED', maxConcurrentSessions: 3 } },
});
backendSrv.loginPing = jest.fn();
const url = '/api/dashboard/';
await backendSrv.datasourceRequest({ url, method: 'GET', retry: 0 }).catch((error) => {
expect(appEventsMock.publish).toHaveBeenCalledTimes(1);
expect(appEventsMock.publish).toHaveBeenCalledWith(
new ShowModalReactEvent({
component: TokenRevokedModal,
props: {
maxConcurrentSessions: 3,
},
})
);
expect(backendSrv.loginPing).not.toHaveBeenCalled();
expect(logoutMock).not.toHaveBeenCalled();
expectRequestCallChain({ url, method: 'GET', retry: 0 });
});
});
});
describe('when making an unsuccessful call and conditions for retry are favorable and retry throws', () => {
it('then it throw error', async () => {
const { backendSrv, logoutMock, expectRequestCallChain } = getTestContext({