root / shibauth.py @ 1:bf4fa008f2a5
History | View | Annotate | Download (2.1 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
from mechanize import Browser |
4 |
from getpass import getpass |
5 |
import sys, re |
6 |
|
7 |
class ShibError(Exception): |
8 |
def __init__(self, value): |
9 |
self.parameter = value
|
10 |
|
11 |
def __str__(self): |
12 |
return repr(self.parameter) |
13 |
|
14 |
def getGSSNonce(edupersonPrincipalName): |
15 |
bot = Browser() |
16 |
response = bot.open("http://gss.grnet.gr/gss/nonce?user=%s" % edupersonPrincipalName)
|
17 |
return response.read().rstrip()
|
18 |
|
19 |
def getGSSToken(edupersonPrincipalName, nonce): |
20 |
bot = Browser() |
21 |
response = bot.open("https://gss.grnet.gr/gss/token?user=%s&nonce=%s" % (edupersonPrincipalName,nonce))
|
22 |
return response.read().rstrip()
|
23 |
|
24 |
def shibOpen(url,username="",password=""): |
25 |
# Create our www bot and send it to the website
|
26 |
bot = Browser() |
27 |
bot.open(url) |
28 |
|
29 |
# Check if we were redirected to the WAYF
|
30 |
if not re.match(r'https://wayf\.grnet\.gr',bot.geturl()): |
31 |
raise ShibError("We weren't redirected to WAYF, exiting...\n") |
32 |
|
33 |
sys.stderr.write("Hit the WAYF server\n")
|
34 |
|
35 |
# Fill in the form ;-)
|
36 |
bot.select_form(name="IdPList")
|
37 |
bot["user_idp"] = ['http://www.grnet.gr/aai:admin.grnet.gr'] |
38 |
|
39 |
# Submit the form. This will almost certainly
|
40 |
# fail with a 401.
|
41 |
try:
|
42 |
response = bot.submit() |
43 |
except:
|
44 |
bot.add_password(bot.geturl(),username,password) |
45 |
response = bot.reload() |
46 |
|
47 |
# Check if we were redirected back to the SP
|
48 |
if not re.match(r'.*/SSO\?shire=', bot.geturl()): |
49 |
raise ShibError("The IdP didn't send us back to the SP, something's wrong\n") |
50 |
|
51 |
sys.stderr.write("Back at the SP\n")
|
52 |
|
53 |
# Select the first form. The form has no name, only id='shibboleth'
|
54 |
bot.select_form(nr=0)
|
55 |
response = bot.submit() |
56 |
|
57 |
return response
|
58 |
|
59 |
if __name__ == "__main__": |
60 |
if len(sys.argv) != 2: |
61 |
print "Usage: %s <edupersonPrincipalName>" % sys.argv[0] |
62 |
sys.exit(1)
|
63 |
|
64 |
nonce = getGSSNonce(sys.argv[1])
|
65 |
sys.stderr.write("Got nonce: %s\n" % nonce)
|
66 |
response = shibOpen("https://gss.grnet.gr/gss/login?nonce=%s" % nonce, sys.argv[1].split("@")[0], getpass()) |
67 |
print response.read()
|
68 |
print getGSSToken(sys.argv[1],nonce) |