From 3aebdc0cd06c58c471f8a44ad9caa82a8d787436 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 8 Dec 2010 16:16:35 -0500 Subject: [PATCH] Added a PAM authentication backend --- dashboard/auth.py | 21 +++++++++++++++++++++ settings.py | 5 +++++ 2 files changed, 26 insertions(+) create mode 100644 dashboard/auth.py diff --git a/dashboard/auth.py b/dashboard/auth.py new file mode 100644 index 0000000..c45e809 --- /dev/null +++ b/dashboard/auth.py @@ -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 diff --git a/settings.py b/settings.py index 391d730..95ef36a 100755 --- a/settings.py +++ b/settings.py @@ -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.