Fix invalidating auth token when device reports uninstall

This commit is contained in:
Jonas Lochmann
2019-10-21 14:03:38 +02:00
parent 919b0e6984
commit 306bc0646f
2 changed files with 23 additions and 6 deletions
+20 -5
View File
@@ -15,6 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { difference } from 'lodash'
import * as Sequelize from 'sequelize'
import { Database } from '../../database'
@@ -129,11 +130,25 @@ export async function deleteFamilies ({ database, familiyIds }: {
// olddevice
if (oldDeviceAuthTokens.length > 0) {
await database.oldDevice.bulkCreate(
oldDeviceAuthTokens.map((item) => ({
deviceAuthToken: item
}))
)
const knownOldDeviceAuthTokens = await database.oldDevice.findAll({
where: {
deviceAuthToken: {
[Sequelize.Op.in]: oldDeviceAuthTokens
}
},
transaction
}).map((item) => item.deviceAuthToken)
const oldDeviceAuthTokensToAdd = difference(oldDeviceAuthTokens, knownOldDeviceAuthTokens)
if (oldDeviceAuthTokensToAdd.length > 0) {
await database.oldDevice.bulkCreate(
oldDeviceAuthTokensToAdd.map((item) => ({
deviceAuthToken: item
})),
{ transaction }
)
}
}
// family
+3 -1
View File
@@ -35,6 +35,8 @@ export async function reportDeviceRemoved ({ database, deviceAuthToken, websocke
})
if (deviceEntry) {
const currentAuthToken = deviceEntry.deviceAuthToken
deviceEntry.didDeviceReportUninstall = true
deviceEntry.deviceAuthToken = generateAuthToken() // invalidiate the token
deviceEntry.save({ transaction })
@@ -51,7 +53,7 @@ export async function reportDeviceRemoved ({ database, deviceAuthToken, websocke
// add to old devices
await database.oldDevice.create({
deviceAuthToken: deviceEntry.deviceAuthToken
deviceAuthToken: currentAuthToken
}, {
transaction
})