3 from openid
.consumer
.consumer
import Consumer
4 from openid
.extensions
.sreg
import SRegRequest
, SRegResponse
5 from openid
.store
.filestore
import FileOpenIDStore
6 from openid
.yadis
.discover
import DiscoveryFailure
7 from sqlalchemy
.orm
.exc
import NoResultFound
9 from pylons
import request
, response
, session
, tmpl_context
as c
, url
10 from pylons
.controllers
.util
import abort
, redirect
, redirect_to
11 from routes
import url_for
, request_config
13 from floof
.lib
.base
import BaseController
, render
14 import floof
.lib
.helpers
as h
15 from floof
.model
.users
import IdentityURL
, User
17 log
= logging
.getLogger(__name__
)
19 from floof
.model
import UserPage
21 class AccountController(BaseController
):
23 openid_store
= FileOpenIDStore('/var/tmp')
26 c
.bogus_identity_url
= request
.params
.get('bogus_identity_url', None)
27 return render('/account/login.mako')
29 def login_begin(self
):
30 """Step one of logging in with OpenID; we redirect to the provider"""
32 identity_url
= request
.params
['identity_url']
33 cons
= Consumer(session
=session
, store
=self
.openid_store
)
35 auth_request
= cons
.begin(identity_url
)
36 except DiscoveryFailure
:
37 redirect_to(controller
='account', action
='login',
38 bogus_identity_url
=identity_url
)
40 sreg_req
= SRegRequest(optional
=['nickname', 'email', 'dob', 'gender',
41 'country', 'language', 'timezone'])
42 auth_request
.addExtension(sreg_req
)
44 host
= request
.headers
['host']
45 protocol
= request_config().protocol
46 return_url
= url_for(host
=host
, controller
='account', action
='login_finish')
47 new_url
= auth_request
.redirectURL(return_to
=return_url
,
48 realm
=protocol
+ '://' + host
)
51 def login_finish(self
):
52 """Step two of logging in; the OpenID provider redirects back here."""
54 cons
= Consumer(session
=session
, store
=self
.openid_store
)
55 host
= request
.headers
['host']
56 return_url
= url_for(host
=host
, controller
='account', action
='login_finish')
57 res
= cons
.complete(request
.params
, return_url
)
59 if res
.status
!= 'success':
60 return 'Error! %s' % res
.message
63 # Grab an existing user record, if one exists
64 q
= User
.query
.filter(User
.identity_urls
.any(url
=res
.identity_url
))
67 # Unrecognized URL. Redirect to a registration page to ask for a
69 session
['register:identity_url'] = res
.identity_url
71 # Try to pull a name out of the SReg response
72 sreg_res
= SRegResponse
.fromSuccessResponse(res
)
73 if sreg_res
and 'nickname' in sreg_res
:
74 session
['register:nickname'] = sreg_res
['nickname']
77 redirect(url('register'))
79 # Remember who's logged in, and we're good to go
80 session
['user_id'] = user
.id
83 # XXX send me where I came from
89 if 'user_id' in session
:
90 del session
['user_id']
94 # XXX send me where I came from
98 """Logging in with an unrecognized identity URL redirects here."""
100 c
.identity_url
= session
['register:identity_url']
101 c
.nickname
= session
.get('register:nickname', None)
102 # XXX hey, uh. warn if this name is taken already.
104 return render('/account/register.mako')
106 def register_finish(self
):
107 """Complete a new-user registration. Create the user and log in."""
109 identity_url
= session
['register:identity_url']
110 username
= request
.params
.get('username', None)
113 h
.flash(u
'Please enter a username.')
114 return self
.register()
116 if User
.query
.filter_by(name
=username
).count():
117 h
.flash(u
'This username is already taken.')
118 return self
.register()
120 if not User
.is_valid_name(username
):
121 h
.flash(u
'This username is not valid.')
122 return self
.register()
125 user
= User(name
=username
, display_name
=username
)
126 user
.identity_urls
.append(IdentityURL(url
=identity_url
))
128 elixir
.session
.commit()
131 session
['user_id'] = user
.id
133 h
.flash(u
'You are now logged in.')
135 # XXX send me where I came from