Merge pull request #336 from simple-login/test-pr

Test pr
This commit is contained in:
Son Nguyen Kim 2020-11-26 10:27:43 +01:00 committed by GitHub
commit 56a74c961c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 10 deletions

View file

@ -4,7 +4,7 @@ from flask_login import login_required, current_user
from app.dashboard.base import dashboard_bp from app.dashboard.base import dashboard_bp
from app.extensions import db from app.extensions import db
from app.models import Contact from app.models import Contact
from app.pgp_utils import PGPException, load_public_key from app.pgp_utils import PGPException, load_public_key, load_public_key_and_check
@dashboard_bp.route("/contact/<int:contact_id>/", methods=["GET", "POST"]) @dashboard_bp.route("/contact/<int:contact_id>/", methods=["GET", "POST"])
@ -28,7 +28,9 @@ def contact_detail_route(contact_id):
contact.pgp_public_key = request.form.get("pgp") contact.pgp_public_key = request.form.get("pgp")
try: try:
contact.pgp_finger_print = load_public_key(contact.pgp_public_key) contact.pgp_finger_print = load_public_key_and_check(
contact.pgp_public_key
)
except PGPException: except PGPException:
flash("Cannot add the public key, please verify it", "error") flash("Cannot add the public key, please verify it", "error")
else: else:

View file

@ -16,7 +16,7 @@ from app.extensions import db
from app.log import LOG from app.log import LOG
from app.models import Alias, AuthorizedAddress from app.models import Alias, AuthorizedAddress
from app.models import Mailbox from app.models import Mailbox
from app.pgp_utils import PGPException, load_public_key from app.pgp_utils import PGPException, load_public_key, load_public_key_and_check
class ChangeEmailForm(FlaskForm): class ChangeEmailForm(FlaskForm):
@ -133,7 +133,9 @@ def mailbox_detail_route(mailbox_id):
mailbox.pgp_public_key = request.form.get("pgp") mailbox.pgp_public_key = request.form.get("pgp")
try: try:
mailbox.pgp_finger_print = load_public_key(mailbox.pgp_public_key) mailbox.pgp_finger_print = load_public_key_and_check(
mailbox.pgp_public_key
)
except PGPException: except PGPException:
flash("Cannot add the public key, please verify it", "error") flash("Cannot add the public key, please verify it", "error")
else: else:

View file

@ -1735,7 +1735,7 @@ class Mailbox(db.Model, ModelMixin):
return ret return ret
def __repr__(self): def __repr__(self):
return f"<Mailbox {self.email}>" return f"<Mailbox {self.id} {self.email}>"
class AccountActivation(db.Model, ModelMixin): class AccountActivation(db.Model, ModelMixin):

View file

@ -33,18 +33,20 @@ def load_public_key_and_check(public_key: str) -> str:
If the encryption fails, remove the newly created fingerprint. If the encryption fails, remove the newly created fingerprint.
Return the fingerprint Return the fingerprint
""" """
import_result = gpg.import_keys(public_key)
try: try:
import_result = gpg.import_keys(public_key)
fingerprint = import_result.fingerprints[0] fingerprint = import_result.fingerprints[0]
except Exception as e: except Exception as e:
raise PGPException("Cannot load key") from e raise PGPException("Cannot load key") from e
else: else:
dummy_data = BytesIO(b"test") dummy_data = BytesIO(b"test")
r = gpg.encrypt_file(dummy_data, fingerprint) try:
if not r.ok: r = encrypt_file(dummy_data, fingerprint)
except Exception as e:
LOG.exception("Cannot encrypt using the imported key")
# remove the fingerprint # remove the fingerprint
gpg.delete_keys([fingerprint]) gpg.delete_keys([fingerprint])
raise PGPException("Encryption fails with the key") raise PGPException("Encryption fails with the key") from e
return fingerprint return fingerprint

View file

@ -180,7 +180,7 @@ def get_or_create_contact(
_, contact_email = parseaddr_unicode(mail_from) _, contact_email = parseaddr_unicode(mail_from)
if not is_valid_email(contact_email): if not is_valid_email(contact_email):
LOG.exception( LOG.warning(
"invalid contact email %s. Parse from %s %s", "invalid contact email %s. Parse from %s %s",
contact_email, contact_email,
contact_from_header, contact_from_header,