2019-08-27 21:11:49 +00:00
'use strict'
2011-08-04 20:32:01 +00:00
2019-08-27 21:11:49 +00:00
const querystring = require ( 'querystring' )
const url = require ( 'url' )
2023-02-21 18:28:58 +00:00
const { DN } = require ( '@ldapjs/dn' )
2022-06-26 20:59:35 +00:00
const filter = require ( '@ldapjs/filter' )
2011-08-04 20:32:01 +00:00
module . exports = {
2012-02-18 08:15:52 +00:00
parse : function ( urlStr , parseDN ) {
2019-11-16 15:48:00 +00:00
let parsedURL
try {
parsedURL = new url . URL ( urlStr )
} catch ( error ) {
throw new TypeError ( urlStr + ' is an invalid LDAP url (scope)' )
}
2019-08-27 21:11:49 +00:00
if ( ! parsedURL . protocol || ! ( parsedURL . protocol === 'ldap:' || parsedURL . protocol === 'ldaps:' ) ) { throw new TypeError ( urlStr + ' is an invalid LDAP url (protocol)' ) }
const u = {
protocol : parsedURL . protocol ,
hostname : parsedURL . hostname ,
port : parsedURL . port ,
pathname : parsedURL . pathname ,
search : parsedURL . search ,
href : parsedURL . href
}
2011-08-04 20:32:01 +00:00
2019-08-27 21:11:49 +00:00
u . secure = ( u . protocol === 'ldaps:' )
2011-08-04 20:32:01 +00:00
2019-08-27 21:11:49 +00:00
if ( ! u . hostname ) { u . hostname = 'localhost' }
2011-08-04 20:32:01 +00:00
2011-08-26 22:07:10 +00:00
if ( ! u . port ) {
2019-08-27 21:11:49 +00:00
u . port = ( u . secure ? 636 : 389 )
2011-08-26 22:07:10 +00:00
} else {
2019-08-27 21:11:49 +00:00
u . port = parseInt ( u . port , 10 )
2011-08-26 22:07:10 +00:00
}
2011-08-04 20:32:01 +00:00
if ( u . pathname ) {
2019-08-27 21:11:49 +00:00
u . pathname = querystring . unescape ( u . pathname . substr ( 1 ) )
2023-02-21 18:28:58 +00:00
u . DN = parseDN ? DN . fromString ( u . pathname ) : u . pathname
2011-08-04 20:32:01 +00:00
}
if ( u . search ) {
2019-08-27 21:11:49 +00:00
u . attributes = [ ]
2020-10-31 21:07:32 +00:00
const tmp = u . search . substr ( 1 ) . split ( '?' )
2011-08-04 20:32:01 +00:00
if ( tmp && tmp . length ) {
if ( tmp [ 0 ] ) {
2012-02-18 08:15:52 +00:00
tmp [ 0 ] . split ( ',' ) . forEach ( function ( a ) {
2019-08-27 21:11:49 +00:00
u . attributes . push ( querystring . unescape ( a . trim ( ) ) )
} )
2011-08-04 20:32:01 +00:00
}
}
if ( tmp [ 1 ] ) {
2019-08-27 21:11:49 +00:00
if ( tmp [ 1 ] !== 'base' && tmp [ 1 ] !== 'one' && tmp [ 1 ] !== 'sub' ) { throw new TypeError ( urlStr + ' is an invalid LDAP url (scope)' ) }
u . scope = tmp [ 1 ]
2011-08-04 20:32:01 +00:00
}
if ( tmp [ 2 ] ) {
2019-08-27 21:11:49 +00:00
u . filter = querystring . unescape ( tmp [ 2 ] )
2011-08-04 20:32:01 +00:00
}
if ( tmp [ 3 ] ) {
2019-08-27 21:11:49 +00:00
u . extensions = querystring . unescape ( tmp [ 3 ] )
2011-08-04 20:32:01 +00:00
}
2013-10-28 21:12:12 +00:00
2019-08-27 21:11:49 +00:00
if ( ! u . scope ) { u . scope = 'base' }
if ( ! u . filter ) { u . filter = filter . parseString ( '(objectclass=*)' ) } else { u . filter = filter . parseString ( u . filter ) }
2011-08-04 20:32:01 +00:00
}
2019-08-27 21:11:49 +00:00
return u
2011-08-04 20:32:01 +00:00
}
2019-08-27 21:11:49 +00:00
}