예제 #1
0
 def test_encode_decode_json_bytes(self):
     obj = {"hello": "world"}
     self.assertEquals(obj, decode_json_bytes(encode_json_bytes(obj)))
     self.assertRaises(ValueError, decode_json_bytes,
                       encode_bytes("NOJSON4U"))
     self.assertRaises(ValueError, decode_json_bytes, encode_bytes("42"))
     self.assertRaises(ValueError, decode_json_bytes,
                       encode_bytes("[1, 2, 3]"))
     self.assertRaises(ValueError, encode_json_bytes, 42)
     self.assertRaises(ValueError, encode_json_bytes, [1, 3, 3])
예제 #2
0
 def test_encode_decode_json_bytes(self):
     obj = {"hello": "world"}
     self.assertEquals(obj, decode_json_bytes(encode_json_bytes(obj)))
     self.assertRaises(ValueError,
                       decode_json_bytes, encode_bytes("NOJSON4U"))
     self.assertRaises(ValueError,
                       decode_json_bytes, encode_bytes("42"))
     self.assertRaises(ValueError,
                       decode_json_bytes, encode_bytes("[1, 2, 3]"))
     self.assertRaises(ValueError, encode_json_bytes, 42)
     self.assertRaises(ValueError, encode_json_bytes, [1, 3, 3])
예제 #3
0
파일: dummy.py 프로젝트: junkafarian/PyVEP
    def make_assertion(cls,
                       email,
                       audience,
                       issuer=None,
                       exp=None,
                       assertion_sig=None,
                       certificate_sig=None,
                       new_style=True):
        """Generate a new dummy assertion for the given email address.

        This method lets you generate VEP assertions using dummy private keys.
        Called with just an email and audience it will generate an assertion
        from browserid.org.

        By specifying the "exp", "assertion_sig" or "certificate_sig" arguments
        it is possible generate invalid assertions for testing purposes.
        """
        if issuer is None:
            issuer = "browserid.org"
        if exp is None:
            exp = int((time.time() + 60) * 1000)
        # Get private key for the email address itself.
        email_pub, email_priv = cls._get_keypair(email)
        # Get private key for the hostname so we can sign it.
        iss_pub, iss_priv = cls._get_keypair(issuer)
        # Generate the assertion, signed with email's public key.
        assertion = {
            "exp": exp,
            "aud": audience,
        }
        assertion = jwt.generate(assertion, email_priv)
        if assertion_sig is not None:
            assertion = ".".join(
                assertion.split(".")[:-1] + [encode_bytes(assertion_sig)])
        # Generate the certificate signing the email's public key
        # with the issuer's public key.
        certificate = {
            "iss": issuer,
            "exp": exp,
            "principal": {
                "email": email
            },
            "public-key": email_pub,
        }
        certificate = jwt.generate(certificate, iss_priv)
        if certificate_sig is not None:
            certificate = ".".join(
                certificate.split(".")[:-1] + [encode_bytes(certificate_sig)])
        # Combine them into a VEP bundled assertion.
        return bundle_certs_and_assertion([certificate], assertion, new_style)
예제 #4
0
파일: jwt.py 프로젝트: almet/PyVEP
def generate(payload, key):
    """Generate and sign a JWT for a dict payload."""
    alg = key.__class__.__name__[:-3]
    algorithm = encode_json_bytes({"alg": alg})
    payload = encode_json_bytes(payload)
    signature = encode_bytes(key.sign(".".join((algorithm, payload))))
    return ".".join((algorithm, payload, signature))
예제 #5
0
 def test_error_jwt_with_no_algorithm(self):
     token = ".".join((
         encode_json_bytes({}),
         encode_json_bytes({}),
         encode_bytes("signature"),
     ))
     self.assertRaises(ValueError, jwt.parse, token)
예제 #6
0
 def test_error_jwt_with_no_algorithm(self):
     token = ".".join((
       encode_json_bytes({}),
       encode_json_bytes({}),
       encode_bytes("signature"),
     ))
     self.assertRaises(ValueError, jwt.parse, token)
예제 #7
0
 def generate(cls, payload, key):
     """Generate and sign a JWT for a dict payload."""
     alg = key.__class__.__name__[:-3]
     algorithm = encode_json_bytes({"alg": alg})
     payload = encode_json_bytes(payload)
     signature = encode_bytes(key.sign(".".join((algorithm, payload))))
     return ".".join((algorithm, payload, signature))
예제 #8
0
    def make_assertion(cls, email, audience, issuer=None, exp=None,
                       assertion_sig=None, certificate_sig=None):
        """Generate a new dummy assertion for the given email address.

        This method lets you generate VEP assertions using dummy private keys.
        Called with just an email and audience it will generate an assertion
        from browserid.org.

        By specifying the "exp", "assertion_sig" or "certificate_sig" arguments
        it is possible generate invalid assertions for testing purposes.
        """
        if issuer is None:
            issuer = "browserid.org"
        if exp is None:
            exp = int((time.time() + 60) * 1000)
        # Get private key for the email address itself.
        email_pub, email_priv = cls._get_keypair(email)
        # Get private key for the hostname so we can sign it.
        iss_pub, iss_priv = cls._get_keypair(issuer)
        # Generate the assertion, signed with email's public key.
        assertion = {
          "exp": exp,
          "aud": audience,
        }
        assertion = JWT.generate(assertion, email_priv)
        if assertion_sig is not None:
            assertion = ".".join(assertion.split(".")[:-1] +
                                 [encode_bytes(assertion_sig)])
        # Generate the certificate signing the email's public key
        # with the issuer's public key.
        certificate = {
          "iss": issuer,
          "exp": exp,
          "principal": {"email": email},
          "public-key": email_pub,
        }
        certificate = JWT.generate(certificate, iss_priv)
        if certificate_sig is not None:
            certificate = ".".join(certificate.split(".")[:-1] +
                                   [encode_bytes(certificate_sig)])
        # Combine them into a VEP bundled assertion.
        data = {
          "certificates": [certificate],
          "assertion": assertion,
        }
        return encode_bytes(json.dumps(data))
예제 #9
0
 def test_encode_decode_bytes(self):
     self.assertEquals("HELLO", decode_bytes(encode_bytes("HELLO")))
     self.assertEquals("HELLO", decode_bytes(encode_bytes(u"HELLO")))
     self.assertRaises(ValueError, decode_bytes, u"\N{SNOWMAN}")
     self.assertRaises(ValueError, decode_bytes, "A===")
예제 #10
0
 def test_encode_decode_bytes(self):
     self.assertEquals("HELLO", decode_bytes(encode_bytes("HELLO")))
     self.assertEquals("HELLO", decode_bytes(encode_bytes(u"HELLO")))
     self.assertRaises(ValueError, decode_bytes, u"\N{SNOWMAN}")
     self.assertRaises(ValueError, decode_bytes, "A===")