Commit 535f28d9 by Ed Crewe

got proxy ticket

parent dfcfde5c
......@@ -41,23 +41,36 @@ def get_auth():
print 'python proxy_test.py username password'
def get_token(opener, url, token=TOKEN):
""" Get CSRF token - make this less ugly and more generic with regex """
""" Get CSRF token """
r = opener.open(url)
page = r.read()
end = page.find('<input type="hidden" name="%s"' % token)
start = end + page[end:].find('value="') + len('value="')
end = start + page[start:].find('"')
token = page[start:end]
return token
starts = ['<input type="hidden" name="%s"' % token,
'value="']
return find_in_page(page, starts, '"')
def get_ticket(page, app_url):
""" Get CSRF token """
starts = [app_url,'?ticket=']
return find_in_page(page, starts, '"')
def find_in_page(page, starts, stop):
""" make this less ugly and more generic with regex """
end = page.find(starts[0])
start = end + page[end:].find(starts[1]) + len(starts[1])
end = start + page[start:].find(stop)
found = page[start:end]
return found
def login(opener, auth):
""" Login to CAS server """
url = '%s/login?service=%s' % (CAS_SERVER_URL, APP_URL)
ticket = ''
token = get_token(opener, url)
if token:
auth[TOKEN] = token
else:
return 'FAILED CSRF Token could not be found on page'
print 'FAILED CSRF Token could not be found on page'
return ticket
auth['service'] = APP_URL
data = urllib.urlencode(auth)
sso_resp = opener.open(url, data)
......@@ -65,10 +78,11 @@ def login(opener, auth):
found = sso_page.find(CAS_SUCCESS) > -1
sso_resp.close()
if found:
return 'PASS CAS logged in to %s' % url
ticket = get_ticket(sso_page, APP_URL)
print 'PASS CAS logged in to %s' % url
else:
return 'FAILED CAS login to %s' % url
print 'FAILED CAS login to %s' % url
return ticket
def get_restricted(opener):
""" Access a restricted URL and see if its accessible """
......@@ -77,16 +91,17 @@ def get_restricted(opener):
ok = app_resp.code == 200
app_resp.close()
if ok:
return 'PASS logged in to restricted app at %s' % url
print 'PASS logged in to restricted app at %s' % url
else:
return 'FAILED to log in to restricted app at %s' % url
print 'FAILED to log in to restricted app at %s' % url
return
def get_proxy(ticket):
def get_proxy(opener, ticket):
""" Use login ticket to get proxy """
url_args = (CAS_SERVER_URL, ticket, APP_URL, PROXY_URL)
iou_url = '%s/serviceValidate?ticket=%s&service=%s&pgtUrl=%s'
iou_url = '%s/serviceValidate?ticket=%s&service=%s&pgtUrl=%s' % url_args
iou = opener.open(iou_url)
print iou
print iou.read()
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
......@@ -94,8 +109,9 @@ urllib2.install_opener(opener)
auth = get_auth()
print 'Test ordinary CAS login'
print '-----------------------'
print login(opener, auth)
print get_restricted(opener)
ticket = login(opener, auth)
get_restricted(opener)
print ''
print 'Test proxy CAS login'
print '--------------------'
get_proxy(opener, ticket)
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