Skip to content

[BREAK] Suspend push notifications when login token is invalidated #20913

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

Merged
merged 22 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8aca78b
first proposal
gabrieleiro Feb 26, 2021
fe921e8
move logout hook
gabrieleiro Feb 26, 2021
36e020a
fix linter
gabrieleiro Feb 26, 2021
bfcbac7
Bump version to 3.12.0-rc.0
sampaiodiego Feb 21, 2021
6103e74
validate auth token before sending notification
gabrieleiro Mar 2, 2021
9f884ce
revert onlogout hook
gabrieleiro Mar 2, 2021
9088913
revert raw collection access
gabrieleiro Mar 2, 2021
a3ea4df
removes push token from collection if auth token is expired
gabrieleiro Mar 2, 2021
9608c84
Merge remote-tracking branch 'origin/develop' into push-token
sampaiodiego Sep 29, 2021
b1ff60c
Merge branch 'develop' into push-token
pierre-lehnen-rc Feb 14, 2022
eed3d82
Revert History file
pierre-lehnen-rc Feb 14, 2022
49d1f74
Prettier
pierre-lehnen-rc Feb 14, 2022
983b266
Revert History file
pierre-lehnen-rc Feb 15, 2022
c83c56c
improved code
pierre-lehnen-rc Feb 15, 2022
b927d30
Merge branch 'develop' into push-token
pierre-lehnen-rc Apr 19, 2022
3591793
Merge remote-tracking branch 'origin/develop' into push-token
pierre-lehnen-rc Jun 24, 2022
8685cee
Merge remote-tracking branch 'origin/develop' into push-token
pierre-lehnen-rc Jun 27, 2022
e43c177
Allow existing tokens to continue working for a week
pierre-lehnen-rc Jun 27, 2022
827feca
Merge remote-tracking branch 'origin/develop' into push-token
sampaiodiego Jun 28, 2022
fbd1566
Merge remote-tracking branch 'origin/develop' into push-token
sampaiodiego Jun 28, 2022
d05834f
Merge remote-tracking branch 'origin/develop' into push-token
sampaiodiego Jun 29, 2022
30c6b16
Remove push tokens when login tokens are removed
sampaiodiego Jun 29, 2022
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
1 change: 1 addition & 0 deletions apps/meteor/app/api/server/v1/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ API.v1.addRoute(
Meteor.call('raix:push-update', {
id: deviceId,
token: { [type]: value },
authToken: this.request.headers['x-auth-token'],
appName,
userId: this.userId,
}),
Expand Down
3 changes: 3 additions & 0 deletions apps/meteor/app/push/server/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Meteor.methods({
check(options, {
id: Match.Optional(String),
token: _matchToken,
authToken: String,
appName: String,
userId: Match.OneOf(String, null),
metadata: Match.Optional(Object),
Expand Down Expand Up @@ -48,6 +49,7 @@ Meteor.methods({
// Rig default doc
doc = {
token: options.token,
authToken: options.authToken,
appName: options.appName,
userId: options.userId,
enabled: true,
Expand All @@ -71,6 +73,7 @@ Meteor.methods({
$set: {
updatedAt: new Date(),
token: options.token,
authToken: options.authToken,
},
},
);
Expand Down
53 changes: 50 additions & 3 deletions apps/meteor/app/push/server/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { initAPN, sendAPN } from './apn';
import { sendGCM } from './gcm';
import { logger } from './logger';
import { settings } from '../../settings/server';
import { Users } from '../../models/server';

export const _matchToken = Match.OneOf({ apn: String }, { gcm: String });
export const appTokensCollection = new Mongo.Collection('_raix_push_app_tokens');
Expand Down Expand Up @@ -77,17 +78,60 @@ export class PushClass {
return !!this.options.gateways && settings.get('Register_Server') && settings.get('Cloud_Service_Agree_PrivacyTerms');
}

_validateAuthTokenByPushToken(pushToken) {
const pushTokenQuery = appTokensCollection.findOne({ token: pushToken });

if (!pushTokenQuery) {
return false;
}

const { authToken, userId, expiration, usesLeft, _id } = pushTokenQuery;
if (!authToken) {
if (expiration && expiration > Date.now()) {
return true;
}
if (usesLeft > 0) {
appTokensCollection.rawCollection().updateOne(
{
_id,
},
{
$inc: {
usesLeft: -1,
},
},
);

return true;
}
}

const user = authToken && userId && Users.findOneByIdAndLoginToken(userId, authToken, { projection: { _id: 1 } });

if (!user) {
this._removeToken(pushToken);
return false;
}

return true;
}

sendNotificationNative(app, notification, countApn, countGcm) {
logger.debug('send to token', app.token);

const validToken = (app.token.apn || app.token.gcm) && this._validateAuthTokenByPushToken(app.token);
if (!validToken) {
throw new Error('send got a faulty query');
}

if (app.token.apn) {
countApn.push(app._id);
// Send to APN
if (this.options.apn) {
notification.topic = app.appName;
sendAPN({ userToken: app.token.apn, notification, _removeToken: this._removeToken });
}
} else if (app.token.gcm) {
} else {
countGcm.push(app._id);

// Send to GCM
Expand All @@ -102,8 +146,6 @@ export class PushClass {
options: this.options,
});
}
} else {
throw new Error('send got a faulty query');
}
}

Expand Down Expand Up @@ -169,6 +211,11 @@ export class PushClass {
for (const gateway of this.options.gateways) {
logger.debug('send to token', app.token);

const validToken = (app.token.apn || app.token.gcm) && this._validateAuthTokenByPushToken(app.token);
if (!validToken) {
continue;
}

if (app.token.apn) {
countApn.push(app._id);
notification.topic = app.appName;
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/server/startup/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,5 @@ import './v267';
import './v268';
import './v269';
import './v271';
import './v272';
import './xrun';
17 changes: 17 additions & 0 deletions apps/meteor/server/startup/migrations/v272.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { appTokensCollection } from '../../../app/push/server/push';
import { addMigration } from '../../lib/migrations';

addMigration({
version: 272,
async up() {
return appTokensCollection.rawCollection().updateMany(
{},
{
$set: {
expiration: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
usesLeft: 7,
},
},
);
},
});