diff --git a/keyserver/src/cron/cron.js b/keyserver/src/cron/cron.js --- a/keyserver/src/cron/cron.js +++ b/keyserver/src/cron/cron.js @@ -1,5 +1,6 @@ // @flow +import type { Account as OlmAccount } from '@commapp/olm'; import cluster from 'cluster'; import schedule from 'node-schedule'; @@ -23,6 +24,8 @@ import { deleteInaccessibleThreads } from '../deleters/thread-deleters.js'; import { deleteExpiredUpdates } from '../deleters/update-deleters.js'; import { deleteUnassignedUploads } from '../deleters/upload-deleters.js'; +import { fetchCallUpdateOlmAccount } from '../updaters/olm-account-updater.js'; +import { validateAccountPrekey } from '../utils/olm-utils.js'; if (cluster.isMaster) { schedule.scheduleJob( @@ -90,4 +93,19 @@ } }, ); + + schedule.scheduleJob( + '0 0 * * *', // every day at midnight in the keyserver's timezone + async () => { + try { + const callback = async (account: OlmAccount) => { + await validateAccountPrekey(account); + }; + await fetchCallUpdateOlmAccount('primary', callback); + await fetchCallUpdateOlmAccount('notifications', callback); + } catch (e) { + console.warn('encountered error while trying to validate prekeys', e); + } + }, + ); } diff --git a/keyserver/src/utils/olm-utils.js b/keyserver/src/utils/olm-utils.js --- a/keyserver/src/utils/olm-utils.js +++ b/keyserver/src/utils/olm-utils.js @@ -48,4 +48,38 @@ return cachedOLMUtility; } -export { createPickledOlmAccount, getOlmUtility, unpicklePickledOlmAccount }; +async function validateAccountPrekey(account: OlmAccount): Promise { + const prekey = JSON.parse(account.prekey()); + + const hasPrekey = Object.keys(prekey.curve25519).length !== 0; + const prekeyPublished = account.last_prekey_publish_time() !== 0; + + const currentDate = new Date(); + const lastPrekeyPublishDate = new Date(account.last_prekey_publish_time()); + + const maxPublishedPrekeyAge = 30 * 24 * 60 * 60 * 1000; + if ( + !hasPrekey || + (prekeyPublished && + currentDate - lastPrekeyPublishDate > maxPublishedPrekeyAge) + ) { + // If there is no prekey or the current prekey is older than month + // we need to generate new one. + account.generate_prekey(); + } + + const maxOldPrekeyAge = 24 * 60 * 60 * 1000; + if ( + prekeyPublished && + currentDate - lastPrekeyPublishDate >= maxOldPrekeyAge + ) { + account.forget_old_prekey(); + } +} + +export { + createPickledOlmAccount, + getOlmUtility, + unpicklePickledOlmAccount, + validateAccountPrekey, +};