1- import { format as toURL } from 'url' ;
2-
3- import AUTHENICATION_TO_AUTH_MECHANISM from './constants/auth-strategy-to-auth-mechanism' ;
41import AUTH_STRATEGIES from './constants/auth-strategies' ;
52import READ_PREFERENCES from './constants/read-preferences' ;
63import SSL_METHODS from './constants/ssl-methods' ;
@@ -9,8 +6,6 @@ import SSH_TUNNEL_TYPES from './constants/ssh-tunnel-types';
96// Defaults.
107const AUTH_STRATEGY_DEFAULT = AUTH_STRATEGIES . NONE ;
118const READ_PREFERENCE_DEFAULT = READ_PREFERENCES . PRIMARY ;
12- const MONGODB_DATABASE_NAME_DEFAULT = 'admin' ;
13- const KERBEROS_SERVICE_NAME_DEFAULT = 'mongodb' ;
149const SSL_DEFAULT = SSL_METHODS . NONE ;
1510const SSH_TUNNEL_DEFAULT = SSH_TUNNEL_TYPES . NONE ;
1611
@@ -91,148 +86,6 @@ class ConnectionModel {
9186 sshTunnelPassphrase ?: string ;
9287}
9388
94- const getDriverAuthMechanism = (
95- connectionModel : ConnectionModel
96- ) : string | undefined => {
97- return AUTHENICATION_TO_AUTH_MECHANISM [ connectionModel . authStrategy ] ;
98- } ;
99-
100- // eslint-disable-next-line complexity
101- const getBaseUrlFromConnectionModel = (
102- connectionModel : ConnectionModel
103- ) : string => {
104- const req : any = {
105- protocol : 'mongodb' ,
106- port : null ,
107- slashes : true ,
108- pathname : '/' ,
109- query : { }
110- } ;
111-
112- // In the `mongodb+srv` protocol the comma separated list of host names is
113- // replaced with a single hostname.
114- // The format is: `mongodb+srv://{hostname}.{domainname}/{options}`
115- if ( connectionModel . isSrvRecord ) {
116- req . protocol = 'mongodb+srv' ;
117- req . hostname = connectionModel . hostname ;
118- } else if ( connectionModel . hosts . length === 1 ) {
119- // Driver adds sharding info to the original hostname.
120- // And returnes a list of all coresponding hosts.
121- // If driver returns a list of hosts which size is equal one,
122- // we can use hostname attribute that stores unmodified value.
123- req . hostname = connectionModel . hostname ;
124- req . port = connectionModel . port ;
125- } else {
126- req . host = connectionModel . hosts
127- . map ( ( item ) => `${ item . host } :${ item . port } ` )
128- . join ( ',' ) ;
129- }
130-
131- if ( connectionModel . ns ) {
132- req . pathname = `/${ connectionModel . ns } ` ;
133- }
134-
135- // Encode auth for url format
136- if ( connectionModel . authStrategy === AUTH_STRATEGIES . MONGODB ) {
137- req . auth = 'AUTH_TOKEN' ;
138- req . query . authSource =
139- connectionModel . mongodbDatabaseName || MONGODB_DATABASE_NAME_DEFAULT ;
140- } else if ( connectionModel . authStrategy === 'SCRAM-SHA-256' ) {
141- req . auth = 'AUTH_TOKEN' ;
142- req . query . authSource =
143- connectionModel . mongodbDatabaseName || MONGODB_DATABASE_NAME_DEFAULT ;
144- req . query . authMechanism = getDriverAuthMechanism ( connectionModel ) ;
145- } else if ( connectionModel . authStrategy === AUTH_STRATEGIES . KERBEROS ) {
146- req . auth = 'AUTH_TOKEN' ;
147- req . query . gssapiServiceName =
148- connectionModel . kerberosServiceName || KERBEROS_SERVICE_NAME_DEFAULT ;
149- req . query . authMechanism = getDriverAuthMechanism ( connectionModel ) ;
150- } else if ( connectionModel . authStrategy === AUTH_STRATEGIES . X509 ) {
151- req . auth = 'AUTH_TOKEN' ;
152- req . query . authMechanism = getDriverAuthMechanism ( connectionModel ) ;
153- } else if ( connectionModel . authStrategy === AUTH_STRATEGIES . LDAP ) {
154- req . auth = 'AUTH_TOKEN' ;
155- req . query . authMechanism = getDriverAuthMechanism ( connectionModel ) ;
156- }
157-
158- if ( req . query . readPreference !== undefined ) {
159- req . query . readPreference = connectionModel . readPreference ;
160- }
161- if ( req . query . replicaSet !== undefined ) {
162- req . query . replicaSet = connectionModel . replicaSet ;
163- }
164-
165- if ( connectionModel . sslMethod === SSL_METHODS . NONE ) {
166- req . query . ssl = 'false' ;
167- } else {
168- req . query . ssl = 'true' ;
169- }
170-
171- const reqClone = {
172- ...req
173- } ;
174-
175- return toURL ( reqClone ) ;
176- } ;
177-
178- // eslint-disable-next-line complexity
179- export const getDriverUrlFromConnectionModel = (
180- connectionModel : ConnectionModel
181- ) : string => {
182- let username = '' ;
183- let password = '' ;
184- let authField = '' ;
185- let result = getBaseUrlFromConnectionModel ( connectionModel ) ;
186-
187- // Post url.format() workaround for
188- // https://github.com/nodejs/node/issues/1802
189- if (
190- connectionModel . authStrategy === 'MONGODB' ||
191- connectionModel . authStrategy === 'SCRAM-SHA-256'
192- ) {
193- username = encodeURIComponent ( connectionModel . mongodbUsername || '' ) ;
194- password = encodeURIComponent ( connectionModel . mongodbPassword || '' ) ;
195- authField = `${ username } :${ password } ` ;
196- } else if ( connectionModel . authStrategy === 'LDAP' ) {
197- username = encodeURIComponent ( connectionModel . ldapUsername || '' ) ;
198- password = encodeURIComponent ( connectionModel . ldapPassword || '' ) ;
199- authField = `${ username } :${ password } ` ;
200- } else if ( connectionModel . authStrategy === 'X509' ) {
201- username = encodeURIComponent ( connectionModel . x509Username || '' ) ;
202- authField = username ;
203- } else if (
204- connectionModel . authStrategy === 'KERBEROS' &&
205- connectionModel . kerberosPassword
206- ) {
207- username = encodeURIComponent ( connectionModel . kerberosPrincipal || '' ) ;
208- password = encodeURIComponent ( connectionModel . kerberosPassword ) ;
209- authField = `${ username } :${ password } ` ;
210- } else if ( connectionModel . authStrategy === 'KERBEROS' ) {
211- username = encodeURIComponent ( connectionModel . kerberosPrincipal || '' ) ;
212- authField = `${ username } :` ;
213- }
214-
215- // The auth component comes straight after `the mongodb://`
216- // so a single string replace should always work.
217- result = result . replace ( 'AUTH_TOKEN' , authField ) ;
218-
219- if (
220- connectionModel . authStrategy === AUTH_STRATEGIES . KERBEROS ||
221- connectionModel . authStrategy === AUTH_STRATEGIES . LDAP
222- ) {
223- result = `${ result } &authSource=$external` ;
224- }
225-
226- if (
227- connectionModel . authStrategy === AUTH_STRATEGIES . KERBEROS &&
228- connectionModel . kerberosCanonicalizeHostname
229- ) {
230- result = `${ result } &authMechanismProperties=CANONICALIZE_HOST_NAME:true` ;
231- }
232-
233- return result ;
234- } ;
235-
23689/**
23790 * Enforce constraints for SSL.
23891 * @param {Object } attrs - Incoming attributes.
0 commit comments