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.

deref.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf-8 -*-
  2. """
  3. ldap.controls.deref - classes for
  4. (see https://tools.ietf.org/html/draft-masarati-ldap-deref)
  5. See https://www.python-ldap.org/ for project details.
  6. """
  7. __all__ = [
  8. 'DEREF_CONTROL_OID',
  9. 'DereferenceControl',
  10. ]
  11. import ldap.controls
  12. from ldap.controls import LDAPControl,KNOWN_RESPONSE_CONTROLS
  13. import pyasn1_modules.rfc2251
  14. from pyasn1.type import namedtype,univ,tag
  15. from pyasn1.codec.ber import encoder,decoder
  16. from pyasn1_modules.rfc2251 import LDAPDN,AttributeDescription,AttributeDescriptionList,AttributeValue
  17. DEREF_CONTROL_OID = '1.3.6.1.4.1.4203.666.5.16'
  18. # Request types
  19. #---------------------------------------------------------------------------
  20. # For compatibility with ASN.1 declaration in I-D
  21. AttributeList = AttributeDescriptionList
  22. class DerefSpec(univ.Sequence):
  23. componentType = namedtype.NamedTypes(
  24. namedtype.NamedType(
  25. 'derefAttr',
  26. AttributeDescription()
  27. ),
  28. namedtype.NamedType(
  29. 'attributes',
  30. AttributeList()
  31. ),
  32. )
  33. class DerefSpecs(univ.SequenceOf):
  34. componentType = DerefSpec()
  35. # Response types
  36. #---------------------------------------------------------------------------
  37. class AttributeValues(univ.SetOf):
  38. componentType = AttributeValue()
  39. class PartialAttribute(univ.Sequence):
  40. componentType = namedtype.NamedTypes(
  41. namedtype.NamedType('type', AttributeDescription()),
  42. namedtype.NamedType('vals', AttributeValues()),
  43. )
  44. class PartialAttributeList(univ.SequenceOf):
  45. componentType = PartialAttribute()
  46. tagSet = univ.Sequence.tagSet.tagImplicitly(
  47. tag.Tag(tag.tagClassContext,tag.tagFormatConstructed,0)
  48. )
  49. class DerefRes(univ.Sequence):
  50. componentType = namedtype.NamedTypes(
  51. namedtype.NamedType('derefAttr', AttributeDescription()),
  52. namedtype.NamedType('derefVal', LDAPDN()),
  53. namedtype.OptionalNamedType('attrVals', PartialAttributeList()),
  54. )
  55. class DerefResultControlValue(univ.SequenceOf):
  56. componentType = DerefRes()
  57. class DereferenceControl(LDAPControl):
  58. controlType = DEREF_CONTROL_OID
  59. def __init__(self,criticality=False,derefSpecs=None):
  60. LDAPControl.__init__(self,self.controlType,criticality)
  61. self.derefSpecs = derefSpecs or {}
  62. def _derefSpecs(self):
  63. deref_specs = DerefSpecs()
  64. i = 0
  65. for deref_attr,deref_attribute_names in self.derefSpecs.items():
  66. deref_spec = DerefSpec()
  67. deref_attributes = AttributeList()
  68. for j in range(len(deref_attribute_names)):
  69. deref_attributes.setComponentByPosition(j,deref_attribute_names[j])
  70. deref_spec.setComponentByName('derefAttr',AttributeDescription(deref_attr))
  71. deref_spec.setComponentByName('attributes',deref_attributes)
  72. deref_specs.setComponentByPosition(i,deref_spec)
  73. i += 1
  74. return deref_specs
  75. def encodeControlValue(self):
  76. return encoder.encode(self._derefSpecs())
  77. def decodeControlValue(self,encodedControlValue):
  78. decodedValue,_ = decoder.decode(encodedControlValue,asn1Spec=DerefResultControlValue())
  79. self.derefRes = {}
  80. for deref_res in decodedValue:
  81. deref_attr,deref_val,deref_vals = deref_res[0],deref_res[1],deref_res[2]
  82. partial_attrs_dict = {
  83. str(tv[0]): [str(v) for v in tv[1]]
  84. for tv in deref_vals or []
  85. }
  86. try:
  87. self.derefRes[str(deref_attr)].append((str(deref_val),partial_attrs_dict))
  88. except KeyError:
  89. self.derefRes[str(deref_attr)] = [(str(deref_val),partial_attrs_dict)]
  90. KNOWN_RESPONSE_CONTROLS[DereferenceControl.controlType] = DereferenceControl