def verify_id(): if os.environ.has_key('HTTP_COOKIE'): cookie = Cookie.SimpleCookie(os.environ['HTTP_COOKIE']) if cookie.has_key("email") and cookie.has_key("session"): matches = Player.selectBy(email = cookie["email"].value, session = cookie["session"].value) if matches.count() > 0: if matches[0].session != "": return matches[0] # If verification fails, kick 'em back out to index.html print_redirect("index.html") sys.exit(1)
if __name__ == "__main__": cgitb.enable() q = cgi.FieldStorage() print "Content-type: text/html" print assertion = q.getfirst("assertion", "") email = verifyBrowserId(assertion) if email == False: print simplejson.dumps({"logged_in": "false"}) else: session = str(uuid.uuid1()) matches = Player.selectBy(email=email) if matches.count() == 0: # user has not logged in before: create account kwargs = { "email": email, "name": email.split("@")[0], # use first part of email address as username "session": session, "avatarURL": DEFAULT_AVATAR_URL, } newUser = Player(**kwargs) else: oldUser = matches[0] oldUser.session = session # Return JSON to the client's XHR containing email and session uuid print simplejson.dumps({"logged_in": "true", "email": email, "session": session})