Skip to content

Commit

Permalink
Treat 0 and 0.0 Mongo responses equally (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhcarvalho committed May 3, 2024
1 parent 9f5e71d commit 6071f18
Showing 1 changed file with 8 additions and 4 deletions.
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

0 comments on commit 6071f18

Please sign in to comment.