From 9710461c84cbffb44cf01ee0353ca349e4a4fe71 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Fri, 3 May 2024 11:47:41 +0200 Subject: [PATCH] Treat 0 and 0.0 Mongo responses equally --- lib/mongo/auth/cr.ex | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/mongo/auth/cr.ex b/lib/mongo/auth/cr.ex index 4c2a09f7..4e04aa10 100644 --- a/lib/mongo/auth/cr.ex +++ b/lib/mongo/auth/cr.ex @@ -11,9 +11,13 @@ 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] @@ -21,7 +25,7 @@ defmodule Mongo.Auth.CR 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} ->