from openid.consumer.consumer import Consumer
from openid.extensions.sreg import SRegRequest, SRegResponse
from openid.store.filestore import FileOpenIDStore
+from openid.yadis.discover import DiscoveryFailure
from sqlalchemy.orm.exc import NoResultFound
from pylons import config, request, response, session, tmpl_context as c, url
from spline import model
from spline.model import meta
+from spline.lib import helpers as h
from spline.lib.base import BaseController, render
log = logging.getLogger(__name__)
openid_store = FileOpenIDStore('/var/tmp')
- def index(self):
- # Return a rendered template
- # return render('/template.mako')
- # or, Return a response
- return str(request.headers) + request.environ.get('scheme', '')
+ def _bail(self, reason):
+ # Used for bailing on a login attempt; reshows the login page
+ c.error = reason
+ c.attempted_openid = request.params.get('openid', '')
+ return render('/users/login.mako')
+
+
+ def login(self):
+ c.error = None
+ c.attempted_openid = None
+ return render('/users/login.mako')
def login_begin(self):
"""Step one of logging in with OpenID; we redirect to the provider"""
cons = Consumer(session=session, store=self.openid_store)
- auth_request = cons.begin(request.params['openid'])
+
+ try:
+ openid_url = request.params['openid']
+ except KeyError:
+ return self._bail("Gotta enter an OpenID to log in.")
+
+ try:
+ auth_request = cons.begin(openid_url)
+ except DiscoveryFailure:
+ return self._bail(
+ "Can't connect to '{0}'. You sure it's an OpenID?"
+ .format(openid_url)
+ )
+
sreg_req = SRegRequest(optional=['nickname', 'email', 'dob', 'gender',
'country', 'language', 'timezone'])
auth_request.addExtension(sreg_req)
# Create db records
user = model.User(name=username)
meta.Session.add(user)
+
openid = model.OpenID(openid=res.identity_url)
user.openids.append(openid)
+
meta.Session.commit()
# Remember who's logged in, and we're good to go
session['user_id'] = user.id
session.save()
- redirect_to(url('/'))
+ h.flash(u"""Hello, {0}!""".format(user.name),
+ icon='user')
+
+ redirect_to('/', _code=303)
+
+ def logout(self):
+ """Logs the user out."""
+
+ if 'user_id' in session:
+ del session['user_id']
+ session.save()
+
+ h.flash(u"""Logged out.""",
+ icon='user-silhouette')
+
+ redirect_to('/', _code=303)