1 |
|
2 |
|
3 | const assert = require('assert-plus')
|
4 | const util = require('util')
|
5 |
|
6 | const asn1 = require('asn1')
|
7 |
|
8 | const LDAPMessage = require('./message')
|
9 | const Protocol = require('../protocol')
|
10 |
|
11 |
|
12 |
|
13 | const Ber = asn1.Ber
|
14 | const LDAP_BIND_SIMPLE = 'simple'
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function BindRequest (options) {
|
20 | options = options || {}
|
21 | assert.object(options)
|
22 |
|
23 | options.protocolOp = Protocol.LDAP_REQ_BIND
|
24 | LDAPMessage.call(this, options)
|
25 |
|
26 | this.version = options.version || 0x03
|
27 | this.name = options.name || null
|
28 | this.authentication = options.authentication || LDAP_BIND_SIMPLE
|
29 | this.credentials = options.credentials || ''
|
30 | }
|
31 | util.inherits(BindRequest, LDAPMessage)
|
32 | Object.defineProperties(BindRequest.prototype, {
|
33 | type: {
|
34 | get: function getType () { return 'BindRequest' },
|
35 | configurable: false
|
36 | },
|
37 | _dn: {
|
38 | get: function getDN () { return this.name },
|
39 | configurable: false
|
40 | }
|
41 | })
|
42 |
|
43 | BindRequest.prototype._parse = function (ber) {
|
44 | assert.ok(ber)
|
45 |
|
46 | this.version = ber.readInt()
|
47 | this.name = ber.readString()
|
48 |
|
49 | const t = ber.peek()
|
50 |
|
51 |
|
52 | if (t !== Ber.Context) { throw new Error('authentication 0x' + t.toString(16) + ' not supported') }
|
53 |
|
54 | this.authentication = LDAP_BIND_SIMPLE
|
55 | this.credentials = ber.readString(Ber.Context)
|
56 |
|
57 | return true
|
58 | }
|
59 |
|
60 | BindRequest.prototype._toBer = function (ber) {
|
61 | assert.ok(ber)
|
62 |
|
63 | ber.writeInt(this.version)
|
64 | ber.writeString((this.name || '').toString())
|
65 |
|
66 | ber.writeString((this.credentials || ''), Ber.Context)
|
67 |
|
68 | return ber
|
69 | }
|
70 |
|
71 | BindRequest.prototype._json = function (j) {
|
72 | assert.ok(j)
|
73 |
|
74 | j.version = this.version
|
75 | j.name = this.name
|
76 | j.authenticationType = this.authentication
|
77 | j.credentials = this.credentials
|
78 |
|
79 | return j
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 | module.exports = BindRequest
|