Skip to content

Commit 8dd7071

Browse files
authored
Merge pull request #407 from upfluence/mb/dra-1884
ActivityWatcher now disconnects users when token_destroyed is received
2 parents d1e772b + eae5932 commit 8dd7071

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

addon/services/activity-watcher.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export default class ActivityWatcher extends Service {
137137
@service declare eventsService: EventsService;
138138
@service declare toast: ToastService;
139139
@service declare intl: IntlService;
140+
@service declare session: any;
140141

141142
private declare _observer: Observable<ResourceEvent> | null;
142143

@@ -147,7 +148,7 @@ export default class ActivityWatcher extends Service {
147148

148149
this._observer = this.eventsService.watch(prefixPath('/notification'));
149150
this._observer.subscribe((evt: NotificationEvent) => {
150-
this._processEvent(evt);
151+
this.processEvent(evt);
151152
});
152153
}
153154

@@ -160,8 +161,12 @@ export default class ActivityWatcher extends Service {
160161
this._observer = null;
161162
}
162163

163-
private _processEvent(evt: NotificationEvent): void {
164-
const notif = this._buildNotification(evt);
164+
private processEvent(evt: NotificationEvent): void {
165+
if (evt.payload.notification_type === 'token_destroyed') {
166+
this.checkIfUserNeedsToBeDisconnected(evt);
167+
return;
168+
}
169+
const notif = this.buildNotification(evt);
165170

166171
if (!notif) {
167172
return;
@@ -182,7 +187,14 @@ export default class ActivityWatcher extends Service {
182187
}
183188
}
184189

185-
private _buildNotification(evt: NotificationEvent): RenderedNotification | null {
190+
private checkIfUserNeedsToBeDisconnected(event: NotificationEvent): void {
191+
if (this.session.data.authenticated.access_token === event.payload.data.access_token) {
192+
this.session.tooManyConnections = true;
193+
this.session.invalidate();
194+
}
195+
}
196+
197+
private buildNotification(evt: NotificationEvent): RenderedNotification | null {
186198
const renderer = renderersByNotificationType[evt.payload.notification_type];
187199

188200
if (!renderer) {

tests/unit/services/activity-watcher-test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@ import { setupTest } from 'ember-qunit';
55
import EventsServiceMock from '@upfluence/hyperevents/test-support/services/events-service';
66

77
import sinon from 'sinon';
8+
import Service from '@ember/service';
9+
10+
class SessionServiceMock extends Service {
11+
tooManyConnections: boolean = false;
12+
13+
invalidate = sinon.stub();
14+
}
815

916
module('Unit | Service | activity-watcher', function (hooks) {
1017
setupTest(hooks);
1118

1219
hooks.beforeEach(function () {
1320
this.owner.register('service:events-service', EventsServiceMock);
21+
this.owner.register('service:session', SessionServiceMock);
1422

1523
this.eventService = this.owner.lookup('service:events-service');
24+
this.sessionService = this.owner.lookup('service:session');
1625
this.activityWatcher = this.owner.lookup('service:activity-watcher');
1726
});
1827

@@ -31,6 +40,22 @@ module('Unit | Service | activity-watcher', function (hooks) {
3140
this.activityWatcher.watch();
3241
});
3342

43+
test('When an event of type "token_destroyed" is received, it invalidates the session if the token is the same', function (assert) {
44+
this.sessionService.data = { authenticated: { access_token: 'some-token' } };
45+
46+
this.eventService.dispatch({
47+
resource: '/notification/some-uu-id',
48+
payload: {
49+
notification_type: 'token_destroyed',
50+
data: {
51+
access_token: 'some-token'
52+
}
53+
}
54+
});
55+
56+
assert.true(this.sessionService.invalidate.calledOnce);
57+
});
58+
3459
const eventTypesTestCases = [
3560
{
3661
notification: {

0 commit comments

Comments
 (0)