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