* @param {Object} options * @param {openpgp.key.Key} options.key - key to unlock * @param {String} [options.reason] - optional explanation for password dialog * @param {Boolean} [options.openPopup=true] - password popup required (false if dialog appears integrated) * @param {Function} [o
(options)
| 94 | * @return {Promise<Object, Error>} - resolves with unlocked key and password {key: openpgp.key.Key, password: String} |
| 95 | */ |
| 96 | async unlock(options) { |
| 97 | this.options = options; |
| 98 | this.options.reason ??= ''; |
| 99 | this.options.openPopup ??= true; |
| 100 | const cacheEntry = await pwdCache.get(this.options.key.getFingerprint(), options.message); |
| 101 | if (cacheEntry && !options.noCache) { |
| 102 | if (cacheEntry.key) { |
| 103 | return cacheEntry; |
| 104 | } |
| 105 | // password is in cache |
| 106 | this.options.password = cacheEntry.password; |
| 107 | } |
| 108 | if (this.keyIsDecrypted(this.options) && !options.noCache) { |
| 109 | // secret-key data is not encrypted, nothing to do |
| 110 | return {key: this.options.key, password: this.options.password}; |
| 111 | } |
| 112 | if (this.options.password) { |
| 113 | // secret-key data is encrypted, but we have password |
| 114 | const key = await pwdCache.unlock(this.options); |
| 115 | return {key, password: this.options.password}; |
| 116 | } |
| 117 | this.passwordRequest = Promise.withResolvers(); |
| 118 | if (this.options.beforePasswordRequest) { |
| 119 | this.options.beforePasswordRequest(this.id); |
| 120 | } |
| 121 | if (this.options.openPopup) { |
| 122 | setTimeout(async () => { |
| 123 | const popup = await mvelo.windows.openPopup(`components/enter-password/passwordDialog.html?id=${this.id}`, {width: 580, height: 490}); |
| 124 | this.receivedPortMsg = false; |
| 125 | this.pwdPopup = popup; |
| 126 | popup.addRemoveListener(() => { |
| 127 | if (!this.receivedPortMsg) { |
| 128 | this.pwdPopup = null; |
| 129 | this.onCancel(); |
| 130 | } |
| 131 | }); |
| 132 | }, 50); |
| 133 | } |
| 134 | return this.passwordRequest.promise; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Check if key is decrypted. As openpgp.decryptKey always decrypts all key packets, we only check the primary key status. |
no test coverage detected