from django.contrib.auth.models import User, get_hexdigest
from magicword.models import MagicWord

class MagicBackend:
    
    def authenticate(self, username=None, password=None):
        username = "guest"
        try:
            magic_word = MagicWord.objects.get(password=password)
            if magic_word.enabled:
                try:
                    user = User.objects.get(username=username)
                except User.DoesNotExist:
                    user = User(username=username, password=get_hexdigest("sha1", "54L7", username))
                    user.first_name = "Guest"
                    user.last_name = "User"
                    user.is_active = True
                    user.is_staff = False
                    user.is_superuser = False
                    user.save()
                return user
        except MagicWord.DoesNotExist:
            pass
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
@@ -1,12 +1,45 @@
+from django.conf import settings%0A
 from django.
@@ -85,16 +85,17 @@
 xdigest%0A
+%0A
 from mag
@@ -126,16 +126,233 @@
 icWord%0A%0A
+MAGICAUTH_USERNAME = getattr(settings, 'MAGICAUTH_USERNAME', 'guest')%0AMAGICAUTH_FIRST_NAME = getattr(settings, 'MAGICAUTH_FIRST_NAME', 'Guest')%0AMAGICAUTH_LAST_NAME = getattr(settings, 'MAGICAUTH_LAST_NAME', 'User')%0A%0A%0A
 class Ma
@@ -358,21 +358,29 @@
 agic
+Word
 Backend
-:%0A    
+(object):%0A
 %0A   
@@ -434,16 +434,17 @@
 =None):%0A
+%0A
         
@@ -458,53 +458,38 @@
 e = 
-%22guest%22%0A        try:%0A            magic_word =
+MAGICAUTH_USERNAME%0A%0A        if
  Mag
@@ -503,19 +503,22 @@
 objects.
-get
+exists
 (passwor
@@ -531,44 +531,28 @@
 word
-)%0A            if magic_word.enabled:
+, is_enabled=True):%0A
 %0A   
@@ -556,37 +556,29 @@
             
-    
 try:%0A
-    
             
@@ -632,28 +632,24 @@
             
-   
- 
 except User.
@@ -654,36 +654,33 @@
 r.DoesNotExist:%0A
-    
+%0A
                 
@@ -682,39 +682,8 @@
     
- user = User(username=username,
  pas
@@ -687,17 +687,19 @@
 password
-=
+ = 
 get_hexd
@@ -729,17 +729,17 @@
 sername)
-)
+%0A
 %0A       
@@ -733,32 +733,88 @@
 ame)%0A%0A          
+      user = User(username=username, password=password)%0A
           user.f
@@ -803,24 +803,30 @@
 )%0A          
+      
 user.first_n
@@ -835,20 +835,29 @@
 e = 
-%22Guest%22%0A    
+MAGICAUTH_FIRST_NAME%0A
     
@@ -889,16 +889,28 @@
 e = 
-%22User%22%0A 
+MAGICAUTH_LAST_NAME%0A
     
@@ -913,35 +913,32 @@
                 
-   
 user.is_active =
@@ -943,20 +943,16 @@
  = True%0A
-    
         
@@ -989,36 +989,32 @@
                 
-   
- 
 user.is_superuse
@@ -1035,28 +1035,24 @@
             
-    
 user.save()%0A
@@ -1051,19 +1051,17 @@
 .save()%0A
-   
+%0A
         
@@ -1060,25 +1060,24 @@
             
- 
 return user%0A
@@ -1079,84 +1079,8 @@
 user
-%0A        except MagicWord.DoesNotExist:%0A            pass%0A        return None
 %0A%0A  
@@ -1224,12 +1224,13 @@
  return None
+%0A

clean up of backend, use optional settings parameters for configuration