Add test for Proxy SPNEGO auth
[mod_auth_gssapi.git] / tests / t_spnego_proxy.py
1 #!/usr/bin/python
2 # Copyright (C) 2015 - mod_auth_gssapi contributors, see COPYING for license.
3
4 import os
5 import requests
6 import gssapi
7 from base64 import b64encode
8
9 def getAuthToken(target):
10     spnego_mech = gssapi.raw.OID.from_int_seq('1.3.6.1.5.5.2')
11
12     name = gssapi.Name('HTTP@%s' % target,
13                        gssapi.NameType.hostbased_service)
14
15     ctx = gssapi.SecurityContext(name=name, mech=spnego_mech)
16     token = ctx.step()
17
18     return 'Negotiate %s' % b64encode(token)
19
20
21 if __name__ == '__main__':
22     s = requests.Session()
23
24     target = os.environ['NSS_WRAPPER_HOSTNAME']
25     url = 'http://%s/spnego/' % target
26
27     proxy = 'http://%s:%s' % (target, os.environ['WRAP_PROXY_PORT'])
28     proxies = { "http" : proxy, }
29
30     s.headers.update({'Proxy-Authorization': getAuthToken(target)})
31     s.headers.update({'Authorization': getAuthToken(target)})
32
33     r = s.get(url, proxies=proxies)
34     if r.status_code != 200:
35         raise ValueError('Spnego Proxy Auth Failed')