Skip to content

Commit 7a7337e

Browse files
committed
chore: simplify the Auth module, remove auth configuration parameter
1 parent b735670 commit 7a7337e

File tree

4 files changed

+35
-43
lines changed

4 files changed

+35
-43
lines changed

lib/mongo.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ defmodule Mongo do
104104
`:hostname` (optional)
105105
* `:username` - The User to connect with (optional)
106106
* `:password` - The password to connect with (optional)
107-
* `:auth` - List of additional users to authenticate as a keyword list with
108-
`:username` and `:password` keys (optional)
109107
* `:auth_source` - The database to authenticate against
110108
* `:appname` - The name of the application used the driver for the MongoDB-Handshake
111109
* `:set_name` - The name of the replica set to connect to (required if

lib/mongo/auth.ex

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,46 @@ defmodule Mongo.Auth do
44
alias Mongo.PasswordSafe
55

66
def run(opts, state) do
7-
db = opts[:database]
8-
auth = setup(opts)
9-
auther = mechanism(state)
107
auth_source = opts[:auth_source]
11-
wire_version = state[:wire_version]
8+
mechanism = mechanism(state)
129

1310
# change database for auth
14-
state =
15-
case auth_source != nil && wire_version > 0 do
16-
true -> Map.put(state, :database, auth_source)
17-
false -> state
18-
end
11+
auth_state =
12+
case auth_source != nil && state.wire_version > 0 do
13+
true ->
14+
Map.put(state, :database, auth_source)
1915

20-
# do auth
21-
# restore old database
22-
Enum.find_value(auth, fn credentials ->
23-
case auther.auth(credentials, db, state) do
24-
# everything is okay, then return nil
25-
:ok ->
26-
nil
27-
28-
error ->
29-
{mod, socket} = state.connection
30-
mod.close(socket)
31-
error
16+
false ->
17+
state
3218
end
33-
end) || {:ok, Map.put(state, :database, opts[:database])}
19+
20+
case opts |> credentials() |> mechanism.auth(state.database, auth_state) do
21+
:ok ->
22+
{:ok, state}
23+
24+
error ->
25+
{mod, socket} = state.connection
26+
mod.close(socket)
27+
error
28+
end
3429
end
3530

36-
defp setup(opts) do
31+
defp credentials(opts) do
3732
username = opts[:username]
3833
pw_safe = opts[:pw_safe]
39-
password = PasswordSafe.get_pasword(pw_safe)
40-
auth = opts[:auth] || []
34+
password = PasswordSafe.get_password(pw_safe)
35+
{username, password}
36+
end
4137

42-
auth =
43-
Enum.map(auth, fn opts ->
44-
username = opts[:username]
45-
password = PasswordSafe.get_pasword(pw_safe)
46-
{username, password}
47-
end)
38+
defp mechanism(%{wire_version: version, auth_mechanism: :x509}) when version >= 3 do
39+
Mongo.Auth.X509
40+
end
4841

49-
if username && password, do: auth ++ [{username, password}], else: auth
42+
defp mechanism(%{wire_version: version}) when version >= 3 do
43+
Mongo.Auth.SCRAM
5044
end
5145

52-
defp mechanism(%{wire_version: version, auth_mechanism: :x509}) when version >= 3, do: Mongo.Auth.X509
53-
defp mechanism(%{wire_version: version}) when version >= 3, do: Mongo.Auth.SCRAM
54-
defp mechanism(_), do: Mongo.Auth.CR
46+
defp mechanism(_) do
47+
Mongo.Auth.CR
48+
end
5549
end

lib/mongo/auth/x509.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ defmodule Mongo.Auth.X509 do
33
alias Mongo.MongoDBConnection.Utils
44

55
def auth({username, _password}, _db, s) do
6-
IO.inspect(username)
76
cmd = [authenticate: 1, user: username, mechanism: "MONGODB-X509"]
87

98
case Utils.command(-2, cmd, s) do
10-
{:ok, _flags, message} ->
11-
IO.inspect(message)
9+
{:ok, _flags, _message} ->
1210
:ok
13-
_error -> {:error, "X509 auth failed"}
11+
12+
_error ->
13+
{:error, "X509 auth failed"}
1414
end
1515
end
1616
end

lib/mongo/password_safe.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ defmodule Mongo.PasswordSafe do
1818
GenServer.cast(pid, {:set, password})
1919
end
2020

21-
def get_pasword(nil), do: nil
21+
def get_password(nil), do: nil
2222

23-
def get_pasword(pid) do
23+
def get_password(pid) do
2424
GenServer.call(pid, :get)
2525
end
2626

0 commit comments

Comments
 (0)