activation_code=activation_code, email=email,
first_name=first_name, last_name=last_name)
- def create(self, uid, email, first_name, last_name):
- activation_code = util.random_string(24)
+ def activate(self, uid, activation_code):
+ res = self.db.get("DELETE FROM account_activations \
+ WHERE uid = %s AND activation_code = %s AND expires_at > NOW() \
+ RETURNING *", uid, activation_code)
+
+ # Return nothing when account was not found
+ if not res:
+ return
+ # Create a new account on the LDAP database
+ return self.create(uid, res.email,
+ first_name=res.first_name, last_name=res.last_name)
+
+ def create(self, uid, email, first_name, last_name):
# Account Parameters
account = {
"objectClass" : [b"top", b"person", b"inetOrgPerson"],
- "userPassword" : activation_code.encode(),
"mail" : email.encode(),
# Name
"givenName" : first_name.encode(),
}
+ logging.info("Creating new account: %s: %s" % (uid, account))
+
+ # Create DN
+ dn = "uid=%s,ou=People,dc=mcfly,dc=local" % uid
+
# Create account on LDAP
- self.ldap.add_s("uid=%s,ou=People,dc=mcfly,dc=local" % uid, ldap.modlist.addModlist(account))
+ self.ldap.add_s(dn, ldap.modlist.addModlist(account))
- # TODO Send email with activation code
- pass
+ # Return account
+ return self._get_account_from_dn(dn)
# Session stuff
<div class="col col-md-4">
<h5 class=" mb-4">{{ _("Activate Your Account") }}</h5>
+ <p>
+ {{ _("Thank you for creating a new account.") }}
+ {{ _("To activate it, please enter a strong password.") }}
+ </p>
+
<form action="" method="POST">
{% raw xsrf_form_html() %}
- {% module Password(account) %}
+ {% module Password() %}
<button type="submit" class="btn btn-primary btn-block">
{{ _("Activate Account") }}
class ActivateHandler(AuthenticationMixin, base.BaseHandler):
def get(self, uid, activation_code):
- # Fetch the account
- account = self.backend.accounts.get_by_uid(uid)
- if not account:
- raise tornado.web.HTTPError(400, "Account not found: %s" % uid)
-
- # Validate activation code
- if not account.check_password(activation_code):
- raise tornado.web.HTTPError(400, "Activation code did not match: %s" % activation_code)
-
- self.render("auth/activate.html", account=account)
+ self.render("auth/activate.html")
def post(self, uid, activation_code):
- password = self.get_argument("password1")
+ password1 = self.get_argument("password1")
+ password2 = self.get_argument("password2")
- # Fetch the account
- account = self.backend.accounts.get_by_uid(uid)
- if not account:
- raise tornado.web.HTTPError(404, "Account not found: %s" % uid)
+ if not password1 == password2:
+ raise tornado.web.HTTPError(400, "Passwords do not match")
- # Validate activation code
- if not account.check_password(activation_code):
- raise tornado.web.HTTPError(403, "Activation code did not match: %s" % activation_code)
+ with self.db.transaction():
+ account = self.backend.accounts.activate(uid, activation_code)
+ if not account:
+ raise tornado.web.HTTPError(400, "Account not found: %s" % uid)
- # Set the new password
- account.passwd(password)
+ # Set the new password
+ account.passwd(password1)
- # Create session
- self.login(account)
+ # Create session
+ self.login(account)
# Redirect to main page
self.redirect("/")