~ddellav/ubuntu/wily/python-pysaml2/debian-merge

« back to all changes in this revision

Viewing changes to tests/test_42_enc.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2014-09-08 16:11:53 UTC
  • Revision ID: package-import@ubuntu.com-20140908161153-vms9r4gu0oz4v4ai
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from saml2.authn_context import INTERNETPROTOCOLPASSWORD
 
2
from saml2.server import Server
 
3
from saml2.sigver import pre_encryption_part, ASSERT_XPATH, EncryptError
 
4
from saml2.sigver import CryptoBackendXmlSec1
 
5
from saml2.sigver import pre_encrypt_assertion
 
6
from pathutils import xmlsec_path
 
7
 
 
8
__author__ = 'roland'
 
9
 
 
10
TMPL = """<?xml version='1.0' encoding='UTF-8'?>
 
11
<ns0:EncryptedData xmlns:ns0="http://www.w3.org/2001/04/xmlenc#" xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" Id="ED" Type="http://www.w3.org/2001/04/xmlenc#Element"><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /><ns1:KeyInfo><ns0:EncryptedKey Id="EK"><ns0:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /><ns1:KeyInfo><ns1:KeyName>my-rsa-key</ns1:KeyName></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedKey></ns1:KeyInfo><ns0:CipherData><ns0:CipherValue /></ns0:CipherData></ns0:EncryptedData>"""
 
12
 
 
13
IDENTITY = {"eduPersonAffiliation": ["staff", "member"],
 
14
            "surName": ["Jeter"], "givenName": ["Derek"],
 
15
            "mail": ["foo@gmail.com"],
 
16
            "title": ["shortstop"]}
 
17
 
 
18
 
 
19
AUTHN = {
 
20
    "class_ref": INTERNETPROTOCOLPASSWORD,
 
21
    "authn_auth": "http://www.example.com/login"
 
22
}
 
23
 
 
24
 
 
25
def test_pre_enc():
 
26
    tmpl = pre_encryption_part()
 
27
    print tmpl
 
28
    assert "%s" % tmpl == TMPL
 
29
 
 
30
 
 
31
def test_reshuffle_response():
 
32
    server = Server("idp_conf")
 
33
    name_id = server.ident.transient_nameid(
 
34
        "urn:mace:example.com:saml:roland:sp", "id12")
 
35
 
 
36
    resp_ = server.create_authn_response(
 
37
        IDENTITY, "id12", "http://lingon.catalogix.se:8087/",
 
38
        "urn:mace:example.com:saml:roland:sp", name_id=name_id)
 
39
 
 
40
    resp2 = pre_encrypt_assertion(resp_)
 
41
 
 
42
    print resp2
 
43
    assert resp2.encrypted_assertion.extension_elements
 
44
 
 
45
 
 
46
def test_enc1():
 
47
    server = Server("idp_conf")
 
48
    name_id = server.ident.transient_nameid(
 
49
        "urn:mace:example.com:saml:roland:sp", "id12")
 
50
 
 
51
    resp_ = server.create_authn_response(
 
52
        IDENTITY, "id12", "http://lingon.catalogix.se:8087/",
 
53
        "urn:mace:example.com:saml:roland:sp", name_id=name_id)
 
54
 
 
55
    statement = pre_encrypt_assertion(resp_)
 
56
 
 
57
    tmpl = "enc_tmpl.xml"
 
58
    # tmpl_file = open(tmpl, "w")
 
59
    # tmpl_file.write("%s" % pre_encryption_part())
 
60
    # tmpl_file.close()
 
61
 
 
62
    data = "pre_enc.xml"
 
63
    # data_file = open(data, "w")
 
64
    # data_file.write("%s" % statement)
 
65
    # data_file.close()
 
66
 
 
67
    key_type = "des-192"
 
68
    com_list = [xmlsec_path, "encrypt", "--pubkey-cert-pem", "pubkey.pem",
 
69
                "--session-key", key_type, "--xml-data", data,
 
70
                "--node-xpath", ASSERT_XPATH]
 
71
 
 
72
    crypto = CryptoBackendXmlSec1(xmlsec_path)
 
73
    (_stdout, _stderr, output) = crypto._run_xmlsec(
 
74
        com_list, [tmpl], exception=EncryptError, validate_output=False)
 
75
 
 
76
    print output
 
77
    assert _stderr == ""
 
78
    assert _stdout == ""
 
79
 
 
80
 
 
81
def test_enc2():
 
82
    crypto = CryptoBackendXmlSec1(xmlsec_path)
 
83
 
 
84
    server = Server("idp_conf")
 
85
    name_id = server.ident.transient_nameid(
 
86
        "urn:mace:example.com:saml:roland:sp", "id12")
 
87
 
 
88
    resp_ = server.create_authn_response(
 
89
        IDENTITY, "id12", "http://lingon.catalogix.se:8087/",
 
90
        "urn:mace:example.com:saml:roland:sp", name_id=name_id)
 
91
 
 
92
    enc_resp = crypto.encrypt_assertion(resp_, "pubkey.pem",
 
93
                                        pre_encryption_part())
 
94
 
 
95
    print enc_resp
 
96
    assert enc_resp
 
97
 
 
98
if __name__ == "__main__":
 
99
    test_enc1()
 
 
b'\\ No newline at end of file'