A simple web application that allows invitation based registration to a matrix instance
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. import safe
  2. from flask_wtf import FlaskForm
  3. from wtforms import StringField, PasswordField, IntegerField, SubmitField
  4. from wtforms.fields.html5 import DateField
  5. from wtforms.validators import DataRequired, Length, EqualTo, InputRequired, \
  6. ValidationError, NumberRange, Optional
  7. from db import get_registration_code, get_registered_user
  8. def safe_password_validator(form: FlaskForm, field):
  9. strength = safe.check(field.data, level=safe.MEDIUM)
  10. if not strength.valid:
  11. raise ValidationError("Password is not secure enough: %s" % strength.message)
  12. def registration_code_validator(form: FlaskForm, field):
  13. registration_code = get_registration_code(field.data)
  14. if registration_code is None:
  15. raise ValidationError("Registration code invalid!")
  16. if registration_code.is_expired():
  17. raise ValidationError("Registration code expired!")
  18. if not registration_code.has_available_uses():
  19. raise ValidationError("Registration code out of uses!")
  20. def username_availability_validator(form: FlaskForm, field):
  21. registered_user = get_registered_user(field.data)
  22. if registered_user is not None:
  23. raise ValidationError("Username already registered!")
  24. class RegistrationForm(FlaskForm):
  25. username = StringField(
  26. 'Username',
  27. validators=[
  28. DataRequired(),
  29. Length(min=3, max=30),
  30. username_availability_validator
  31. ])
  32. password = PasswordField(
  33. 'Password',
  34. validators=[
  35. InputRequired(),
  36. EqualTo('confirm', message='Passwords must match'),
  37. safe_password_validator
  38. ])
  39. confirm = PasswordField('Repeat Password')
  40. registration_code = StringField(
  41. 'Registration Code', validators=[DataRequired(), registration_code_validator])
  42. class LoginForm(FlaskForm):
  43. username = StringField('Username', validators=[DataRequired()])
  44. token = PasswordField('Token', validators=[DataRequired()])
  45. class RegistrationCodeForm(FlaskForm):
  46. expiration_time = DateField('Expiration Time', validators=[Optional()])
  47. max_usages = IntegerField('Max Usages', validators=[NumberRange(min=1)])
  48. class ExpireRegistrationCodeForm(FlaskForm):
  49. registration_code = StringField('Registration Code')
  50. expire = SubmitField(label='Expire')
  51. delete = SubmitField(label='Delete')