03c9392188a810895bdd8fb347ffd1cccb880604
2 from openid
.consumer
.consumer
import Consumer
3 from openid
.extensions
.sreg
import SRegRequest
, SRegResponse
4 from openid
.store
.filestore
import FileOpenIDStore
5 from sqlalchemy
.orm
.exc
import NoResultFound
7 from pylons
import config
, request
, response
, session
, tmpl_context
as c
, url
8 from pylons
.controllers
.util
import abort
, redirect_to
9 from routes
import request_config
11 from spline
import model
12 from spline
.model
import meta
13 from spline
.lib
.base
import BaseController
, render
15 log
= logging
.getLogger(__name__
)
17 class AccountsController(BaseController
):
19 openid_store
= FileOpenIDStore('/var/tmp')
22 # Return a rendered template
23 # return render('/template.mako')
24 # or, Return a response
25 return str(request
.headers
) + request
.environ
.get('scheme', '')
27 def login_begin(self
):
28 """Step one of logging in with OpenID; we redirect to the provider"""
30 cons
= Consumer(session
=session
, store
=self
.openid_store
)
31 auth_request
= cons
.begin(request
.params
['openid'])
32 sreg_req
= SRegRequest(optional
=['nickname', 'email', 'dob', 'gender',
33 'country', 'language', 'timezone'])
34 auth_request
.addExtension(sreg_req
)
36 host
= request
.headers
['host']
37 protocol
= request_config().protocol
38 return_url
= url(host
=host
, controller
='accounts', action
='login_finish')
39 new_url
= auth_request
.redirectURL(return_to
=return_url
,
40 realm
=protocol
+ '://' + host
)
43 def login_finish(self
):
44 """Step two of logging in; the OpenID provider redirects back here."""
46 cons
= Consumer(session
=session
, store
=self
.openid_store
)
47 host
= request
.headers
['host']
48 return_url
= url(host
=host
, controller
='accounts', action
='login_finish')
49 res
= cons
.complete(request
.params
, return_url
)
51 if res
.status
!= 'success':
52 return 'Error! %s' % res
.message
55 # Grab an existing user record, if one exists
56 q
= meta
.Session
.query(model
.User
) \
57 .filter(model
.User
.openids
.any(openid
=res
.identity_url
))
60 # Try to pull a name out of the SReg response
61 sreg_res
= SRegResponse
.fromSuccessResponse(res
)
63 username
= sreg_res
['nickname']
64 except (KeyError, TypeError):
65 # KeyError if sreg has no nickname; TypeError if sreg is None
66 username
= 'Anonymous'
69 user
= model
.User(name
=username
)
70 meta
.Session
.add(user
)
71 openid
= model
.OpenID(openid
=res
.identity_url
)
72 user
.openids
.append(openid
)
75 # Remember who's logged in, and we're good to go
76 session
['user_id'] = user
.id