CMS 3D CMS Logo

Classes | Functions | Variables

ws_sso_content_reader Namespace Reference

Classes

class  HTTPSClientAuthHandler

Functions

def _getResponse
def checkRequiredArguments
def getContent
def getFile
def getParentURL
def getResponseContent
def getResponseURL
def getSSOCookie

Variables

tuple content = getContent(opts.url, opts.cert_path, opts.key_path, opts.postdata, opts.debug)
tuple parser = OptionParser(usage="%prog [-d(ebug)] -o(ut) COOKIE_FILENAME -c(cert) CERN-PEM -k(ey) CERT-KEY -u(rl) URL")

Function Documentation

def ws_sso_content_reader::_getResponse (   opener,
  url,
  post_data = None,
  debug = False 
) [private]

Definition at line 26 of file ws_sso_content_reader.py.

00027                                                           :
00028   response = opener.open(url, post_data)
00029   if debug:
00030     sys.stderr.write("Code: %s\n" % response.code)
00031     sys.stderr.write("Headers: %s\n" % response.headers)
00032     sys.stderr.write("Msg: %s\n" % response.msg)
00033     sys.stderr.write("Url: %s\n" % response.url)
00034   return response

def ws_sso_content_reader::checkRequiredArguments (   opts,
  parser 
)

Definition at line 100 of file ws_sso_content_reader.py.

00101                                         :
00102   missing_options = []
00103   for option in parser.option_list:
00104     if re.match(r'^\[REQUIRED\]', option.help) and eval('opts. %s' % option.dest) == None:
00105       missing_options.extend(option._long_opts)
00106     if len(missing_options) > 0:
00107       parser.error('Missing REQUIRED parameters: %s' % str(missing_options))    

def ws_sso_content_reader::getContent (   target_url,
  cert_path,
  key_path,
  post_data = None,
  debug = False,
  adfslogin = None 
)

Definition at line 64 of file ws_sso_content_reader.py.

00065                                                                                             :
00066   opener = urllib2.build_opener(urllib2.HTTPSHandler())
00067   if adfslogin:
00068     opener.addheaders = [('Adfs-Login', adfslogin)] #local version of tc test
00069   
00070   #try to access the url first
00071   try:
00072     content = getResponseContent(opener, target_url, post_data, debug)
00073     if not 'Sign in with your CERN account' in content:
00074       return content
00075   except Exception:
00076     if debug:
00077       sys.stderr.write("The request has an error, will try to create a new cookie\n")
00078 
00079   cookie = cookielib.CookieJar()
00080   opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie), HTTPSClientAuthHandler(key_path, cert_path))  #will use private key and ceritifcate
00081   if debug:
00082     sys.stderr.write("The return page is sso login page, will request cookie.")
00083   hasCookie = False
00084   # if the access gave an exception, try to get a cookie
00085   try:
00086     getSSOCookie(opener, target_url, cookie, debug)
00087     hasCookie = True 
00088     result = getResponseContent(opener, target_url, post_data, debug)
00089   except Exception, e:
00090     result = ""
00091     print sys.stderr.write("ERROR:"+str(e))
00092   if hasCookie:
00093     burl = getParentURL(target_url)
00094     try:
00095       _getResponse(opener, burl+"signOut").read()
00096       _getResponse(opener, "https://login.cern.ch/adfs/ls/?wa=wsignout1.0").read()
00097     except:
00098       sys.stderr.write("Error, could not logout correctly from server") 
00099   return result

def ws_sso_content_reader::getFile (   path)

Definition at line 6 of file ws_sso_content_reader.py.

00007                  :
00008   npath = os.path.expanduser(path)
00009   while os.path.islink(npath):
00010     path = os.readlink(npath)
00011     if path[0] != "/": path = os.path.join(os.path.dirname(npath),path)
00012     npath = path
00013   return npath

def ws_sso_content_reader::getParentURL (   url)

Definition at line 41 of file ws_sso_content_reader.py.

00042                      :
00043   items = url.split("/")
00044   return '%s//%s/%s/' % (items[0],items[2],items[3])

def ws_sso_content_reader::getResponseContent (   opener,
  url,
  post_data = None,
  debug = False 
)

Definition at line 35 of file ws_sso_content_reader.py.

00036                                                                 :
00037   return _getResponse(opener, url, post_data, debug).read()

def ws_sso_content_reader::getResponseURL (   opener,
  url,
  post_data = None,
  debug = False 
)

Definition at line 38 of file ws_sso_content_reader.py.

00039                                                             :
00040   return urllib2.unquote(_getResponse(opener, url, post_data, debug).url)

def ws_sso_content_reader::getSSOCookie (   opener,
  target_url,
  cookie,
  debug = False 
)

Definition at line 45 of file ws_sso_content_reader.py.

00046                                                          :
00047   opener.addheaders = [('User-agent', 'curl-sso-certificate/0.0.2')] #in sync with cern-get-sso-cookie tool
00048   url = getResponseURL(opener, getParentURL(target_url), debug=debug)
00049   content = getResponseContent(opener, url, debug=debug)
00050   ret = re.search('<form .+? action="(.+?)">', content)
00051   if ret == None:
00052     raise Exception("error: The page doesn't have the form with adfs url, check 'User-agent' header")
00053   url = urllib2.unquote(ret.group(1))
00054   h = HTMLParser.HTMLParser()
00055   post_data_local = ''
00056   for match in re.finditer('input type="hidden" name="([^"]*)" value="([^"]*)"', content):
00057     post_data_local += "&%s=%s" % (match.group(1), urllib.quote(h.unescape(match.group(2))))
00058     is_link_found = True
00059   
00060   if not is_link_found:
00061     raise Exception("error: The page doesn't have the form with security attributes, check 'User-agent' header")
00062   post_data_local = post_data_local[1:] #remove first &
00063   getResponseContent(opener, url, post_data_local, debug)


Variable Documentation

tuple ws_sso_content_reader::content = getContent(opts.url, opts.cert_path, opts.key_path, opts.postdata, opts.debug)
tuple ws_sso_content_reader::parser = OptionParser(usage="%prog [-d(ebug)] -o(ut) COOKIE_FILENAME -c(cert) CERN-PEM -k(ey) CERT-KEY -u(rl) URL")

Definition at line 109 of file ws_sso_content_reader.py.