예제 #1
0
 def _obscure_json_response(*args, **kwargs):
     req = args[0]
     https_req, error = get_optional_parameter(req,
                                               'https',
                                               default=0,
                                               formatter=int)
     if error:
         https_req = 1
     retval = func(*args, **kwargs)
     if not isinstance(retval, HttpResponse):
         charset = req.META.get('HTTP_ACCEPT_CHARSET', 'utf-8')
         try:
             charsets = parse_charsets(charset)
             content = simplejson.dumps(retval, ensure_ascii=False)
             content, charset = try_encode(content, charsets, 'utf-8')
             if https_req < 1:
                 #only obscure response for request without https
                 content = obscure(content)
             response = HttpResponse(
                 content,
                 content_type='application/json; charset=%s' % charset)
         except ValueError, err:
             _LOGGER.error(err)
             return internal_server_error(req, err, exc_info=sys.exc_info())
         except Exception, err:
             _LOGGER.error('failed to convert encoding: %s' % err,
                           exc_info=1)
             return internal_server_error(req,
                                          'Unexpected error happened.',
                                          exc_info=sys.exc_info())
예제 #2
0
        def authenticate_func(*args, **kwargs):
            if not self._enabled:
                return func(*args, **kwargs)

            req = args[0]
            user_key = req.COOKIES.get("user_key", None)
            auth_id = None
            key_name = None
            if self._key_pos is not None:
                try:
                    auth_id = args[self._key_pos]
                except IndexError as err:
                    return internal_server_error(req, err, sys.exc_info())
            else:
                for i in range(len(self._key_names)):
                    # default formatter is str
                    key_name = self._key_names[i]
                    fmt = self._key_formatters[i] if i < len(self._key_formatters) else str
                    auth_id, error = get_optional_parameter(req, key_name, formatter=fmt, default=None, method=self._method)
                    if error:
                        return error
                    elif auth_id is not None:
                        break

            success = self._authenticate(key_name, auth_id, user_key)
            if success:
                retval = func(*args, **kwargs)
                if isinstance(retval, HttpResponse) and user_key is not None:
                    retval.set_cookie("user_key", user_key, path=self._cookie_path, domain=self._cookie_domain, httponly=True)
                    return retval
                else:
                    return retval
            else:
                return authentication_fail(req, self._fail_info)
예제 #3
0
 def _unobscure_post_request(*args, **kwargs):
     req = args[0]
     https_req, error = get_optional_parameter(req, 'https', default=0, formatter=int)
     if error:
         https_req = 1
     if https_req < 1:
         #only need to unobscure parameters for request without https
         post_data = unobscure(req.raw_post_data)
         req.POST = QueryDict(post_data)
     return func(*args, **kwargs)
예제 #4
0
 def _unobscure_post_request(*args, **kwargs):
     req = args[0]
     https_req, error = get_optional_parameter(req,
                                               'https',
                                               default=0,
                                               formatter=int)
     if error:
         https_req = 1
     if https_req < 1:
         #only need to unobscure parameters for request without https
         post_data = unobscure(req.raw_post_data)
         req.POST = QueryDict(post_data)
     return func(*args, **kwargs)
예제 #5
0
        def authenticate_func(*args, **kwargs):
            if not self._enabled:
                return func(*args, **kwargs)

            req = args[0]
            user_key = req.COOKIES.get("user_key", None)
            auth_id = None
            key_name = None
            if self._key_pos is not None:
                try:
                    auth_id = args[self._key_pos]
                except IndexError as err:
                    return internal_server_error(req, err, sys.exc_info())
            else:
                for i in range(len(self._key_names)):
                    # default formatter is str
                    key_name = self._key_names[i]
                    fmt = self._key_formatters[i] if i < len(
                        self._key_formatters) else str
                    auth_id, error = get_optional_parameter(
                        req,
                        key_name,
                        formatter=fmt,
                        default=None,
                        method=self._method)
                    if error:
                        return error
                    elif auth_id is not None:
                        break

            success = self._authenticate(key_name, auth_id, user_key)
            if success:
                retval = func(*args, **kwargs)
                if isinstance(retval, HttpResponse) and user_key is not None:
                    retval.set_cookie("user_key",
                                      user_key,
                                      path=self._cookie_path,
                                      domain=self._cookie_domain,
                                      httponly=True)
                    return retval
                else:
                    return retval
            else:
                return authentication_fail(req, self._fail_info)
예제 #6
0
 def _obscure_json_response(*args, **kwargs):
     req = args[0]
     https_req, error = get_optional_parameter(req, 'https', default=0, formatter=int)
     if error:
         https_req = 1
     retval = func(*args, **kwargs)
     if not isinstance(retval, HttpResponse):
         charset = req.META.get('HTTP_ACCEPT_CHARSET', 'utf-8')
         try:
             charsets = parse_charsets(charset)
             content = simplejson.dumps(retval, ensure_ascii=False)
             content, charset = try_encode(content, charsets, 'utf-8')
             if https_req < 1:
                 #only obscure response for request without https
                 content = obscure(content)
             response = HttpResponse(content, content_type='application/json; charset=%s' % charset)
         except ValueError, err:
             _LOGGER.error(err)
             return internal_server_error(req, err, exc_info=sys.exc_info())
         except Exception, err:
             _LOGGER.error('failed to convert encoding: %s' % err, exc_info=1)
             return internal_server_error(req, 'Unexpected error happened.', exc_info=sys.exc_info())