Skip to content

Commit

Permalink
Merge pull request #9 from nextleap-project/claim-content
Browse files Browse the repository at this point in the history
dicts as claim content
  • Loading branch information
hpk42 authored Apr 11, 2018
2 parents 2c6a93a + c1243f0 commit 73f8648
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 41 deletions.
47 changes: 17 additions & 30 deletions muacryptcc/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def process_incoming_gossip(self, addr2pagh, account_key, dec_msg):
recipients = get_target_emailadr(dec_msg)
for recipient in recipients:
pagh = addr2pagh[recipient]
value = self.read_claim_from(peers_chain, recipient)
value = self.read_claim(recipient, chain=peers_chain)
if value:
# for now we can only read claims about ourselves...
# so if we get a value it must be our head imprint.
assert value == bytes2ascii(pagh.keydata)
assert value['key'] == bytes2ascii(pagh.keydata)

@hookimpl
def process_before_encryption(self, sender_addr, sender_keyhandle,
Expand All @@ -65,10 +65,13 @@ def process_before_encryption(self, sender_addr, sender_keyhandle,
logging.error("no recipients found.\n")

for recipient in recipients:
claim = recipient, bytes2ascii(recipient2keydata.get(recipient))
key = recipient
value = dict(
key=bytes2ascii(recipient2keydata.get(recipient))
)
for reader in recipients:
pk = self.addr2pk.get(reader)
self.add_claim(claim, access_pk=pk)
self.add_claim((key, value), access_pk=pk)

self.commit_to_chain()
payload_msg["GossipClaims"] = self.head_imprint
Expand Down Expand Up @@ -122,37 +125,21 @@ def commit_to_chain(self):
with self.params.as_default():
self.head = self.state.commit(chain)

def read_claim(self, claimkey):
return self.read_claim_as(self, claimkey)

def read_claim_as(self, other, claimkey):
assert callable(getattr(claimkey, 'encode', None))
print("read-claim-as", other, repr(claimkey))
chain = self._get_current_chain()
with other.params.as_default():
return View(chain)[claimkey.encode('utf-8')].decode('utf-8')

def read_claim_from(self, chain, claimkey):
assert callable(getattr(claimkey, 'encode', None))
def read_claim(self, claimkey, chain=None, reader=None):
if chain is None:
chain = self._get_current_chain()
if reader is None:
reader = self
try:
with self.params.as_default():
return View(chain)[claimkey.encode('utf-8')].decode('utf-8')
with reader.params.as_default():
value = View(chain)[claimkey.encode('utf-8')]
return json.loads(value.decode('utf-8'))
except (KeyError, ValueError):
return None

def has_readable_claim(self, claimkey):
return self.has_readable_claim_for(self, claimkey)

def has_readable_claim_for(self, other, claimkey):
assert isinstance(claimkey, bytes)
try:
self.read_claim_as(other, claimkey)
except (KeyError, ValueError):
return False
return True

def add_claim(self, claim, access_pk=None):
key, value = claim[0].encode('utf-8'), claim[1].encode('utf-8')
key = claim[0].encode('utf-8')
value = json.dumps(claim[1]).encode('utf-8')
assert isinstance(key, bytes)
assert isinstance(value, bytes)
self.state[key] = value
Expand Down
8 changes: 5 additions & 3 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_claim_headers_in_encrypted_mail(account_maker):
dec_msg = send_encrypted_mail(acc2, acc1)[1].dec_msg
cc2 = get_cc_account(dec_msg['ChainStore'])
assert dec_msg['GossipClaims'] == cc2.head_imprint
assert cc2.has_readable_claim(acc1.addr)
assert cc2.read_claim(acc1.addr)


def test_claims_contain_keys(account_maker):
Expand All @@ -35,7 +35,8 @@ def test_claims_contain_keys(account_maker):
cc2, ac2 = get_cc_and_ac(send_encrypted_mail(acc2, acc1))
cc1, ac1 = get_cc_and_ac(send_encrypted_mail(acc1, acc2))

assert cc1.read_claim_as(cc2, acc2.addr) == bytes2ascii(ac2.keydata)
data = cc1.read_claim(acc2.addr, reader=cc2)
assert data['key'] == bytes2ascii(ac2.keydata)


def test_gossip_claims(account_maker):
Expand All @@ -47,7 +48,8 @@ def test_gossip_claims(account_maker):
cc3, ac3 = get_cc_and_ac(send_encrypted_mail(acc3, acc1))
cc1, ac1 = get_cc_and_ac(send_encrypted_mail(acc1, [acc2, acc3]))

assert cc1.read_claim_as(cc2, acc3.addr) == bytes2ascii(ac3.keydata)
data = cc1.read_claim(acc3.addr, reader=cc2)
assert data['key'] == bytes2ascii(ac3.keydata)


# send a mail from acc1 with autocrypt key to acc2
Expand Down
16 changes: 8 additions & 8 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ def test_account_can_be_propertly_instanted_from_store(make_account):

def test_add_claim_with_access_control(make_account):
cc_alice = make_account("alice")
cc_bob = make_account("bob")
cc_bob = make_account("bo")
bob_pk = cc_bob.get_public_key()

assert not cc_alice.has_readable_claim(b"bob_hair")
assert not cc_alice.read_claim("bob_hair")

cc_alice.add_claim(
claim=(b"bob_hair", b"black")
claim=("bob_hair", "black")
)
cc_alice.commit_to_chain()
assert cc_alice.has_readable_claim(b"bob_hair")
assert cc_alice.read_claim("bob_hair")

cc_alice.add_claim(claim=(b"bob_feet", b"4"), access_pk=bob_pk)
cc_alice.add_claim(claim=("bob_feet", "4"), access_pk=bob_pk)
cc_alice.commit_to_chain()
assert cc_alice.has_readable_claim_for(cc_bob, b"bob_feet")
assert cc_alice.has_readable_claim_for(cc_alice, b"bob_feet")
assert not cc_alice.has_readable_claim_for(cc_bob, b"bob_hair")
assert cc_alice.read_claim("bob_feet", reader=cc_bob)
assert cc_alice.read_claim("bob_feet", reader=cc_alice)
assert not cc_alice.read_claim("bob_hair", reader=cc_bob)

0 comments on commit 73f8648

Please sign in to comment.