我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.contrib.auth.BACKEND_SESSION_KEY。
def shortcut_login(self, **credentials): user = authenticate(**credentials) if not user: raise ValueError("User {0} was not authenticated".format(user)) session_auth_hash = '' if hasattr(user, 'get_session_auth_hash'): session_auth_hash = user.get_session_auth_hash() # Mimicking django.contrib.auth functionality self.set_session_data({AUTH_ID_SESSION_KEY: user.pk, AUTH_HASH_SESSION_KEY: session_auth_hash, AUTH_BACKEND_SESSION_KEY: user.backend})
def shortcut_logout(self): self.set_session_data({AUTH_BACKEND_SESSION_KEY: ''})
def clean_username(self, username, request): """ Allows the backend to clean the username, if the backend defines a clean_username method. """ backend_str = request.session[auth.BACKEND_SESSION_KEY] backend = auth.load_backend(backend_str) try: username = backend.clean_username(username) except AttributeError: # Backend has no clean_username method. pass return username
def _remove_invalid_user(self, request): """ Removes the current authenticated user in the request which is invalid but only if the user is authenticated via the RemoteUserBackend. """ try: stored_backend = load_backend(request.session.get(auth.BACKEND_SESSION_KEY, '')) except ImportError: # backend failed to load auth.logout(request) else: if isinstance(stored_backend, RemoteUserBackend): auth.logout(request)
def setUpClass(cls): super(IntegrationTests, cls).setUpClass() # we do not display cls.display = Display(visible=0, size=(1024, 768)) cls.display.start() cls.selenium = WebDriver() # we create a user password = "password" cls.user = User(username="UserForLiveTests") cls.user.set_password(password) cls.user.save() # we log him in # source http://stackoverflow.com/questions/22494583/login-with-code-when-using-liveservertestcase-with-django # we need a session session = SessionStore() session[SESSION_KEY] = cls.user.id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = cls.user.get_session_auth_hash() session.save() # the cookie dict cls.cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'secure': False, 'path': '/', } # we launch centrifugo cls.centrifugo = subprocess.Popen(["centrifugo --config=tests/config.json --port={0}".format(getattr(settings, "CENTRIFUGO_PORT", 8802))], stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid) # we create participants cls.participant1 = Participant.objects.create(id=cls.user.id) cls.participant2 = Participant.objects.create(id=2) cls.participant3 = Participant.objects.create(id=3) cls.participant4 = Participant.objects.create(id=4) # we create a fake request cls.request = RequestFactory() cls.request.rest_messaging_participant = cls.participant1 # and wait for it to run time.sleep(4)
def process_request(self, request): # AuthenticationMiddleware is required so that request.user exists. if not hasattr(request, 'user'): raise ImproperlyConfigured( "The Django remote user auth middleware requires the" " authentication middleware to be installed. Edit your" " MIDDLEWARE_CLASSES setting to insert" " 'django.contrib.auth.middleware.AuthenticationMiddleware'" " before the RemoteUserMiddleware class.") try: username = request.META[self.header] except KeyError: # If specified header doesn't exist then remove any existing # authenticated remote-user, or return (leaving request.user set to # AnonymousUser by the AuthenticationMiddleware). if request.user.is_authenticated(): try: stored_backend = load_backend(request.session.get( auth.BACKEND_SESSION_KEY, '')) if isinstance(stored_backend, RemoteUserBackend): auth.logout(request) except ImproperlyConfigured as e: # backend failed to load auth.logout(request) return # If the user is already authenticated and that user is the user we are # getting passed in the headers, then the correct user is already # persisted in the session and we don't need to continue. if request.user.is_authenticated(): if request.user.get_username() == self.clean_username(username, request): return # We are seeing this user for the first time in this session, attempt # to authenticate the user. user = auth.authenticate(remote_user=username) if user: # User is valid. Set request.user and persist user in the session # by logging the user in. request.user = user auth.login(request, user)
def dispatch(self, request, *args, **kwargs): if request.user.is_authenticated and request.session[BACKEND_SESSION_KEY] != 'front.backend.MailLinkBackend': return super().dispatch(request, *args, **kwargs) return redirect_to_login(request.get_full_path(), self.unlogged_redirect_url)
def get(self, request, *args, **kwargs): self.former_backend = request.session.get(BACKEND_SESSION_KEY, None) logout(request) return super().get(request, *args, **kwargs)
def has_permission(self, request): return ( super().has_permission(request) and request.session[BACKEND_SESSION_KEY] == 'people.backend.PersonBackend' )