Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add socket api #1

Merged
merged 4 commits into from
Oct 14, 2021
Merged

Add socket api #1

merged 4 commits into from
Oct 14, 2021

Conversation

taylor-sutton
Copy link

@taylor-sutton taylor-sutton commented Sep 24, 2021

This adds a new way to construct LDAP clients which allows the user (meaning the user of this lib, the person creating the LDAP client) to use custom logic for constructing a network connection to the LDAP server. Previously, this connection was always created by ldapjs calling either net.connect or tls.connect (from Node stdlib) to the provided URL. Most of the time, this is sufficient, and it remains the default. In rare cases (described in a sec), we may want to specify some custom way of creating the socket to the server. This doesn't change any of the LDAP logic - all that occurs at a higher level of abstraction on the network hierarchy (layer 7), so we can just make a small change here to change where the connection comes from.

We plan to use this feature to connect to an LDAP server over an explicit proxy i.e. the client will first establish a connection to the proxy, then use that connection for LDAP. Clever-internal details about this setup are at https://github.com/Clever/ldap-user-service/pull/65.

About the actual code changes. There was already an if-else for calling the appropriate connect method, which seemed like a good place to add a third branch to use the user-defined logic. However, it required a little refactor - moving some logic into the onSocketDefined function - because there was no longer a line of code in the client where we could be sure the socket variable was set to a real vaue.

In addition, in order to modify the TypeScript declarations, I pulled in the existing type declarations from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/ldapjs and made the one-line modification (adding the parameter to ClientOptions at https://github.com/Clever/node-ldapjs/pull/1/files#diff-cfc7807f6b174e1c3705c5c2747f56e49a889a52999dc96e4080a3af0131a534R60 .

Discussed merging into upstream in ldapjs#761 - seems like not gonna happen any time soon.

createConnection is a parameter that lets the user supply their own method of creating a socket to
the LDAP server, as an alternative for the existing logic, which uses net.connect or tls.connect
depending on whether the URL is ldaps or ldap.

The primary use case I have in mind is connecting to an LDAP server through an explicit proxy which
uses the HTTP CONNECT method. In this case, the user can supply a createConnection function handles
making the HTTP CONNECT request and (for ldaps) doing a TLS handshake over the resulting connection.
The type declarations are reproduced (under MIT license) from DefinitelyTyped, which underpins the
@types/ldapjs package, except with a one line modification, the addition of the createConnection
parameter to ClientOptions, which is specific to this fork.
@taylor-sutton taylor-sutton marked this pull request as ready for review October 14, 2021 16:47
Copy link

@OiCMudkips OiCMudkips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the types!

@taylor-sutton taylor-sutton merged commit 55e9369 into master Oct 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants