3 import os, urllib, urllib2, httplib, cookielib, sys, HTMLParser, re
4 from optparse
import OptionParser
7 npath = os.path.expanduser(path)
8 while os.path.islink(npath):
9 path = os.readlink(npath)
10 if path[0] !=
"/": path = os.path.join(os.path.dirname(npath),path)
16 urllib2.HTTPSHandler.__init__(self)
24 return httplib.HTTPSConnection(host, key_file=self.
key, cert_file=self.
cert)
27 response = opener.open(url, post_data)
29 sys.stderr.write(
"Code: %s\n" % response.code)
30 sys.stderr.write(
"Headers: %s\n" % response.headers)
31 sys.stderr.write(
"Msg: %s\n" % response.msg)
32 sys.stderr.write(
"Url: %s\n" % response.url)
39 return urllib2.unquote(
_getResponse(opener, url, post_data, debug).url)
42 items = url.split(
"/")
43 return '%s//%s/%s/' % (items[0],items[2],items[3])
46 opener.addheaders = [(
'User-agent',
'curl-sso-certificate/0.0.2')]
49 ret = re.search(
'<form .+? action="(.+?)">', content)
51 raise Exception(
"error: The page doesn't have the form with adfs url, check 'User-agent' header")
52 url = urllib2.unquote(ret.group(1))
53 h = HTMLParser.HTMLParser()
55 for match
in re.finditer(
'input type="hidden" name="([^"]*)" value="([^"]*)"', content):
56 post_data_local +=
"&%s=%s" % (match.group(1), urllib.quote(h.unescape(match.group(2))))
60 raise Exception(
"error: The page doesn't have the form with security attributes, check 'User-agent' header")
61 post_data_local = post_data_local[1:]
64 def getContent(target_url, cert_path, key_path, post_data=None, debug=False, adfslogin=None):
65 opener = urllib2.build_opener(urllib2.HTTPSHandler())
67 opener.addheaders = [(
'Adfs-Login', adfslogin)]
72 if not 'Sign in with your CERN account' in content:
76 sys.stderr.write(
"The request has an error, will try to create a new cookie\n")
78 cookie = cookielib.CookieJar()
79 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie),
HTTPSClientAuthHandler(key_path, cert_path))
81 sys.stderr.write(
"The return page is sso login page, will request cookie.")
90 print sys.stderr.write(
"ERROR:"+str(e))
97 sys.stderr.write(
"Error, could not logout correctly from server")
102 for option
in parser.option_list:
103 if re.match(
r'^\[REQUIRED\]', option.help)
and eval(
'opts. %s' % option.dest) ==
None:
104 missing_options.extend(option._long_opts)
105 if len(missing_options) > 0:
106 parser.error(
'Missing REQUIRED parameters: %s' % str(missing_options))
108 if __name__ ==
"__main__":
109 parser = OptionParser(usage=
"%prog [-d(ebug)] -o(ut) COOKIE_FILENAME -c(cert) CERN-PEM -k(ey) CERT-KEY -u(rl) URL")
110 parser.add_option(
"-d",
"--debug", dest=
"debug", help=
"Enable pycurl debugging. Prints to data and headers to stderr.", action=
"store_true", default=
False)
111 parser.add_option(
"-p",
"--postdata", dest=
"postdata", help=
"Data to be sent as post request", action=
"store", default=
None)
112 parser.add_option(
"-c",
"--cert", dest=
"cert_path", help=
"[REQUIRED] Absolute path to cert file.", action=
"store")
113 parser.add_option(
"-k",
"--key", dest=
"key_path", help=
"[REQUIRED] Absolute path to key file.", action=
"store")
114 parser.add_option(
"-u",
"--url", dest=
"url", help=
"[REQUIRED] Url to a service behind the SSO", action=
"store")
115 (opts, args) = parser.parse_args()
117 content =
getContent(opts.url, opts.cert_path, opts.key_path, opts.postdata, opts.debug)
def checkRequiredArguments