Commits

Jannis Leidel committed f78276b

Added AX support with pluggable callback backends.

Comments (0)

Files changed (4)

 * Fixed landing page view to handle redirect URL GET params correctly.
 * Added setting OPENID_SREG_DATA_CALLBACK setting for custom SREG callback
   functions.
+* Added AX support, new OPENID_AX_DATA_CALLBACK callback.
 
 v0.4
 ----

openid_provider/conf.py

 
 SREG_DATA_CALLBACK = getattr(settings, 'OPENID_SREG_DATA_CALLBACK',
                              'openid_provder.utils.get_default_sreg_data')
+
+AX_DATA_CALLBACK = getattr(setting, 'OPENID_AX_DATA_CALLBACK',
+                           'openid_provder.utils.get_default_ax_data')

openid_provider/utils.py

 # some code from http://www.djangosnippets.org/snippets/310/ by simon
 # and from examples/djopenid from python-openid-2.2.4
 from openid_provider import conf
-from openid.extensions import sreg
+from openid.extensions import ax, sreg
 
 from django.core.exceptions import ImproperlyConfigured
 from django.utils.importlib import import_module
         'fullname': request.user.get_full_name(),
     }
 
+def get_default_ax_data(request, orequest):
+    return {
+        'http://axschema.org/contact/email': request.user.email,
+        'http://axschema.org/namePerson': request.user.get_full_name(),
+        'http://axschema.org/namePerson/friendly': request.user.username,
+        'http://axschema.org/namePerson/first': request.user.first_name,
+        'http://axschema.org/namePerson/last': request.user.last_name,
+    }
+
 def add_sreg_data(request, orequest, oresponse):
     callback = get_sreg_callback()
     if callback is None or not callable(callback):
     sreg_resp = sreg.SRegResponse.extractResponse(sreg_req, sreg_data)
     oresponse.addExtension(sreg_resp)
 
+def add_ax_data(request, orequest, oresponse):
+    callback = get_ax_callback()
+    if callback is None or not callable(callback):
+        return
+    ax_data = callback(request, orequest)
+    ax_req = ax.FetchRequest.fromOpenIDRequest(orequest)
+    ax_resp = ax.FetchResponse(ax_req)
+    for attr in ax_req.getRequiredAttrs():
+        ax_resp.addValue(attr, ax_data.get(attr))
+    oresponse.addExtension(sreg_resp)
+
 def get_sreg_callback():
     try:
         return import_module_attr(conf.SREG_DATA_CALLBACK)
     except (ImportError, AttributeError):
         return None
 
+def get_ax_callback():
+    try:
+        return import_module_attr(conf.AX_DATA_CALLBACK)
+    except (ImportError, AttributeError):
+        return None
+
 def get_store(request):
     try:
         store_class = import_module_attr(conf.STORE)

openid_provider/views.py

 from openid.yadis.discover import DiscoveryFailure
 from openid.yadis.constants import YADIS_CONTENT_TYPE
 
-from openid_provider.utils import add_sreg_data, get_store
+from openid_provider.utils import add_sreg_data, add_ax_data, get_store
 
 @csrf_exempt
 def openid_server(request):
         oresponse = server.handleRequest(orequest)
     if request.user.is_authenticated():
         add_sreg_data(request, orequest, oresponse)
+        add_ax_data(request, orequest, oresponse)
     # Convert a webresponse from the OpenID library in to a Django HttpResponse
     webresponse = server.encodeResponse(oresponse)
     response = HttpResponse(webresponse.body)