__init__.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # coding: utf-8
  2. # Copyright (C) 2014 Enrico Zini <enrico@debian.org>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from __future__ import print_function
  17. from __future__ import absolute_import
  18. from __future__ import division
  19. from __future__ import unicode_literals
  20. def authenticate(request):
  21. """
  22. Get the user from the Api-Key: header set in the request
  23. Returns the User if one was found, else None
  24. """
  25. from . import models as amodels
  26. value = request.META.get("HTTP_API_KEY", None)
  27. if value is None: return None
  28. try:
  29. key = amodels.Key.objects.get(value=value)
  30. except amodels.Key.DoesNotExists:
  31. return None
  32. amodels.AuditLog.objects.create(
  33. key=key,
  34. key_enabled=key.enabled,
  35. remote_addr=request.META.get("REMOTE_ADDR", "(unknown)"),
  36. request_method=request.method,
  37. absolute_uri=request.build_absolute_uri(),
  38. )
  39. if not key.enabled:
  40. return None
  41. return key.user