saslwrapper.pyx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2015 Cloudera Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #cython: language_level=3
  15. from six import string_types, PY3
  16. from libcpp cimport bool
  17. from libc.stdint cimport uint32_t
  18. from libcpp.string cimport string as string_t
  19. cdef extern from 'saslwrapper.h' namespace 'saslwrapper':
  20. cdef cppclass ClientImpl:
  21. ClientImpl() except +
  22. bool setAttr(const string_t& key, const string_t& value)
  23. bool setAttr(const string_t& key, uint32_t value)
  24. bool init()
  25. bool start(const string_t& mechList, string_t& chosen, string_t& initialResponse)
  26. bool step(const string_t& challenge, string_t& response)
  27. bool encode(const string_t& clearText, string_t& cipherText)
  28. bool decode(const string_t& cipherText, string_t& clearText)
  29. bool getUserId(string_t& userId)
  30. bool getSSF(int *ssf)
  31. void getError(string_t& error)
  32. cpdef string_t to_bytes(bytes_or_str):
  33. if PY3 and isinstance(bytes_or_str, string_types):
  34. return bytes_or_str.encode('utf-8')
  35. return bytes_or_str
  36. cpdef to_string(bytes_or_str):
  37. if isinstance(bytes_or_str, bytes):
  38. return bytes_or_str.decode('utf-8')
  39. return bytes_or_str
  40. cdef class Client:
  41. cdef ClientImpl _this
  42. cpdef setAttr(self, key, value):
  43. if isinstance(value, int):
  44. return self._this.setAttr(to_bytes(key), <uint32_t> value)
  45. elif isinstance(value, string_types):
  46. return self._this.setAttr(to_bytes(key), <string_t> to_bytes(value))
  47. cpdef init(self):
  48. return self._this.init()
  49. cpdef start(self, mech_list):
  50. cdef string_t chosen
  51. cdef string_t initial_response
  52. success = self._this.start(to_bytes(mech_list), chosen, initial_response)
  53. return (success, chosen, initial_response)
  54. cpdef step(self, challenge):
  55. cdef string_t response
  56. success = self._this.step(to_bytes(challenge), response)
  57. return (success, response)
  58. cpdef encode(self, clear_text):
  59. cdef string_t cipher_text
  60. success = self._this.encode(to_bytes(clear_text), cipher_text)
  61. return (success, cipher_text)
  62. cpdef decode(self, cipher_text):
  63. cdef string_t clear_text
  64. success = self._this.decode(to_bytes(cipher_text), clear_text)
  65. return (success, clear_text)
  66. cpdef getUserId(self):
  67. cdef string_t user_id
  68. success = self._this.getUserId(user_id)
  69. return (success, user_id)
  70. cpdef getSSF(self):
  71. cdef int ssf
  72. success = self._this.getSSF(&ssf)
  73. return (success, ssf)
  74. cpdef getError(self):
  75. cdef string_t error
  76. self._this.getError(error)
  77. return error