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.

modifyPassword.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. """
  3. # Created on 2014.04.30
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2014 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from ... import HASHED_NONE
  25. from ...extend.operation import ExtendedOperation
  26. from ...protocol.rfc3062 import PasswdModifyRequestValue, PasswdModifyResponseValue
  27. from ...utils.hashed import hashed
  28. from ...protocol.sasl.sasl import validate_simple_password
  29. from ...utils.dn import safe_dn
  30. from ...core.results import RESULT_SUCCESS
  31. # implements RFC3062
  32. class ModifyPassword(ExtendedOperation):
  33. def config(self):
  34. self.request_name = '1.3.6.1.4.1.4203.1.11.1'
  35. self.request_value = PasswdModifyRequestValue()
  36. self.asn1_spec = PasswdModifyResponseValue()
  37. self.response_attribute = 'new_password'
  38. def __init__(self, connection, user=None, old_password=None, new_password=None, hash_algorithm=None, salt=None, controls=None):
  39. ExtendedOperation.__init__(self, connection, controls) # calls super __init__()
  40. if user:
  41. if connection.check_names:
  42. user = safe_dn(user)
  43. self.request_value['userIdentity'] = user
  44. if old_password:
  45. if not isinstance(old_password, bytes): # bytes are returned raw, as per RFC (4.2)
  46. old_password = validate_simple_password(old_password, True)
  47. self.request_value['oldPasswd'] = old_password
  48. if new_password:
  49. if not isinstance(new_password, bytes): # bytes are returned raw, as per RFC (4.2)
  50. new_password = validate_simple_password(new_password, True)
  51. if hash_algorithm is None or hash_algorithm == HASHED_NONE:
  52. self.request_value['newPasswd'] = new_password
  53. else:
  54. self.request_value['newPasswd'] = hashed(hash_algorithm, new_password, salt)
  55. def populate_result(self):
  56. try:
  57. self.result[self.response_attribute] = str(self.decoded_response['genPasswd'])
  58. except TypeError: # optional field can be absent, so returns True if operation is successful else False
  59. if self.result['result'] == RESULT_SUCCESS:
  60. self.result[self.response_attribute] = True
  61. else: # change was not successful, raises exception if raise_exception = True in connection or returns the operation result, error code is in result['result']
  62. self.result[self.response_attribute] = False
  63. if not self.connection.raise_exceptions:
  64. from ...core.exceptions import LDAPOperationResult
  65. raise LDAPOperationResult(result=self.result['result'], description=self.result['description'], dn=self.result['dn'], message=self.result['message'], response_type=self.result['type'])