Skip to content

Commit

Permalink
fix(embedded): Ensure guest token is passed to log endpoint (apache#2…
Browse files Browse the repository at this point in the history
…0647)

* Pass guest token to sendBeacon

* Add tests

(cherry picked from commit dfab521)
  • Loading branch information
jfrag1 authored and jinghua-qa committed Jul 12, 2022
1 parent 4c29351 commit be4f896
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
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

0 comments on commit be4f896

Please sign in to comment.