3 from __future__
import print_function
4 import os, urllib, urllib2, httplib, cookielib, sys, HTMLParser, re
5 from optparse
import OptionParser
8 npath = os.path.expanduser(path)
9 while os.path.islink(npath):
10 path = os.readlink(npath)
11 if path[0] !=
"/": path = os.path.join(os.path.dirname(npath),path)
17 urllib2.HTTPSHandler.__init__(self)
25 return httplib.HTTPSConnection(host, key_file=self.
key, cert_file=self.
cert)
28 response = opener.open(url, post_data)
30 sys.stderr.write(
"Code: %s\n" % response.code)
31 sys.stderr.write(
"Headers: %s\n" % response.headers)
32 sys.stderr.write(
"Msg: %s\n" % response.msg)
33 sys.stderr.write(
"Url: %s\n" % response.url)
37 return _getResponse(opener, url, post_data, debug).read()
40 return urllib2.unquote(
_getResponse(opener, url, post_data, debug).url)
43 items = url.split(
"/")
44 return '%s//%s/%s/' % (items[0],items[2],items[3])
47 opener.addheaders = [(
'User-agent',
'curl-sso-certificate/0.0.2')]
50 ret = re.search(
'<form .+? action="(.+?)">', content)
52 raise Exception(
"error: The page doesn't have the form with adfs url, check 'User-agent' header")
53 url = urllib2.unquote(ret.group(1))
54 h = HTMLParser.HTMLParser()
56 for match
in re.finditer(
'input type="hidden" name="([^"]*)" value="([^"]*)"', content):
57 post_data_local +=
"&%s=%s" % (match.group(1), urllib.quote(h.unescape(match.group(2))))
61 raise Exception(
"error: The page doesn't have the form with security attributes, check 'User-agent' header")
62 post_data_local = post_data_local[1:]
65 def getContent(target_url, cert_path, key_path, post_data=None, debug=False, adfslogin=None):
66 opener = urllib2.build_opener(urllib2.HTTPSHandler())
68 opener.addheaders = [(
'Adfs-Login', adfslogin)]
73 if not 'Sign in with your CERN account' in content:
77 sys.stderr.write(
"The request has an error, will try to create a new cookie\n")
79 cookie = cookielib.CookieJar()
80 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie),
HTTPSClientAuthHandler(key_path, cert_path))
82 sys.stderr.write(
"The return page is sso login page, will request cookie.")
89 except Exception
as e:
91 print(sys.stderr.write(
"ERROR:"+
str(e)))
96 _getResponse(opener,
"https://login.cern.ch/adfs/ls/?wa=wsignout1.0").read()
98 sys.stderr.write(
"Error, could not logout correctly from server")
103 for option
in parser.option_list:
104 if re.match(
r'^\[REQUIRED\]', option.help)
and eval(
'opts. %s' % option.dest) ==
None:
105 missing_options.extend(option._long_opts)
106 if len(missing_options) > 0:
107 parser.error(
'Missing REQUIRED parameters: %s' %
str(missing_options))
109 if __name__ ==
"__main__":
110 parser = OptionParser(usage=
"%prog [-d(ebug)] -o(ut) COOKIE_FILENAME -c(cert) CERN-PEM -k(ey) CERT-KEY -u(rl) URL")
111 parser.add_option(
"-d",
"--debug", dest=
"debug", help=
"Enable pycurl debugging. Prints to data and headers to stderr.", action=
"store_true", default=
False)
112 parser.add_option(
"-p",
"--postdata", dest=
"postdata", help=
"Data to be sent as post request", action=
"store", default=
None)
113 parser.add_option(
"-c",
"--cert", dest=
"cert_path", help=
"[REQUIRED] Absolute path to cert file.", action=
"store")
114 parser.add_option(
"-k",
"--key", dest=
"key_path", help=
"[REQUIRED] Absolute path to key file.", action=
"store")
115 parser.add_option(
"-u",
"--url", dest=
"url", help=
"[REQUIRED] Url to a service behind the SSO", action=
"store")
116 (opts, args) = parser.parse_args()
118 content =
getContent(opts.url, opts.cert_path, opts.key_path, opts.postdata, opts.debug)
def getResponseContent(opener, url, post_data=None, debug=False)
def getSSOCookie(opener, target_url, cookie, debug=False)
def checkRequiredArguments(opts, parser)
def getConnection(self, host, timeout=300)
def getContent(target_url, cert_path, key_path, post_data=None, debug=False, adfslogin=None)
S & print(S &os, JobReport::InputFile const &f)
def __init__(self, key, cert)
def getResponseURL(opener, url, post_data=None, debug=False)
def https_open(self, req)
def _getResponse(opener, url, post_data=None, debug=False)