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.

extended.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """
  2. """
  3. # Created on 2013.05.31
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2013 - 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 pyasn1.type.univ import OctetString
  25. from pyasn1.type.base import Asn1Item
  26. from ..core.results import RESULT_CODES
  27. from ..protocol.rfc4511 import ExtendedRequest, RequestName, ResultCode, RequestValue
  28. from ..protocol.convert import referrals_to_list
  29. from ..utils.asn1 import encode
  30. from ..utils.conv import to_unicode
  31. # ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
  32. # requestName [0] LDAPOID,
  33. # requestValue [1] OCTET STRING OPTIONAL }
  34. def extended_operation(request_name,
  35. request_value=None,
  36. no_encode=None):
  37. request = ExtendedRequest()
  38. request['requestName'] = RequestName(request_name)
  39. if request_value and isinstance(request_value, Asn1Item):
  40. request['requestValue'] = RequestValue(encode(request_value))
  41. elif str is not bytes and isinstance(request_value, (bytes, bytearray)): # in Python 3 doesn't try to encode a byte value
  42. request['requestValue'] = request_value
  43. elif request_value and no_encode: # doesn't encode the value
  44. request['requestValue'] = request_value
  45. elif request_value: # tries to encode as a octet string
  46. request['requestValue'] = RequestValue(encode(OctetString(str(request_value))))
  47. # elif request_value is not None:
  48. # raise LDAPExtensionError('unable to encode value for extended operation')
  49. return request
  50. def extended_request_to_dict(request):
  51. # return {'name': str(request['requestName']), 'value': bytes(request['requestValue']) if request['requestValue'] else None}
  52. return {'name': str(request['requestName']), 'value': bytes(request['requestValue']) if 'requestValue' in request and request['requestValue'] is not None and request['requestValue'].hasValue() else None}
  53. def extended_response_to_dict(response):
  54. return {'result': int(response['resultCode']),
  55. 'dn': str(response['matchedDN']),
  56. 'message': str(response['diagnosticMessage']),
  57. 'description': ResultCode().getNamedValues().getName(response['resultCode']),
  58. 'referrals': referrals_to_list(response['referral']),
  59. 'responseName': str(response['responseName']) if response['responseName'] else None,
  60. 'responseValue': bytes(response['responseValue']) if response['responseValue'] is not None and response['responseValue'].hasValue() else bytes()}
  61. def intermediate_response_to_dict(response):
  62. return {'responseName': str(response['responseName']),
  63. 'responseValue': bytes(response['responseValue']) if response['responseValue'] else bytes()}
  64. def extended_response_to_dict_fast(response):
  65. response_dict = dict()
  66. response_dict['result'] = int(response[0][3]) # resultCode
  67. response_dict['description'] = RESULT_CODES[response_dict['result']]
  68. response_dict['dn'] = to_unicode(response[1][3], from_server=True) # matchedDN
  69. response_dict['message'] = to_unicode(response[2][3], from_server=True) # diagnosticMessage
  70. response_dict['referrals'] = None # referrals
  71. response_dict['responseName'] = None # referrals
  72. response_dict['responseValue'] = None # responseValue
  73. for r in response[3:]:
  74. if r[2] == 3: # referrals
  75. response_dict['referrals'] = referrals_to_list(r[3]) # referrals
  76. elif r[2] == 10: # responseName
  77. response_dict['responseName'] = to_unicode(r[3], from_server=True)
  78. response_dict['responseValue'] = b'' # responseValue could be empty
  79. else: # responseValue (11)
  80. response_dict['responseValue'] = bytes(r[3])
  81. return response_dict
  82. def intermediate_response_to_dict_fast(response):
  83. response_dict = dict()
  84. for r in response:
  85. if r[2] == 0: # responseName
  86. response_dict['responseName'] = to_unicode(r[3], from_server=True)
  87. else: # responseValue (1)
  88. response_dict['responseValue'] = bytes(r[3])
  89. return response_dict