Python pyasn1.type.char.PrintableString() Examples

The following are 2 code examples of pyasn1.type.char.PrintableString(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyasn1.type.char , or try the search function .
Example #1
Source File: test_rfc2986.py    From pyasn1-modules with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def testOpenTypes(self):
        openTypesMap = {
            univ.ObjectIdentifier('1.2.840.113549.1.1.1'): univ.Null(""),
            univ.ObjectIdentifier('1.2.840.113549.1.1.5'): univ.Null(""),
            univ.ObjectIdentifier('1.2.840.113549.1.1.11'): univ.Null(""),
        }

        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate,
                                       asn1Spec=rfc2986.CertificationRequest(),
                                       openTypes=openTypesMap,
                                       decodeOpenTypes=True)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        for rdn in asn1Object['certificationRequestInfo']['subject']['rdnSequence']:
            for atv in rdn:
                if atv['type'] == rfc5280.id_at_countryName:
                    self.assertEqual(char.PrintableString('US'), atv['value'])

                else:
                    self.assertGreater(len(atv['value']['utf8String']), 2)

        spki_alg = asn1Object['certificationRequestInfo']['subjectPKInfo']['algorithm']

        self.assertEqual(univ.Null(""), spki_alg['parameters'])

        sig_alg = asn1Object['signatureAlgorithm']

        self.assertEqual(univ.Null(""), sig_alg['parameters']) 
Example #2
Source File: utils.py    From python-libjuju with Apache License 2.0 5 votes vote down vote up
def generate_user_controller_access_token(username, controller_endpoints, secret_key, controller_name):
    """" Implement in python what is currently done in GO
    https://github.com/juju/juju/blob/a5ab92ec9b7f5da3678d9ac603fe52d45af24412/cmd/juju/user/utils.go#L16

    :param username: name of the user to register
    :param controller_endpoints: juju controller endpoints list in the format <ip>:<port>
    :param secret_key: base64 encoded string of the secret-key generated by juju
    :param controller_name: name of the controller to register to.
    """

    # Secret key is returned as base64 encoded string in:
    # https://websockets.readthedocs.io/en/stable/_modules/websockets/protocol.html#WebSocketCommonProtocol.recv
    # Deconding it before marshalling into the ASN.1 message
    secret_key = base64.b64decode(secret_key)
    addr = Addrs()
    for endpoint in controller_endpoints:
        addr.append(endpoint)

    registration_string = RegistrationInfo()
    registration_string.setComponentByPosition(0, char.PrintableString(username))
    registration_string.setComponentByPosition(1, addr)
    registration_string.setComponentByPosition(2, univ.OctetString(secret_key))
    registration_string.setComponentByPosition(3, char.PrintableString(controller_name))
    registration_string = encode(registration_string)
    remainder = len(registration_string) % 3
    registration_string += b"\0" * (3 - remainder)
    return base64.urlsafe_b64encode(registration_string)