def test_deletes_jwt_from_cookies_when_unset(self):
        response = Response()
        response.jwt_cookie = None
        response = self.middleware.process_response(None, response)

        self.assertEqual(response.cookies['jwt'].value, '')
        self.assertEqual(response.cookies['jwt']['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
Exemple #2
0
def test_process_response_unsets_cookie(middleware, jwt):
    response = Response()
    response.jwt_cookie = None
    response = middleware.process_response(Request(), response)

    morsel = response.cookies['jwt']
    assert morsel.value == ''
    assert morsel['expires'] == 'Thu, 01-Jan-1970 00:00:00 GMT'
Exemple #3
0
def serve(request, path, document_root=None, show_indexes=False):
    """
    Usage in the router:
    
    (r'(?P<path>.*)$', 'rivr.views.serve', {'document_root':'/usr/var/http/'})
    """
    
    # Clean up given path to only allow serving files below document_root.
    path = posixpath.normpath(urllib.unquote(path))
    path = path.lstrip('/')
    newpath = ''
    
    for part in path.split('/'):
        if not part:
            # Strip empty path components.
            continue
        
        drive, part = os.path.splitdrive(part)
        head, part = os.path.split(part)
        
        if part in (os.curdir, os.pardir):
            # Strip '.' and '..' in path.
            continue
        
        newpath = os.path.join(newpath, part).replace('\\', '/')
    
    if newpath and path != newpath:
        return ResponseRedirect(newpath)
    
    fullpath = os.path.join(document_root, newpath)
    
    if not os.path.exists(fullpath):
        raise Http404, '"%s" does not exist.' % newpath
    
    if os.path.isdir(fullpath):
        if show_indexes:
            return directory_index(request, newpath, fullpath)
        raise Http404, 'Directory indexes are not allowed here.'
    
    statobj = os.stat(fullpath)
    
    if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'), statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]):
        return ResponseNotModified()
    
    mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
    contents = open(fullpath, 'rb').read()
    
    response = Response(contents, content_type=mimetype)
    response.headers['Last-Modified'] = '%s GMT' % formatdate(statobj[stat.ST_MTIME])[:25]
    response.headers['Content-Length'] = str(len(contents))
    
    return response
Exemple #4
0
    def content_type_providers(self):
        """
        Returns a dictionary of content type providers, key is a string
        of the content type, i.e, `application/json` and the value is a
        function that returns a response for that type.
        """
        def json_provider(deserializer, content_type):
            def inner():
                content = json.dumps(deserializer(self))
                return Response(
                    content,
                    content_type='{}; charset=utf8'.format(content_type))

            return inner

        return {
            'application/json':
            json_provider(deserialize_json, 'application/json'),
            'application/hal+json':
            json_provider(deserialize_hal, 'application/hal+json'),
            'application/vnd.siren+json':
            json_provider(deserialize_siren, 'application/vnd.siren+json'),
            'text/html':
            lambda: Response(deserialize_html(self)),
        }
Exemple #5
0
def test_process_request_calls_401_for_invalid_token():
    jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJleGFtcGxlLmNvbSJ9.INovSA2CyXeBwzR0Bqq-pFuxfQLVgnFpN4x1JP0Ve84'
    middleware = JWTMiddleware(key='secret', audience='prod.example.com')
    middleware.custom_401 = lambda r: Response('custom 401')

    request = Request(headers={'Authorization': 'Bearer {}'.format(jwt)})
    response = middleware.process_request(request)

    assert response.content == 'custom 401'
    assert request.jwt == None
Exemple #6
0
def status_view(request):
    status = {
        'entrants':
        Entrant.select().count(),
        'invited':
        Invitation.select().where(
            Invitation.state == Invitation.INVITED_STATE).count(),
        'accepted':
        Invitation.select().where(
            Invitation.state == Invitation.ACCEPTED_STATE).count(),
        'rejected':
        Invitation.select().where(
            Invitation.state == Invitation.REJECTED_STATE).count(),
    }
    return Response(json.dumps(status), content_type='application/json')
Exemple #7
0
def test_process_response_encodes_cookie(middleware, jwt):
    response = Response()
    response.jwt_cookie = {'name': 'Kyle'}
    response = middleware.process_response(Request(), response)

    assert response.cookies['jwt'].value == jwt
    def test_encodes_jwt_in_cookie(self):
        response = Response()
        response.jwt_cookie = {'name': 'Kyle'}
        response = self.middleware.process_response(None, response)

        self.assertEqual(response.cookies['jwt'].value, self.jwt)
Exemple #9
0
 def inner():
     content = json.dumps(deserializer(self))
     return Response(
         content,
         content_type='{}; charset=utf8'.format(content_type))