1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-12-05 17:02:07 +03:00

add high-level export/import methods

not sure how to test these high-level methods though
This commit is contained in:
Cédric Van Rompay
2020-01-22 16:40:38 +01:00
parent ed01b3b8cf
commit 7b6dabbe9c
3 changed files with 48 additions and 5 deletions

View File

@@ -97,6 +97,11 @@ function keyFromRecoverySession(session, decryptionKey) {
* @param {string} opts.accessToken The access_token for this user. * @param {string} opts.accessToken The access_token for this user.
* *
* @param {string} opts.userId The user ID for this user. * @param {string} opts.userId The user ID for this user.
*
* @param {Object} opts.deviceToImport Device data exported with
* (TODO link to export method) that must be imported to recreate this device.
* Should only be useful for deviced with end-to-end crypto enabled.
* If provided, opts.userId should **not** be provided.
* *
* @param {IdentityServerProvider} [opts.identityServer] * @param {IdentityServerProvider} [opts.identityServer]
* Optional. A provider object with one function `getAccessToken`, which is a * Optional. A provider object with one function `getAccessToken`, which is a
@@ -243,6 +248,18 @@ export function MatrixClient(opts) {
this.deviceId = opts.deviceId || null; this.deviceId = opts.deviceId || null;
if (opts.deviceToImport) {
if (this.deviceId) {
logger.warn('not importing device because device ID is provided to constructor independently of exported data');
} else if (!(opts.deviceToImport.deviceId)){
logger.warn('not importing device because no device ID in exported data');
} else {
this.deviceId = opts.deviceToImport.deviceId;
// will be used during async initialization of the crypto
this._exportedOlmDeviceToImport = opts.deviceToImport.olmDevice;
}
}
const userId = (opts.userId || null); const userId = (opts.userId || null);
this.credentials = { this.credentials = {
userId: userId, userId: userId,
@@ -391,6 +408,17 @@ export function MatrixClient(opts) {
utils.inherits(MatrixClient, EventEmitter); utils.inherits(MatrixClient, EventEmitter);
utils.extend(MatrixClient.prototype, MatrixBaseApis.prototype); utils.extend(MatrixClient.prototype, MatrixBaseApis.prototype);
MatrixClient.prototype.exportDevice = async function() {
if (!(this._crypto)) {
logger.warn('not exporting device if crypto is not enabled');
return;
}
return {
deviceId: this.deviceId,
olmDevice: await this._crypto._olmDevice.export(),
};
};
/** /**
* Clear any data out of the persistent stores used by the client. * Clear any data out of the persistent stores used by the client.
* *
@@ -684,7 +712,10 @@ MatrixClient.prototype.initCrypto = async function() {
]); ]);
logger.log("Crypto: initialising crypto object..."); logger.log("Crypto: initialising crypto object...");
await crypto.init(); await crypto.init({
exportedOlmDevice: this._exportedOlmDeviceToImport,
});
delete this._exportedOlmDeviceToImport;
this.olmVersion = Crypto.getOlmVersion(); this.olmVersion = Crypto.getOlmVersion();

View File

@@ -225,7 +225,7 @@ OlmDevice.prototype._storeAccount = function(txn, account) {
* Export data for re-creating the Olm device later. * Export data for re-creating the Olm device later.
* TODO export data other than just account and (P2P) sessions. * TODO export data other than just account and (P2P) sessions.
* *
* @return {Promise<object>} The export data (see specs for structure) * @return {Promise<object>} The exported data
*/ */
OlmDevice.prototype.export = async function() { OlmDevice.prototype.export = async function() {
const result = { const result = {

View File

@@ -228,12 +228,24 @@ utils.inherits(Crypto, EventEmitter);
* Initialise the crypto module so that it is ready for use * Initialise the crypto module so that it is ready for use
* *
* Returns a promise which resolves once the crypto module is ready for use. * Returns a promise which resolves once the crypto module is ready for use.
*
* @param {Object} kwargs keyword arguments.
* @param {string} kwargs.exportedOlmDevice (Optional) data from exported device
* that must be re-created.
*/ */
Crypto.prototype.init = async function() { Crypto.prototype.init = async function(kwargs) {
const {
exportedOlmDevice,
} = kwargs;
logger.log("Crypto: initialising Olm..."); logger.log("Crypto: initialising Olm...");
await global.Olm.init(); await global.Olm.init();
logger.log("Crypto: initialising Olm device..."); logger.log(
await this._olmDevice.init(); exportedOlmDevice
? "Crypto: initialising Olm device from exported device..."
: "Crypto: initialising Olm device..."
);
await this._olmDevice.init(exportedOlmDevice);
logger.log("Crypto: loading device list..."); logger.log("Crypto: loading device list...");
await this._deviceList.load(); await this._deviceList.load();