auth.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # coding: utf-8
  2. # nm.debian.org website authentication
  3. #
  4. # Copyright (C) 2012--2014 Enrico Zini <enrico@debian.org>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. from __future__ import print_function
  19. from __future__ import absolute_import
  20. from __future__ import division
  21. from __future__ import unicode_literals
  22. from django import http
  23. from django.shortcuts import redirect
  24. import backend.models as bmodels
  25. from django.contrib.auth.backends import RemoteUserBackend
  26. class NMUserBackend(RemoteUserBackend):
  27. """
  28. RemoteUserBackend customised to create User objects from Person
  29. """
  30. # Copied from RemoteUserBackend and tweaked to validate against Person
  31. def authenticate(self, remote_user):
  32. """
  33. The username passed as ``remote_user`` is considered trusted. This
  34. method simply returns the ``User`` object with the given username,
  35. creating a new ``User`` object if ``create_unknown_user`` is ``True``.
  36. Returns None if ``create_unknown_user`` is ``False`` and a ``User``
  37. object with the given username is not found in the database.
  38. """
  39. if not remote_user:
  40. return
  41. username = self.clean_username(remote_user)
  42. # Get the Person for this username: Person is authoritative over User
  43. # Allow user@alioth without -guest, for cases like retired DDs who are
  44. # DMs (Edward Betts <edward> is an example)
  45. if username.endswith("@debian.org") or username.endswith("@users.alioth.debian.org"):
  46. try:
  47. return bmodels.Person.objects.get(username=username)
  48. except bmodels.Person.DoesNotExist:
  49. return None
  50. else:
  51. return None