Exemple #1
0
    def authenticate_multi(self,
                           msg: Dict,
                           signatures: Dict[str, str],
                           threshold: int = None,
                           verifier: Verifier = DidVerifier):
        num_sigs = len(signatures)
        if threshold is not None:
            if num_sigs < threshold:
                raise InsufficientSignatures(num_sigs, threshold)
        else:
            threshold = num_sigs
        correct_sigs_from = []
        for idr, sig in signatures.items():
            try:
                sig = base58.b58decode(sig)
            except Exception as ex:
                raise InvalidSignatureFormat from ex

            ser = self.serializeForSig(msg, identifier=idr)
            verkey = self.getVerkey(idr)

            if verkey is None:
                raise CouldNotAuthenticate(
                    'Can not find verkey for {}'.format(idr))

            vr = verifier(verkey, identifier=idr)
            if vr.verify(sig, ser):
                correct_sigs_from.append(idr)
                if len(correct_sigs_from) == threshold:
                    break
        else:
            raise InsufficientCorrectSignatures(len(correct_sigs_from),
                                                threshold)
        return correct_sigs_from
Exemple #2
0
    def authenticate(self,
                     msg: Dict,
                     identifier: str = None,
                     signature: str = None) -> str:
        try:
            if not signature:
                try:
                    signature = msg[f.SIG.nm]
                    if not signature:
                        raise EmptySignature(msg.get(f.IDENTIFIER.nm),
                                             msg.get(f.REQ_ID.nm))
                except KeyError:
                    raise MissingSignature(msg.get(f.IDENTIFIER.nm),
                                           msg.get(f.REQ_ID.nm))
            if not identifier:
                try:
                    identifier = msg[f.IDENTIFIER.nm]
                    if not identifier:
                        raise EmptyIdentifier(None, msg.get(f.REQ_ID.nm))
                except KeyError:
                    raise MissingIdentifier(identifier, msg.get(f.REQ_ID.nm))
            try:
                sig = base58.b58decode(signature)
            except Exception as ex:
                raise InvalidSignatureFormat from ex
            ser = self.serializeForSig(msg, topLevelKeysToIgnore=[f.SIG.nm])
            verkey = self.getVerkey(identifier)

            if verkey is None:
                raise CouldNotAuthenticate(
                    'Can not find verkey for DID {}'.format(identifier))

            vr = DidVerifier(verkey, identifier=identifier)
            isVerified = vr.verify(sig, ser)
            if not isVerified:
                raise InvalidSignature
        except SigningException as e:
            raise e
        except Exception as ex:
            raise CouldNotAuthenticate from ex
        return identifier