Esempio n. 1
0
    def intercept_service(self, continuation, handler_call_details):
        # check session data and check user login state
        try:
            session_key = ''
            metadata = dict(handler_call_details.invocation_metadata)
            print(metadata)
            if 'session_key' in metadata:
                session_key = metadata['session_key']

            print(session_key)
            session = Session.objects.get(pk=session_key)

            data = session.get_decoded()
            if all([data['_auth'] == settings.AUTH_USER_META_VALUE,
                    data['_auth_logged_in'],
                    ]):
                # update last seen date in session
                session_store = SessionStore(session_key)
                session_store['_auth_last_request'] = now().timestamp()
                session_store.save()

                return continuation(handler_call_details)

        except Session.DoesNotExist:
            print("Session does not exist")

        # method_name = str.split(handler_call_details.method, '/')[-1:][0]
        method_name = continuation(handler_call_details).unary_unary.__name__

        if method_name in self._ignored_method:
            return continuation(handler_call_details)

        return self._terminator
Esempio n. 2
0
 def current_user(self, request):
     key = request.COOKIES['sessionid']
     s = SessionStore(key)
     session = s.load()
     if len(session): name = session['user']
     else: name = request.session['user']
     user = User.objects.all().filter(username=name)
     return user[0]
Esempio n. 3
0
 def current_user(self,request):
     key = request.COOKIES['sessionid']
     s = SessionStore(key)
     session = s.load()
     if len(session): name = session['user']
     else: name = request.session['user']
     user = User.objects.all().filter(username=name)
     return user[0]
Esempio n. 4
0
def create_pre_authenticated_session(email):
    user = User.objects.create(email=email)
    session = SessionStore()
    session[SESSION_KEY] = user.pk
    session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
    session.save()

    return session.session_key
Esempio n. 5
0
def test_request(view: callable,
                 method='GET',
                 user: User = SVAnonymousUser,
                 session_data: dict = None,
                 url_kwargs: dict = None,
                 req_kwargs: dict = None,
                 headers: dict = None,
                 just_request: bool = False) -> HttpResponse:
    """Wrapper to make test requests easier

    Args:
        method (str): Request method. Defaults to GET.
        user (User): Requesting user. Defaults to SVAnonymousUser.
        session_data (dict): Optional dictionary of session data.
        url_kwargs (dict): Optional dictionary of URL arguments.
        req_kwargs (dict): Optional dictionary of URL Querystrinng arguments.
        headers (dict): Optional dictionary of headers.
        just_request (bool): Only return the Request. Defaults to False
    """

    if url_kwargs is None:
        url_kwargs = {}
    if req_kwargs is None:
        req_kwargs = {}
    if headers is None:
        headers = {}

    factory = RequestFactory()

    factory_handler = getattr(factory, method.lower(), None)

    if not factory_handler:
        return HttpResponseServerError()

    # pylint: disable=not-callable
    request = factory_handler(view, req_kwargs, **headers)

    session = SessionStore()
    if session_data:
        for key, value in session_data.items():
            session[key] = value
    # Fix django.contrib.messages.api.MessageFailure
    # because this request doesn't have a session or anything
    setattr(request, 'session', session)
    setattr(request, '_messages', FallbackStorage(request))

    if user is SVAnonymousUser:
        user = SVAnonymousUser()
    request.user = user

    if just_request:
        return request

    try:
        return view(request, **url_kwargs)
    except Http404:
        return HttpResponseNotFound('not found')
    else:
        return HttpResponseServerError()
Esempio n. 6
0
 def save(self) -> 'Session':
     store = SessionStore(session_key=self.user.session_key)
     store['session'] = self
     store.set_expiry(SESSION_TTL)
     store.save()
     self.user.session_key = store.session_key
     self.user.save()
     self.__update_restaurant_pool()
     return self
Esempio n. 7
0
 def set_current_user(self,request,name):
     key = request.COOKIES['sessionid']
     s = SessionStore(key)
     s['user'] = name
     s.save()
Esempio n. 8
0
 def set_current_user(self, request, name):
     key = request.COOKIES['sessionid']
     s = SessionStore(key)
     s['user'] = name
     s.save()
Esempio n. 9
0
 def get_session_by_id(self, session_id=None):
     session = SessionStore(session_id)
     return session
Esempio n. 10
0
 def find_by_user(user_id: int) -> Optional['Session']:
     user = User.objects.get(id=user_id)
     store = SessionStore(session_key=user.session_key)
     return store['session'] if 'session' in store else None