Skip to content

Commit 6071f18

Browse files
authored
Treat 0 and 0.0 Mongo responses equally (#237)
1 parent 9f5e71d commit 6071f18

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

lib/mongo/auth/cr.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ defmodule Mongo.Auth.CR do
1111
do: nonce(message, username, password, s)
1212
end
1313

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

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

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

2731
{:ok, _flags, nil} ->

0 commit comments

Comments
 (0)