1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-13 12:45:46 +00:00
seahub/thirdpart/shibboleth/backends.py

52 lines
1.9 KiB
Python
Raw Normal View History

2014-12-17 06:29:29 +00:00
from django.db import connection
from django.contrib.auth.backends import RemoteUserBackend
2014-12-17 06:40:16 +00:00
from seahub.base.accounts import User
2014-12-17 06:29:29 +00:00
class ShibbolethRemoteUserBackend(RemoteUserBackend):
"""
This backend is to be used in conjunction with the ``RemoteUserMiddleware``
found in the middleware module of this package, and is used when the server
is handling authentication outside of Django.
By default, the ``authenticate`` method creates ``User`` objects for
usernames that don't already exist in the database. Subclasses can disable
this behavior by setting the ``create_unknown_user`` attribute to
``False``.
"""
# Create a User object if not already in the database?
create_unknown_user = True
2014-12-17 06:40:16 +00:00
def get_user(self, username):
try:
user = User.objects.get(email=username)
except User.DoesNotExist:
user = None
return user
2014-12-17 06:29:29 +00:00
def authenticate(self, remote_user, shib_meta):
"""
The username passed as ``remote_user`` is considered trusted. This
method simply returns the ``User`` object with the given username,
creating a new ``User`` object if ``create_unknown_user`` is ``True``.
Returns None if ``create_unknown_user`` is ``False`` and a ``User``
object with the given username is not found in the database.
"""
if not remote_user:
return
user = None
username = self.clean_username(remote_user)
# Note that this could be accomplished in one try-except clause, but
# instead we use get_or_create when creating unknown users since it has
# built-in safeguards for multiple threads.
if self.create_unknown_user:
2014-12-17 06:40:16 +00:00
user = User.objects.create_user(email=username)
2014-12-17 06:29:29 +00:00
else:
try:
2014-12-17 06:40:16 +00:00
user = User.objects.get(email=username)
2014-12-17 06:29:29 +00:00
except User.DoesNotExist:
pass
return user