Skip to content

Commit 82d2d81

Browse files
committed
fix: Auth mechanism use consolidation
1 parent 6e1d094 commit 82d2d81

File tree

4 files changed

+28
-48
lines changed

4 files changed

+28
-48
lines changed

src/cmap/auth/mongo_credentials.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Resolves the default auth mechanism according to
22

33
import type { Document } from '../../bson';
4-
import { MongoParseError } from '../../error';
54
import { AuthMechanismId, AuthMechanism } from './defaultAuthProviders';
65

76
// https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst
@@ -10,7 +9,7 @@ function getDefaultAuthMechanism(ismaster?: Document): AuthMechanismId {
109
// If ismaster contains saslSupportedMechs, use scram-sha-256
1110
// if it is available, else scram-sha-1
1211
if (Array.isArray(ismaster.saslSupportedMechs)) {
13-
return ismaster.saslSupportedMechs.indexOf('SCRAM-SHA-256') >= 0
12+
return ismaster.saslSupportedMechs.includes(AuthMechanism.MONGODB_SCRAM_SHA256)
1413
? AuthMechanism.MONGODB_SCRAM_SHA256
1514
: AuthMechanism.MONGODB_SCRAM_SHA1;
1615
}
@@ -112,34 +111,34 @@ export class MongoCredentials {
112111

113112
validate(): void {
114113
if (
115-
(this.mechanism === 'GSSAPI' ||
116-
this.mechanism === 'MONGODB-CR' ||
117-
this.mechanism === 'PLAIN' ||
118-
this.mechanism === 'SCRAM-SHA-1' ||
119-
this.mechanism === 'SCRAM-SHA-256') &&
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) &&
120119
!this.username
121120
) {
122-
throw new MongoParseError(`Username required for mechanism '${this.mechanism}'`);
121+
throw new TypeError(`Username required for mechanism '${this.mechanism}'`);
123122
}
124123

125124
if (
126-
this.mechanism === 'GSSAPI' ||
127-
this.mechanism === 'MONGODB-AWS' ||
128-
this.mechanism === 'MONGODB-X509'
125+
this.mechanism === AuthMechanism.MONGODB_GSSAPI ||
126+
this.mechanism === AuthMechanism.MONGODB_AWS ||
127+
this.mechanism === AuthMechanism.MONGODB_X509
129128
) {
130129
if (this.source != null && this.source !== '$external') {
131-
throw new MongoParseError(
130+
throw new TypeError(
132131
`Invalid source '${this.source}' for mechanism '${this.mechanism}' specified.`
133132
);
134133
}
135134
}
136135

137-
if (this.mechanism === 'PLAIN' && this.source == null) {
138-
throw new MongoParseError('PLAIN Authentication Mechanism needs an auth source');
136+
if (this.mechanism === AuthMechanism.MONGODB_PLAIN && this.source == null) {
137+
throw new TypeError('PLAIN Authentication Mechanism needs an auth source');
139138
}
140139

141-
if (this.mechanism === 'MONGODB-X509' && this.password != null) {
142-
throw new MongoParseError(`Password not allowed for mechanism MONGODB-X509`);
140+
if (this.mechanism === AuthMechanism.MONGODB_X509 && this.password != null) {
141+
throw new TypeError(`Password not allowed for mechanism MONGODB-X509`);
143142
}
144143
}
145144
}

src/cmap/auth/scram.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { MongoCredentials } from './mongo_credentials';
77
import type { HandshakeDocument } from '../connect';
88

99
import { saslprep } from '../../deps';
10+
import { AuthMechanism } from './defaultAuthProviders';
1011

1112
type CryptoMethod = 'sha1' | 'sha256';
1213

@@ -83,7 +84,8 @@ function makeFirstMessage(
8384
nonce: Buffer
8485
) {
8586
const username = cleanUsername(credentials.username);
86-
const mechanism = cryptoMethod === 'sha1' ? 'SCRAM-SHA-1' : 'SCRAM-SHA-256';
87+
const mechanism =
88+
cryptoMethod === 'sha1' ? AuthMechanism.MONGODB_SCRAM_SHA1 : AuthMechanism.MONGODB_SCRAM_SHA256;
8789

8890
// NOTE: This is done b/c Javascript uses UTF-16, but the server is hashing in UTF-8.
8991
// Since the username is not sasl-prep-d, we need to do this here.

src/connection_string.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,7 @@ const BOOLEAN_OPTIONS = new Set([
182182
const STRING_OPTIONS = new Set(['authsource', 'replicaset']);
183183

184184
// Supported text representations of auth mechanisms
185-
// NOTE: this list exists in native already, if it is merged here we should deduplicate
186-
const AUTH_MECHANISMS = new Set([
187-
'GSSAPI',
188-
'MONGODB-AWS',
189-
'MONGODB-X509',
190-
'MONGODB-CR',
191-
'DEFAULT',
192-
'SCRAM-SHA-1',
193-
'SCRAM-SHA-256',
194-
'PLAIN'
195-
]);
185+
export const AUTH_MECHANISMS = new Set([...Object.values(AuthMechanism)]);
196186

197187
// Lookup table used to translate normalized (lower-cased) forms of connection string
198188
// options to their expected camelCase version
@@ -1107,16 +1097,16 @@ export const OPTIONS: Record<keyof MongoClientOptions, OptionDescriptor> = {
11071097
}
11081098
let source = options.credentials.source; // some mechanisms have '$external' as the Auth Source
11091099
if (
1110-
mechanism === 'PLAIN' ||
1111-
mechanism === 'GSSAPI' ||
1112-
mechanism === 'MONGODB-AWS' ||
1113-
mechanism === 'MONGODB-X509'
1100+
mechanism === AuthMechanism.MONGODB_PLAIN ||
1101+
mechanism === AuthMechanism.MONGODB_GSSAPI ||
1102+
mechanism === AuthMechanism.MONGODB_AWS ||
1103+
mechanism === AuthMechanism.MONGODB_X509
11141104
) {
11151105
source = '$external';
11161106
}
11171107

11181108
let password: string | undefined = options.credentials.password;
1119-
if (mechanism === 'MONGODB-X509' && password === '') {
1109+
if (mechanism === AuthMechanism.MONGODB_X509 && password === '') {
11201110
password = undefined;
11211111
}
11221112
return new MongoCredentials({

src/operations/connect.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Logger } from '../logger';
33
import { ReadPreference } from '../read_preference';
44
import { MongoError, AnyError } from '../error';
55
import { Topology, TopologyOptions, ServerAddress } from '../sdam/topology';
6-
import { parseConnectionString } from '../connection_string';
6+
import { AUTH_MECHANISMS, parseConnectionString } from '../connection_string';
77
import { ReadConcern } from '../read_concern';
88
import { emitDeprecationWarning, Callback } from '../utils';
99
import { CMAP_EVENT_NAMES } from '../cmap/events';
@@ -12,20 +12,9 @@ import * as BSON from '../bson';
1212
import type { Document } from '../bson';
1313
import type { MongoClient } from '../mongo_client';
1414
import { ConnectionOptions, Connection } from '../cmap/connection';
15-
import type { AuthMechanismId } from '../cmap/auth/defaultAuthProviders';
15+
import { AuthMechanism, AuthMechanismId } from '../cmap/auth/defaultAuthProviders';
1616
import { Server } from '../sdam/server';
1717

18-
const VALID_AUTH_MECHANISMS = new Set([
19-
'DEFAULT',
20-
'PLAIN',
21-
'GSSAPI',
22-
'MONGODB-CR',
23-
'MONGODB-X509',
24-
'MONGODB-AWS',
25-
'SCRAM-SHA-1',
26-
'SCRAM-SHA-256'
27-
]);
28-
2918
const validOptionNames = [
3019
'poolSize',
3120
'ssl',
@@ -461,11 +450,11 @@ function generateCredentials(
461450
const source = options.authSource || options.authdb || options.dbName;
462451

463452
// authMechanism
464-
const authMechanismRaw = options.authMechanism || 'DEFAULT';
453+
const authMechanismRaw = options.authMechanism || AuthMechanism.MONGODB_DEFAULT;
465454
const mechanism = authMechanismRaw.toUpperCase() as AuthMechanismId;
466455
const mechanismProperties = options.authMechanismProperties;
467456

468-
if (!VALID_AUTH_MECHANISMS.has(mechanism)) {
457+
if (!AUTH_MECHANISMS.has(mechanism)) {
469458
throw new MongoError(`authentication mechanism ${mechanism} not supported`);
470459
}
471460

0 commit comments

Comments
 (0)