Exemplo n.º 1
0
    def test_default_escaper(self):
        chars = '\\*()\0'
        escaped_chars = '\\5c\\2a\\28\\29\\00'

        filters = [
            (pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('key'),
                assertionValue=pureldap.LDAPAttributeValue(chars)),
             '(key={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_initial(value=chars),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_any(value=chars),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_final(value=chars),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_greaterOrEqual(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars)),
             '(key>={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_lessOrEqual(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars)),
             '(key<={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_approxMatch(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars)),
             '(key~={})'.format(escaped_chars)),
        ]

        for filt, expected in filters:
            result = filt.asText()
            self.assertEqual(expected, result)
Exemplo n.º 2
0
    def _tryService(self, services, baseEntry, request, controls, reply):
        try:
            serviceName = services.pop(0)
        except IndexError:
            return None
        timestamp = self.timestamp()
        d = baseEntry.search(
            filterObject=pureldap.LDAPFilter_and([
                pureldap.LDAPFilter_equalityMatch(
                    attributeDesc=pureldap.LDAPAttributeDescription(
                        'objectClass'),
                    assertionValue=pureldap.LDAPAssertionValue(
                        'serviceSecurityObject')),
                pureldap.LDAPFilter_equalityMatch(
                    attributeDesc=pureldap.LDAPAttributeDescription('owner'),
                    assertionValue=pureldap.LDAPAssertionValue(request.dn)),
                pureldap.LDAPFilter_equalityMatch(
                    attributeDesc=pureldap.LDAPAttributeDescription('cn'),
                    assertionValue=pureldap.LDAPAssertionValue(serviceName)),
                pureldap.LDAPFilter_or([
                    # no time
                    pureldap.LDAPFilter_not(
                        pureldap.LDAPFilter_present('validFrom')),
                    # or already valid
                    pureldap.LDAPFilter_lessOrEqual(
                        attributeDesc=pureldap.LDAPAttributeDescription(
                            'validFrom'),
                        assertionValue=pureldap.LDAPAssertionValue(timestamp)),
                ]),
                pureldap.LDAPFilter_or([
                    # no time
                    pureldap.LDAPFilter_not(
                        pureldap.LDAPFilter_present('validUntil')),
                    # or still valid
                    pureldap.LDAPFilter_greaterOrEqual(
                        attributeDesc=pureldap.LDAPAttributeDescription(
                            'validUntil'),
                        assertionValue=pureldap.LDAPAssertionValue(timestamp)),
                ]),
            ]),
            attributes=('1.1', ))

        def _gotEntries(entries):
            if not entries:
                return None
            assert len(entries) == 1  #TODO
            e = entries[0]
            d = e.bind(request.auth)
            return d

        d.addCallback(_gotEntries)
        d.addCallbacks(callback=self._loopIfNone,
                       callbackArgs=(services, baseEntry, request, controls,
                                     reply),
                       errback=self._loopIfBindError,
                       errbackArgs=(services, baseEntry, request, controls,
                                    reply))
        return d
Exemplo n.º 3
0
 def test_greaterOrEqual_noMatch(self):
     o = inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
                                            attributes={
                                                'objectClass': ['a', 'b'],
                                                'aValue': ['b'],
                                                'bValue': [4],
                                            })
     result = o.match(pureldap.LDAPFilter_greaterOrEqual('num', 5))
     self.assertEquals(result, False)
Exemplo n.º 4
0
 def test_greaterOrEqual_match_equal(self):
     o = inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
                                            attributes={
                                                'objectClass': ['a', 'b'],
                                                'aValue': ['b'],
                                                'num': [4],
                                            })
     result = o.match(
         pureldap.LDAPFilter_greaterOrEqual(pureber.BEROctetString('num'),
                                            pureber.BERInteger(4)))
     self.assertEqual(result, True)
Exemplo n.º 5
0
 def test_greaterOrEqual_noMatch(self):
     o = inmemory.ReadOnlyInMemoryLDAPEntry(
         dn="cn=foo,dc=example,dc=com",
         attributes={
             "objectClass": ["a", "b"],
             "aValue": ["b"],
             "bValue": [4],
         },
     )
     result = o.match(
         pureldap.LDAPFilter_greaterOrEqual(pureber.BEROctetString("num"),
                                            pureber.BERInteger(5)))
     self.assertEqual(result, False)
Exemplo n.º 6
0
    def test_custom_escaper(self):
        chars = 'HELLO'
        escaped_chars = '0b10010000b10001010b10011000b10011000b1001111'

        def custom_escaper(s):
            return ''.join(bin(ord(c)) for c in s)

        filters = [
            (pureldap.LDAPFilter_equalityMatch(
                attributeDesc=pureldap.LDAPAttributeDescription('key'),
                assertionValue=pureldap.LDAPAttributeValue(chars),
                escaper=custom_escaper), '(key={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_initial(value=chars,
                                                    escaper=custom_escaper),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_any(value=chars,
                                                escaper=custom_escaper),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_substrings_final(value=chars,
                                                  escaper=custom_escaper),
             '{}'.format(escaped_chars)),
            (pureldap.LDAPFilter_greaterOrEqual(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars),
                escaper=custom_escaper), '(key>={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_lessOrEqual(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars),
                escaper=custom_escaper), '(key<={})'.format(escaped_chars)),
            (pureldap.LDAPFilter_approxMatch(
                attributeDesc=pureldap.LDAPString('key'),
                assertionValue=pureldap.LDAPString(chars),
                escaper=custom_escaper), '(key~={})'.format(escaped_chars)),
        ]

        for filt, expected in filters:
            result = filt.asText()
            self.assertEqual(expected, result)