Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/utils/olm-utils.js
// @flow | // @flow | ||||
import olm from '@commapp/olm'; | import olm from '@commapp/olm'; | ||||
import type { | import type { | ||||
Account as OlmAccount, | Account as OlmAccount, | ||||
Utility as OlmUtility, | Utility as OlmUtility, | ||||
} from '@commapp/olm'; | } from '@commapp/olm'; | ||||
import uuid from 'uuid'; | import uuid from 'uuid'; | ||||
type PickledOlmAccount = { | type PickledOlmAccount = { | ||||
+picklingKey: string, | +picklingKey: string, | ||||
+pickledAccount: string, | +pickledAccount: string, | ||||
}; | }; | ||||
const maxPublishedPrekeyAge = 30 * 24 * 60 * 60 * 1000; | |||||
const maxOldPrekeyAge = 24 * 60 * 60 * 1000; | |||||
async function createPickledOlmAccount(): Promise<PickledOlmAccount> { | async function createPickledOlmAccount(): Promise<PickledOlmAccount> { | ||||
await olm.init(); | await olm.init(); | ||||
const account = new olm.Account(); | const account = new olm.Account(); | ||||
account.create(); | account.create(); | ||||
const picklingKey = uuid.v4(); | const picklingKey = uuid.v4(); | ||||
const pickledAccount = account.pickle(picklingKey); | const pickledAccount = account.pickle(picklingKey); | ||||
Show All 20 Lines | |||||
function getOlmUtility(): OlmUtility { | function getOlmUtility(): OlmUtility { | ||||
if (cachedOLMUtility) { | if (cachedOLMUtility) { | ||||
return cachedOLMUtility; | return cachedOLMUtility; | ||||
} | } | ||||
cachedOLMUtility = new olm.Utility(); | cachedOLMUtility = new olm.Utility(); | ||||
return cachedOLMUtility; | return cachedOLMUtility; | ||||
} | } | ||||
export { createPickledOlmAccount, getOlmUtility, unpickleOlmAccount }; | async function validateAccountPrekey(account: OlmAccount): Promise<void> { | ||||
const currentDate = new Date(); | |||||
const lastPrekeyPublishDate = new Date(account.last_prekey_publish_time()); | |||||
const prekeyPublished = !account.unpublished_prekey(); | |||||
if ( | |||||
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(); | |||||
} | |||||
if ( | |||||
prekeyPublished && | |||||
currentDate - lastPrekeyPublishDate >= maxOldPrekeyAge | |||||
) { | |||||
account.forget_old_prekey(); | |||||
} | |||||
} | |||||
export { | |||||
createPickledOlmAccount, | |||||
getOlmUtility, | |||||
unpickleOlmAccount, | |||||
validateAccountPrekey, | |||||
}; |