Commits

Gregory Petukhov  committed 2723765

Refactor association table

  • Participants
  • Parent commits 5511dea

Comments (0)

Files changed (9)

File demo/templates/base.html

     <body>
         <div>
             {% if user.is_authenticated %}
-                <a href="{% url xauth_logout %}">Log Out</a> ({{ user }})
+            <a href="{% url xauth_logout %}">Log Out</a> ({{ user }}) /
+            <a href="{% url xauth_identity_list %}">Identity List</a>
             {% else %}
-                <a href="{% url xauth_login %}">Sign In</a>
+            <a href="{% url xauth_login %}">Sign In</a>
             {% endif %}
         </div>
         {% for message in messages %}

File xauth/models.py

-"""
-This module imports all models definition from
-all avaible services. Tables will be created for each
-service even if you do not use some services. This
-is the most simple solution.
-"""
+from django.db import models
 
-from xauth.service.openid.models import *
+class XauthAssociation(models.Model):
+    created = models.DateTimeField(auto_now_add=True)
+    service = models.CharField(max_length=50)
+    identity = models.CharField(max_length=255, null=True, blank=True)
+    user = models.ForeignKey('auth.User')
+
+    class Meta:
+        unique_together = ('identity', 'user')

File xauth/service/openid/backend.py

-from xauth.service.openid.models import OpenidAssociation
+from xauth.models import XauthAssociation
 
 def authenticate(xauth_response):
-    op_endpoint = xauth_response['response'].message.getAliasedArg('op_endpoint')
-    identity = xauth_response['response'].message.getAliasedArg('identity')
+    identity = xauth_response['response'].identity_url
 
     try:
-        assoc = OpenidAssociation.objects.get(op_endpoint=op_endpoint, identity=identity)
-    except OpenidAssociation.DoesNotExist:
+        assoc = XauthAssociation.objects.get(identity=identity)
+    except XauthAssociation.DoesNotExist:
         return None
     else:
         return assoc.user

File xauth/service/openid/commands/create_store.py

-from django.db import connection
-from django.conf import settings
-from django.core.exceptions import ImproperlyConfigured
-
-from openid.store.filestore import FileOpenIDStore
-from openid.store import sqlstore
-from openid.yadis.constants import YADIS_CONTENT_TYPE
-
-from xauth.service.openid.util import get_store
-
-def handle(args, options):
-    """
-    * If the chosen engine is not supported by the OpenID library,
-      raise ImproperlyConfigured.
-
-    * If a database store is used, this will create the tables
-      necessary to use it.  The table names will be prefixed with
-      table_prefix.  DO NOT use the same table prefix for both an
-      OpenID consumer and an OpenID server in the same database.
-    """
-
-    store = get_store()
-    store.createTables()

File xauth/service/openid/models.py

-from django.db import models
-
-class OpenidAssociation(models.Model):
-    op_endpoint = models.CharField(max_length=255)
-    identity = models.CharField(max_length=100, null=True, blank=True)
-    user = models.ForeignKey('auth.User')
-
-    class Meta:
-        app_label = 'xauth'
-        unique_together = ('op_endpoint', 'identity', 'user')

File xauth/service/openid/util.py

 import logging
-from openid.store import sqlstore
 
-from django.db import connection
-from django.core.exceptions import ImproperlyConfigured
-from django.conf import settings
-
-from xauth.service.openid.models import OpenidAssociation
+from xauth.models import XauthAssociation
 
 logger = logging.getLogger('xauth.service.openid.util')
 
-#def get_store():
-    #"""
-    #Returns an OpenID association store object based on the database
-    #engine chosen for this Django application.
-
-    #* If a database engine is chosen, a store object for that database
-      #type will be returned.
-
-    #* If the chosen engine is not supported by the OpenID library,
-      #raise ImproperlyConfigured.
-
-    #The result of this function should be passed to the Consumer
-    #constructor as the store parameter.
-    #"""
-    #logger.debug('Setting up the store')
-
-    ## Possible side-effect: create a database connection if one isn't
-    ## already open.
-    #connection.cursor()
-
-    ## Create table names to specify for SQL-backed stores.
-    #tablenames = {
-        #'associations_table': settings.XAUTH_STORE_TABLE_PREFIX + 'openid_associations',
-        #'nonces_table': settings.XAUTH_STORE_TABLE_PREFIX + 'openid_nonces',
-        #}
-
-    #types = {
-        #'postgresql': sqlstore.PostgreSQLStore,
-        #'mysql': sqlstore.MySQLStore,
-        #'sqlite3': sqlstore.SQLiteStore,
-        #}
-
-    #try:
-        #store = types[settings.XAUTH_STORE_ENGINE](connection.connection, **tablenames)
-    #except KeyError:
-        #raise ImproperlyConfigured, \
-              #"Database engine %s not supported by OpenID library" % \
-              #(settings.DATABASE_ENGINE,)
-
-    #return store
-
-
 def save_association(user, xauth_response):
-    assoc = OpenidAssociation()
+    assoc = XauthAssociation()
     assoc.user = user
-    assoc.op_endpoint = xauth_response['response'].message.getAliasedArg('op_endpoint')
-    assoc.identity = xauth_response['response'].message.getAliasedArg('identity')
+    assoc.identity = xauth_response['response'].identity_url
+    assoc.service = xauth_response['service']
     assoc.save()

File xauth/templates/xauth/identity_list.html

+{% extends 'base.html' %}
+
+{% block content %}
+<h1>List of all identities associated with you account</h1>
+<table>
+    <thead>
+        <tr>
+            <th>Date</th>
+            <th>Identity</th>
+            <th>Service</th>
+        </tr>
+    </thead>
+    {% for assoc in idents %}
+    <tr>
+        <td>{{ assoc.created|date }}</td>
+        <td>{{ assoc.identity|urlize }}</td>
+        <td>{{ assoc.service }}</td>
+    </tr>
+    {% endfor %}
+</table
+{% if not idents %}
+<p>No identitites.</p>
+{% endif %}
+
+{% endblock %}

File xauth/urls.py

     url(r'^exec/(\w+)/(\w+)$', 'xauth_exec', name='xauth_exec'),
     url(r'^complete$', 'xauth_complete', name='xauth_complete'),
     url(r'^logout$', 'xauth_logout', name='xauth_logout'),
+    url(r'^identity$', 'xauth_identity_list', name='xauth_identity_list')
 )

File xauth/views.py

 from django.contrib import messages
 from django.contrib import auth
 from django.utils.http import urlquote
+from django.contrib.auth.decorators import login_required
 
 from xauth.forms import ProfileForm
+from xauth.models import XauthAssociation
 
 logger = logging.getLogger('xauth.views')
 
     auth.logout(request)
     messages.success(request, u'You have successuly logged out')
     return redirect(settings.XAUTH_LOGOUT_URL)
+
+
+@login_required
+def xauth_identity_list(request):
+    idents = XauthAssociation.objects.filter(user=request.user)
+    return render(request, 'xauth/identity_list.html', {'idents': idents})