* @param {Object} options * @param {Boolean} [options.force] - if newer version on server available force download * @param {openpgp.key.Key} options.key - key to decrypt sync message * @param {String} [options.password] - password for options.key * @return {Promise<undefined, Error}
(options)
| 105 | * @return {Promise<undefined, Error} |
| 106 | */ |
| 107 | async downloadSyncMessage(options) { |
| 108 | const download = await this.download({eTag: this.keyring.sync.data.eTag}); |
| 109 | if (!download.eTag) { |
| 110 | if (this.keyring.sync.data.eTag) { |
| 111 | // initialize eTag |
| 112 | this.keyring.sync.data.eTag = ''; |
| 113 | // set modified flag to trigger upload |
| 114 | this.modified = true; |
| 115 | } |
| 116 | return; |
| 117 | } |
| 118 | if (!download.keyringMsg) { |
| 119 | return; |
| 120 | } |
| 121 | // new version available on server |
| 122 | const message = await readMessage({armoredMessage: download.keyringMsg}); |
| 123 | const encryptionKeyIds = message.getEncryptionKeyIDs(); |
| 124 | let privKey = this.keyring.getPrivateKeyByIds(encryptionKeyIds); |
| 125 | if (!privKey) { |
| 126 | throw new Error('No private key found to decrypt the sync message'); |
| 127 | } |
| 128 | let password; |
| 129 | if (!equalKey(privKey, options.key)) { |
| 130 | console.log('Key used for sync packet from server is not default key on client'); |
| 131 | if (!options.force && !await this.canUnlockKey('decrypt', {key: privKey})) { |
| 132 | throw new Error('Key used for sync packet is locked'); |
| 133 | } |
| 134 | } else { |
| 135 | privKey = options.key; |
| 136 | password = options.password; |
| 137 | } |
| 138 | // unlock key if still locked |
| 139 | this.pwdControl = await createController('pwdDialog'); |
| 140 | const unlockedKey = await this.pwdControl.unlockKey({ |
| 141 | key: privKey, |
| 142 | reason: 'PWD_DIALOG_REASON_EDITOR', |
| 143 | password |
| 144 | }); |
| 145 | const syncPacket = await decryptSyncMessage(unlockedKey.key, message); |
| 146 | this.keyring.sync.mute(true); |
| 147 | await this.keyring.importKeys(syncPacket.keys); |
| 148 | await this.keyring.sync.merge(syncPacket.changeLog); |
| 149 | // remove keys with change log delete entry |
| 150 | const removeKeyAsync = (await this.keyring.sync.getDeleteEntries()).map(fingerprint => this.keyring.removeKey(fingerprint, 'public')); |
| 151 | await Promise.all(removeKeyAsync); |
| 152 | this.keyring.sync.mute(false); |
| 153 | // set eTag |
| 154 | this.keyring.sync.data.eTag = download.eTag; |
| 155 | } |
| 156 | |
| 157 | async uploadSyncMessage(options) { |
| 158 | // if key is in cache, specific unlock of sign key packet might be required |
no test coverage detected