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

Support authentication mechanism negotiation #38

Merged
merged 2 commits into from
Aug 18, 2023

Conversation

thekid
Copy link
Member

@thekid thekid commented Aug 18, 2023

...by adding saslSupportedMechs: <db.user> to the hello command. This will return an additional field hello.saslSupportedMechs in its result.

Negotation

If an authentication mechanism is explicitely supplied via the connection string, it's used regardless of what the server answers. Otherwise, negotation tries to find the preferred authentication mechanism.

If SCRAM-SHA-256 is present in the list of mechanism, then it MUST be used as the default; otherwise, SCRAM-SHA-1 MUST be used as the default, regardless of whether SCRAM-SHA-1 is in the list. If saslSupportedMechs is not present in the handshake response for mechanism negotiation, then SCRAM-SHA-1 MUST be used

(see https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst#defaults)

Connection string Server-supplied saslSupportedMechs Result
?authMechanism=SCRAM-SHA-256 (anything) SCRAM-SHA-256
?authMechanism=SCRAM-SHA-1 (anything) SCRAM-SHA-1
?authMechanism=PLAIN (anything) Error 1️⃣
(not supplied) [SCRAM-SHA-256, SCRAM-SHA-1] SCRAM-SHA-256
(not supplied) [SCRAM-SHA-1, SCRAM-SHA-256] SCRAM-SHA-256
(not supplied) [SCRAM-SHA-1, AWS] SCRAM-SHA-1
(not supplied) [PLAIN, AWS] Error 2️⃣
(not supplied) (empty or missing) SCRAM-SHA-1

Errors

  1. Unsupported authentication mechanism "PLAIN"
  2. None of the supplied authentication mechanisms "PLAIN", "AWS" is supported

Real-life examples

...when adding a var_dump() of the returned hello.saslSupportedMechs key:

Local 5.0.20 (Ubuntu)

$ xp connect.script.php mongodb://user:*********@localhost/test
array(2) {
  [0]=>
  string(11) "SCRAM-SHA-1"
  [1]=>
  string(13) "SCRAM-SHA-256"
}

MongoDB Atlas

$ xp connect.script.php mongodb+srv://user:*********@example.abcdefg.mongodb.net
array(1) {
  [0]=>
  string(11) "SCRAM-SHA-1"
}

...by adding `saslSupportedMechs: <db.user>` to the *hello* command. This will return an
additional field hello.saslSupportedMechs in its result. The order the server returns the
mechanisms in is used as an indicator for its preference
@thekid thekid added the enhancement New feature or request label Aug 18, 2023
@thekid thekid merged commit 9dcf3b3 into master Aug 18, 2023
18 checks passed
@thekid thekid deleted the feature/auth-mechanism-negotiation branch August 18, 2023 11:47
thekid added a commit that referenced this pull request Aug 18, 2023
@thekid
Copy link
Member Author

thekid commented Aug 18, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant