UNPKG

2.77 kBJavaScriptView Raw
1var assert = require('assert-plus');
2var util = require('util');
3
4var asn1 = require('asn1');
5
6var Control = require('./control');
7var CODES = require('../errors/codes');
8
9
10///--- Globals
11
12var BerReader = asn1.BerReader;
13var BerWriter = asn1.BerWriter;
14
15var VALID_CODES = [
16 CODES.LDAP_SUCCESS,
17 CODES.LDAP_OPERATIONS_ERROR,
18 CODES.LDAP_TIME_LIMIT_EXCEEDED,
19 CODES.LDAP_STRONG_AUTH_REQUIRED,
20 CODES.LDAP_ADMIN_LIMIT_EXCEEDED,
21 CODES.LDAP_NO_SUCH_ATTRIBUTE,
22 CODES.LDAP_INAPPROPRIATE_MATCHING,
23 CODES.LDAP_INSUFFICIENT_ACCESS_RIGHTS,
24 CODES.LDAP_BUSY,
25 CODES.LDAP_UNWILLING_TO_PERFORM,
26 CODES.LDAP_OTHER
27];
28
29function ServerSideSortingResponseControl(options) {
30 assert.optionalObject(options);
31 options = options || {};
32 options.type = ServerSideSortingResponseControl.OID;
33 options.criticality = false;
34
35 if (options.value) {
36 if (Buffer.isBuffer(options.value)) {
37 this.parse(options.value);
38 } else if (typeof (options.value) === 'object') {
39 if (VALID_CODES.indexOf(options.value.result) === -1) {
40 throw new Error('Invalid result code');
41 }
42 if (options.value.failedAttribute &&
43 typeof (options.value.failedAttribute) !== 'string') {
44 throw new Error('failedAttribute must be String');
45 }
46
47 this._value = options.value;
48 } else {
49 throw new TypeError('options.value must be a Buffer or Object');
50 }
51 options.value = null;
52 }
53 Control.call(this, options);
54}
55util.inherits(ServerSideSortingResponseControl, Control);
56Object.defineProperties(ServerSideSortingResponseControl.prototype, {
57 value: {
58 get: function () { return this._value || {}; },
59 configurable: false
60 }
61});
62
63ServerSideSortingResponseControl.prototype.parse = function parse(buffer) {
64 assert.ok(buffer);
65
66 var ber = new BerReader(buffer);
67 if (ber.readSequence(0x30)) {
68 this._value = {};
69 this._value.result = ber.readEnumeration();
70 if (ber.peek() == 0x80) {
71 this._value.failedAttribute = ber.readString(0x80);
72 }
73 return true;
74 }
75 return false;
76};
77
78ServerSideSortingResponseControl.prototype._toBer = function (ber) {
79 assert.ok(ber);
80
81 if (!this._value || this.value.length === 0)
82 return;
83
84 var writer = new BerWriter();
85 writer.startSequence(0x30);
86 writer.writeEnumeration(this.value.result);
87 if (this.value.result !== CODES.LDAP_SUCCESS && this.value.failedAttribute) {
88 writer.writeString(this.value.failedAttribute, 0x80);
89 }
90 writer.endSequence();
91 ber.writeBuffer(writer.buffer, 0x04);
92};
93
94ServerSideSortingResponseControl.prototype._json = function (obj) {
95 obj.controlValue = this.value;
96 return obj;
97};
98
99ServerSideSortingResponseControl.OID = '1.2.840.113556.1.4.474';
100
101
102///--- Exports
103module.exports = ServerSideSortingResponseControl;