예제 #1
0
    def __call__(self, cookie=None, **settings):
        """
        Create a new session by calling a :cls:`sessionmaker` instance.
        """
        if not hasattr(self, "settings"):
            raise SessionConfigurationError("Session not configured yet.")
        self._check_call_settings_sanity(settings)

        all_settings = self.settings.copy()
        # Add custom added params
        if settings:
            all_settings.update(settings)

        # If requesting a different backend, give it
        if 'backend' in settings:
            session_class = get_session_class(settings['backend'])
        else:
            if not hasattr(self, 'session_class'):
                raise SessionConfigurationError("No backend was configured")
            session_class = self.session_class

        # Delete the sessionmaker only settings
        for sessionmaker_only in ["enable_encryption", "secret_file",
                                  "backend"]:
            if sessionmaker_only in all_settings:
                del all_settings[sessionmaker_only]

        # Create a new instance of a session
        return session_class(cookie, **all_settings)
예제 #2
0
    def configure(self, **settings):
        """
        Configure sessions with default arguments. The settings passed in here
        will be used for all newly created sessions, unless superseded by the
        settings explicity passed to :meth:`sessionmaker.__call__`.

        The following configuration settings are available exclusively to the
        sessionmaker:

        :param backend: The name of the backend to be used. This setting is
                        mandatory, but can also be passed individually on
                        session creation.

        :param secret_file: If this is specified, a file is used to store keys
                            for both encryption and signing of the cookie.
                            This option conflicts with the ``signature_key``
                            and ``encryption_key`` options, so you can only use
                            one of them. Defaults to a file called `secret` in
                            the current working directory.

        :param enable_encryption: Only useful in conjunction with
                                  ``secret_file``, as it specifies to not only
                                  load a signature key but also an encryption
                                  key. This requires `PyCrypto`_. Defaults to
                                  no encryption.

        .. _PyCrypto: https://www.dlitz.net/software/pycrypto/
        """
        if hasattr(self, 'settings'):
            raise SessionConfigurationError("Session already configured.")
        self._check_settings_sanity(settings)
        self._init_keys(settings)
        assert 'signature_key' in settings
        self.settings = settings
        try:
            backend = self.settings['backend']
            self.session_class = get_session_class(backend)
        except ValueError:
            raise SessionConfigurationError("Backend %s not found." % backend)
        except KeyError:
            raise SessionConfigurationError("No backend given, please "
                                            "specifiy a 'backend' setting.")

        # Check if we can encrypt:
        # This is just a failsafe check to throw up at configuration already
        if 'encryption_key' in self.settings and not encryption_available():
            raise CryptoError("Encryption not available, install pycrypto.")