fix: allow overriding sign via contact settings if set

This commit is contained in:
James Houlahan
2020-08-25 16:29:51 +02:00
parent ef2ace0afe
commit 3916ddc8e4
3 changed files with 30 additions and 31 deletions

View File

@ -95,9 +95,8 @@ func (b *sendPreferencesBuilder) shouldEncrypt() bool {
return false return false
} }
func (b *sendPreferencesBuilder) withSign() { func (b *sendPreferencesBuilder) withSign(sign bool) {
v := true b.sign = &sign
b.sign = &v
} }
func (b *sendPreferencesBuilder) withSignDefault() { func (b *sendPreferencesBuilder) withSignDefault() {
@ -258,7 +257,7 @@ func (b *sendPreferencesBuilder) setInternalPGPSettings(
// We always encrypt and sign internal mail. // We always encrypt and sign internal mail.
b.withEncrypt(true) b.withEncrypt(true)
b.withSign() b.withSign(true)
// We use a custom scheme for internal messages. // We use a custom scheme for internal messages.
b.withScheme(pmInternal) b.withScheme(pmInternal)
@ -369,7 +368,7 @@ func (b *sendPreferencesBuilder) setExternalPGPSettingsWithWKDKeys(
// We always encrypt and sign external mail if WKD keys are present. // We always encrypt and sign external mail if WKD keys are present.
b.withEncrypt(true) b.withEncrypt(true)
b.withSign() b.withSign(true)
// If the contact has a specific Scheme preference, we set it (otherwise we // If the contact has a specific Scheme preference, we set it (otherwise we
// leave it unset to allow it to be filled in with the default value later). // leave it unset to allow it to be filled in with the default value later).
@ -402,13 +401,13 @@ func (b *sendPreferencesBuilder) setExternalPGPSettingsWithoutWKDKeys(
) (err error) { ) (err error) {
b.withEncrypt(vCardData.Encrypt) b.withEncrypt(vCardData.Encrypt)
if !vCardData.SignMissing && vCardData.Sign { if vCardData.SignIsSet {
b.withSign() b.withSign(vCardData.Sign)
} }
// Sign must be enabled whenever encrypt is. // Sign must be enabled whenever encrypt is.
if vCardData.Sign || vCardData.Encrypt { if vCardData.Encrypt {
b.withSign() b.withSign(true)
} }
// If the contact has a specific Scheme preference, we set it (otherwise we // If the contact has a specific Scheme preference, we set it (otherwise we
@ -479,7 +478,7 @@ func (b *sendPreferencesBuilder) setEncryptionPreferences(mailSettings pmapi.Mai
} }
if b.shouldEncrypt() { if b.shouldEncrypt() {
b.withSign() b.withSign(true)
} }
// If undefined, default to the user mail setting "Default PGP scheme". // If undefined, default to the user mail setting "Default PGP scheme".

View File

@ -243,7 +243,7 @@ func TestPreferencesBuilder(t *testing.T) {
{ {
name: "external with sign enabled", name: "external with sign enabled",
contactMeta: &ContactMetadata{Sign: true}, contactMeta: &ContactMetadata{Sign: true, SignIsSet: true},
receivedKeys: []pmapi.PublicKey{}, receivedKeys: []pmapi.PublicKey{},
isInternal: false, isInternal: false,
mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPMIMEPackage, DraftMIMEType: "text/html"}, mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPMIMEPackage, DraftMIMEType: "text/html"},
@ -272,7 +272,7 @@ func TestPreferencesBuilder(t *testing.T) {
{ {
name: "external with pinned contact public key, encrypted and signed", name: "external with pinned contact public key, encrypted and signed",
contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true}, contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true, SignIsSet: true},
receivedKeys: []pmapi.PublicKey{}, receivedKeys: []pmapi.PublicKey{},
isInternal: false, isInternal: false,
mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPMIMEPackage, DraftMIMEType: "text/html"}, mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPMIMEPackage, DraftMIMEType: "text/html"},
@ -287,7 +287,7 @@ func TestPreferencesBuilder(t *testing.T) {
{ {
name: "external with pinned contact public key, encrypted and signed using contact-specific pgp-inline", name: "external with pinned contact public key, encrypted and signed using contact-specific pgp-inline",
contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true, Scheme: pgpInline}, contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true, Scheme: pgpInline, SignIsSet: true},
receivedKeys: []pmapi.PublicKey{}, receivedKeys: []pmapi.PublicKey{},
isInternal: false, isInternal: false,
mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPMIMEPackage, DraftMIMEType: "text/html"}, mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPMIMEPackage, DraftMIMEType: "text/html"},
@ -302,7 +302,7 @@ func TestPreferencesBuilder(t *testing.T) {
{ {
name: "external with pinned contact public key, encrypted and signed using global pgp-inline", name: "external with pinned contact public key, encrypted and signed using global pgp-inline",
contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true}, contactMeta: &ContactMetadata{Keys: []string{testContactKey}, Encrypt: true, Sign: true, SignIsSet: true},
receivedKeys: []pmapi.PublicKey{}, receivedKeys: []pmapi.PublicKey{},
isInternal: false, isInternal: false,
mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPInlinePackage, DraftMIMEType: "text/html"}, mailSettings: pmapi.MailSettings{PGPScheme: pmapi.PGPInlinePackage, DraftMIMEType: "text/html"},

View File

@ -27,13 +27,13 @@ import (
) )
type ContactMetadata struct { type ContactMetadata struct {
Email string Email string
Keys []string Keys []string
Scheme string Scheme string
Sign bool Sign bool
SignMissing bool SignIsSet bool
Encrypt bool Encrypt bool
MIMEType string MIMEType string
} }
const ( const (
@ -72,22 +72,22 @@ func GetContactMetadataFromVCards(cards []pmapi.Card, email string) (contactMeta
// Warn: ParseBool treats 1, T, True, true as true and 0, F, Fale, false as false. // Warn: ParseBool treats 1, T, True, true as true and 0, F, Fale, false as false.
// However PMEL declares 'true' is true, 'false' is false. every other string is true // However PMEL declares 'true' is true, 'false' is false. every other string is true
encrypt, _ := strconv.ParseBool(parsedCard.GetValueByGroup(FieldPMEncrypt, group)) encrypt, _ := strconv.ParseBool(parsedCard.GetValueByGroup(FieldPMEncrypt, group))
var sign, signMissing bool var sign, signIsSet bool
if len(parsedCard[FieldPMSign]) == 0 { if len(parsedCard[FieldPMSign]) == 0 {
signMissing = true signIsSet = false
} else { } else {
sign, _ = strconv.ParseBool(parsedCard.GetValueByGroup(FieldPMSign, group)) sign, _ = strconv.ParseBool(parsedCard.GetValueByGroup(FieldPMSign, group))
signMissing = false signIsSet = true
} }
mimeType := parsedCard.GetValueByGroup(FieldPMMIMEType, group) mimeType := parsedCard.GetValueByGroup(FieldPMMIMEType, group)
return &ContactMetadata{ return &ContactMetadata{
Email: email, Email: email,
Keys: keys, Keys: keys,
Scheme: scheme, Scheme: scheme,
Sign: sign, Sign: sign,
SignMissing: signMissing, SignIsSet: signIsSet,
Encrypt: encrypt, Encrypt: encrypt,
MIMEType: mimeType, MIMEType: mimeType,
}, nil }, nil
} }
return &ContactMetadata{}, nil return &ContactMetadata{}, nil