~joe-topjian-v/django-openid-auth/jtopjian-dev

« back to all changes in this revision

Viewing changes to django_openid_auth/views.py

Add support for requesting user details via the Attribute Exchange extension.  This allows us to retrieve user details from providers that don't implement the Simple Registration extension (e.g. Google).  Fixes bug #517393.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
from openid.consumer.consumer import (
45
45
    Consumer, SUCCESS, CANCEL, FAILURE)
46
46
from openid.consumer.discover import DiscoveryFailure
47
 
from openid.extensions import sreg
 
47
from openid.extensions import sreg, ax
48
48
 
49
49
from django_openid_auth import teams
50
50
from django_openid_auth.forms import OpenIDLoginForm
163
163
        return render_failure(
164
164
            request, "OpenID discovery error: %s" % (str(exc),), status=500)
165
165
 
166
 
    # Request some user details.
167
 
    openid_request.addExtension(
168
 
        sreg.SRegRequest(optional=['email', 'fullname', 'nickname']))
 
166
    # Request some user details.  If the provider advertises support
 
167
    # for attribute exchange, use that.
 
168
    if openid_request.endpoint.supportsType(ax.AXMessage.ns_uri):
 
169
        fetch_request = ax.FetchRequest()
 
170
        # We mark all the attributes as required, since Google ignores
 
171
        # optional attributes.  We request both the full name and
 
172
        # first/last components since some providers offer one but not
 
173
        # the other.
 
174
        for (attr, alias) in [
 
175
            ('http://axschema.org/contact/email', 'email'),
 
176
            ('http://axschema.org/namePerson', 'fullname'),
 
177
            ('http://axschema.org/namePerson/first', 'firstname'),
 
178
            ('http://axschema.org/namePerson/last', 'lastname'),
 
179
            ('http://axschema.org/namePerson/friendly', 'nickname')]:
 
180
            fetch_request.add(ax.AttrInfo(attr, alias=alias, required=True))
 
181
        openid_request.addExtension(fetch_request)
 
182
    else:
 
183
        openid_request.addExtension(
 
184
            sreg.SRegRequest(optional=['email', 'fullname', 'nickname']))
169
185
 
170
186
    # Request team info
171
187
    teams_mapping_auto = getattr(settings, 'OPENID_LAUNCHPAD_TEAMS_MAPPING_AUTO', False)