Mailspring/internal_packages/keybase/lib/decrypt-button.cjsx
Logan Davis 40f9e5172a pgp-plugin update (#2534)
* Fix private key email-adder, add "no private key" error

The decrypt UI is seriously confusing some people. This commit
adds an error message that should at least stop them from trying
to decrypt a message without a private key to speak of. Also,
there was a dumb hardcoded true in validAddress.

* Adds incorrect passphrase notification; enables carriage return for popover

The passphrase popover was woefully inadequate. It didn't tell users
when they had the wrong password - it just closed without saying anything -
and you couldn't even use carriage return to submit the password.
This commit fixes those mistakes by buffing out passphrase-popover.cjsx.

* Adds private key popover to decrypt button

The decrypt UI was confusing and didn't provide the user with
an option to get a key imported from the message view. This
mondo commit adds an entirely new popover so that the user
never again will be forced to go to the preferences page.

* Adds more forgiving encrypted block parsing

* Overhauls decryption error handling

The decrypt UI didn't clearly communicate error messages from the
failure in PGPKeyStore.decrypt up to the user. This commit adds
nice error surfacing as well as some pretty colors.

* Fix encrypt modal key miscount error via getKeyContents coercion

On Linux and Windows, fs.watch double-triggers on some actions in
the key folder, for reasons that aren't super clear. This was causing
issues where the encrypt modal would report not having a key loaded
even though the key was totally loaded and saved. This commit sort
of kludgily forces the modal to run getKeyContents for every key
it has saved right before it returns them all. This would probably
be better fixed with a refactor the the PGP Keystore.

* remember to close popover, d'oh

* patch key picker modal styling for Linux and Windows

* response to review
2016-07-08 11:29:10 -07:00

144 lines
5.2 KiB
CoffeeScript
Executable file

{MessageStore, React, ReactDOM, FileDownloadStore, MessageBodyProcessor, Actions} = require 'nylas-exports'
PGPKeyStore = require './pgp-key-store'
{remote} = require 'electron'
PassphrasePopover = require './passphrase-popover'
PrivateKeyPopover = require './private-key-popover'
pgp = require 'kbpgp'
_ = require 'underscore'
class DecryptMessageButton extends React.Component
@displayName: 'DecryptMessageButton'
@propTypes:
message: React.PropTypes.object.isRequired
constructor: (props) ->
super(props)
@state = @_getStateFromStores()
_getStateFromStores: ->
return {
isDecrypted: PGPKeyStore.isDecrypted(@props.message)
wasEncrypted: PGPKeyStore.hasEncryptedComponent(@props.message)
encryptedAttachments: PGPKeyStore.fetchEncryptedAttachments(@props.message)
status: PGPKeyStore.msgStatus(@props.message)
}
componentDidMount: ->
@unlistenKeystore = PGPKeyStore.listen(@_onKeystoreChange, @)
componentWillUnmount: ->
@unlistenKeystore()
_onKeystoreChange: ->
# every time a new key gets unlocked/fetched, try to decrypt this message
if not @state.isDecrypted
PGPKeyStore.decrypt(@props.message)
@setState(@_getStateFromStores())
_onClickDecrypt: (event) =>
popoverTarget = event.target.getBoundingClientRect()
if @_noPrivateKeys()
Actions.openPopover(
<PrivateKeyPopover
addresses={_.pluck(@props.message.to, "email")}
callback={() => @_openPassphrasePopover(popoverTarget, @decryptPopoverDone)}/>,
{originRect: popoverTarget, direction: 'down'}
)
else
@_openPassphrasePopover(popoverTarget, @decryptPopoverDone)
_displayError: (err) ->
dialog = remote.dialog
dialog.showErrorBox('Decryption Error', err.toString())
_onClickDecryptAttachments: (event) =>
popoverTarget = event.target.getBoundingClientRect()
if @_noPrivateKeys()
Actions.openPopover(
<PrivateKeyPopover
addresses={_.pluck(@props.message.to, "email")}
callback={() => @_openPassphrasePopover(popoverTarget, @decryptAttachmentsPopoverDone)}/>,
{originRect: popoverTarget, direction: 'down'}
)
else
@_openPassphrasePopover(popoverTarget, @decryptAttachmentsPopoverDone)
decryptPopoverDone: (passphrase) =>
for recipient in @props.message.to
# right now, just try to unlock all possible keys
# (many will fail - TODO?)
privateKeys = PGPKeyStore.privKeys(address: recipient.email, timed: false)
for privateKey in privateKeys
PGPKeyStore.getKeyContents(key: privateKey, passphrase: passphrase)
decryptAttachmentsPopoverDone: (passphrase) =>
for recipient in @props.message.to
privateKeys = PGPKeyStore.privKeys(address: recipient.email, timed: false)
for privateKey in privateKeys
PGPKeyStore.getKeyContents(key: privateKey, passphrase: passphrase, callback: (identity) => PGPKeyStore.decryptAttachments(identity, @state.encryptedAttachments))
_openPassphrasePopover: (target, callback) =>
Actions.openPopover(
<PassphrasePopover addresses={_.pluck(@props.message.to, "email")} onPopoverDone={callback} />,
{originRect: target, direction: 'down'}
)
_noPrivateKeys: =>
numKeys = 0
for recipient in @props.message.to
numKeys = numKeys + PGPKeyStore.privKeys(address: recipient.email, timed: false).length
return numKeys < 1
render: =>
if not (@state.wasEncrypted or @state.encryptedAttachments.length > 0)
return false
title = "Message Encrypted"
decryptLabel = "Decrypt"
borderClass = "border"
decryptClass = "decrypt-bar"
if @state.status?
if @state.status.indexOf("Message decrypted") >= 0
title = @state.status
borderClass = "border done-border"
decryptClass = "decrypt-bar done-decrypt-bar"
else if @state.status.indexOf("Unable to decrypt message.") >= 0
title = @state.status
borderClass = "border error-border"
decryptClass = "decrypt-bar error-decrypt-bar"
decryptLabel = "Try Again"
decryptBody = false
if !@state.isDecrypted and !(@state.status?.indexOf("malformed") >= 0)
decryptBody = <button title="Decrypt email body" className="btn btn-toolbar" onClick={@_onClickDecrypt} ref="button">{decryptLabel}</button>
decryptAttachments = false
if @state.encryptedAttachments?.length >= 1
title = if @state.encryptedAttachments.length == 1 then "Attachment Encrypted" else "Attachments Encrypted"
buttonLabel = if @state.encryptedAttachments.length == 1 then "Decrypt Attachment" else "Decrypt Attachments"
decryptAttachments = <button onClick={ @_onClickDecryptAttachments } className="btn btn-toolbar">{buttonLabel}</button>
if decryptAttachments or decryptBody
decryptionInterface =
<div className="decryption-interface">
{decryptBody}
{decryptAttachments}
</div>
<div className="keybase-decrypt">
<div className="line-w-label">
<div className={borderClass}></div>
<div className={decryptClass}>
<div className="title-text">
{title}
</div>
{decryptionInterface}
</div>
<div className={borderClass}></div>
</div>
</div>
module.exports = DecryptMessageButton