Skip to content

Commit 4e5e37a

Browse files
authored
fix(cli-repl): invert and fix passwordless-auth-mechanism check (#1533)
Currently, if a username is specified but no password, mongosh will prompt for a password before connecting. Kerberos/GSSAPI was made an exception, since it rarely makes use of a password. However, there are other mechanisms where a username can, and often will, be provided without a password (AWS, OIDC, and technically even X.509). This commit prevents mongosh from prompting for a password for those mechanisms, and inverts the check to explicitly list the mechanisms that *do* require a password (the list of auth mechanisms is unlikely to change soon, but if it does, it likely wouldn’t include a new password-based auth mechanism, so avoiding false positive return values seems like a better choice than avoiding false negative return values).
1 parent d5530af commit 4e5e37a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

packages/cli-repl/src/cli-repl.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -804,11 +804,12 @@ export class CliRepl implements MongoshIOProvider {
804804
*/
805805
isPasswordMissingURI(cs: ConnectionString): boolean {
806806
return !!(
807-
(
808-
cs.username &&
809-
!cs.password &&
810-
cs.searchParams.get('authMechanism') !== 'GSSAPI'
811-
) // no need for a password for Kerberos
807+
cs.username &&
808+
!cs.password &&
809+
// Only password-based mechanisms require a password, including the default SCRAM-SHA-* ones
810+
['', 'MONGODB-CR', 'PLAIN', 'SCRAM-SHA-1', 'SCRAM-SHA-256'].includes(
811+
cs.searchParams.get('authMechanism') ?? ''
812+
)
812813
);
813814
}
814815

packages/cli-repl/test/e2e-oidc.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ describe('OIDC auth e2e', function () {
144144
shell.assertNoErrors();
145145
});
146146

147+
it('can successfully authenticate using OIDC Auth Code Flow when a username is specified', async function () {
148+
shell = TestShell.start({
149+
args: [
150+
await testServer.connectionString(),
151+
'--username=testuser',
152+
'--authenticationMechanism=MONGODB-OIDC',
153+
'--oidcRedirectUri=http://localhost:0/',
154+
`--browser=${fetchBrowserFixture}`,
155+
],
156+
});
157+
await shell.waitForPrompt();
158+
159+
await verifyUser(shell, 'testuser', 'testServer-group');
160+
shell.assertNoErrors();
161+
});
162+
147163
it('can successfully authenticate using OIDC Device Auth Flow', async function () {
148164
shell = TestShell.start({
149165
args: [

0 commit comments

Comments
 (0)