compare-request.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. 'use strict'
  2. const { operations } = require('@ldapjs/protocol')
  3. const { DN } = require('@ldapjs/dn')
  4. const LdapMessage = require('../ldap-message')
  5. /**
  6. * Implements the compare request message as described in
  7. * https://www.rfc-editor.org/rfc/rfc4511.html#section-4.10.
  8. */
  9. class CompareRequest extends LdapMessage {
  10. #attribute
  11. #entry
  12. #value
  13. /**
  14. * @typedef {LdapMessageOptions} CompareRequestOptions
  15. * @property {string|null} [attribute] The attribute name to compare
  16. * against.
  17. * @property {string} [entry] The target LDAP entity whose attribute
  18. * will be compared.
  19. * @property {string} [value] The value of the attribute to compare.
  20. */
  21. /**
  22. * @param {CompareRequestOptions} [options]
  23. */
  24. constructor (options = {}) {
  25. options.protocolOp = operations.LDAP_REQ_COMPARE
  26. super(options)
  27. this.attribute = options.attribute || ''
  28. this.entry = options.entry || null
  29. this.value = options.value || ''
  30. }
  31. /**
  32. * The property of an LDAP entry to compare against.
  33. *
  34. * @returns {string}
  35. */
  36. get attribute () {
  37. return this.#attribute
  38. }
  39. /**
  40. * Define the LDAP entry property to compare against.
  41. *
  42. * @param {string} value
  43. */
  44. set attribute (value) {
  45. this.#attribute = value
  46. }
  47. /**
  48. * The LDAP entry that will be inspected.
  49. *
  50. * @returns {string | null}
  51. */
  52. get entry () {
  53. return this.#entry ?? null
  54. }
  55. /**
  56. * Define the LDAP entity to inspect.
  57. *
  58. * @param {string | null} value
  59. */
  60. set entry (value) {
  61. if (value === null) return
  62. if (typeof value === 'string') {
  63. this.#entry = DN.fromString(value)
  64. } else if (Object.prototype.toString.call(value) === '[object LdapDn]') {
  65. this.#entry = value
  66. } else {
  67. throw Error('entry must be a valid DN string or instance of LdapDn')
  68. }
  69. }
  70. /**
  71. * The name of the request type.
  72. *
  73. * @type {string}
  74. */
  75. get type () {
  76. return 'CompareRequest'
  77. }
  78. /**
  79. * The value the attribute should be set to.
  80. *
  81. * @returns {string}
  82. */
  83. get value () {
  84. return this.#value
  85. }
  86. /**
  87. * Define the value the attribute should match.
  88. *
  89. * @param {string} value
  90. */
  91. set value (value) {
  92. this.#value = value
  93. }
  94. get _dn () {
  95. return this.#entry
  96. }
  97. /**
  98. * Internal use only.
  99. *
  100. * @param {import('@ldapjs/asn1').BerWriter} ber
  101. *
  102. * @returns {import('@ldapjs/asn1').BerWriter}
  103. */
  104. _toBer (ber) {
  105. ber.startSequence(operations.LDAP_REQ_COMPARE)
  106. ber.writeString(this.#entry.toString())
  107. ber.startSequence()
  108. ber.writeString(this.#attribute)
  109. ber.writeString(this.#value)
  110. ber.endSequence()
  111. ber.endSequence()
  112. return ber
  113. }
  114. /**
  115. * Internal use only.
  116. *
  117. * @param {object}
  118. *
  119. * @returns {object}
  120. */
  121. _pojo (obj = {}) {
  122. obj.attribute = this.#attribute
  123. obj.entry = this.#entry ? this.#entry.toString() : null
  124. obj.value = this.#value
  125. return obj
  126. }
  127. /**
  128. * Implements the standardized `parseToPojo` method.
  129. *
  130. * @see LdapMessage.parseToPojo
  131. *
  132. * @param {import('@ldapjs/asn1').BerReader} ber
  133. */
  134. static parseToPojo (ber) {
  135. const protocolOp = ber.readSequence()
  136. if (protocolOp !== operations.LDAP_REQ_COMPARE) {
  137. const op = protocolOp.toString(16).padStart(2, '0')
  138. throw Error(`found wrong protocol operation: 0x${op}`)
  139. }
  140. const entry = ber.readString()
  141. ber.readSequence()
  142. const attribute = ber.readString()
  143. const value = ber.readString()
  144. return {
  145. protocolOp,
  146. entry,
  147. attribute,
  148. value
  149. }
  150. }
  151. }
  152. module.exports = CompareRequest