mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 1 | from django.contrib.auth.models import User, Group, check_password |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 2 | from django.contrib.auth import backends |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 3 | from django.contrib import auth |
| 4 | from django import http |
| 5 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 6 | from autotest_lib.frontend import thread_local |
| 7 | from autotest_lib.frontend.afe import models, management |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 8 | |
| 9 | DEBUG_USER = 'debug_user' |
| 10 | |
showard | a5288b4 | 2009-07-28 20:06:08 +0000 | [diff] [blame] | 11 | class SimpleAuthBackend(backends.ModelBackend): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 12 | """ |
| 13 | Automatically allows any login. This backend is for use when Apache is |
| 14 | doing the real authentication. Also ensures logged-in user exists in |
| 15 | frontend.afe.models.User database. |
| 16 | """ |
| 17 | def authenticate(self, username=None, password=None): |
| 18 | try: |
| 19 | user = User.objects.get(username=username) |
| 20 | except User.DoesNotExist: |
| 21 | # password is meaningless |
| 22 | user = User(username=username, |
| 23 | password='apache authentication') |
| 24 | user.is_staff = True |
| 25 | user.save() # need to save before adding groups |
| 26 | user.groups.add(Group.objects.get( |
| 27 | name=management.BASIC_ADMIN)) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 28 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 29 | SimpleAuthBackend.check_afe_user(username) |
| 30 | return user |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 31 | |
| 32 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 33 | @staticmethod |
| 34 | def check_afe_user(username): |
showard | 3dd47c2 | 2008-07-10 00:41:36 +0000 | [diff] [blame] | 35 | user, created = models.User.objects.get_or_create(login=username) |
| 36 | if created: |
| 37 | user.save() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 38 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 39 | def get_user(self, user_id): |
| 40 | try: |
| 41 | return User.objects.get(pk=user_id) |
| 42 | except User.DoesNotExist: |
| 43 | return None |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 44 | |
| 45 | |
showard | 6f1593c | 2008-07-11 16:56:16 +0000 | [diff] [blame] | 46 | class GetApacheUserMiddleware(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 47 | """ |
| 48 | Middleware for use when Apache is doing authentication. Looks for |
showard | 6f1593c | 2008-07-11 16:56:16 +0000 | [diff] [blame] | 49 | REMOTE_USER in headers and passed the username found to |
| 50 | thread_local.set_user(). If no such header is found, looks for |
| 51 | HTTP_AUTHORIZATION header with username (this allows CLI to authenticate). |
| 52 | If neither of those are found, DEBUG_USER is used. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 53 | """ |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 54 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 55 | def process_request(self, request): |
| 56 | # look for a username from Apache |
| 57 | user = request.META.get('REMOTE_USER') |
| 58 | if user is None: |
| 59 | # look for a user in headers. This is insecure but |
| 60 | # it's our temporarily solution for CLI auth. |
| 61 | user = request.META.get('HTTP_AUTHORIZATION') |
| 62 | if user is None: |
| 63 | # no user info - assume we're in development mode |
| 64 | user = DEBUG_USER |
showard | 6f1593c | 2008-07-11 16:56:16 +0000 | [diff] [blame] | 65 | thread_local.set_user(user) |
| 66 | |
| 67 | |
| 68 | class ApacheAuthMiddleware(GetApacheUserMiddleware): |
| 69 | """ |
| 70 | Like GetApacheUserMiddleware, but also logs the user into Django's auth |
| 71 | system, and replaces the username in thread_local with the actual User model |
| 72 | object. |
| 73 | """ |
| 74 | |
showard | a79583c | 2008-07-17 17:01:15 +0000 | [diff] [blame] | 75 | |
showard | 6f1593c | 2008-07-11 16:56:16 +0000 | [diff] [blame] | 76 | def process_request(self, request): |
| 77 | super(ApacheAuthMiddleware, self).process_request(request) |
| 78 | username = thread_local.get_user() |
showard | a79583c | 2008-07-17 17:01:15 +0000 | [diff] [blame] | 79 | thread_local.set_user(None) |
showard | 6f1593c | 2008-07-11 16:56:16 +0000 | [diff] [blame] | 80 | user_object = auth.authenticate(username=username, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 81 | password='') |
| 82 | auth.login(request, user_object) |
showard | 6f1593c | 2008-07-11 16:56:16 +0000 | [diff] [blame] | 83 | thread_local.set_user(models.User.objects.get(login=username)) |