Commit 535f28d9 by Ed Crewe

got proxy ticket

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