示例#1
0
 def test_authorize_authenticated(self):
     # Annotation (even without consumer key) is actionable if the action
     # list includes the special string 'group:__authenticated__' and the user
     # is authenticated (i.e. a user and consumer tuple is provided)
     ann = {'permissions': {'read': ['group:__authenticated__']}}
     assert not authorize(ann, 'read')
     assert authorize(ann, 'read', h.MockUser('bob'))
示例#2
0
 def test_authorize_world(self):
     # Annotation (even without consumer key) is actionable if the action
     # list includes the special string 'group:__world__'
     ann = {'permissions': {'read': ['group:__world__']}}
     assert authorize(ann, 'read')
     assert authorize(ann, 'read', h.MockUser('bob'))
     assert authorize(ann, 'read', h.MockUser('bob', 'consumerkey'))
示例#3
0
 def test_authorize_consumer(self):
     # Annotation (WITH consumer key) is actionable if the action
     # list includes the special string 'group:__consumer__' and the user
     # is authenticated to the same consumer as that of the annotation
     ann = {'permissions': {'read': ['group:__consumer__']}}
     assert not authorize(ann, 'read')
     assert not authorize(ann, 'read', h.MockUser('bob'))
     assert not authorize(ann, 'read', h.MockUser('bob', 'consumerkey'))
     ann = {
         'consumer': 'consumerkey',
         'permissions': {
             'read': ['group:__consumer__']
         }
     }
     assert not authorize(ann, 'read')
     assert not authorize(ann, 'read', h.MockUser('bob'))
     assert authorize(ann, 'read', h.MockUser('alice', 'consumerkey'))
     assert authorize(ann, 'read', h.MockUser('bob', 'consumerkey'))
     assert not authorize(ann, 'read',
                          h.MockUser('bob', 'adifferentconsumerkey'))
     assert not authorize(ann, 'read',
                          h.MockUser('group:__consumer__', 'consumerkey'))
     assert not authorize(
         ann, 'read',
         h.MockUser('group:__consumer__', 'adifferentconsumerkey'))
示例#4
0
 def test_authorize_admin(self):
     # An admin user can do anything
     ann = {'consumer': 'consumerkey', 'user': '******'}
     admin = h.MockUser('walter', 'consumerkey')
     admin.is_admin = True
     assert authorize(ann, 'read', admin)
     assert authorize(ann, 'update', admin)
     assert authorize(ann, 'admin', admin)
 def test_authorize_authenticated(self):
     # Annotation (even without consumer key) is actionable if the action
     # list includes the special string 'group:__authenticated__' and the user
     # is authenticated (i.e. a user and consumer tuple is provided)
     ann = {
         'permissions': {'read': ['group:__authenticated__']}
     }
     assert not authorize(ann, 'read')
     assert authorize(ann, 'read', h.MockUser('bob'))
示例#6
0
 def test_authorize_world(self):
     # Annotation (even without consumer key) is actionable if the action
     # list includes the special string 'group:__world__'
     ann = {
         'permissions': {'read': ['group:__world__']}
     }
     assert authorize(ann, 'read')
     assert authorize(ann, 'read', 'bob')
     assert authorize(ann, 'read', 'bob', 'consumerkey')
示例#7
0
 def test_authorize_owner(self):
     # The annotation-owning user can do anything ('user' is a string)
     ann = {
         'consumer': 'consumerkey',
         'user': '******',
         'permissions': {'read': ['alice', 'charlie']}
     }
     assert authorize(ann, 'read', 'bob', 'consumerkey')
     assert not authorize(ann, 'read', 'bob', 'adifferentconsumer')
     assert not authorize(ann, 'read', 'sally', 'consumerkey')
示例#8
0
 def test_authorize_read_annotation_user_dict(self):
     # The annotation-owning user can do anything ('user' is an object)
     ann = {
         'consumer': 'consumerkey',
         'user': {'id': 'bob'},
         'permissions': {'read': ['alice', 'charlie']}
     }
     assert authorize(ann, 'read', 'bob', 'consumerkey')
     assert not authorize(ann, 'read', 'bob', 'adifferentconsumer')
     assert not authorize(ann, 'read', 'sally', 'consumerkey')
 def test_authorize_admin(self):
     # An admin user can do anything
     ann = {
         'consumer': 'consumerkey',
         'user': '******'
     }
     admin = h.MockUser('walter', 'consumerkey')
     admin.is_admin = True
     assert authorize(ann, 'read', admin)
     assert authorize(ann, 'update', admin)
     assert authorize(ann, 'admin', admin)
示例#10
0
 def test_authorize_owner(self):
     # The annotation-owning user can do anything ('user' is a string)
     ann = {
         'consumer': 'consumerkey',
         'user': '******',
         'permissions': {
             'read': ['alice', 'charlie']
         }
     }
     assert authorize(ann, 'read', h.MockUser('bob', 'consumerkey'))
     assert not authorize(ann, 'read',
                          h.MockUser('bob', 'adifferentconsumer'))
     assert not authorize(ann, 'read', h.MockUser('sally', 'consumerkey'))
示例#11
0
def authorize(annotation, action, user=None):
    action_field = annotation.get('permissions', {}).get(action, [])

    if not action_field:
        return True
    else:
        return authz.authorize(annotation, action, user)
示例#12
0
文件: store.py 项目: BigBlueHat/h
def authorize(annotation, action, user=None):
    action_field = annotation.get('permissions', {}).get(action, [])

    if not action_field:
        return True
    else:
        return authz.authorize(annotation, action, user)
 def test_authorize_consumer(self):
     # Annotation (WITH consumer key) is actionable if the action
     # list includes the special string 'group:__consumer__' and the user
     # is authenticated to the same consumer as that of the annotation
     ann = {
         'permissions': {'read': ['group:__consumer__']}
     }
     assert not authorize(ann, 'read')
     assert not authorize(ann, 'read', h.MockUser('bob'))
     assert not authorize(ann, 'read', h.MockUser('bob', 'consumerkey'))
     ann = {
         'consumer': 'consumerkey',
         'permissions': {'read': ['group:__consumer__']}
     }
     assert not authorize(ann, 'read')
     assert not authorize(ann, 'read', h.MockUser('bob'))
     assert authorize(ann, 'read', h.MockUser('alice', 'consumerkey'))
     assert authorize(ann, 'read', h.MockUser('bob', 'consumerkey'))
     assert not authorize(ann, 'read', h.MockUser('bob', 'adifferentconsumerkey'))
     assert not authorize(ann, 'read', h.MockUser('group:__consumer__', 'consumerkey'))
     assert not authorize(ann, 'read', h.MockUser('group:__consumer__', 'adifferentconsumerkey'))
示例#14
0
def search_annotations():
    kwargs = dict(request.args.items())
    uid = current_user_id()

    if uid:
        if not auth.verify_request(request):
            return _failed_auth_response()

    results = Annotation.search(**kwargs)
    results = filter(lambda a: authz.authorize(a, 'read', uid), results)
    total = Annotation.count(**kwargs)
    return jsonify({
        'total': total,
        'rows': results,
    })
示例#15
0
def search_annotations():
    kwargs = dict(request.args.items())
    uid = current_user_id()

    if uid:
        if not auth.verify_request(request):
            return _failed_auth_response()

    results = Annotation.search(**kwargs)
    results = filter(lambda a: authz.authorize(a, 'read', uid), results)
    total = Annotation.count(**kwargs)
    return jsonify({
        'total': total,
        'rows': results,
    })
示例#16
0
文件: streamer.py 项目: BigBlueHat/h
def after_action(event):
    request = event.request
    action = event.action
    annotation = event.annotation

    annotation.update(url_values_from_document(annotation))

    manager = request.get_sockjs_manager()
    for session in manager.active_sessions():
        if not authz.authorize(annotation, 'read', session.request.user):
            continue

        if not session.filter.match(annotation, action):
            continue

        session.send([annotation, action])
示例#17
0
    def test_authorize_basic(self):
        # Annotation with consumer and permissions fields is actionable as
        # per the permissions spec
        ann = {'consumer': 'consumerkey', 'permissions': {'read': ['bob']}}

        assert not authorize(ann, 'read')
        assert not authorize(ann, 'read', h.MockUser('bob'))
        assert authorize(ann, 'read', h.MockUser('bob', 'consumerkey'))
        assert not authorize(ann, 'read', h.MockUser('alice', 'consumerkey'))

        assert not authorize(ann, 'update')
        assert not authorize(ann, 'update', h.MockUser('bob', 'consumerkey'))
示例#18
0
def after_action(event):
    action = event.action
    annotation = event.annotation

    annotation.update(url_values_from_document(annotation))

    for connection in StreamerSession.connections:
        if not authz.authorize(annotation, 'read', connection.request.user):
            continue

        if not connection.filter.match(annotation, action):
            continue

        try:
            connection.send([annotation, action])
        except:
            log.info(traceback.format_exc())
            log.info('Filter error!')
            connection.close()
示例#19
0
def after_action(event):
    action = event.action
    annotation = event.annotation

    annotation.update(url_values_from_document(annotation))

    for connection in StreamerSession.connections:
        if not authz.authorize(annotation, 'read', connection.request.user):
            continue

        if not connection.filter.match(annotation, action):
            continue

        try:
            connection.send([annotation, action])
        except:
            log.info(traceback.format_exc())
            log.info('Filter error!')
            connection.close()
示例#20
0
    def test_authorize_basic(self):
        # Annotation with consumer and permissions fields is actionable as
        # per the permissions spec
        ann = {
            'consumer': 'consumerkey',
            'permissions': {'read': ['bob']}
        }

        assert not authorize(ann, 'read')
        assert not authorize(ann, 'read', 'bob')
        assert authorize(ann, 'read', 'bob', 'consumerkey')
        assert not authorize(ann, 'read', 'alice', 'consumerkey')

        assert not authorize(ann, 'update')
        assert not authorize(ann, 'update', 'bob', 'consumerkey')
示例#21
0
def update_annotation(id):
    annotation = Annotation.fetch(id)
    if not annotation:
        return jsonify('Annotation not found! No update performed.', status=404)

    failure = _check_action(annotation, 'update', current_user_id())
    if failure:
        return failure

    if request.json:
        updated = _filter_input(request.json)
        updated['id'] = id # use id from URL, regardless of what arrives in JSON payload

        if 'permissions' in updated and updated['permissions'] != annotation.get('permissions', {}):
            if not authz.authorize(annotation, 'admin', current_user_id()):
                return _failed_authz_response('permissions update')

        annotation.update(updated)
        annotation.save()

    return jsonify(annotation)
示例#22
0
def update_annotation(id):
    annotation = Annotation.fetch(id)
    if not annotation:
        return jsonify('Annotation not found! No update performed.',
                       status=404)

    failure = _check_action(annotation, 'update', current_user_id())
    if failure:
        return failure

    if request.json:
        updated = _filter_input(request.json)
        updated[
            'id'] = id  # use id from URL, regardless of what arrives in JSON payload

        if 'permissions' in updated and updated[
                'permissions'] != annotation.get('permissions', {}):
            if not authz.authorize(annotation, 'admin', current_user_id()):
                return _failed_authz_response('permissions update')

        annotation.update(updated)
        annotation.save()

    return jsonify(annotation)
示例#23
0
 def test_authorize_admin_user(self):
     ann = Annotation(permissions={ACTION.ADMIN: ["bob"]})
     assert authorize(ann, "admin", "bob")
     assert not authorize(ann, "admin", "alice")
示例#24
0
 def test_authorize_null_consumer(self):
     # An annotation with no consumer set is private
     ann = {'permissions': {'read': ['bob']}}
     assert not authorize(ann, 'read')
     assert not authorize(ann, 'read', h.MockUser('bob'))
     assert not authorize(ann, 'read', h.MockUser('bob', 'consumerkey'))
示例#25
0
 def test_authorize_admin_user(self):
     ann = Annotation(permissions={'admin': ['bob']})
     assert authorize(ann, 'admin', 'bob')
     assert not authorize(ann, 'admin', 'alice')
示例#26
0
 def test_authorize_delete_user(self):
     ann = Annotation(permissions={'delete': ['bob']})
     assert authorize(ann, 'delete', 'bob')
     assert not authorize(ann, 'delete', 'alice')
示例#27
0
 def test_authorize_read_user(self):
     ann = Annotation(permissions={'read': ['bob']})
     assert authorize(ann, 'read', 'bob')
     assert not authorize(ann, 'read', 'alice')
示例#28
0
 def test_authorize_admin_user(self):
     ann = Annotation(permissions={'admin': ['bob']})
     assert authorize(ann, 'admin', 'bob')
     assert not authorize(ann, 'admin', 'alice')
示例#29
0
 def test_authorize_delete_user(self):
     ann = Annotation(permissions={'delete': ['bob']})
     assert authorize(ann, 'delete', 'bob')
     assert not authorize(ann, 'delete', 'alice')
示例#30
0
def _check_action(annotation, action, uid):
    if not authz.authorize(annotation, action, uid):
        return _failed_authz_response()

    if uid and not auth.verify_request(request):
        return _failed_auth_response()
示例#31
0
 def test_authorize_empty(self):
     # An annotation with no permissions field is private
     ann = {}
     assert not authorize(ann, 'read')
     assert not authorize(ann, 'read', 'bob')
     assert not authorize(ann, 'read', 'bob', 'consumerkey')
示例#32
0
def _check_action(annotation, action, uid):
    if not authz.authorize(annotation, action, uid):
        return _failed_authz_response()

    if uid and not auth.verify_request(request):
        return _failed_auth_response()
示例#33
0
 def test_authorize_null_consumer(self):
     # An annotation with no consumer set is private
     ann = {'permissions': {'read': ['bob']}}
     assert not authorize(ann, 'read')
     assert not authorize(ann, 'read', 'bob')
     assert not authorize(ann, 'read', 'bob', 'consumerkey')
示例#34
0
 def test_authorize_read_user(self):
     ann = Annotation(permissions={'read': ['bob']})
     assert authorize(ann, 'read', 'bob')
     assert not authorize(ann, 'read', 'alice')
示例#35
0
 def test_authorize_read_nouser(self):
     ann = Annotation()
     assert authorize(ann, "read")
     assert authorize(ann, "read", "bob")
示例#36
0
 def test_authorize_delete_nouser(self):
     ann = Annotation()
     assert not authorize(ann, 'delete')
     assert authorize(ann, 'delete', 'bob')
示例#37
0
 def test_authorize_read_user(self):
     ann = Annotation(permissions={ACTION.READ: ["bob"]})
     assert authorize(ann, "read", "bob")
     assert not authorize(ann, "read", "alice")
示例#38
0
 def test_authorize_admin_nouser(self):
     ann = Annotation()
     assert not authorize(ann, 'admin')
     assert authorize(ann, 'admin', 'bob')
示例#39
0
 def test_authorize_update_user(self):
     ann = Annotation(permissions={ACTION.UPDATE: ["bob"]})
     assert authorize(ann, "update", "bob")
     assert not authorize(ann, "update", "alice")
示例#40
0
 def test_authorize_read_nouser(self):
     ann = Annotation()
     assert authorize(ann, 'read')
     assert authorize(ann, 'read', 'bob')
示例#41
0
 def test_authorize_delete_nouser(self):
     ann = Annotation()
     assert authorize(ann, "delete")
     assert authorize(ann, "delete", "bob")
示例#42
0
 def test_authorize_delete_nouser(self):
     ann = Annotation()
     assert not authorize(ann, 'delete')
     assert authorize(ann, 'delete', 'bob')
示例#43
0
 def test_authorize_delete_user(self):
     ann = Annotation(permissions={ACTION.DELETE: ["bob"]})
     assert authorize(ann, "delete", "bob")
     assert not authorize(ann, "delete", "alice")
示例#44
0
 def test_authorize_admin_nouser(self):
     ann = Annotation()
     assert not authorize(ann, 'admin')
     assert authorize(ann, 'admin', 'bob')
示例#45
0
 def test_authorize_admin_nouser(self):
     ann = Annotation()
     assert authorize(ann, "admin")
     assert authorize(ann, "admin", "bob")
示例#46
0
 def test_authorize_read_nouser(self):
     ann = Annotation()
     assert authorize(ann, 'read')
     assert authorize(ann, 'read', 'bob')
示例#47
0
 def test_authorize_empty(self):
     # An annotation with no permissions field is private
     ann = {}
     assert not authorize(ann, 'read')
     assert not authorize(ann, 'read', h.MockUser('bob'))
     assert not authorize(ann, 'read', h.MockUser('bob', 'consumerkey'))