-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Support for the PLAIN authMechanism (#223)
* Add support for LDAP authentication (#5) * Add support for LDAP authentication * Unit test for url parser changes * Mention PLAIN auth support in the Readme (#6) * Mention PLAIN auth support in the Readme * tweak wording * Update readme (#7)
- Loading branch information
1 parent
2823e26
commit f01d497
Showing
5 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule Mongo.Auth.PLAIN do | ||
@moduledoc false | ||
alias Mongo.MongoDBConnection.Utils | ||
|
||
def auth({nil, nil}, _db, _s) do | ||
:ok | ||
end | ||
|
||
def auth({username, password}, _db, 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -199,5 +199,25 @@ defmodule Mongo.UrlParserTest do | |
username = Keyword.get(opts, :username) | ||
assert username == real_username | ||
end | ||
|
||
test "external auth source " do | ||
encoded_external_auth_source = URI.encode_www_form("$external") | ||
url = "mongodb://user:[email protected]:27017,seed2.domain.com:27017,seed3.domain.com:27017/db_name?replicaSet=set-name&authMechanism=PLAIN&authSource=#{encoded_external_auth_source}&tls=true" | ||
|
||
assert UrlParser.parse_url(url: url) |> Keyword.drop([:pw_safe]) == [ | ||
password: "*****", | ||
username: "user", | ||
database: "db_name", | ||
tls: true, | ||
auth_source: "$external", | ||
auth_mechanism: :plain, | ||
set_name: "set-name", | ||
seeds: [ | ||
"seed1.domain.com:27017", | ||
"seed2.domain.com:27017", | ||
"seed3.domain.com:27017" | ||
] | ||
] | ||
end | ||
end | ||
end |