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.

ppolicy.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. """
  3. ldap.controls.ppolicy - classes for Password Policy controls
  4. (see https://tools.ietf.org/html/draft-behera-ldap-password-policy)
  5. See https://www.python-ldap.org/ for project details.
  6. """
  7. __all__ = [
  8. 'PasswordPolicyControl'
  9. ]
  10. # Imports from python-ldap 2.4+
  11. from ldap.controls import (
  12. ResponseControl, ValueLessRequestControl, KNOWN_RESPONSE_CONTROLS
  13. )
  14. # Imports from pyasn1
  15. from pyasn1.type import tag,namedtype,namedval,univ,constraint
  16. from pyasn1.codec.der import decoder
  17. class PasswordPolicyWarning(univ.Choice):
  18. componentType = namedtype.NamedTypes(
  19. namedtype.NamedType('timeBeforeExpiration',univ.Integer().subtype(
  20. implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,0)
  21. )),
  22. namedtype.NamedType('graceAuthNsRemaining',univ.Integer().subtype(
  23. implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,1)
  24. )),
  25. )
  26. class PasswordPolicyError(univ.Enumerated):
  27. namedValues = namedval.NamedValues(
  28. ('passwordExpired',0),
  29. ('accountLocked',1),
  30. ('changeAfterReset',2),
  31. ('passwordModNotAllowed',3),
  32. ('mustSupplyOldPassword',4),
  33. ('insufficientPasswordQuality',5),
  34. ('passwordTooShort',6),
  35. ('passwordTooYoung',7),
  36. ('passwordInHistory',8)
  37. )
  38. subtypeSpec = univ.Enumerated.subtypeSpec + constraint.SingleValueConstraint(0,1,2,3,4,5,6,7,8)
  39. class PasswordPolicyResponseValue(univ.Sequence):
  40. componentType = namedtype.NamedTypes(
  41. namedtype.OptionalNamedType(
  42. 'warning',
  43. PasswordPolicyWarning().subtype(
  44. implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,0)
  45. ),
  46. ),
  47. namedtype.OptionalNamedType(
  48. 'error',PasswordPolicyError().subtype(
  49. implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,1)
  50. )
  51. ),
  52. )
  53. class PasswordPolicyControl(ValueLessRequestControl,ResponseControl):
  54. controlType = '1.3.6.1.4.1.42.2.27.8.5.1'
  55. def __init__(self,criticality=False):
  56. self.criticality = criticality
  57. def decodeControlValue(self,encodedControlValue):
  58. ppolicyValue,_ = decoder.decode(encodedControlValue,asn1Spec=PasswordPolicyResponseValue())
  59. self.timeBeforeExpiration = None
  60. self.graceAuthNsRemaining = None
  61. self.error = None
  62. warning = ppolicyValue.getComponentByName('warning')
  63. if warning.hasValue():
  64. if 'timeBeforeExpiration' in warning:
  65. self.timeBeforeExpiration = int(
  66. warning.getComponentByName('timeBeforeExpiration'))
  67. if 'graceAuthNsRemaining' in warning:
  68. self.graceAuthNsRemaining = int(
  69. warning.getComponentByName('graceAuthNsRemaining'))
  70. error = ppolicyValue.getComponentByName('error')
  71. if error.hasValue():
  72. self.error = int(error)
  73. KNOWN_RESPONSE_CONTROLS[PasswordPolicyControl.controlType] = PasswordPolicyControl