CCrypto VPN public website https://vpn.ccrypto.org/
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.
 
 
 
 
 
 

91 lines
2.5 KiB

  1. import base64
  2. from django import forms
  3. from django.contrib.auth.models import User
  4. from django.utils.translation import ugettext_lazy as _
  5. from django.utils.safestring import mark_safe
  6. class FormPureRender:
  7. def as_pure_aligned(self):
  8. html = ""
  9. for f in self:
  10. html += '<div class="pure-control-group">\n'
  11. html += str(f.label_tag()) + "\n"
  12. html += str(f) + "\n"
  13. if f.errors:
  14. html += str(f.errors) + "\n"
  15. html += "</div>\n"
  16. return mark_safe(html)
  17. class UserField(forms.RegexField):
  18. def clean(self, value):
  19. super(UserField, self).clean(value)
  20. try:
  21. User.objects.get(username=value)
  22. raise forms.ValidationError(_("Username taken."))
  23. except User.DoesNotExist:
  24. return value
  25. class SignupForm(forms.Form, FormPureRender):
  26. username = UserField(
  27. label=_("Username"),
  28. min_length=2,
  29. max_length=16,
  30. regex="^[a-zA-Z0-9_-]+$",
  31. widget=forms.TextInput(
  32. attrs={
  33. "required": "true",
  34. "pattern": "[a-zA-Z0-9_-]{2,32}",
  35. "placeholder": _("Username"),
  36. "autofocus": "true",
  37. }
  38. ),
  39. )
  40. password = forms.CharField(
  41. label=_("Password"),
  42. widget=forms.PasswordInput(attrs={"placeholder": _("Anything")}),
  43. )
  44. password2 = forms.CharField(
  45. label=_("Repeat"),
  46. widget=forms.PasswordInput(attrs={"placeholder": _("Same Anything")}),
  47. )
  48. email = forms.EmailField(
  49. label=_("E-Mail"),
  50. widget=forms.EmailInput(attrs={"placeholder": _("E-Mail")}),
  51. required=False,
  52. )
  53. def clean_password(self):
  54. if self.data["password"] != self.data["password2"]:
  55. raise forms.ValidationError(_("Passwords are not the same"))
  56. return self.data["password"]
  57. class ReqEmailForm(forms.Form, FormPureRender):
  58. email = forms.EmailField(
  59. label=_("E-Mail"),
  60. widget=forms.EmailInput(attrs={"placeholder": _("E-Mail")}),
  61. )
  62. def wg_pk_validator(s):
  63. try:
  64. data = base64.b64decode(s, validate=True)
  65. except:
  66. raise forms.ValidationError("Invalid public key format")
  67. if len(data) != 32:
  68. raise forms.ValidationError("Invalid public key length")
  69. class WgPeerForm(forms.Form):
  70. public_key = forms.CharField(
  71. min_length=3,
  72. max_length=100,
  73. strip=True,
  74. required=False,
  75. validators=[wg_pk_validator],
  76. )
  77. name = forms.CharField(max_length=21, required=False)