2011-08-04 20:32:01 +00:00
|
|
|
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const assert = require('assert-plus')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Helpers
|
2015-11-03 04:54:35 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function invalidDN (name) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const e = new Error()
|
2019-08-27 21:11:49 +00:00
|
|
|
e.name = 'InvalidDistinguishedNameError'
|
|
|
|
e.message = name
|
|
|
|
return e
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function isAlphaNumeric (c) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const re = /[A-Za-z0-9]/
|
2019-08-27 21:11:49 +00:00
|
|
|
return re.test(c)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function isWhitespace (c) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const re = /\s/
|
2019-08-27 21:11:49 +00:00
|
|
|
return re.test(c)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function repeatChar (c, n) {
|
2020-10-31 21:07:32 +00:00
|
|
|
let out = ''
|
|
|
|
const max = n || 0
|
|
|
|
for (let i = 0; i < max; i++) { out += c }
|
2019-08-27 21:11:49 +00:00
|
|
|
return out
|
2015-11-03 04:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- API
|
2015-11-03 04:54:35 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function RDN (obj) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
2019-08-27 21:11:49 +00:00
|
|
|
this.attrs = {}
|
2011-08-30 18:12:34 +00:00
|
|
|
|
|
|
|
if (obj) {
|
2012-02-18 08:15:52 +00:00
|
|
|
Object.keys(obj).forEach(function (k) {
|
2019-08-27 21:11:49 +00:00
|
|
|
self.set(k, obj[k])
|
|
|
|
})
|
2011-08-30 18:12:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
RDN.prototype.set = function rdnSet (name, value, opts) {
|
|
|
|
assert.string(name, 'name (string) required')
|
|
|
|
assert.string(value, 'value (string) required')
|
2011-08-30 18:12:34 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
|
|
|
const lname = name.toLowerCase()
|
2015-02-16 20:53:42 +00:00
|
|
|
this.attrs[lname] = {
|
|
|
|
value: value,
|
|
|
|
name: name
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2015-02-16 20:53:42 +00:00
|
|
|
if (opts && typeof (opts) === 'object') {
|
|
|
|
Object.keys(opts).forEach(function (k) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (k !== 'value') { self.attrs[lname][k] = opts[k] }
|
|
|
|
})
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2015-02-16 20:53:42 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
RDN.prototype.equals = function rdnEquals (rdn) {
|
|
|
|
if (typeof (rdn) !== 'object') { return false }
|
2015-02-16 20:53:42 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const ourKeys = Object.keys(this.attrs)
|
|
|
|
const theirKeys = Object.keys(rdn.attrs)
|
2019-08-27 21:11:49 +00:00
|
|
|
if (ourKeys.length !== theirKeys.length) { return false }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ourKeys.sort()
|
|
|
|
theirKeys.sort()
|
2015-02-16 20:53:42 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
for (let i = 0; i < ourKeys.length; i++) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (ourKeys[i] !== theirKeys[i]) { return false }
|
|
|
|
if (this.attrs[ourKeys[i]].value !== rdn.attrs[ourKeys[i]].value) { return false }
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
|
|
|
}
|
2015-02-16 20:53:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert RDN to string according to specified formatting options.
|
|
|
|
* (see: DN.format for option details)
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
RDN.prototype.format = function rdnFormat (options) {
|
|
|
|
assert.optionalObject(options, 'options must be an object')
|
|
|
|
options = options || {}
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
|
|
|
let str = ''
|
2019-08-27 21:11:49 +00:00
|
|
|
|
|
|
|
function escapeValue (val, forceQuote) {
|
2020-10-31 21:07:32 +00:00
|
|
|
let out = ''
|
|
|
|
let cur = 0
|
|
|
|
const len = val.length
|
|
|
|
let quoted = false
|
2015-02-16 20:53:42 +00:00
|
|
|
/* BEGIN JSSTYLED */
|
2019-08-27 21:11:49 +00:00
|
|
|
// TODO: figure out what this regex is actually trying to test for and
|
|
|
|
// fix it to appease the linter.
|
|
|
|
/* eslint-disable-next-line no-useless-escape */
|
2020-10-31 21:07:32 +00:00
|
|
|
const escaped = /[\\\"]/
|
|
|
|
const special = /[,=+<>#;]/
|
2015-02-16 20:53:42 +00:00
|
|
|
/* END JSSTYLED */
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
// Wrap strings with trailing or leading spaces in quotes
|
2019-08-27 21:11:49 +00:00
|
|
|
quoted = forceQuote || (val[0] === ' ' || val[len - 1] === ' ')
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (cur < len) {
|
|
|
|
if (escaped.test(val[cur]) || (!quoted && special.test(val[cur]))) {
|
2019-08-27 21:11:49 +00:00
|
|
|
out += '\\'
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
out += val[cur++]
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
if (quoted) { out = '"' + out + '"' }
|
|
|
|
return out
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
function sortParsed (a, b) {
|
|
|
|
return self.attrs[a].order - self.attrs[b].order
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
function sortStandard (a, b) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const nameCompare = a.localeCompare(b)
|
2015-02-16 20:53:42 +00:00
|
|
|
if (nameCompare === 0) {
|
|
|
|
// TODO: Handle binary values
|
2019-08-27 21:11:49 +00:00
|
|
|
return self.attrs[a].value.localeCompare(self.attrs[b].value)
|
2015-02-16 20:53:42 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
return nameCompare
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const keys = Object.keys(this.attrs)
|
2015-02-16 20:53:42 +00:00
|
|
|
if (options.keepOrder) {
|
2019-08-27 21:11:49 +00:00
|
|
|
keys.sort(sortParsed)
|
2015-02-16 20:53:42 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
keys.sort(sortStandard)
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keys.forEach(function (key) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const attr = self.attrs[key]
|
2019-08-27 21:11:49 +00:00
|
|
|
if (str.length) { str += '+' }
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2015-02-16 20:53:42 +00:00
|
|
|
if (options.keepCase) {
|
2019-08-27 21:11:49 +00:00
|
|
|
str += attr.name
|
2015-02-16 20:53:42 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (options.upperName) { str += key.toUpperCase() } else { str += key }
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
str += '=' + escapeValue(attr.value, (options.keepQuote && attr.quoted))
|
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return str
|
|
|
|
}
|
2015-02-16 20:53:42 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
RDN.prototype.toString = function rdnToString () {
|
|
|
|
return this.format()
|
|
|
|
}
|
2015-02-16 20:53:42 +00:00
|
|
|
|
2011-08-10 17:57:58 +00:00
|
|
|
// Thank you OpenJDK!
|
2019-08-27 21:11:49 +00:00
|
|
|
function parse (name) {
|
|
|
|
if (typeof (name) !== 'string') { throw new TypeError('name (string) required') }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
let cur = 0
|
|
|
|
const len = name.length
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function parseRdn () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const rdn = new RDN()
|
|
|
|
let order = 0
|
2019-08-27 21:11:49 +00:00
|
|
|
rdn.spLead = trim()
|
2011-08-04 20:32:01 +00:00
|
|
|
while (cur < len) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const opts = {
|
2015-02-16 20:53:42 +00:00
|
|
|
order: order
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2020-10-31 21:07:32 +00:00
|
|
|
const attr = parseAttrType()
|
2019-08-27 21:11:49 +00:00
|
|
|
trim()
|
|
|
|
if (cur >= len || name[cur++] !== '=') { throw invalidDN(name) }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
trim()
|
2015-02-16 20:53:42 +00:00
|
|
|
// Parameters about RDN value are set in 'opts' by parseAttrValue
|
2020-10-31 21:07:32 +00:00
|
|
|
const value = parseAttrValue(opts)
|
2019-08-27 21:11:49 +00:00
|
|
|
rdn.set(attr, value, opts)
|
|
|
|
rdn.spTrail = trim()
|
|
|
|
if (cur >= len || name[cur] !== '+') { break }
|
|
|
|
++cur
|
|
|
|
++order
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return rdn
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function trim () {
|
2020-10-31 21:07:32 +00:00
|
|
|
let count = 0
|
2013-10-24 23:27:42 +00:00
|
|
|
while ((cur < len) && isWhitespace(name[cur])) {
|
2019-08-27 21:11:49 +00:00
|
|
|
++cur
|
|
|
|
count++
|
2013-10-24 23:27:42 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return count
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function parseAttrType () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const beg = cur
|
2011-08-04 20:32:01 +00:00
|
|
|
while (cur < len) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const c = name[cur]
|
2011-08-04 20:32:01 +00:00
|
|
|
if (isAlphaNumeric(c) ||
|
2019-08-27 21:11:49 +00:00
|
|
|
c === '.' ||
|
|
|
|
c === '-' ||
|
|
|
|
c === ' ') {
|
|
|
|
++cur
|
2011-08-04 20:32:01 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
break
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Back out any trailing spaces.
|
2019-08-27 21:11:49 +00:00
|
|
|
while ((cur > beg) && (name[cur - 1] === ' ')) { --cur }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (beg === cur) { throw invalidDN(name) }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return name.slice(beg, cur)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function parseAttrValue (opts) {
|
|
|
|
if (cur < len && name[cur] === '#') {
|
|
|
|
opts.binary = true
|
|
|
|
return parseBinaryAttrValue()
|
|
|
|
} else if (cur < len && name[cur] === '"') {
|
|
|
|
opts.quoted = true
|
|
|
|
return parseQuotedAttrValue()
|
2011-08-04 20:32:01 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
return parseStringAttrValue()
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function parseBinaryAttrValue () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const beg = cur++
|
2019-08-27 21:11:49 +00:00
|
|
|
while (cur < len && isAlphaNumeric(name[cur])) { ++cur }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return name.slice(beg, cur)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function parseQuotedAttrValue () {
|
2020-10-31 21:07:32 +00:00
|
|
|
let str = ''
|
2019-08-27 21:11:49 +00:00
|
|
|
++cur // Consume the first quote
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
while ((cur < len) && name[cur] !== '"') {
|
|
|
|
if (name[cur] === '\\') { cur++ }
|
|
|
|
str += name[cur++]
|
|
|
|
}
|
|
|
|
if (cur++ >= len) {
|
|
|
|
// no closing quote
|
|
|
|
throw invalidDN(name)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return str
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function parseStringAttrValue () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const beg = cur
|
|
|
|
let str = ''
|
|
|
|
let esc = -1
|
2011-08-04 20:32:01 +00:00
|
|
|
|
|
|
|
while ((cur < len) && !atTerminator()) {
|
|
|
|
if (name[cur] === '\\') {
|
2015-02-16 20:53:42 +00:00
|
|
|
// Consume the backslash and mark its place just in case it's escaping
|
|
|
|
// whitespace which needs to be preserved.
|
2019-08-27 21:11:49 +00:00
|
|
|
esc = cur++
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
if (cur === len) {
|
|
|
|
// backslash followed by nothing
|
|
|
|
throw invalidDN(name)
|
|
|
|
}
|
|
|
|
str += name[cur++]
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2015-02-16 20:53:42 +00:00
|
|
|
// Trim off (unescaped) trailing whitespace and rewind cursor to the end of
|
|
|
|
// the AttrValue to record whitespace length.
|
|
|
|
for (; cur > beg; cur--) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!isWhitespace(name[cur - 1]) || (esc === (cur - 1))) { break }
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return str.slice(0, cur - beg)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function atTerminator () {
|
2011-08-04 20:32:01 +00:00
|
|
|
return (cur < len &&
|
|
|
|
(name[cur] === ',' ||
|
|
|
|
name[cur] === ';' ||
|
2019-08-27 21:11:49 +00:00
|
|
|
name[cur] === '+'))
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const rdns = []
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2015-02-16 20:53:42 +00:00
|
|
|
// Short-circuit for empty DNs
|
2019-08-27 21:11:49 +00:00
|
|
|
if (len === 0) { return new DN(rdns) }
|
2015-02-16 20:53:42 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
rdns.push(parseRdn())
|
2011-08-04 20:32:01 +00:00
|
|
|
while (cur < len) {
|
|
|
|
if (name[cur] === ',' || name[cur] === ';') {
|
2019-08-27 21:11:49 +00:00
|
|
|
++cur
|
|
|
|
rdns.push(parseRdn())
|
2011-08-04 20:32:01 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
throw invalidDN(name)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return new DN(rdns)
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function DN (rdns) {
|
|
|
|
assert.optionalArrayOfObject(rdns, '[object] required')
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.rdns = rdns ? rdns.slice() : []
|
|
|
|
this._format = {}
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
2015-11-03 04:54:35 +00:00
|
|
|
Object.defineProperties(DN.prototype, {
|
|
|
|
length: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function getLength () { return this.rdns.length },
|
2015-11-03 04:54:35 +00:00
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2015-02-16 20:53:42 +00:00
|
|
|
/**
|
|
|
|
* Convert DN to string according to specified formatting options.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - options: formatting parameters (optional, details below)
|
|
|
|
*
|
|
|
|
* Options are divided into two types:
|
|
|
|
* - Preservation options: Using data recorded during parsing, details of the
|
|
|
|
* original DN are preserved when converting back into a string.
|
|
|
|
* - Modification options: Alter string formatting defaults.
|
|
|
|
*
|
|
|
|
* Preservation options _always_ take precedence over modification options.
|
|
|
|
*
|
|
|
|
* Preservation Options:
|
|
|
|
* - keepOrder: Order of multi-value RDNs.
|
|
|
|
* - keepQuote: RDN values which were quoted will remain so.
|
|
|
|
* - keepSpace: Leading/trailing spaces will be output.
|
|
|
|
* - keepCase: Parsed attr name will be output instead of lowercased version.
|
|
|
|
*
|
|
|
|
* Modification Options:
|
|
|
|
* - upperName: RDN names will be uppercased instead of lowercased.
|
|
|
|
* - skipSpace: Disable trailing space after RDN separators
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.format = function dnFormat (options) {
|
|
|
|
assert.optionalObject(options, 'options must be an object')
|
|
|
|
options = options || this._format
|
2015-11-03 04:54:35 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
let str = ''
|
2012-02-18 08:15:52 +00:00
|
|
|
this.rdns.forEach(function (rdn) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const rdnString = rdn.format(options)
|
2015-02-16 20:53:42 +00:00
|
|
|
if (str.length !== 0) {
|
2019-08-27 21:11:49 +00:00
|
|
|
str += ','
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
|
|
|
if (options.keepSpace) {
|
|
|
|
str += (repeatChar(' ', rdn.spLead) +
|
2019-08-27 21:11:49 +00:00
|
|
|
rdnString + repeatChar(' ', rdn.spTrail))
|
2015-02-16 20:53:42 +00:00
|
|
|
} else if (options.skipSpace === true || str.length === 0) {
|
2019-08-27 21:11:49 +00:00
|
|
|
str += rdnString
|
2015-02-16 20:53:42 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
str += ' ' + rdnString
|
2015-02-16 20:53:42 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
|
|
|
return str
|
|
|
|
}
|
2013-10-24 23:27:42 +00:00
|
|
|
|
2015-02-16 20:53:42 +00:00
|
|
|
/**
|
|
|
|
* Set default string formatting options.
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.setFormat = function setFormat (options) {
|
|
|
|
assert.object(options, 'options must be an object')
|
2015-11-03 04:54:35 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this._format = options
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.toString = function dnToString () {
|
|
|
|
return this.format()
|
|
|
|
}
|
2015-02-16 20:53:42 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.parentOf = function parentOf (dn) {
|
|
|
|
if (typeof (dn) !== 'object') { dn = parse(dn) }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (this.rdns.length >= dn.rdns.length) { return false }
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const diff = dn.rdns.length - this.rdns.length
|
|
|
|
for (let i = this.rdns.length - 1; i >= 0; i--) {
|
|
|
|
const myRDN = this.rdns[i]
|
|
|
|
const theirRDN = dn.rdns[i + diff]
|
2011-11-08 01:23:38 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!myRDN.equals(theirRDN)) { return false }
|
2011-08-04 20:32:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.childOf = function childOf (dn) {
|
|
|
|
if (typeof (dn) !== 'object') { dn = parse(dn) }
|
|
|
|
return dn.parentOf(this)
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.isEmpty = function isEmpty () {
|
|
|
|
return (this.rdns.length === 0)
|
|
|
|
}
|
2011-08-04 20:32:01 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.equals = function dnEquals (dn) {
|
|
|
|
if (typeof (dn) !== 'object') { dn = parse(dn) }
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (this.rdns.length !== dn.rdns.length) { return false }
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
for (let i = 0; i < this.rdns.length; i++) {
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!this.rdns[i].equals(dn.rdns[i])) { return false }
|
2011-08-10 17:57:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
|
|
|
}
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.parent = function dnParent () {
|
2015-02-16 20:53:42 +00:00
|
|
|
if (this.rdns.length !== 0) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const save = this.rdns.shift()
|
|
|
|
const dn = new DN(this.rdns)
|
2019-08-27 21:11:49 +00:00
|
|
|
this.rdns.unshift(save)
|
|
|
|
return dn
|
2011-08-10 17:57:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return null
|
|
|
|
}
|
2011-08-10 17:57:58 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.clone = function dnClone () {
|
2020-10-31 21:07:32 +00:00
|
|
|
const dn = new DN(this.rdns)
|
2019-08-27 21:11:49 +00:00
|
|
|
dn._format = this._format
|
|
|
|
return dn
|
|
|
|
}
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.reverse = function dnReverse () {
|
|
|
|
this.rdns.reverse()
|
|
|
|
return this
|
|
|
|
}
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.pop = function dnPop () {
|
|
|
|
return this.rdns.pop()
|
|
|
|
}
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.push = function dnPush (rdn) {
|
|
|
|
assert.object(rdn, 'rdn (RDN) required')
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return this.rdns.push(rdn)
|
|
|
|
}
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.shift = function dnShift () {
|
|
|
|
return this.rdns.shift()
|
|
|
|
}
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.prototype.unshift = function dnUnshift (rdn) {
|
|
|
|
assert.object(rdn, 'rdn (RDN) required')
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return this.rdns.unshift(rdn)
|
|
|
|
}
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
DN.isDN = function isDN (dn) {
|
2014-09-30 23:39:19 +00:00
|
|
|
if (!dn || typeof (dn) !== 'object') {
|
2019-08-27 21:11:49 +00:00
|
|
|
return false
|
2014-09-30 23:39:19 +00:00
|
|
|
}
|
|
|
|
if (dn instanceof DN) {
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
2014-09-30 23:39:19 +00:00
|
|
|
}
|
|
|
|
if (Array.isArray(dn.rdns)) {
|
|
|
|
// Really simple duck-typing for now
|
2019-08-27 21:11:49 +00:00
|
|
|
return true
|
2014-09-30 23:39:19 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Exports
|
2011-10-18 15:50:44 +00:00
|
|
|
|
2011-08-04 20:32:01 +00:00
|
|
|
module.exports = {
|
|
|
|
parse: parse,
|
|
|
|
DN: DN,
|
|
|
|
RDN: RDN
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|