1
# Copyright 2010 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
# We use Django forms for webservice input validation
6
from django import forms
7
from django.forms import fields
8
from django.utils.translation import ugettext as _
9
from lazr.restful.utils import get_current_browser_request
11
from identityprovider.utils import password_policy_compliant
12
from identityprovider.models.captcha import Captcha
15
class WebserviceCreateAccountForm(forms.Form):
16
email = fields.EmailField()
17
password = fields.CharField(max_length=256)
18
captcha_id = fields.CharField(max_length=1024)
19
captcha_solution = fields.CharField(max_length=256)
21
def clean_password(self):
22
if 'password' in self.cleaned_data:
23
password = self.cleaned_data['password']
26
except UnicodeEncodeError:
27
raise forms.ValidationError(
28
_("Invalid characters in password"))
29
if not password_policy_compliant(password):
30
raise forms.ValidationError(_("Password must be at least "
31
"8 characters long, and must contain at least one "
32
"number and an upper case letter."))
36
cleaned_data = self.cleaned_data
37
captcha_id = cleaned_data.get('captcha_id')
38
captcha_solution = cleaned_data.get('captcha_solution')
40
request = get_current_browser_request()
41
captcha = Captcha(request.environment, captcha_id)
42
if not captcha.verify(captcha_solution):
43
raise forms.ValidationError(_("Wrong captcha solution."))