Commits

Mauro De Giorgi  committed cf93308

initial commit

  • Participants
  • Parent commits 72a636f

Comments (0)

Files changed (2)

File piston/authentication.py

 from django.core.exceptions import ImproperlyConfigured
 from django.shortcuts import render_to_response
 from django.template import RequestContext
+from models import ApiKey
 
 from piston import forms
 
         oauth_server, oauth_request = initialize_server_request(request)
         return oauth_server.verify_request(oauth_request)
 
+class SimpleKeyAuthentication(object):
+    """
+    A very simple authentication system based on a auto generate key associated to a user.
+    Every user can have only one key.   
+    """
+
+    def is_authenticated(self, request):
+        key = request.GET.get('key', None)
+        if not key:
+            return False
+        try:
+            api_key = ApiKey.objects.get(key=key) 
+            if api_key.status: return True
+            return True
+        except:
+            return False
+        
+    def challenge(self):
+        resp = HttpResponse("Authorization Required")
+        resp.status_code = 401
+        return resp

File piston/models.py

 from django.contrib import admin
 from django.core.mail import send_mail, mail_admins
 
+
 # Piston imports
 from managers import TokenManager, ConsumerManager, ResourceManager
 from signals import consumer_post_save, consumer_post_delete
         
 admin.site.register(Token)
 
+
+class ApiKey(models.Model):
+    """Api key associated to a user"""
+    key = models.CharField(max_length=40, default=generate_random(40))
+    user = models.ForeignKey(User, unique=True)
+    status = models.BooleanField(default=False)
+
+    class Meta:
+        ordering = ["user"]
+
+    def __unicode__(self):
+        return self.user.username+" - "+self.key
+ 
+admin.site.register(ApiKey)
+
 # Attach our signals
 post_save.connect(consumer_post_save, sender=Consumer)
 post_delete.connect(consumer_post_delete, sender=Consumer)