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.

142 lines
3.9 KiB

10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
  1. import base64
  2. import binascii
  3. from datetime import timedelta as td
  4. from django import forms
  5. from django.core.exceptions import ValidationError
  6. from django.contrib.auth import authenticate
  7. from django.contrib.auth.models import User
  8. from hc.api.models import TokenBucket
  9. class LowercaseEmailField(forms.EmailField):
  10. def clean(self, value):
  11. value = super(LowercaseEmailField, self).clean(value)
  12. return value.lower()
  13. class Base64Field(forms.CharField):
  14. def to_python(self, value):
  15. if value is None:
  16. return None
  17. try:
  18. return base64.b64decode(value.encode())
  19. except binascii.Error:
  20. raise ValidationError(message="Cannot decode base64")
  21. class AvailableEmailForm(forms.Form):
  22. # Call it "identity" instead of "email"
  23. # to avoid some of the dumber bots
  24. identity = LowercaseEmailField(
  25. error_messages={"required": "Please enter your email address."}
  26. )
  27. def clean_identity(self):
  28. v = self.cleaned_data["identity"]
  29. if len(v) > 254:
  30. raise forms.ValidationError("Address is too long.")
  31. if User.objects.filter(email=v).exists():
  32. raise forms.ValidationError(
  33. "An account with this email address already exists."
  34. )
  35. return v
  36. class EmailLoginForm(forms.Form):
  37. # Call it "identity" instead of "email"
  38. # to avoid some of the dumber bots
  39. identity = LowercaseEmailField()
  40. def clean_identity(self):
  41. v = self.cleaned_data["identity"]
  42. if not TokenBucket.authorize_login_email(v):
  43. raise forms.ValidationError("Too many attempts, please try later.")
  44. try:
  45. self.user = User.objects.get(email=v)
  46. except User.DoesNotExist:
  47. raise forms.ValidationError("Incorrect email address.")
  48. return v
  49. class PasswordLoginForm(forms.Form):
  50. email = LowercaseEmailField()
  51. password = forms.CharField()
  52. def clean(self):
  53. username = self.cleaned_data.get("email")
  54. password = self.cleaned_data.get("password")
  55. if username and password:
  56. if not TokenBucket.authorize_login_password(username):
  57. raise forms.ValidationError("Too many attempts, please try later.")
  58. self.user = authenticate(username=username, password=password)
  59. if self.user is None or not self.user.is_active:
  60. raise forms.ValidationError("Incorrect email or password.")
  61. return self.cleaned_data
  62. class ReportSettingsForm(forms.Form):
  63. reports_allowed = forms.BooleanField(required=False)
  64. nag_period = forms.IntegerField(min_value=0, max_value=86400)
  65. def clean_nag_period(self):
  66. seconds = self.cleaned_data["nag_period"]
  67. if seconds not in (0, 3600, 86400):
  68. raise forms.ValidationError("Bad nag_period: %d" % seconds)
  69. return td(seconds=seconds)
  70. class SetPasswordForm(forms.Form):
  71. password = forms.CharField(min_length=8)
  72. class ChangeEmailForm(forms.Form):
  73. error_css_class = "has-error"
  74. email = LowercaseEmailField()
  75. def clean_email(self):
  76. v = self.cleaned_data["email"]
  77. if User.objects.filter(email=v).exists():
  78. raise forms.ValidationError("%s is already registered" % v)
  79. return v
  80. class InviteTeamMemberForm(forms.Form):
  81. email = LowercaseEmailField(max_length=254)
  82. rw = forms.BooleanField(required=False)
  83. class RemoveTeamMemberForm(forms.Form):
  84. email = LowercaseEmailField()
  85. class ProjectNameForm(forms.Form):
  86. name = forms.CharField(max_length=60)
  87. class TransferForm(forms.Form):
  88. email = LowercaseEmailField()
  89. class AddCredentialForm(forms.Form):
  90. name = forms.CharField(max_length=100)
  91. client_data_json = Base64Field()
  92. attestation_object = Base64Field()
  93. class WebAuthnForm(forms.Form):
  94. credential_id = Base64Field()
  95. client_data_json = Base64Field()
  96. authenticator_data = Base64Field()
  97. signature = Base64Field()