예제 #1
0
 def test_test_request(self):
     request = TestRequest()
     self.assertTrue(IRequest.providedBy(request))
     self.assertEqual(
         request.physicalPathToURL('/root'), 'http://localhost/root')
     self.assertEqual(
         request.getURL(), 'http://localhost')
예제 #2
0
def negotiate(request):
    """Monkey patch zope.i18n.negotiate not to rely on environment
    variable, and to cache its computation.
    """
    if IRequest.providedBy(request):
        if 'I18N_LANGUAGE' in request.other:
            return request.other['I18N_LANGUAGE']
        adapter = IUserPreferredLanguages(request)
        languages = adapter.getPreferredLanguages()
        if languages:
            language = languages[0]
        else:
            language = 'en'
        request.other['I18N_LANGUAGE'] = language
        return language
    return None
예제 #3
0
    def unauthorized(self, request, response, message=None):
        service = queryUtility(ISecretService)
        if service is None:
            return False

        rewrite_url = None
        if IRequest.providedBy(request):
            vhm_plugin = request.get_plugin(IVirtualHosting)
            if vhm_plugin is not None:
                rewrite_url = vhm_plugin.rewrite_url

        # 1. find the currently unauthorized URL.
        came_from = request.get('__ac.field.origin', None)
        if came_from is None:
            came_from = request.get('ACTUAL_URL', '')
            query = request.form.copy()
            if query:
                for bad in ['login_status', '-C']:
                    if bad in query:
                        del query[bad]
            if query:
                came_from += encode_query(query)

        # 2. do the optional redirect to the wanted backend.
        if self.redirect_to_url:
            if (not came_from.startswith(self.redirect_to_url) and
                rewrite_url is not None):
                response.redirect(rewrite_url(self.redirect_to_url, came_from))
                return True

        # 3. Cleanup, if we already have a auth cookie, delete it.
        if response.cookies.has_key(self.cookie_name):
            del response.cookies[self.cookie_name]

        # 4. Get the login page.
        page = self._get_login_page(request)
        if page is None:
            return False

        options = {}
        if self.include_session_token:
            secret = service.digest(str(IClientId(request)), came_from)
            session = self._get_session(request)
            session.set('secret', secret)
            options['__ac.field.secret'] = secret

        if self.redirect_to_path and rewrite_url is not None:
            # Only include the path
            options['__ac.field.origin'] = rewrite_url(None, came_from)
        else:
            options['__ac.field.origin'] = came_from

        # Set options. The page should not accept to render if action
        # is not set.
        page.message = message
        page.action = absoluteURL(self, request) + '/login'
        request.form = options
        # It is not very nice but we don't have lot of choice.
        response.setStatus(401)
        response.write(page())
        return True