UNPKG

1.89 kBJavaScriptView Raw
1// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2
3const assert = require('assert-plus')
4const util = require('util')
5
6const asn1 = require('asn1')
7
8const LDAPMessage = require('./message')
9const Protocol = require('../protocol')
10
11/// --- Globals
12
13const Ber = asn1.Ber
14const LDAP_BIND_SIMPLE = 'simple'
15// var LDAP_BIND_SASL = 'sasl'
16
17/// --- API
18
19function 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}
31util.inherits(BindRequest, LDAPMessage)
32Object.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
43BindRequest.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 // TODO add support for SASL et al
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
60BindRequest.prototype._toBer = function (ber) {
61 assert.ok(ber)
62
63 ber.writeInt(this.version)
64 ber.writeString((this.name || '').toString())
65 // TODO add support for SASL et al
66 ber.writeString((this.credentials || ''), Ber.Context)
67
68 return ber
69}
70
71BindRequest.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/// --- Exports
83
84module.exports = BindRequest