Commit 0b33f160 by Ed Crewe

finish of cas steps

parent 1ab31f42
...@@ -57,18 +57,36 @@ class TestCAS(unittest.TestCase): ...@@ -57,18 +57,36 @@ class TestCAS(unittest.TestCase):
print '-----------------------' print '-----------------------'
self.ticket = self.login() self.ticket = self.login()
self.get_restricted() self.get_restricted()
print ''
print 'Test proxy CAS login' print 'Test proxy CAS login'
print '--------------------' print '--------------------'
iou = self.get_proxy_iou()
iou = self.proxy1_iou()
if iou.startswith('PGT'): if iou.startswith('PGT'):
print 'PASS: Got IOU - %s for %s' % (iou, PROXY_URL) print 'PASS: Got IOU - %s for %s' % (iou, PROXY_URL)
else: else:
print iou print iou
pgt = self.get_proxy_pgt(iou)
pgt = self.proxy2_pgt(iou)
if pgt.startswith('PGT'): if pgt.startswith('PGT'):
print 'PASS: Got PGT - %s' % pgt print 'PASS: Got PGT - %s' % pgt
else: else:
print pgt print pgt
pt = self.proxy3_pt(pgt)
if pt.startswith('PT'):
print 'PASS: Got PT - %s' % pt
else:
print pt
proxy = self.proxy4_login(pt)
if proxy:
print 'PASS: Logged in successfully to %s via %s' % (APP_URL, proxy)
else:
print 'FAIL: The proxy login to %s via %s has failed' % (APP_URL, PROXY_URL)
def get_auth(self): def get_auth(self):
""" Get authentication by passing to this script on the command line """ """ Get authentication by passing to this script on the command line """
...@@ -106,13 +124,23 @@ class TestCAS(unittest.TestCase): ...@@ -106,13 +124,23 @@ class TestCAS(unittest.TestCase):
""" Replace this with find_in_dom ? """ Replace this with find_in_dom ?
Although without knowing the CAS login page this Although without knowing the CAS login page this
is probably more generic. is probably more generic.
Starts is a list to allow a series of marker points
in case a single start point marker is not unique
""" """
end = page.find(starts[0]) pagepart = page
start = end + page[end:].find(starts[1]) + len(starts[1]) start = 0
endnum = page[start:].find(stop) for part in starts:
if endnum == -1: point = pagepart.find(part)
endnum = len(page[start:]) if point>-1:
end = start + endnum start += point
else:
return "FAIL: Couldnt find '%s' in page" % part
pagepart = pagepart[start:]
start = start + len(part)
end = page[start:].find(stop)
if end == -1:
end = len(page[start:])
end = start + end
found = page[start:end] found = page[start:end]
return found.strip() return found.strip()
...@@ -122,7 +150,11 @@ class TestCAS(unittest.TestCase): ...@@ -122,7 +150,11 @@ class TestCAS(unittest.TestCase):
ticket = '' ticket = ''
token = self.get_token(url) token = self.get_token(url)
if token: if token:
self.auth[TOKEN] = token if token.startswith('FAIL'):
print token
return ticket
else:
self.auth[TOKEN] = token
else: else:
print 'FAIL: CSRF Token could not be found on page' print 'FAIL: CSRF Token could not be found on page'
return ticket return ticket
...@@ -151,7 +183,7 @@ class TestCAS(unittest.TestCase): ...@@ -151,7 +183,7 @@ class TestCAS(unittest.TestCase):
print 'FAIL: couldnt log in to restricted app at %s' % url print 'FAIL: couldnt log in to restricted app at %s' % url
return return
def get_proxy_iou(self): def proxy1_iou(self):
""" Use login ticket to get proxy iou """ Use login ticket to get proxy iou
NB: SSO server installation may require PROXY_URL/?pgtIou be called at the root NB: SSO server installation may require PROXY_URL/?pgtIou be called at the root
""" """
...@@ -177,31 +209,47 @@ class TestCAS(unittest.TestCase): ...@@ -177,31 +209,47 @@ class TestCAS(unittest.TestCase):
return 'FAIL: PGIOU Response failed authentication' return 'FAIL: PGIOU Response failed authentication'
return None return None
def get_proxy_pgt(self, iou): def proxy2_pgt(self, iou):
""" Get the proxy granting ticket from our django database backend """ Dig out the proxy granting ticket using shell script so this test class
Fire off shell script to django shell environment so this test class is is independent of CAS implementation - eg. can substitute this function
independent of CAS implementation - can substitute this function
to get proxy ticket from Java CAS instead of django-cas for example to get proxy ticket from Java CAS instead of django-cas for example
For a django-cas implementation this can be read from the ORM
by calling the django shell environment
""" """
out = commands.getoutput(SCRIPT) out = commands.getoutput(SCRIPT)
pgt = self.find_in_page(out, ['>>>','PGT'], ' ') pgt = self.find_in_page(out, ['PGT',], ' ')
return 'PGT%s' % pgt return 'PGT%s' % pgt
def get_proxy_pt(self, pgt): def proxy3_pt(self, pgt):
""" Use login ticket to get proxy """ """ Use granting ticket to get proxy """
url_args = (CAS_SERVER_URL, APP_URL, pgt) url_args = (CAS_SERVER_URL, APP_URL, pgt)
url = '%s?targetService=%s&pgt=%s' % url_args url = '%s/proxy?targetService=%s&pgt=%s' % url_args
try:
pt = self.opener.open(url)
except:
return 'FAIL: PTURL=%s not found' % url
page = pt.read()
if page.find('cas:serviceResponse') > -1:
pt_ticket = self.find_in_dom(page,['cas:proxySuccess',
'cas:proxyTicket'])
return pt_ticket
return None
def proxy4_login(self, pt):
""" Use proxy ticket to login """
url_args = (CAS_SERVER_URL, APP_URL, pt)
url = '%s/proxyValidate?targetService=%s&ticket=%s' % url_args
try: try:
pgt = self.opener.open(url) login = self.opener.open(url)
except: except:
return 'FAIL: PTURL=%s not found' % url return 'FAIL: PTURL=%s not found' % url
page = pgt.read() page = login.read()
return page
if page.find('cas:authenticationSuccess') > -1: if page.find('cas:authenticationSuccess') > -1:
pgt_ticket = self.find_in_dom(page,['cas:serviceResponse', proxy = self.find_in_dom(page,['cas:proxies',
'cas:authenticationSuccess', 'cas:proxy'])
'cas:proxyGrantingTicket']) return proxy
return pgt_ticket
return None return None
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment