~ubuntuone-pqm-team/canonical-identity-provider/trunk

« back to all changes in this revision

Viewing changes to identityprovider/webservice/forms.py

  • Committer: Danny Tamez
  • Date: 2010-04-21 15:29:24 UTC
  • Revision ID: danny.tamez@canonical.com-20100421152924-lq1m92tstk2iz75a
Canonical SSO Provider (Open Source) - Initial Commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
# We use Django forms for webservice input validation
 
5
 
 
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
 
10
 
 
11
from identityprovider.utils import password_policy_compliant
 
12
from identityprovider.models.captcha import Captcha
 
13
 
 
14
 
 
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)
 
20
 
 
21
    def clean_password(self):
 
22
        if 'password' in self.cleaned_data:
 
23
            password = self.cleaned_data['password']
 
24
            try:
 
25
                str(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."))
 
33
            return password
 
34
 
 
35
    def clean(self):
 
36
        cleaned_data = self.cleaned_data
 
37
        captcha_id = cleaned_data.get('captcha_id')
 
38
        captcha_solution = cleaned_data.get('captcha_solution')
 
39
 
 
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."))
 
44
 
 
45
        return cleaned_data