def test_parse_uris(self): tests = [ (u"com.myapp.<product:int>.update", [ (u"com.myapp.0.update", {u'product': 0}), (u"com.myapp.123456.update", {u'product': 123456}), (u"com.myapp.aaa.update", None), (u"com.myapp..update", None), (u"com.myapp.0.delete", None), ] ), (u"com.myapp.<product:string>.update", [ (u"com.myapp.box.update", {u'product': u'box'}), (u"com.myapp.123456.update", {u'product': u'123456'}), (u"com.myapp..update", None), ] ) ] for test in tests: pat = Pattern(test[0], Pattern.URI_TARGET_ENDPOINT) for ptest in test[1]: uri = ptest[0] kwargs_should = ptest[1] if kwargs_should is not None: args_is, kwargs_is = pat.match(uri) self.assertEqual(kwargs_is, kwargs_should) else: self.assertRaises(Exception, pat.match, uri)
def test_parse_uris(self): tests = [(u"com.myapp.<product:int>.update", [ (u"com.myapp.0.update", { u'product': 0 }), (u"com.myapp.123456.update", { u'product': 123456 }), (u"com.myapp.aaa.update", None), (u"com.myapp..update", None), (u"com.myapp.0.delete", None), ]), (u"com.myapp.<product:string>.update", [ (u"com.myapp.box.update", { u'product': u'box' }), (u"com.myapp.123456.update", { u'product': u'123456' }), (u"com.myapp..update", None), ])] for test in tests: pat = Pattern(test[0], Pattern.URI_TARGET_ENDPOINT) for ptest in test[1]: uri = ptest[0] kwargs_should = ptest[1] if kwargs_should is not None: args_is, kwargs_is = pat.match(uri) self.assertEqual(kwargs_is, kwargs_should) else: self.assertRaises(Exception, pat.match, uri)
def test_parse_uris(self): tests = [("com.myapp.<product:int>.update", [ ("com.myapp.0.update", { 'product': 0 }), ("com.myapp.123456.update", { 'product': 123456 }), ("com.myapp.aaa.update", None), ("com.myapp..update", None), ("com.myapp.0.delete", None), ]), ("com.myapp.<product:string>.update", [ ("com.myapp.box.update", { 'product': 'box' }), ("com.myapp.123456.update", { 'product': '123456' }), ("com.myapp..update", None), ]), ("com.myapp.<product>.update", [ ("com.myapp.0.update", { 'product': '0' }), ("com.myapp.abc.update", { 'product': 'abc' }), ("com.myapp..update", None), ]), ("com.myapp.<category:string>.<subcategory:string>.list", [ ("com.myapp.cosmetic.shampoo.list", { 'category': 'cosmetic', 'subcategory': 'shampoo' }), ("com.myapp...list", None), ("com.myapp.cosmetic..list", None), ("com.myapp..shampoo.list", None), ])] for test in tests: pat = Pattern(test[0], Pattern.URI_TARGET_ENDPOINT) for ptest in test[1]: uri = ptest[0] kwargs_should = ptest[1] if kwargs_should is not None: args_is, kwargs_is = pat.match(uri) self.assertEqual(kwargs_is, kwargs_should) else: self.assertRaises(Exception, pat.match, uri)
def test_valid_uris(self): for u in [ u"com.myapp.proc1", u"123", u"com.myapp.<product:int>.update", u"com.myapp.<category:string>.<subcategory>.list" ]: p = Pattern(u, Pattern.URI_TARGET_ENDPOINT) self.assertIsInstance(p, Pattern)
def test_valid_uris(self): for u in [ u"com.myapp.proc1", u"com.myapp.<product:int>.update", ]: p = Pattern(u, Pattern.URI_TARGET_ENDPOINT) self.assertIsInstance(p, Pattern)
def define(self, exception, error=None): if error is None: assert(hasattr(exception, '_wampuris')) self._ecls_to_uri_pat[exception] = exception._wampuris self._uri_to_ecls[exception._wampuris[0].uri()] = exception else: assert(not hasattr(exception, '_wampuris')) self._ecls_to_uri_pat[exception] = [Pattern(error, Pattern.URI_TARGET_HANDLER)] self._uri_to_ecls[error] = exception
def decorate(cls): assert (issubclass(cls, Exception)) if not hasattr(cls, '_wampuris'): cls._wampuris = [] cls._wampuris.append(Pattern(uri, Pattern.URI_TARGET_EXCEPTION)) return cls
def decorate(f): assert (callable(f)) if not hasattr(f, '_wampuris'): f._wampuris = [] f._wampuris.append(Pattern(uri, Pattern.URI_TARGET_HANDLER)) return f
def authorize(self, session, uri, action, options): """ Authorize a session connected under this role to perform the given action on the given URI. :param session: The WAMP session that requests the action. :type session: Instance of :class:`autobahn.wamp.protocol.ApplicationSession` :param uri: The URI on which to perform the action. :type uri: str :param action: The action to be performed. :type action: str :return: bool -- Flag indicating whether session is authorized or not. """ self.log.debug( "CrossbarRouterRoleStaticAuth.authorize {myuri} {uri} {action}", myuri=self.uri, uri=uri, action=action) try: # longest prefix match of the URI to be authorized against our Trie # of configured URIs for permissions permissions = self._permissions.longest_prefix_value(uri) # if there is a _prefix_ matching URI, check that this is actually the # match policy on the permission (otherwise, apply default permissions)! if permissions.match != u'prefix' and uri != permissions.uri: permissions = self._default except KeyError: # workaround because of https://bitbucket.org/gsakkis/pytrie/issues/4/string-keys-of-zero-length-are-not permissions = self._permissions.get(u'', self._default) # if we found a non-"exact" match, there might be a better one in the wildcards if permissions.match != u'exact': try: wildperm = self._wild_permissions.longest_prefix_value(uri) Pattern(wildperm.uri, Pattern.URI_TARGET_ENDPOINT).match(uri) except (KeyError, Exception): # match() raises Exception on no match wildperm = None if wildperm is not None: permissions = wildperm # we now have some permissions, either from matching something # or via self._default if action == u'publish': return { u'allow': permissions.publish, u'disclose': permissions.disclose_publisher, u'cache': permissions.cache } elif action == u'subscribe': return { u'allow': permissions.subscribe, u'cache': permissions.cache } elif action == u'call': return { u'allow': permissions.call, u'disclose': permissions.disclose_caller, u'cache': permissions.cache } elif action == u'register': return { u'allow': permissions.register, u'cache': permissions.cache } else: # should not arrive here raise Exception('logic error')
def decorate(f): assert(callable(f)) if not hasattr(f, '_wampuris'): f._wampuris = [] f._wampuris.append(Pattern(six.u(uri), Pattern.URI_TARGET_ENDPOINT)) return f