~django-openid-auth/django-openid-auth/trunk

« back to all changes in this revision

Viewing changes to django_openid_auth/auth.py

  • Committer: Anthony Lenton
  • Date: 2011-07-22 13:34:26 UTC
  • mfrom: (82.1.6 810978-required-sreg)
  • Revision ID: anthony.lenton@canonical.com-20110722133426-galsg7dcs142akyr
MergedĀ inĀ lp:~michael.nelson/django-openid-auth/810978-required-sreg

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
class StrictUsernameViolation(Exception):
46
46
    pass
47
47
 
 
48
class RequiredAttributeNotReturned(Exception):
 
49
    pass
 
50
 
48
51
class OpenIDBackend:
49
52
    """A django.contrib.auth backend that authenticates the user based on
50
53
    an OpenID response."""
74
77
                claimed_id__exact=openid_response.identity_url)
75
78
        except UserOpenID.DoesNotExist:
76
79
            if getattr(settings, 'OPENID_CREATE_USERS', False):
77
 
                try:
78
 
                    user = self.create_user_from_openid(openid_response)
79
 
                except StrictUsernameViolation:
80
 
                    return None
 
80
                user = self.create_user_from_openid(openid_response)
81
81
        else:
82
82
            user = user_openid.user
83
83
 
144
144
                    first_name=first_name, last_name=last_name)
145
145
 
146
146
    def _get_available_username(self, nickname, identity_url):
147
 
        # If we're being strict about usernames, throw an error if we didn't
148
 
        # get one back from the provider
149
 
        if getattr(settings, 'OPENID_STRICT_USERNAMES', False):
150
 
            if nickname is None or nickname == '':
151
 
                raise StrictUsernameViolation("No username")
152
 
 
153
147
        # If we don't have a nickname, and we're not being strict, use a default
154
148
        nickname = nickname or 'openiduser'
155
149
 
184
178
 
185
179
        if getattr(settings, 'OPENID_STRICT_USERNAMES', False):
186
180
            if User.objects.filter(username__exact=nickname).count() > 0:
187
 
                raise StrictUsernameViolation("Duplicate username: %s" % nickname)
 
181
                raise StrictUsernameViolation(
 
182
                    "The username (%s) with which you tried to log in is "
 
183
                    "already in use for a different account." % nickname)
188
184
 
189
185
        # Pick a username for the user based on their nickname,
190
186
        # checking for conflicts.
202
198
 
203
199
    def create_user_from_openid(self, openid_response):
204
200
        details = self._extract_user_details(openid_response)
 
201
        required_attrs = getattr(settings, 'OPENID_SREG_REQUIRED_FIELDS', [])
 
202
        if getattr(settings, 'OPENID_STRICT_USERNAMES', False):
 
203
            required_attrs.append('nickname')
 
204
 
 
205
        for required_attr in required_attrs:
 
206
            if required_attr not in details or not details[required_attr]:
 
207
                raise RequiredAttributeNotReturned(
 
208
                    "An attribute required for logging in was not "
 
209
                    "returned ({0}).".format(required_attr))
 
210
 
205
211
        nickname = details['nickname'] or 'openiduser'
206
212
        email = details['email'] or ''
207
213