sso.py 6.4 KB
Newer Older
marcus-tun's avatar
marcus-tun committed
1
2
3
4
5
6
7
8
#!/usr/bin/env python 

from mod_python import apache
from mod_python import util
import httplib2
import hashlib
from base64 import b64encode, b64decode

9
from Crypto.Cipher import Blowfish
marcus-tun's avatar
marcus-tun committed
10
from Crypto.Cipher import AES
11
12
from Crypto import Random
from struct import pack
marcus-tun's avatar
marcus-tun committed
13

14
15
import urllib
from string import replace
marcus-tun's avatar
marcus-tun committed
16
17
18
from binascii import hexlify, unhexlify

from interop import decrypt
19
20
21
22
23
24
25

def write_var (req, var, filename):
    log_path=req.document_root() + '/assertions/' + filename
    logfile=open(log_path, 'w')
    logfile.write(var)
    logfile.close

marcus-tun's avatar
marcus-tun committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

def handler(req):
    assertionLocation = str(req.subprocess_env['Shib-Assertion-01'])
    h1 = httplib2.HTTPSConnectionWithTimeout('saml-delegation.data.kit.edu')

    # get the path portion of the assertion link
    (i, assertionPath) = assertionLocation.split('https://saml-delegation.data.kit.edu')

    # get the assertion
    h1.request('GET', assertionPath)
    response = h1.getresponse()
    assertion = response.read()

    # remove newlines
    assertion = assertion.replace("\n", "")

    # find out at which url we were called
    (none, none, location) = req.uri.split('/')

    # sso
    if location == 'sso.py':
        req.content_type = 'text/html'
        req.write('''<!DOCTYPE html>
            <html>
            <head>
            <meta http-equiv="refresh" content="1
                  URL=https://saml-delegation.data.kit.edu/sd/ecp.py">
                  <!--   URI:     %s -->
            </head>
            <body>
            <br/>
            Save your assertion as "/tmp/samlup_uXXXX.tmp" where you replace
                  "XXXX" with your user id.<br>
            </body>
            </html>'''% req.uri)
        return apache.OK

marcus-tun's avatar
marcus-tun committed
63
64
65
    ############################
     # js: sso via javascript #
    ############################
marcus-tun's avatar
marcus-tun committed
66
67
    if location == 'js.py':
        req.content_type = 'text/html'
marcus-tun's avatar
marcus-tun committed
68
        req.write('''<!DOCTYPE html> <meta charset="ASCII" />
marcus-tun's avatar
marcus-tun committed
69
            <html>
marcus-tun's avatar
marcus-tun committed
70
71
72
73
74
75
76
77
78
79
80
<!-- TODO(daniel): host locally; jquery v1 for included IE support.. --!>
<script src="js/crypto-js/aes.js"></script>
<script src="js/jquery-1.11.1.min.js"></script>
                  <p id="data"> data </p>
                  <p id="stat"> stat </p>
                  <p id="stat_post"> post </p>
                  <p id="url"> url </p>
<script src="js-new.js"></script>
                <head> </head>
                <body> </body>
            </html> ''')
marcus-tun's avatar
marcus-tun committed
81
82
83
84
        return apache.OK

    # ecp
    if location == 'ecp.py':
85
86
87
88
        # octet stream will force saving to disk, while
        # text will allow to open with a text editor
        #req.content_type='application/octet-stream'\
        req.content_type='application/text'\
marcus-tun's avatar
marcus-tun committed
89
90
91
92
                '\nContent-Disposition: attachment; filename=samlup_uXXXX.tmp'
        req.write(assertion)
        return apache.OK

marcus-tun's avatar
marcus-tun committed
93
94
95
96
97
98
99
100
101
102
    ########
    # test #
    ########
    if location in ( 'test_transport.py') :
        buf = req.read()
        write_var (req, buf, 'buf')
        req.write('Transport test done')
        return apache.OK

    if location in ( 'test_assertion.py') :
103
104
        req.content_type = 'text/plain'
        form = util.FieldStorage(req)
marcus-tun's avatar
marcus-tun committed
105
106
        b64_encrypted_assertion = form.get("encrypted_assertion", "Error: No assertion field sent").replace(' ', '+')
        write_var (req, b64_encrypted_assertion, "b64_encrypted_assertion")
107
108
        return apache.OK

marcus-tun's avatar
marcus-tun committed
109
110
111
    ############
    #  upload  #
    ############
112
    if location in ( 'upload.py', 'jsupload.py'):
marcus-tun's avatar
marcus-tun committed
113
114
115
116
        req.content_type = 'text/plain'
        # we expect the data via post in encrypted assertion.
        # we will return the url of where to collect the assertion
        # request.
marcus-tun's avatar
marcus-tun committed
117

118
119
120
121
        if req.method != 'POST':
            req.write("Error, i was expecting a post request")
            return apache.OK

marcus-tun's avatar
marcus-tun committed
122
        form = util.FieldStorage(req)
123
        client = form.get ("client", "oops").replace(' ', '+')
124
125
126
        if not form.has_key("encrypted_assertion"):
            req.write ("Error: did not obtain the encrypted_assertion")
            return apache.OK
marcus-tun's avatar
marcus-tun committed
127
128
        b64 = form.get("encrypted_assertion", "Error: no assertion present").replace(' ', '+')
        write_var (req, b64, 'b64')
129
        client_version = form.get ("client_verions", "oops").replace(' ', '+')
marcus-tun's avatar
marcus-tun committed
130

131
        # decode assertion
132
        if client == 'perl':
marcus-tun's avatar
marcus-tun committed
133
134
135
136
137
138
139
            encrypted_assertion = b64decode(b64)
        elif client == 'javascript':
            encrypted_assertion = unhexlify(b64)
        else:
            req.write ('client not supported')
            return apache.OK
        write_var (req, encrypted_assertion, 'encrypted_assertion')
140
141
142
143
144

        # create hash
        assertion_hash = str(hashlib.md5(encrypted_assertion).hexdigest())
        assertion_url ='https://' + req.hostname + '/assertions/' + assertion_hash

marcus-tun's avatar
marcus-tun committed
145
146
        # return the url as key=value FIXME
        req.write("url=%s" % assertion_url)
147
148
149
150
151
152

        #########
        # debug #
        #########
        # decrypt assertion in case password is provided

marcus-tun's avatar
marcus-tun committed
153
154
155
156
157
158
159
160
161
162
163
164
        if form.has_key("key"):
            enc_key = form.get("key", "")
            client = form.get("client","")
            if client == "perl":
                key = b64decode(enc_key)
            if  client == "javascript":
                iv  = unhexlify(form.get("iv",""))
                key = unhexlify(enc_key)
                enc_ass = encrypted_assertion
                encrypted_assertion = iv + enc_ass

            write_var(req,enc_key, "key")
165
            write_var(req, encrypted_assertion, 'encrypted_assertion')
166

marcus-tun's avatar
marcus-tun committed
167
168
169
170
171
172
173
            # some logging
            log_path=req.document_root() + '/assertions/' + 'log'
            logfile=open(log_path, 'w')
            logfile.write("keylen:    %d\n" % len(key))
            logfile.write("cipherlen: %d\n" % len(encrypted_assertion))
            #logfile.write("iv:        %d\n" % len(iv))
            logfile.close
marcus-tun's avatar
marcus-tun committed
174

175

marcus-tun's avatar
marcus-tun committed
176
            plaintext = decrypt(key, encrypted_assertion)
177
            write_var(req, plaintext, 'plaintext')
marcus-tun's avatar
marcus-tun committed
178
179
180
181
182
183
        return apache.OK

    req.content_type = 'text/plain'
    req.write("Error: Your request was not understood")
    return apache.OK
    #return apache.HTTP_BAD_REQUEST
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198


# Some leftovers:

#bs = Blowfish.block_size
#iv = '12333123'
#cipher = Blowfish.new(key, Blowfish.MODE_ECB, iv)
#cipher = new Crypt::CBC (symmetric_key, 'Twofish');
#my plaintext = cipher->decrypt(encrypted_assertion);
#print ("\n".plaintext."\n");

##plaintext = b'docendo discimus '
##plen = bs - divmod(len(plaintext),bs)[1]
##padding = [plen]*plen
##padding = pack('b'*plen, *padding)