@@ -538,32 +538,30 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
538538 self_device_ids = list (self_signatures .keys ())
539539 try :
540540 # get our self-signing key to verify the signatures
541- self_signing_key , self_signing_key_id , self_signing_verify_key \
542- = yield self ._get_e2e_cross_signing_verify_key (
543- user_id , "self_signing"
544- )
541+ self_signing_key , self_signing_key_id , self_signing_verify_key = yield self ._get_e2e_cross_signing_verify_key (
542+ user_id , "self_signing"
543+ )
545544
546545 # get our master key, since it may be signed
547- master_key , master_key_id , master_verify_key \
548- = yield self ._get_e2e_cross_signing_verify_key (
549- user_id , "master"
550- )
546+ master_key , master_key_id , master_verify_key = yield self ._get_e2e_cross_signing_verify_key (
547+ user_id , "master"
548+ )
551549
552550 # fetch our stored devices. This is used to 1. verify
553551 # signatures on the master key, and 2. to can compare with what
554552 # was sent if the device was signed
555553 devices = yield self .store .get_e2e_device_keys ([(user_id , None )])
556554
557555 if user_id not in devices :
558- raise SynapseError (
559- 404 , "No device keys found" , Codes .NOT_FOUND
560- )
556+ raise SynapseError (404 , "No device keys found" , Codes .NOT_FOUND )
561557
562558 devices = devices [user_id ]
563559 for device_id , device in self_signatures .items ():
564560 try :
565- if ("signatures" not in device or
566- user_id not in device ["signatures" ]):
561+ if (
562+ "signatures" not in device
563+ or user_id not in device ["signatures" ]
564+ ):
567565 # no signature was sent
568566 raise SynapseError (
569567 400 , "Invalid signature" , Codes .INVALID_SIGNATURE
@@ -581,15 +579,21 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
581579 # signature list. (In practice, we're likely to
582580 # only have only one signature anyways.)
583581 master_key_signature_list = []
584- for signing_key_id , signature in device ["signatures" ][user_id ].items ():
582+ for signing_key_id , signature in device ["signatures" ][
583+ user_id
584+ ].items ():
585585 alg , signing_device_id = signing_key_id .split (":" , 1 )
586- if (signing_device_id not in devices or
587- signing_key_id not in
588- devices [signing_device_id ]["keys" ]["keys" ]):
586+ if (
587+ signing_device_id not in devices
588+ or signing_key_id
589+ not in devices [signing_device_id ]["keys" ]["keys" ]
590+ ):
589591 # signed by an unknown device, or the
590592 # device does not have the key
591593 raise SynapseError (
592- 400 , "Invalid signature" , Codes .INVALID_SIGNATURE
594+ 400 ,
595+ "Invalid signature" ,
596+ Codes .INVALID_SIGNATURE ,
593597 )
594598
595599 sigs = device ["signatures" ]
@@ -600,12 +604,12 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
600604 master_key .pop ("unsigned" , None )
601605
602606 if master_key != device :
603- raise SynapseError (
604- 400 , "Key does not match"
605- )
607+ raise SynapseError (400 , "Key does not match" )
606608
607609 # get the key and check the signature
608- pubkey = devices [signing_device_id ]["keys" ]["keys" ][signing_key_id ]
610+ pubkey = devices [signing_device_id ]["keys" ]["keys" ][
611+ signing_key_id
612+ ]
609613 verify_key = decode_verify_key_bytes (
610614 signing_key_id , decode_base64 (pubkey )
611615 )
@@ -614,7 +618,9 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
614618 verify_signed_json (device , user_id , verify_key )
615619 except SignatureVerifyException :
616620 raise SynapseError (
617- 400 , "Invalid signature" , Codes .INVALID_SIGNATURE
621+ 400 ,
622+ "Invalid signature" ,
623+ Codes .INVALID_SIGNATURE ,
618624 )
619625
620626 master_key_signature_list .append (
@@ -636,11 +642,10 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
636642 try :
637643 stored_device = devices [device_id ]["keys" ]
638644 except KeyError :
639- raise SynapseError (
640- 404 , "Unknown device" , Codes .NOT_FOUND
641- )
642- if self_signing_key_id in stored_device .get ("signatures" , {}) \
643- .get (user_id , {}):
645+ raise SynapseError (404 , "Unknown device" , Codes .NOT_FOUND )
646+ if self_signing_key_id in stored_device .get (
647+ "signatures" , {}
648+ ).get (user_id , {}):
644649 # we already have a signature on this device, so we
645650 # can skip it, since it should be exactly the same
646651 continue
@@ -654,8 +659,9 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
654659 (self_signing_key_id , user_id , device_id , signature )
655660 )
656661 except SynapseError as e :
657- failures .setdefault (user_id , {})[device_id ] \
658- = _exception_to_failure (e )
662+ failures .setdefault (user_id , {})[
663+ device_id
664+ ] = _exception_to_failure (e )
659665 except SynapseError as e :
660666 failures [user_id ] = {
661667 device : _exception_to_failure (e )
@@ -669,20 +675,18 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
669675
670676 try :
671677 # get our user-signing key to verify the signatures
672- user_signing_key , user_signing_key_id , user_signing_verify_key \
673- = yield self ._get_e2e_cross_signing_verify_key (
674- user_id , "user_signing"
675- )
678+ user_signing_key , user_signing_key_id , user_signing_verify_key = yield self ._get_e2e_cross_signing_verify_key (
679+ user_id , "user_signing"
680+ )
676681
677682 for user , devicemap in signatures .items ():
678683 device_id = None
679684 try :
680685 # get the user's master key, to make sure it matches
681686 # what was sent
682- stored_key , stored_key_id , _ \
683- = yield self ._get_e2e_cross_signing_verify_key (
684- user , "master" , user_id
685- )
687+ stored_key , stored_key_id , _ = yield self ._get_e2e_cross_signing_verify_key (
688+ user , "master" , user_id
689+ )
686690
687691 # make sure that the user's master key is the one that
688692 # was signed (and no others)
@@ -693,26 +697,25 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
693697 device_id = None
694698 logger .error (
695699 "upload signature: wrong device: %s vs %s" ,
696- device , devicemap
697- )
698- raise SynapseError (
699- 404 , "Unknown device" , Codes .NOT_FOUND
700+ device ,
701+ devicemap ,
700702 )
703+ raise SynapseError (404 , "Unknown device" , Codes .NOT_FOUND )
701704 key = devicemap [device_id ]
702705 del devicemap [device_id ]
703706 if len (devicemap ) > 0 :
704707 # other devices were signed -- mark those as failures
705708 logger .error ("upload signature: too many devices specified" )
706- failure = _exception_to_failure (SynapseError (
707- 404 , "Unknown device" , Codes .NOT_FOUND
708- ))
709+ failure = _exception_to_failure (
710+ SynapseError ( 404 , "Unknown device" , Codes .NOT_FOUND )
711+ )
709712 failures [user ] = {
710- device : failure
711- for device in devicemap .keys ()
713+ device : failure for device in devicemap .keys ()
712714 }
713715
714- if user_signing_key_id in stored_key .get ("signatures" , {}) \
715- .get (user_id , {}):
716+ if user_signing_key_id in stored_key .get ("signatures" , {}).get (
717+ user_id , {}
718+ ):
716719 # we already have the signature, so we can skip it
717720 continue
718721
@@ -729,17 +732,15 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
729732 failure = _exception_to_failure (e )
730733 if device_id is None :
731734 failures [user ] = {
732- device_id : failure
733- for device_id in devicemap .keys ()
735+ device_id : failure for device_id in devicemap .keys ()
734736 }
735737 else :
736738 failures .setdefault (user , {})[device_id ] = failure
737739 except SynapseError as e :
738740 failure = _exception_to_failure (e )
739741 for user , devicemap in signature .items ():
740742 failures [user ] = {
741- device_id : failure
742- for device_id in devicemap .keys ()
743+ device_id : failure for device_id in devicemap .keys ()
743744 }
744745
745746 # store the signature, and send the appropriate notifications for sync
@@ -749,7 +750,9 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
749750 if len (self_device_ids ):
750751 yield self .device_handler .notify_device_update (user_id , self_device_ids )
751752 if len (signed_users ):
752- yield self .device_handler .notify_user_signature_update (user_id , signed_users )
753+ yield self .device_handler .notify_user_signature_update (
754+ user_id , signed_users
755+ )
753756
754757 defer .returnValue ({"failures" : failures })
755758
@@ -761,9 +764,7 @@ def _get_e2e_cross_signing_verify_key(self, user_id, key_type, from_user_id=None
761764 if key is None :
762765 logger .error ("no %s key found for %s" , key_type , user_id )
763766 raise SynapseError (
764- 404 ,
765- "No %s key found for %s" % (key_type , user_id ),
766- Codes .NOT_FOUND
767+ 404 , "No %s key found for %s" % (key_type , user_id ), Codes .NOT_FOUND
767768 )
768769 key_id , verify_key = get_verify_key_from_cross_signing_key (key )
769770 return key , key_id , verify_key
@@ -811,14 +812,13 @@ def _check_device_signature(user_id, verify_key, signed_device, stored_device):
811812 key_id = "%s:%s" % (verify_key .alg , verify_key .version )
812813
813814 # make sure the device is signed
814- if ("signatures" not in signed_device or user_id not in signed_device ["signatures" ]
815- or key_id not in signed_device ["signatures" ][user_id ]):
815+ if (
816+ "signatures" not in signed_device
817+ or user_id not in signed_device ["signatures" ]
818+ or key_id not in signed_device ["signatures" ][user_id ]
819+ ):
816820 logger .error ("upload signature: user not found in signatures" )
817- raise SynapseError (
818- 400 ,
819- "Invalid signature" ,
820- Codes .INVALID_SIGNATURE
821- )
821+ raise SynapseError (400 , "Invalid signature" , Codes .INVALID_SIGNATURE )
822822
823823 signature = signed_device ["signatures" ][user_id ][key_id ]
824824
@@ -831,28 +831,19 @@ def _check_device_signature(user_id, verify_key, signed_device, stored_device):
831831 if signed_device != stored_device :
832832 logger .error (
833833 "upload signatures: key does not match %s vs %s" ,
834- signed_device , stored_device
835- )
836- raise SynapseError (
837- 400 , "Key does not match" ,
834+ signed_device ,
835+ stored_device ,
838836 )
837+ raise SynapseError (400 , "Key does not match" )
839838
840839 # check the signature
841- signed_device ["signatures" ] = {
842- user_id : {
843- key_id : signature
844- }
845- }
840+ signed_device ["signatures" ] = {user_id : {key_id : signature }}
846841
847842 try :
848843 verify_signed_json (signed_device , user_id , verify_key )
849844 except SignatureVerifyException :
850845 logger .error ("invalid signature on key" )
851- raise SynapseError (
852- 400 ,
853- "Invalid signature" ,
854- Codes .INVALID_SIGNATURE
855- )
846+ raise SynapseError (400 , "Invalid signature" , Codes .INVALID_SIGNATURE )
856847
857848
858849def _exception_to_failure (e ):
0 commit comments