Beispiel #1
0
    def _authorizedResource(self, request):
        """
        Get the L{IResource} which the given request is authorized to receive.
        If the proper authorization headers are present, the resource will be
        requested from the portal.  If not, an anonymous login attempt will be
        made.
        """
        authheader = request.getHeader(b"authorization")
        if not authheader:
            return util.DeferredResource(self._login(Anonymous()))

        factory, respString = self._selectParseHeader(authheader)
        if factory is None:
            return UnauthorizedResource(self._credentialFactories)
        try:
            credentials = factory.decode(respString, request)
        except error.LoginFailed:
            return UnauthorizedResource(self._credentialFactories)
        except BaseException:
            self._log.failure("Unexpected failure from credentials factory")
            return ErrorPage(500, None, None)
        else:
            return util.DeferredResource(self._login(credentials))
Beispiel #2
0
    def test_render(self):
        """
        L{DeferredResource} uses the request object's C{render} method to
        render the resource which is the result of the L{Deferred} being
        handled.
        """
        rendered = []
        request = DummyRequest([])
        request.render = rendered.append

        result = resource.Resource()
        deferredResource = util.DeferredResource(defer.succeed(result))
        deferredResource.render(request)
        self.assertEquals(rendered, [result])
Beispiel #3
0
 def getChild(self, path, request):
     if path == '': path = 'index'
     path = path.replace(".", "_")
     cm = getattr(self, "wchild_" + path, None)
     if cm:
         p = cm(request)
         if isinstance(p, Deferred):
             return util.DeferredResource(p)
         adapter = IResource(p, None)
         if adapter is not None:
             return adapter
     # maybe we want direct support for ModelLoader?
     # cl = getattr(self, "wload_"+path, None) #???
     return Resource.getChild(self, path, request)
Beispiel #4
0
    def getChild(self, filename, request):
        # If we still have another component of the path, then we have
        # an old URL that encodes the content ID. We want to keep supporting
        # these, so we just ignore the content id that is currently in
        # self.aliasID and extract the real one from the URL. Note that
        # tokens do not work with the old URL style: they are URL specific.
        if len(request.postpath) == 1:
            try:
                self.aliasID = int(filename)
            except ValueError:
                log.msg(
                    "404 (old URL): alias is not an int: %r" % (filename,))
                return fourOhFour
            filename = request.postpath[0]

        # IFF the request has a .restricted. subdomain, ensure there is a
        # alias id in the right most subdomain, and that it matches
        # self.aliasIDd, And that the host precisely matches what we generate
        # (specifically to stop people putting a good prefix to the left of an
        # attacking one).
        hostname = request.getRequestHostname()
        if '.restricted.' in hostname:
            # Configs can change without warning: evaluate every time.
            download_url = config.librarian.download_url
            parsed = list(urlparse(download_url))
            netloc = parsed[1]
            # Strip port if present
            if netloc.find(':') > -1:
                netloc = netloc[:netloc.find(':')]
            expected_hostname = 'i%d.restricted.%s' % (self.aliasID, netloc)
            if expected_hostname != hostname:
                log.msg(
                    '404: expected_hostname != hostname: %r != %r' %
                    (expected_hostname, hostname))
                return fourOhFour

        token = request.args.get('token', [None])[0]
        path = request.path
        deferred = deferToThread(
            self._getFileAlias, self.aliasID, token, path)
        deferred.addCallback(
                self._cb_getFileAlias, filename, request
                )
        deferred.addErrback(self._eb_getFileAlias)
        return util.DeferredResource(deferred)
Beispiel #5
0
 def getChildWithDefault(self, name, request):
     d = defer.succeed(self.default)
     return util.DeferredResource(d).getChildWithDefault(name, request)
Beispiel #6
0
 def getChild(self, path, request):
     d = self.loadModel(path, request)
     templateFile = (self.templateFile or self.__class__.__name__ + '.html')
     d.addCallback(lambda result: self.parent.makeView(
         self.modelClass(result), templateFile, 1))
     return util.DeferredResource(d)
Beispiel #7
0
 def _authorizedResource(self, request):
     creds = SessionCredential(request)
     return util.DeferredResource(self._login(creds))
Beispiel #8
0
 def getChild(self, path, request):
     return util.DeferredResource(self.getDeferredChild(path, request))
Beispiel #9
0
                request._outDict = outDict  # CHOMP CHOMP!
                # I wanted better default behavior for debugging, so I could
                # see the arguments passed, but there is no channel for this in
                # the existing callback structure.  So, here it goes.
                if isinstance(outObj, defer.Deferred):

                    def _ebModel(err):
                        if err.trap(formmethod.FormException):
                            mf = self.errorModelFactory(
                                request.args, outDict, err.value)
                            return self.errback(mf)
                        raise err

                    (outObj.addCallback(self.modelFactory).addCallback(
                        self.callback).addErrback(_ebModel))
                    return util.DeferredResource(outObj).render(request)
                else:
                    return self.callback(
                        self.modelFactory(outObj)).render(request)

    def errorModelFactory(self, args, out, err):
        return FormErrorModel(self.formMethod, args, err)

    def errorViewFactory(self, m):
        v = view.View(m)
        v.template = '''
        <html>
        <head>
        <title> Form Error View </title>
        <style>
        .formDescription {color: green}