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.

operation.py 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. """
  3. # Created on 2014.07.04
  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 ..core.results import RESULT_SUCCESS
  25. from ..core.exceptions import LDAPExtensionError
  26. from ..utils.asn1 import decoder
  27. class ExtendedOperation(object):
  28. def __init__(self, connection, controls=None):
  29. self.connection = connection
  30. self.decoded_response = None
  31. self.result = None
  32. self.asn1_spec = None # if None the response_value is returned without encoding
  33. self.request_name = None
  34. self.response_name = None
  35. self.request_value = None
  36. self.response_value = None
  37. self.response_attribute = None
  38. self.controls = controls
  39. self.config()
  40. def send(self):
  41. if self.connection.check_names and self.connection.server.info is not None and self.connection.server.info.supported_extensions is not None: # checks if extension is supported
  42. for request_name in self.connection.server.info.supported_extensions:
  43. if request_name[0] == self.request_name:
  44. break
  45. else:
  46. raise LDAPExtensionError('extension not in DSA list of supported extensions')
  47. resp = self.connection.extended(self.request_name, self.request_value, self.controls)
  48. if not self.connection.strategy.sync:
  49. _, self.result = self.connection.get_response(resp)
  50. else:
  51. self.result = self.connection.result
  52. self.decode_response()
  53. self.populate_result()
  54. self.set_response()
  55. return self.response_value
  56. def populate_result(self):
  57. pass
  58. def decode_response(self):
  59. if not self.result:
  60. return None
  61. if self.result['result'] not in [RESULT_SUCCESS]:
  62. if self.connection.raise_exceptions:
  63. raise LDAPExtensionError('extended operation error: ' + self.result['description'] + ' - ' + self.result['message'])
  64. else:
  65. return None
  66. if not self.response_name or self.result['responseName'] == self.response_name:
  67. if self.result['responseValue']:
  68. if self.asn1_spec is not None:
  69. decoded, unprocessed = decoder.decode(self.result['responseValue'], asn1Spec=self.asn1_spec)
  70. if unprocessed:
  71. raise LDAPExtensionError('error decoding extended response value')
  72. self.decoded_response = decoded
  73. else:
  74. self.decoded_response = self.result['responseValue']
  75. else:
  76. raise LDAPExtensionError('invalid response name received')
  77. def set_response(self):
  78. self.response_value = self.result[self.response_attribute] if self.result and self.response_attribute in self.result else None
  79. self.connection.response = self.response_value
  80. def config(self):
  81. pass