Added a PAM authentication backend

This commit is contained in:
Correl Roush 2010-12-08 16:16:35 -05:00
parent be02342af5
commit 3aebdc0cd0
2 changed files with 26 additions and 0 deletions

21
dashboard/auth.py Normal file
View file

@ -0,0 +1,21 @@
import pam
from django.contrib.auth.models import User, Group
class PAMBackend:
def authenticate(self, username=None, password=None):
if pam.authenticate(username, password, service='login'):
try:
return User.objects.get(username=username)
except User.DoesNotExist:
# Create new django user
user = User(username=username)
user.set_password(password)
user.save()
user.groups.add(Group.objects.get(name='Users'))
return user
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

View file

@ -78,6 +78,11 @@ MIDDLEWARE_CLASSES = (
ROOT_URLCONF = 'codereview.urls'
LOGIN_URL = '/dashboard/login/'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'codereview.dashboard.auth.PAMBackend',
)
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.