]>
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("Cache-Control", "no-store")
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
, secure
=True)
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
):
43 next
= self
.get_argument("next", None)
45 self
.render("auth/login.html", next
=next
,
46 incorrect
=False, username
=None)
48 @base.ratelimit(minutes
=15, requests
=10)
50 username
= self
.get_argument("username")
51 password
= self
.get_argument("password")
52 next
= self
.get_argument("next", "/")
55 account
= self
.backend
.accounts
.auth(username
, password
)
57 logging
.error("Unknown user or invalid password: %s" % username
)
62 # Render login page again
63 return self
.render("auth/login.html",
64 incorrect
=True, username
=username
, next
=next
,
68 with self
.db
.transaction():
72 return self
.redirect(next
)
75 class LogoutHandler(AuthenticationMixin
, base
.BaseHandler
):
77 with self
.db
.transaction():
80 # Get back to the start page
84 class RegisterHandler(CacheMixin
, base
.BaseHandler
):
86 # Redirect logged in users away
91 self
.render("auth/register.html")
93 @base.ratelimit(minutes
=15, requests
=5)
95 uid
= self
.get_argument("uid")
96 email
= self
.get_argument("email")
98 first_name
= self
.get_argument("first_name")
99 last_name
= self
.get_argument("last_name")
103 with self
.db
.transaction():
104 self
.backend
.accounts
.register(uid
, email
,
105 first_name
=first_name
, last_name
=last_name
,
106 country_code
=self
.current_country_code
)
107 except ValueError as e
:
108 raise tornado
.web
.HTTPError(400, "%s" % e
) from e
110 self
.render("auth/register-success.html")
113 class ActivateHandler(AuthenticationMixin
, base
.BaseHandler
):
114 def get(self
, uid
, activation_code
):
115 self
.render("auth/activate.html")
117 def post(self
, uid
, activation_code
):
118 password1
= self
.get_argument("password1")
119 password2
= self
.get_argument("password2")
121 if not password1
== password2
:
122 raise tornado
.web
.HTTPError(400, "Passwords do not match")
124 with self
.db
.transaction():
125 account
= self
.backend
.accounts
.activate(uid
, activation_code
)
127 raise tornado
.web
.HTTPError(400, "Account not found: %s" % uid
)
129 # Set the new password
130 account
.passwd(password1
)
135 # Redirect to success page
136 self
.render("auth/activated.html", account
=account
)
139 class PasswordResetInitiationHandler(CacheMixin
, base
.BaseHandler
):
141 username
= self
.get_argument("username", None)
143 self
.render("auth/password-reset-initiation.html", username
=username
)
145 @base.ratelimit(minutes
=15, requests
=10)
147 username
= self
.get_argument("username")
149 # Fetch account and submit password reset
150 account
= self
.backend
.accounts
.get_by_uid(username
)
152 with self
.db
.transaction():
153 account
.request_password_reset()
155 self
.render("auth/password-reset-successful.html")
158 class PasswordResetHandler(AuthenticationMixin
, base
.BaseHandler
):
159 def get(self
, uid
, reset_code
):
160 account
= self
.backend
.accounts
.get_by_uid(uid
)
162 raise tornado
.web
.HTTPError(404, "Could not find account: %s" % uid
)
164 self
.render("auth/password-reset.html", account
=account
)
166 def post(self
, uid
, reset_code
):
167 account
= self
.backend
.accounts
.get_by_uid(uid
)
169 raise tornado
.web
.HTTPError(404, "Could not find account: %s" % uid
)
171 password1
= self
.get_argument("password1")
172 password2
= self
.get_argument("password2")
174 if not password1
== password2
:
175 raise tornado
.web
.HTTPError(400, "Passwords do not match")
177 # Try to perform password reset
178 with self
.db
.transaction():
179 account
.reset_password(reset_code
, password1
)
181 # Login the user straight away after reset was successful
188 class APICheckUID(base
.APIHandler
):
189 @base.ratelimit(minutes
=1, requests
=100)
191 uid
= self
.get_argument("uid")
197 # Check if the username is syntactically valid
198 elif not self
.backend
.accounts
.uid_is_valid(uid
):
201 # Check if the username is already taken
202 elif self
.backend
.accounts
.uid_exists(uid
):
205 # Username seems to be okay
206 self
.finish({ "result" : result
or "ok" })
209 class APICheckEmail(base
.APIHandler
):
210 @base.ratelimit(minutes
=1, requests
=100)
212 email
= self
.get_argument("email")
218 elif not self
.backend
.accounts
.mail_is_valid(email
):
221 # Check if this email address is blacklisted
222 elif self
.backend
.accounts
.mail_is_blacklisted(email
):
223 result
= "blacklisted"
225 # Check if this email address is already useed
226 elif self
.backend
.accounts
.get_by_mail(email
):
229 self
.finish({ "result" : result
or "ok" })