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

Treat 0 and 0.0 Mongo responses equally #237

Merged
merged 1 commit into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/mongo/auth/cr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ defmodule Mongo.Auth.CR do
do: nonce(message, username, password, s)
end

defp nonce(%{"nonce" => nonce, "ok" => ok}, username, password, s)
# to support a response that returns 1 or 1.0
when ok == 1 do
# Note that we use numeric comparisons in guards (e.g., `... when ok == 1`)
# instead of pattern matching below. This is to accommodate responses that
# return either integer or float values. Pattern matching treats 1 and 1.0,
# and 0, 0.0 and -0.0 (OTP 27+), as distinct values due to their different
# types/internal representation. By using numeric comparisons, we can ensure
# correct behavior regardless of the numeric type returned.
defp nonce(%{"nonce" => nonce, "ok" => ok}, username, password, s) when ok == 1 do
digest = Utils.digest(nonce, username, password)
command = [authenticate: 1, user: username, nonce: nonce, key: digest]

case Utils.command(-3, command, s) do
{:ok, _flags, %{"ok" => ok}} when ok == 1 ->
:ok

{:ok, _flags, %{"ok" => 0.0, "errmsg" => reason, "code" => code}} ->
{:ok, _flags, %{"ok" => ok, "errmsg" => reason, "code" => code}} when ok == 0 ->
{:error, Mongo.Error.exception(message: "auth failed for '#{username}': #{reason}", code: code)}

{:ok, _flags, nil} ->
Expand Down
Loading