Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6b5292f9cd
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 70 lines (59 sloc) 1.914 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#!/usr/bin/python

import cgi
import os
import requests

def print_login_form():
    print """<html>
<head>
<script src="https://browserid.org/include.js"></script>
<script>
function login() {
    navigator.id.get(function (assertion) {
        if (assertion) {
            var assertion_field = document.getElementById("assertion-field");
            assertion_field.value = assertion;
            var login_form = document.getElementById("login-form");
            login_form.submit();
        }
    });
}
</script>
</head>

<body>
<form id="login-form" method="POST">
<input id="assertion-field" type="hidden" name="assertion" value="">
</form>

<p><a href="javascript:login()">Login</a></p>
</body>
</html>"""

def verify_assertion(assertion):
    audience = 'http://'
    if 'HTTPS' in os.environ:
        audience = 'https://'
    audience += os.environ['SERVER_NAME'] + ':' + os.environ['SERVER_PORT']

    try:
        page = requests.post('https://browserid.org/verify',
                             verify=True,
                             data={ "assertion": assertion,
                                    "audience": audience})
        data = page.json
    except requests.exceptions.SSLError:
        data = { "status": "failed",
                 "reason": "Could not verify SSL certificate" }
    except requests.exceptions.ConnectionError:
        data = { "status": "failed",
                 "reason": "Could not connect to server" }

    return data

print 'Content-type: text/html\n\n'

form = cgi.FieldStorage()
if 'assertion' in form:
    print "<html><body>"
    result = verify_assertion(form['assertion'].value)
    if result['status'] == 'okay':
        print "<p>Logged in as: " + result['email'] + "</p>"
    else:
        print "<p>Error: " + result['reason'] + "</p>"

    print '<p><a href="python.cgi">Back to login page</p>'
    print "</body></html>"
else:
    print_login_form()
Something went wrong with that request. Please try again.