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.

simple.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. """
  3. ldap.controls.simple - classes for some very simple LDAP controls
  4. See https://www.python-ldap.org/ for details.
  5. """
  6. import struct,ldap
  7. from ldap.controls import RequestControl,ResponseControl,LDAPControl,KNOWN_RESPONSE_CONTROLS
  8. class ValueLessRequestControl(RequestControl):
  9. """
  10. Base class for controls without a controlValue.
  11. The presence of the control in a LDAPv3 request changes the server's
  12. behaviour when processing the request simply based on the controlType.
  13. controlType
  14. OID of the request control
  15. criticality
  16. criticality request control
  17. """
  18. def __init__(self,controlType=None,criticality=False):
  19. self.controlType = controlType
  20. self.criticality = criticality
  21. def encodeControlValue(self):
  22. return None
  23. class OctetStringInteger(LDAPControl):
  24. """
  25. Base class with controlValue being unsigend integer values
  26. integerValue
  27. Integer to be sent as OctetString
  28. """
  29. def __init__(self,controlType=None,criticality=False,integerValue=None):
  30. self.controlType = controlType
  31. self.criticality = criticality
  32. self.integerValue = integerValue
  33. def encodeControlValue(self):
  34. return struct.pack('!Q',self.integerValue)
  35. def decodeControlValue(self,encodedControlValue):
  36. self.integerValue = struct.unpack('!Q',encodedControlValue)[0]
  37. class BooleanControl(LDAPControl):
  38. """
  39. Base class for simple request controls with boolean control value.
  40. Constructor argument and class attribute:
  41. booleanValue
  42. Boolean (True/False or 1/0) which is the boolean controlValue.
  43. """
  44. boolean2ber = { 1:'\x01\x01\xFF', 0:'\x01\x01\x00' }
  45. ber2boolean = { '\x01\x01\xFF':1, '\x01\x01\x00':0 }
  46. def __init__(self,controlType=None,criticality=False,booleanValue=False):
  47. self.controlType = controlType
  48. self.criticality = criticality
  49. self.booleanValue = booleanValue
  50. def encodeControlValue(self):
  51. return self.boolean2ber[int(self.booleanValue)]
  52. def decodeControlValue(self,encodedControlValue):
  53. self.booleanValue = self.ber2boolean[encodedControlValue]
  54. class ManageDSAITControl(ValueLessRequestControl):
  55. """
  56. Manage DSA IT Control
  57. """
  58. def __init__(self,criticality=False):
  59. ValueLessRequestControl.__init__(self,ldap.CONTROL_MANAGEDSAIT,criticality=False)
  60. KNOWN_RESPONSE_CONTROLS[ldap.CONTROL_MANAGEDSAIT] = ManageDSAITControl
  61. class RelaxRulesControl(ValueLessRequestControl):
  62. """
  63. Relax Rules Control
  64. """
  65. def __init__(self,criticality=False):
  66. ValueLessRequestControl.__init__(self,ldap.CONTROL_RELAX,criticality=False)
  67. KNOWN_RESPONSE_CONTROLS[ldap.CONTROL_RELAX] = RelaxRulesControl
  68. class ProxyAuthzControl(RequestControl):
  69. """
  70. Proxy Authorization Control
  71. authzId
  72. string containing the authorization ID indicating the identity
  73. on behalf which the server should process the request
  74. """
  75. def __init__(self,criticality,authzId):
  76. RequestControl.__init__(self,ldap.CONTROL_PROXY_AUTHZ,criticality,authzId)
  77. class AuthorizationIdentityRequestControl(ValueLessRequestControl):
  78. """
  79. Authorization Identity Request and Response Controls
  80. """
  81. controlType = '2.16.840.1.113730.3.4.16'
  82. def __init__(self,criticality):
  83. ValueLessRequestControl.__init__(self,self.controlType,criticality)
  84. class AuthorizationIdentityResponseControl(ResponseControl):
  85. """
  86. Authorization Identity Request and Response Controls
  87. Class attributes:
  88. authzId
  89. decoded authorization identity
  90. """
  91. controlType = '2.16.840.1.113730.3.4.15'
  92. def decodeControlValue(self,encodedControlValue):
  93. self.authzId = encodedControlValue
  94. KNOWN_RESPONSE_CONTROLS[AuthorizationIdentityResponseControl.controlType] = AuthorizationIdentityResponseControl
  95. class GetEffectiveRightsControl(RequestControl):
  96. """
  97. Get Effective Rights Control
  98. """
  99. def __init__(self,criticality,authzId=None):
  100. RequestControl.__init__(self,'1.3.6.1.4.1.42.2.27.9.5.2',criticality,authzId)