How to use the pypsrp._utils.to_bytes function in pypsrp

To help you get started, we’ve selected a few pypsrp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jborean93 / pypsrp / tests / runner.py View on Github external
xml_obj = ET.fromstring(to_bytes(xml))

            # convert the To hostname in the headers to the generic one
            to_field = xml_obj.find("s:Header/wsa:To", NAMESPACES)
            if to_field is not None:
                to_field.text = self.endpoint

            for override in overrides:
                override_element = xml_obj.find(override['path'], NAMESPACES)
                if override.get('text'):
                    override_element.text = override['text']
                attributes = override.get('attributes', {})
                for attr_key, attr_value in attributes.items():
                    override_element.attrib[attr_key] = attr_value
        else:
            xml_obj = ET.fromstring(to_bytes(xml))

        # convert the string to an XML object, for Python 2.6 (lxml) we need
        # to change the namespace handling to mimic the ElementTree way of
        # working so the string compare works
        if sys.version_info[0] == 2 and sys.version_info[1] < 7:
            namespaces = {}
            new_xml_obj = self._simplify_namespaces(namespaces, xml_obj)
            for key, value in namespaces.items():
                new_xml_obj.attrib["xmlns:%s" % key] = value

            xml_obj = new_xml_obj

        return to_string(ETNew.tostring(xml_obj, encoding='utf-8'))
github jborean93 / pypsrp / tests / runner.py View on Github external
def _normalise_xml(self, xml, generify=True, overrides=None):
        overrides = overrides if overrides is not None else []

        if generify:
            # convert all UUID values to the blank UUID
            xml = re.sub(self._uuid_pattern,
                         "00000000-0000-0000-0000-000000000000", xml)

            xml_obj = ET.fromstring(to_bytes(xml))

            # convert the To hostname in the headers to the generic one
            to_field = xml_obj.find("s:Header/wsa:To", NAMESPACES)
            if to_field is not None:
                to_field.text = self.endpoint

            for override in overrides:
                override_element = xml_obj.find(override['path'], NAMESPACES)
                if override.get('text'):
                    override_element.text = override['text']
                attributes = override.get('attributes', {})
                for attr_key, attr_value in attributes.items():
                    override_element.attrib[attr_key] = attr_value
        else:
            xml_obj = ET.fromstring(to_bytes(xml))
github jborean93 / pypsrp / tests / test_utils.py View on Github external
def test_bytes_to_bytes():
    expected = b"\x01\x02\x03\x04"
    actual = to_bytes(b"\x01\x02\x03\x04")
    assert actual == expected
github jborean93 / pypsrp / pypsrp / client.py View on Github external
for data in iter((lambda: src_file.read(buffer_size)), b""):
                    log.debug("Reading data of file at offset=%d with size=%d"
                              % (offset, buffer_size))
                    offset += len(data)
                    sha1.update(data)
                    b64_data = base64.b64encode(data)

                    result = [to_unicode(b64_data)]
                    if offset == total_size:
                        result.append(to_unicode(base64.b64encode(to_bytes(sha1.hexdigest()))))

                    yield result

                # the file was empty, return empty buffer
                if offset == 0:
                    yield [u"", to_unicode(base64.b64encode(to_bytes(sha1.hexdigest())))]
github jborean93 / pypsrp / pypsrp / negotiate.py View on Github external
def _set_auth_token(request, token, auth_provider):
        encoded_token = base64.b64encode(token)
        auth_header = to_bytes("%s " % auth_provider) + encoded_token
        request.headers['Authorization'] = auth_header
github jborean93 / pypsrp / pypsrp / encryption.py View on Github external
def _wrap_message(self, message, hostname):
        msg_length = str(len(message))
        wrapped_data = self._wrap(message, hostname)

        payload = "\r\n".join([
            self.MIME_BOUNDARY,
            "\tContent-Type: %s" % self.protocol,
            "\tOriginalContent: type=application/soap+xml;charset=UTF-8;"
            "Length=%s" % msg_length,
            self.MIME_BOUNDARY,
            "\tContent-Type: application/octet-stream",
            ""
        ])
        payload = to_bytes(payload) + wrapped_data

        return payload
github jborean93 / pypsrp / pypsrp / spnego.py View on Github external
mechs=[mech])
                # raises ExpiredCredentialsError if it has expired
                cred.lifetime
            except gssapi.raw.GSSError:
                # we can't acquire the cred if no password was supplied
                if password is None:
                    raise
                cred = None
        elif username is None or password is None:
            raise ValueError("Can only use implicit credentials with kerberos "
                             "authentication")

        if cred is None:
            # error when trying to access the existing cache, get our own
            # credentials with the password specified
            b_password = to_bytes(password)
            cred = gssapi.raw.acquire_cred_with_password(username, b_password,
                                                         usage='initiate',
                                                         mechs=[mech])
            cred = cred.creds

        flags = gssapi.RequirementFlag.mutual_authentication | \
            gssapi.RequirementFlag.out_of_sequence_detection
        if delegate:
            flags |= gssapi.RequirementFlag.delegate_to_peer
        if wrap_required:
            flags |= gssapi.RequirementFlag.confidentiality

        context = gssapi.SecurityContext(name=server_name,
                                         creds=cred,
                                         usage='initiate',
                                         mech=mech,