]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/auth.py
8 class CacheMixin(object):
10 # Mark this as private when someone is logged in
12 self
.add_header("Cache-Control", "private")
14 self
.add_header("Vary", "Cookie")
17 class AuthenticationMixin(CacheMixin
):
18 def login(self
, account
):
19 # User has logged in, create a session
20 session_id
, session_expires
= self
.backend
.accounts
.create_session(
21 account
, self
.request
.host
)
23 # Check if a new session was created
25 raise tornado
.web
.HTTPError(500, "Could not create session")
27 # Send session cookie to the client
28 self
.set_cookie("session_id", session_id
,
29 domain
=self
.request
.host
, expires
=session_expires
)
32 session_id
= self
.get_cookie("session_id")
36 success
= self
.backend
.accounts
.destroy_session(session_id
, self
.request
.host
)
38 self
.clear_cookie("session_id")
41 class LoginHandler(AuthenticationMixin
, base
.BaseHandler
):
44 next
= self
.get_argument("next", None)
46 self
.render("auth/login.html", next
=next
,
47 incorrect
=False, username
=None)
50 @base.ratelimit(minutes
=60, requests
=5)
52 username
= self
.get_argument("username")
53 password
= self
.get_argument("password")
54 next
= self
.get_argument("next", "/")
57 account
= self
.backend
.accounts
.auth(username
, password
)
59 logging
.error("Unknown user or invalid password: %s" % username
)
64 # Render login page again
65 return self
.render("auth/login.html",
66 incorrect
=True, username
=username
, next
=next
,
70 with self
.db
.transaction():
74 return self
.redirect(next
)
77 class LogoutHandler(AuthenticationMixin
, base
.BaseHandler
):
79 with self
.db
.transaction():
82 # Get back to the start page
86 class RegisterHandler(base
.BaseHandler
):
89 # Redirect logged in users away
93 self
.render("auth/register.html")
95 @base.ratelimit(minutes
=24*60, requests
=5)
97 uid
= self
.get_argument("uid")
98 email
= self
.get_argument("email")
100 first_name
= self
.get_argument("first_name")
101 last_name
= self
.get_argument("last_name")
103 # Check if this is a spam account
104 is_spam
= await self
.backend
.accounts
.check_spam(uid
, email
,
105 address
=self
.get_remote_ip())
108 self
.render("auth/register-spam.html")
113 with self
.db
.transaction():
114 self
.backend
.accounts
.register(uid
, email
,
115 first_name
=first_name
, last_name
=last_name
,
116 country_code
=self
.current_country_code
)
117 except ValueError as e
:
118 raise tornado
.web
.HTTPError(400, "%s" % e
) from e
120 self
.render("auth/register-success.html")
123 class ActivateHandler(AuthenticationMixin
, base
.BaseHandler
):
124 def get(self
, uid
, activation_code
):
125 self
.render("auth/activate.html")
127 def post(self
, uid
, activation_code
):
128 password1
= self
.get_argument("password1")
129 password2
= self
.get_argument("password2")
131 if not password1
== password2
:
132 raise tornado
.web
.HTTPError(400, "Passwords do not match")
134 with self
.db
.transaction():
135 account
= self
.backend
.accounts
.activate(uid
, activation_code
)
137 raise tornado
.web
.HTTPError(400, "Account not found: %s" % uid
)
139 # Set the new password
140 account
.passwd(password1
)
145 # Redirect to success page
146 self
.render("auth/activated.html", account
=account
)
149 class PasswordResetInitiationHandler(base
.BaseHandler
):
151 username
= self
.get_argument("username", None)
153 self
.render("auth/password-reset-initiation.html", username
=username
)
155 @base.ratelimit(minutes
=60, requests
=5)
157 username
= self
.get_argument("username")
159 # Fetch account and submit password reset
160 account
= self
.backend
.accounts
.get_by_uid(username
)
162 with self
.db
.transaction():
163 account
.request_password_reset()
165 self
.render("auth/password-reset-successful.html")
168 class PasswordResetHandler(AuthenticationMixin
, base
.BaseHandler
):
169 def get(self
, uid
, reset_code
):
170 account
= self
.backend
.accounts
.get_by_uid(uid
)
172 raise tornado
.web
.HTTPError(404, "Could not find account: %s" % uid
)
174 self
.render("auth/password-reset.html", account
=account
)
176 def post(self
, uid
, reset_code
):
177 account
= self
.backend
.accounts
.get_by_uid(uid
)
179 raise tornado
.web
.HTTPError(404, "Could not find account: %s" % uid
)
181 password1
= self
.get_argument("password1")
182 password2
= self
.get_argument("password2")
184 if not password1
== password2
:
185 raise tornado
.web
.HTTPError(400, "Passwords do not match")
187 # Try to perform password reset
188 with self
.db
.transaction():
189 account
.reset_password(reset_code
, password1
)
191 # Login the user straight away after reset was successful
198 class APICheckUID(base
.APIHandler
):
199 @base.ratelimit(minutes
=10, requests
=100)
201 uid
= self
.get_argument("uid")
207 # Check if the username is syntactically valid
208 elif not self
.backend
.accounts
.uid_is_valid(uid
):
211 # Check if the username is already taken
212 elif self
.backend
.accounts
.uid_exists(uid
):
215 # Username seems to be okay
216 self
.finish({ "result" : result
or "ok" })