1
1
// Resolves the default auth mechanism according to
2
2
3
3
import type { Document } from '../../bson' ;
4
- import { AuthMechanism } from './defaultAuthProviders' ;
4
+ import { AuthMechanismId , AuthMechanism } from './defaultAuthProviders' ;
5
5
6
6
// https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst
7
- function getDefaultAuthMechanism ( ismaster ?: Document ) : AuthMechanism {
7
+ function getDefaultAuthMechanism ( ismaster ?: Document ) : AuthMechanismId {
8
8
if ( ismaster ) {
9
9
// If ismaster contains saslSupportedMechs, use scram-sha-256
10
10
// if it is available, else scram-sha-1
11
11
if ( Array . isArray ( ismaster . saslSupportedMechs ) ) {
12
- return ismaster . saslSupportedMechs . indexOf ( 'SCRAM-SHA-256' ) >= 0
12
+ return ismaster . saslSupportedMechs . includes ( AuthMechanism . MONGODB_SCRAM_SHA256 )
13
13
? AuthMechanism . MONGODB_SCRAM_SHA256
14
14
: AuthMechanism . MONGODB_SCRAM_SHA1 ;
15
15
}
@@ -30,7 +30,7 @@ export interface MongoCredentialsOptions {
30
30
password : string ;
31
31
source : string ;
32
32
db ?: string ;
33
- mechanism ?: AuthMechanism ;
33
+ mechanism ?: AuthMechanismId ;
34
34
mechanismProperties : Document ;
35
35
}
36
36
@@ -46,7 +46,7 @@ export class MongoCredentials {
46
46
/** The database that the user should authenticate against */
47
47
readonly source : string ;
48
48
/** The method used to authenticate */
49
- readonly mechanism : AuthMechanism ;
49
+ readonly mechanism : AuthMechanismId ;
50
50
/** Special properties used by some types of auth mechanisms */
51
51
readonly mechanismProperties : Document ;
52
52
@@ -108,4 +108,54 @@ export class MongoCredentials {
108
108
109
109
return this ;
110
110
}
111
+
112
+ validate ( ) : void {
113
+ if (
114
+ ( this . mechanism === AuthMechanism . MONGODB_GSSAPI ||
115
+ this . mechanism === AuthMechanism . MONGODB_CR ||
116
+ this . mechanism === AuthMechanism . MONGODB_PLAIN ||
117
+ this . mechanism === AuthMechanism . MONGODB_SCRAM_SHA1 ||
118
+ this . mechanism === AuthMechanism . MONGODB_SCRAM_SHA256 ) &&
119
+ ! this . username
120
+ ) {
121
+ throw new TypeError ( `Username required for mechanism '${ this . mechanism } '` ) ;
122
+ }
123
+
124
+ if (
125
+ this . mechanism === AuthMechanism . MONGODB_GSSAPI ||
126
+ this . mechanism === AuthMechanism . MONGODB_AWS ||
127
+ this . mechanism === AuthMechanism . MONGODB_X509
128
+ ) {
129
+ if ( this . source != null && this . source !== '$external' ) {
130
+ throw new TypeError (
131
+ `Invalid source '${ this . source } ' for mechanism '${ this . mechanism } ' specified.`
132
+ ) ;
133
+ }
134
+ }
135
+
136
+ if ( this . mechanism === AuthMechanism . MONGODB_PLAIN && this . source == null ) {
137
+ throw new TypeError ( 'PLAIN Authentication Mechanism needs an auth source' ) ;
138
+ }
139
+
140
+ if ( this . mechanism === AuthMechanism . MONGODB_X509 && this . password != null ) {
141
+ if ( this . password === '' ) {
142
+ Reflect . set ( this , 'password' , undefined ) ;
143
+ return ;
144
+ }
145
+ throw new TypeError ( `Password not allowed for mechanism MONGODB-X509` ) ;
146
+ }
147
+ }
148
+
149
+ static merge (
150
+ creds : MongoCredentials ,
151
+ options : Partial < MongoCredentialsOptions >
152
+ ) : MongoCredentials {
153
+ return new MongoCredentials ( {
154
+ username : options . username ?? creds . username ,
155
+ password : options . password ?? creds . password ,
156
+ mechanism : options . mechanism ?? creds . mechanism ,
157
+ mechanismProperties : options . mechanismProperties ?? creds . mechanismProperties ,
158
+ source : options . source ?? creds . source ?? options . db
159
+ } ) ;
160
+ }
111
161
}
0 commit comments