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

fix(embedded): Ensure guest token is passed to log endpoint #20647

Merged
merged 2 commits into from
Jul 8, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const SupersetClient: SupersetClientInterface = {
get: request => getInstance().get(request),
init: force => getInstance().init(force),
isAuthenticated: () => getInstance().isAuthenticated(),
getGuestToken: () => getInstance().getGuestToken(),
post: request => getInstance().post(request),
postForm: (...args) => getInstance().postForm(...args),
put: request => getInstance().put(request),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ export default class SupersetClientClass {
return this.csrfToken !== null && this.csrfToken !== undefined;
}

getGuestToken() {
return this.guestToken;
}

async get<T extends ParseMethod = 'json'>(
requestConfig: RequestConfig & { parseMethod?: T },
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export interface SupersetClientInterface
| 'init'
| 'isAuthenticated'
| 'reAuthenticate'
| 'getGuestToken'
> {
configure: (config?: ClientConfig) => SupersetClientInterface;
reset: () => void;
Expand Down
35 changes: 35 additions & 0 deletions superset-frontend/src/middleware/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,39 @@ describe('logger middleware', () => {
SupersetClient.post.getCall(0).args[0].postPayload.events,
).toHaveLength(3);
});

it('should use navigator.sendBeacon if it exists', () => {
const clock = sinon.useFakeTimers();
const beaconMock = jest.fn();
Object.defineProperty(navigator, 'sendBeacon', {
writable: true,
value: beaconMock,
});

logger(mockStore)(next)(action);
expect(beaconMock.mock.calls.length).toBe(0);
clock.tick(2000);

expect(beaconMock.mock.calls.length).toBe(1);
const endpoint = beaconMock.mock.calls[0][0];
expect(endpoint).toMatch('/superset/log/');
});

it('should pass a guest token to sendBeacon if present', () => {
const clock = sinon.useFakeTimers();
const beaconMock = jest.fn();
Object.defineProperty(navigator, 'sendBeacon', {
writable: true,
value: beaconMock,
});
SupersetClient.configure({ guestToken: 'token' });

logger(mockStore)(next)(action);
expect(beaconMock.mock.calls.length).toBe(0);
clock.tick(2000);
expect(beaconMock.mock.calls.length).toBe(1);

const formData = beaconMock.mock.calls[0][1];
expect(formData.getAll('guest_token')[0]).toMatch('token');
});
});
4 changes: 4 additions & 0 deletions superset-frontend/src/middleware/loggerMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const sendBeacon = events => {
if (navigator.sendBeacon) {
const formData = new FormData();
formData.append('events', safeStringify(events));
if (SupersetClient.getGuestToken()) {
// if we have a guest token, we need to send it for auth via the form
formData.append('guest_token', SupersetClient.getGuestToken());
}
navigator.sendBeacon(endpoint, formData);
} else {
SupersetClient.post({
Expand Down