How to use the klein.interfaces.ISession function in klein

To help you get started, we’ve selected a few klein examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github twisted / klein / src / klein / storage / _memory.py View on Github external
from twisted.python.components import Componentized

from zope.interface import implementer
from zope.interface.interfaces import IInterface

from klein.interfaces import (
    ISession,
    ISessionStore,
    NoSuchSession,
    SessionMechanism,
)

_authCB = Callable[[IInterface, ISession, Componentized], Any]


@implementer(ISession)  # type: ignore[misc]
@attr.s
class MemorySession(object):
    """
    An in-memory session.
    """

    identifier = attr.ib(type=Text)
    isConfidential = attr.ib(type=bool)
    authenticatedBy = attr.ib(type=SessionMechanism)
    _authorizationCallback = attr.ib(type=_authCB)  # type: ignore[misc]
    _components = attr.ib(default=Factory(Componentized), type=Componentized)

    def authorize(self, interfaces):
        # type: (List[IInterface]) -> Deferred
        """
        Authorize each interface by calling back to the session store's
github twisted / klein / docs / introduction / codeexamples / forms.py View on Github external
@requirer.prerequisite([ISession])
def procurer(request):
    return SessionProcurer(sessions).procureSession(request)
github twisted / klein / src / klein / storage / _memory.py View on Github external
from attr import Factory

from twisted.internet.defer import Deferred, fail, succeed
from twisted.python.components import Componentized

from zope.interface import implementer
from zope.interface.interfaces import IInterface

from klein.interfaces import (
    ISession,
    ISessionStore,
    NoSuchSession,
    SessionMechanism,
)

_authCB = Callable[[IInterface, ISession, Componentized], Any]


@implementer(ISession)  # type: ignore[misc]
@attr.s
class MemorySession(object):
    """
    An in-memory session.
    """

    identifier = attr.ib(type=Text)
    isConfidential = attr.ib(type=bool)
    authenticatedBy = attr.ib(type=SessionMechanism)
    _authorizationCallback = attr.ib(type=_authCB)  # type: ignore[misc]
    _components = attr.ib(default=Factory(Componentized), type=Componentized)

    def authorize(self, interfaces):
github twisted / klein / loginspike.py View on Github external
    session=RequestComponent(ISession),
)
@inlineCallbacks
def sessions(binding, form, session):
    # type: (ISimpleAccountBinding, Form, ISession) -> Deferred
    if binding is None:
        returnValue({"sessions": []})
    else:
        dump = {
            "sessions": [
                oneSession.widget(
                    session_info, form, session.identifier == session_info.id
                )
                for session_info in (yield binding.boundSessionInformation())
            ]
        }
        returnValue(dump)
github twisted / klein / loginspike.py View on Github external
@requirer.prerequisite([ISession])
def getSessionProcurer(request):
    # type: (IRequest) -> ISessionProcurer
    return procurer.procureSession(request)