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.

__init__.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. """
  3. controls.py - support classes for LDAP controls
  4. See https://www.python-ldap.org/ for details.
  5. Description:
  6. The ldap.controls module provides LDAPControl classes.
  7. Each class provides support for a certain control.
  8. """
  9. from ldap.pkginfo import __version__
  10. import _ldap
  11. assert _ldap.__version__==__version__, \
  12. ImportError('ldap %s and _ldap %s version mismatch!' % (__version__,_ldap.__version__))
  13. import ldap
  14. from pyasn1.error import PyAsn1Error
  15. __all__ = [
  16. 'KNOWN_RESPONSE_CONTROLS',
  17. # Classes
  18. 'AssertionControl',
  19. 'BooleanControl',
  20. 'LDAPControl',
  21. 'ManageDSAITControl',
  22. 'MatchedValuesControl',
  23. 'RelaxRulesControl',
  24. 'RequestControl',
  25. 'ResponseControl',
  26. 'SimplePagedResultsControl',
  27. 'ValueLessRequestControl',
  28. # Functions
  29. 'RequestControlTuples',
  30. 'DecodeControlTuples',
  31. ]
  32. # response control OID to class registry
  33. KNOWN_RESPONSE_CONTROLS = {}
  34. class RequestControl:
  35. """
  36. Base class for all request controls
  37. controlType
  38. OID as string of the LDAPv3 extended request control
  39. criticality
  40. sets the criticality of the control (boolean)
  41. encodedControlValue
  42. control value of the LDAPv3 extended request control
  43. (here it is the BER-encoded ASN.1 control value)
  44. """
  45. def __init__(self,controlType=None,criticality=False,encodedControlValue=None):
  46. self.controlType = controlType
  47. self.criticality = criticality
  48. self.encodedControlValue = encodedControlValue
  49. def encodeControlValue(self):
  50. """
  51. sets class attribute encodedControlValue to the BER-encoded ASN.1
  52. control value composed by class attributes set before
  53. """
  54. return self.encodedControlValue
  55. class ResponseControl:
  56. """
  57. Base class for all response controls
  58. controlType
  59. OID as string of the LDAPv3 extended response control
  60. criticality
  61. sets the criticality of the received control (boolean)
  62. """
  63. def __init__(self,controlType=None,criticality=False):
  64. self.controlType = controlType
  65. self.criticality = criticality
  66. def decodeControlValue(self,encodedControlValue):
  67. """
  68. decodes the BER-encoded ASN.1 control value and sets the appropriate
  69. class attributes
  70. """
  71. self.encodedControlValue = encodedControlValue
  72. class LDAPControl(RequestControl,ResponseControl):
  73. """
  74. Base class for combined request/response controls mainly
  75. for backward-compatibility to python-ldap 2.3.x
  76. """
  77. def __init__(self,controlType=None,criticality=False,controlValue=None,encodedControlValue=None):
  78. self.controlType = controlType
  79. self.criticality = criticality
  80. self.controlValue = controlValue
  81. self.encodedControlValue = encodedControlValue
  82. def RequestControlTuples(ldapControls):
  83. """
  84. Return list of readily encoded 3-tuples which can be directly
  85. passed to C module _ldap
  86. ldapControls
  87. sequence-type of RequestControl objects
  88. """
  89. if ldapControls is None:
  90. return None
  91. else:
  92. result = [
  93. (c.controlType,c.criticality,c.encodeControlValue())
  94. for c in ldapControls
  95. ]
  96. return result
  97. def DecodeControlTuples(ldapControlTuples,knownLDAPControls=None):
  98. """
  99. Returns list of readily decoded ResponseControl objects
  100. ldapControlTuples
  101. Sequence-type of 3-tuples returned by _ldap.result4() containing
  102. the encoded ASN.1 control values of response controls.
  103. knownLDAPControls
  104. Dictionary mapping extended control's OID to ResponseControl class
  105. of response controls known by the application. If None
  106. ldap.controls.KNOWN_RESPONSE_CONTROLS is used here.
  107. """
  108. knownLDAPControls = knownLDAPControls or KNOWN_RESPONSE_CONTROLS
  109. result = []
  110. for controlType,criticality,encodedControlValue in ldapControlTuples or []:
  111. try:
  112. control = knownLDAPControls[controlType]()
  113. except KeyError:
  114. if criticality:
  115. raise ldap.UNAVAILABLE_CRITICAL_EXTENSION('Received unexpected critical response control with controlType %s' % (repr(controlType)))
  116. else:
  117. control.controlType,control.criticality = controlType,criticality
  118. try:
  119. control.decodeControlValue(encodedControlValue)
  120. except PyAsn1Error:
  121. if criticality:
  122. raise
  123. else:
  124. result.append(control)
  125. return result
  126. # Import the standard sub-modules
  127. from ldap.controls.simple import *
  128. from ldap.controls.libldap import *