Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sss.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. """
  3. ldap.controls.sss - classes for Server Side Sorting
  4. (see RFC 2891)
  5. See https://www.python-ldap.org/ for project details.
  6. """
  7. __all__ = [
  8. 'SSSRequestControl',
  9. 'SSSResponseControl',
  10. ]
  11. import ldap
  12. from ldap.ldapobject import LDAPObject
  13. from ldap.controls import (RequestControl, ResponseControl,
  14. KNOWN_RESPONSE_CONTROLS, DecodeControlTuples)
  15. from pyasn1.type import univ, namedtype, tag, namedval, constraint
  16. from pyasn1.codec.ber import encoder, decoder
  17. # SortKeyList ::= SEQUENCE OF SEQUENCE {
  18. # attributeType AttributeDescription,
  19. # orderingRule [0] MatchingRuleId OPTIONAL,
  20. # reverseOrder [1] BOOLEAN DEFAULT FALSE }
  21. class SortKeyType(univ.Sequence):
  22. componentType = namedtype.NamedTypes(
  23. namedtype.NamedType('attributeType', univ.OctetString()),
  24. namedtype.OptionalNamedType('orderingRule',
  25. univ.OctetString().subtype(
  26. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)
  27. )
  28. ),
  29. namedtype.DefaultedNamedType('reverseOrder', univ.Boolean(False).subtype(
  30. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))))
  31. class SortKeyListType(univ.SequenceOf):
  32. componentType = SortKeyType()
  33. class SSSRequestControl(RequestControl):
  34. '''Order result server side
  35. >>> s = SSSRequestControl(ordering_rules=['-cn'])
  36. '''
  37. controlType = '1.2.840.113556.1.4.473'
  38. def __init__(
  39. self,
  40. criticality=False,
  41. ordering_rules=None,
  42. ):
  43. RequestControl.__init__(self,self.controlType,criticality)
  44. self.ordering_rules = ordering_rules
  45. if isinstance(ordering_rules, basestring):
  46. ordering_rules = [ordering_rules]
  47. for rule in ordering_rules:
  48. rule = rule.split(':')
  49. assert len(rule) < 3, 'syntax for ordering rule: [-]<attribute-type>[:ordering-rule]'
  50. def asn1(self):
  51. p = SortKeyListType()
  52. for i, rule in enumerate(self.ordering_rules):
  53. q = SortKeyType()
  54. reverse_order = rule.startswith('-')
  55. if reverse_order:
  56. rule = rule[1:]
  57. if ':' in rule:
  58. attribute_type, ordering_rule = rule.split(':')
  59. else:
  60. attribute_type, ordering_rule = rule, None
  61. q.setComponentByName('attributeType', attribute_type)
  62. if ordering_rule:
  63. q.setComponentByName('orderingRule', ordering_rule)
  64. if reverse_order:
  65. q.setComponentByName('reverseOrder', 1)
  66. p.setComponentByPosition(i, q)
  67. return p
  68. def encodeControlValue(self):
  69. return encoder.encode(self.asn1())
  70. class SortResultType(univ.Sequence):
  71. componentType = namedtype.NamedTypes(
  72. namedtype.NamedType('sortResult', univ.Enumerated().subtype(
  73. namedValues=namedval.NamedValues(
  74. ('success', 0),
  75. ('operationsError', 1),
  76. ('timeLimitExceeded', 3),
  77. ('strongAuthRequired', 8),
  78. ('adminLimitExceeded', 11),
  79. ('noSuchAttribute', 16),
  80. ('inappropriateMatching', 18),
  81. ('insufficientAccessRights', 50),
  82. ('busy', 51),
  83. ('unwillingToPerform', 53),
  84. ('other', 80)),
  85. subtypeSpec=univ.Enumerated.subtypeSpec + constraint.SingleValueConstraint(
  86. 0, 1, 3, 8, 11, 16, 18, 50, 51, 53, 80))),
  87. namedtype.OptionalNamedType('attributeType',
  88. univ.OctetString().subtype(
  89. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)
  90. )
  91. ))
  92. class SSSResponseControl(ResponseControl):
  93. controlType = '1.2.840.113556.1.4.474'
  94. def __init__(self,criticality=False):
  95. ResponseControl.__init__(self,self.controlType,criticality)
  96. def decodeControlValue(self, encoded):
  97. p, rest = decoder.decode(encoded, asn1Spec=SortResultType())
  98. assert not rest, 'all data could not be decoded'
  99. sort_result = p.getComponentByName('sortResult')
  100. self.sortResult = int(sort_result)
  101. attribute_type = p.getComponentByName('attributeType')
  102. if attribute_type.hasValue():
  103. self.attributeType = attribute_type
  104. else:
  105. self.attributeType = None
  106. # backward compatibility class attributes
  107. self.result = self.sortResult
  108. self.attribute_type_error = self.attributeType
  109. KNOWN_RESPONSE_CONTROLS[SSSResponseControl.controlType] = SSSResponseControl