This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparse-to-message.js
205 lines (172 loc) · 4.83 KB
/
parse-to-message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict'
const { operations } = require('@ldapjs/protocol')
const { getControl } = require('@ldapjs/controls')
const messageClasses = {
AbandonRequest: require('./messages/abandon-request'),
AddRequest: require('./messages/add-request'),
BindRequest: require('./messages/bind-request'),
CompareRequest: require('./messages/compare-request'),
DeleteRequest: require('./messages/delete-request'),
ExtensionRequest: require('./messages/extension-request'),
ModifyRequest: require('./messages/modify-request'),
ModifyDnRequest: require('./messages/modifydn-request'),
SearchRequest: require('./messages/search-request'),
UnbindRequest: require('./messages/unbind-request'),
AbandonResponse: require('./messages/abandon-response'),
AddResponse: require('./messages/add-response'),
BindResponse: require('./messages/bind-response'),
CompareResponse: require('./messages/compare-response'),
DeleteResponse: require('./messages/delete-response'),
ExtensionResponse: require('./messages/extension-response'),
ModifyResponse: require('./messages/modify-response'),
ModifyDnResponse: require('./messages/modifydn-response'),
// Search result messages.
SearchResultEntry: require('./messages/search-result-entry'),
SearchResultReference: require('./messages/search-result-reference'),
SearchResultDone: require('./messages/search-result-done'),
// Miscellaneous messages.
IntermediateResponse: require('./messages/intermediate-response')
}
/**
* Utility function that inspects a BER object and parses it into an instance
* of a specific LDAP message.
*
* @param {import('@ldapjs/asn1').BerReader} ber An object that represents a
* full LDAP Message sequence as described in
* https://www.rfc-editor.org/rfc/rfc4511.html#section-4.1.1.
*
* @returns {LdapMessage} Some specific instance of the base LDAP Message
* type.
*
* @throws When the input data is malformed.
*/
module.exports = function parseToMessage (ber) {
const inputType = Object.prototype.toString.apply(ber)
if (inputType !== '[object BerReader]') {
throw TypeError(`Expected BerReader but got ${inputType}.`)
}
ber.readSequence()
const messageId = ber.readInt()
const messageType = identifyType(ber)
const MessageClass = messageClasses[messageType]
const pojoMessage = MessageClass.parseToPojo(ber)
const message = new MessageClass({
messageId,
...pojoMessage
})
// Look for controls
if (ber.peek() === 0xa0) {
ber.readSequence()
const end = ber.offset + ber.length
while (ber.offset < end) {
const c = getControl(ber)
/* istanbul ignore else */
if (c) {
message.addControl(c)
}
}
}
return message
}
/**
* Determines the type of LDAP message the BER represents, e.g. a "Bind Request"
* message.
*
* @param {BerReader} ber
*
* @returns {string}
*/
function identifyType (ber) {
let result
switch (ber.peek()) {
case operations.LDAP_REQ_ABANDON: {
result = 'AbandonRequest'
break
}
case 0x00: {
result = 'AbandonResponse'
break
}
case operations.LDAP_REQ_ADD: {
result = 'AddRequest'
break
}
case operations.LDAP_RES_ADD: {
result = 'AddResponse'
break
}
case operations.LDAP_REQ_BIND: {
result = 'BindRequest'
break
}
case operations.LDAP_RES_BIND: {
result = 'BindResponse'
break
}
case operations.LDAP_REQ_COMPARE: {
result = 'CompareRequest'
break
}
case operations.LDAP_RES_COMPARE: {
result = 'CompareResponse'
break
}
case operations.LDAP_REQ_DELETE: {
result = 'DeleteRequest'
break
}
case operations.LDAP_RES_DELETE: {
result = 'DeleteResponse'
break
}
case operations.LDAP_REQ_EXTENSION: {
result = 'ExtensionRequest'
break
}
case operations.LDAP_RES_EXTENSION: {
result = 'ExtensionResponse'
break
}
case operations.LDAP_REQ_MODIFY: {
result = 'ModifyRequest'
break
}
case operations.LDAP_RES_MODIFY: {
result = 'ModifyResponse'
break
}
case operations.LDAP_REQ_MODRDN: {
result = 'ModifyDnRequest'
break
}
case operations.LDAP_RES_MODRDN: {
result = 'ModifyDnResponse'
break
}
case operations.LDAP_REQ_SEARCH: {
result = 'SearchRequest'
break
}
case operations.LDAP_RES_SEARCH_ENTRY: {
result = 'SearchResultEntry'
break
}
case operations.LDAP_RES_SEARCH_REF: {
result = 'SearchResultReference'
break
}
case operations.LDAP_RES_SEARCH_DONE: {
result = 'SearchResultDone'
break
}
case operations.LDAP_REQ_UNBIND: {
result = 'UnbindRequest'
break
}
case operations.LDAP_RES_INTERMEDIATE: {
result = 'IntermediateResponse'
break
}
}
return result
}