Packages
mongodb_driver
1.6.4
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.6
1.5.5
1.5.4
1.5.2
1.5.1
1.5.0
1.4.1
1.4.0
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
0.9.2
0.9.1
0.9.0
0.9.0-rc.1
0.9.0-rc.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
The MongoDB driver for Elixir
Current section
Files
Jump to
Current section
Files
lib/mongo/auth/plain.ex
defmodule Mongo.Auth.PLAIN do
@moduledoc false
alias Mongo.MongoDBConnection.Utils
def auth({nil, nil}, _s) do
:ok
end
def auth({username, password}, s) do
auth_payload = build_auth_payload(username, password)
message = [saslStart: 1, mechanism: "PLAIN", payload: auth_payload]
case Utils.command(-3, message, s) do
{:ok, _flags, %{"ok" => ok, "done" => true}} when ok == 1 ->
:ok
{:ok, _flags, %{"ok" => ok, "errmsg" => reason, "code" => code}} when ok == 0 ->
{:error, Mongo.Error.exception(message: "auth failed for user #{username}: #{reason}", code: code)}
error ->
error
end
end
defp build_auth_payload(username, password) do
# https://www.ietf.org/rfc/rfc4616.txt
# Null separate listed of authorization ID (blank), username, password. These are sent as raw UTF-8.
payload = "\0#{username}\0#{password}"
%BSON.Binary{binary: payload}
end
end