Browse Source

Refactor set_identity, parse name_addr in it

master
juga 2 years ago
parent
commit
65f8a7f387
3 changed files with 13 additions and 26 deletions
  1. +2
    -8
      mixnet/client/client.py
  2. +8
    -9
      mixnet/common.py
  3. +3
    -9
      tests/test_remailer.py

+ 2
- 8
mixnet/client/client.py View File

@ -1,5 +1,4 @@
"""Mixnet client library."""
import email
import logging
import secrets
@ -19,11 +18,7 @@ def get_nodes():
);
For now just return identities.
"""
carol = common.set_identity(
constants.CAROL_ADDRESS,
constants.CAROL_NAME,
constants.CAROL_NAME_ADDR,
)
carol = common.set_identity(constants.CAROL_NAME_ADDR, constants.CAROL_FPR)
return [carol]
@ -49,8 +44,7 @@ def onionize(me, msg, route):
def process(from_, to, subject, body):
me = common.set_my_identity(from_)
name, address = email.utils.parseaddr(to)
recipient = common.set_identity(address, name, to)
recipient = common.set_identity(to)
logger.debug(recipient)
msg = common.create_outgoing_msg(me, recipient, subject, body)
enc_msg = msg.encrypt()


+ 8
- 9
mixnet/common.py View File

@ -43,23 +43,23 @@ def import_keys(keyring_data):
raise (e)
def set_identity(address, name, name_address, fpr=""):
def set_identity(name_addr, fpr=""):
import pEp
ident = pEp.Identity(address, name, name_address, fpr, 0, "")
name, address = email.utils.parseaddr(name_addr)
logger.debug("name: %s, address: %s", name, address)
if not re.match(defaults.BASIC_EMAIL_RE, address):
raise exceptions.NoEmailAddress
ident = pEp.Identity(address, name, name_addr, fpr, 0, "")
ident.username = name
return ident
def set_my_identity(name_addr, fpr=None):
def set_my_identity(name_addr, fpr=""):
import pEp
name, address = email.utils.parseaddr(name_addr)
logger.debug("name: %s, address: %s", name, address)
if not re.match(defaults.BASIC_EMAIL_RE, address):
raise exceptions.NoEmailAddress
me = set_identity(name_addr, fpr)
if fpr:
me = set_identity(address, name, name_addr, fpr)
try:
pEp.set_own_key(me, fpr)
except RuntimeError as e:
@ -68,7 +68,6 @@ def set_my_identity(name_addr, fpr=None):
raise (e)
logger.info("Set my own identity to: %s, %s", me, fpr)
else:
me = set_identity(address, name, name_addr)
pEp.myself(me)
logger.info("Set my own identity to: %s", me)
return me


+ 3
- 9
tests/test_remailer.py View File

@ -7,14 +7,10 @@ from mixnet.remailer import remailer
from .constants import (
ALICE_FP,
ALICE_NAME_ADDR,
BOB_ADDRESS,
BOB_FP,
BOB_NAME,
BOB_NAME_ADDR,
BODY,
CAROL_ADDRESS,
CAROL_FP,
CAROL_NAME,
CAROL_NAME_ADDR,
SUBJECT_A_B,
SUBJECT_B_C,
@ -23,7 +19,7 @@ from .constants import (
def test_encrypt_decrypt(import_keys):
alice = common.set_my_identity(ALICE_NAME_ADDR, ALICE_FP)
bob = common.set_identity(BOB_ADDRESS, BOB_NAME, BOB_NAME_ADDR, BOB_FP)
bob = common.set_identity(BOB_NAME_ADDR, BOB_FP)
msg_a_b = common.create_outgoing_msg(alice, bob, SUBJECT_A_B, BODY)
enc_msg_a_b = msg_a_b.encrypt()
assert enc_msg_a_b.enc_format == 3
@ -49,15 +45,13 @@ def test_encrypt_twice_decrypt(import_keys):
Alice -> Carol -> Bob
"""
alice = common.set_my_identity(ALICE_NAME_ADDR, ALICE_FP)
bob = common.set_identity(BOB_ADDRESS, BOB_NAME, BOB_NAME_ADDR, BOB_FP)
bob = common.set_identity(BOB_NAME_ADDR, BOB_FP)
# msg_a_b from Alice to Bob
msg_a_b = common.create_outgoing_msg(alice, bob, SUBJECT_A_B, BODY)
enc_msg_a_b = msg_a_b.encrypt()
assert enc_msg_a_b.enc_format == 3
carol = common.set_identity(
CAROL_ADDRESS, CAROL_NAME, CAROL_NAME_ADDR, CAROL_FP
)
carol = common.set_identity(CAROL_NAME_ADDR, CAROL_FP)
# msg_a_c from Alice to Carol including msg_a_b to Bob
msg_a_c = common.create_outgoing_msg(
alice, carol, SUBJECT_B_C, str(enc_msg_a_b)


Loading…
Cancel
Save