Ejemplo n.º 1
0
    def verifySignature(self, msg: Dict[str, str]):
        signature = msg.get(f.SIG.nm)
        identifier = msg.get(IDENTIFIER)
        msgWithoutSig = {k: v for k, v in msg.items() if k != f.SIG.nm}
        # TODO This assumes the current key is the cryptonym. This is a BAD
        # ASSUMPTION!!! Indy needs to provide the current key.
        ser = serialize_msg_for_signing(msgWithoutSig)
        signature = b58decode(signature.encode())
        typ = msg.get(TYPE)
        # TODO: Maybe keeping ACCEPT_INVITE open is a better option than keeping
        # an if condition here?
        if typ == ACCEPT_INVITE:
            verkey = msg.get(VERKEY)
        else:
            try:
                link = self.getLinkForMsg(msg)
                verkey = self.getVerkeyForLink(link)
            except (ConnectionNotFound, VerkeyNotFound):
                # This is for verification of `NOTIFY` events
                link = self.wallet.getConnectionBy(remote=identifier)
                # TODO: If verkey is None, it should be fetched from Indy.
                # Assuming CID for now.
                verkey = link.remoteVerkey

        v = DidVerifier(verkey, identifier=identifier)
        if not v.verify(signature, ser):
            raise SignatureRejected
        else:
            if typ == ACCEPT_INVITE:
                self.logger.info('Signature accepted.')
            return True
Ejemplo n.º 2
0
    def verifySignature(self, msg: Dict[str, str]):
        signature = msg.get(f.SIG.nm)
        identifier = msg.get(IDENTIFIER)
        msgWithoutSig = {k: v for k, v in msg.items() if k != f.SIG.nm}
        # TODO This assumes the current key is the cryptonym. This is a BAD
        # ASSUMPTION!!! Indy needs to provide the current key.
        ser = serialize_msg_for_signing(msgWithoutSig)
        signature = b58decode(signature.encode())
        typ = msg.get(TYPE)
        # TODO: Maybe keeping ACCEPT_INVITE open is a better option than keeping
        # an if condition here?
        if typ == ACCEPT_INVITE:
            verkey = msg.get(VERKEY)
        else:
            try:
                link = self.getLinkForMsg(msg)
                verkey = self.getVerkeyForLink(link)
            except (ConnectionNotFound, VerkeyNotFound):
                # This is for verification of `NOTIFY` events
                link = self.wallet.getConnectionBy(remote=identifier)
                # TODO: If verkey is None, it should be fetched from Indy.
                # Assuming CID for now.
                verkey = link.remoteVerkey

        v = DidVerifier(verkey, identifier=identifier)
        if not v.verify(signature, ser):
            raise SignatureRejected
        else:
            if typ == ACCEPT_INVITE:
                self.logger.info('Signature accepted.')
            return True
Ejemplo n.º 3
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