Skip to content

Commit 66d84a6

Browse files
committed
RUBY-890 Auth refactor
1 parent b2aaba4 commit 66d84a6

File tree

8 files changed

+76
-61
lines changed

8 files changed

+76
-61
lines changed

lib/mongo/auth.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
require 'mongo/auth/executable'
1615
require 'mongo/auth/cr'
1716
require 'mongo/auth/ldap'
1817
require 'mongo/auth/scram'

lib/mongo/auth/cr.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,21 @@ module Auth
2121
#
2222
# @since 2.0.0
2323
class CR
24-
include Executable
24+
25+
# @return [ Mongo::Auth::User ] The user to authenticate.
26+
attr_reader :user
27+
28+
# Instantiate a new authenticator.
29+
#
30+
# @example Create the authenticator.
31+
# Mongo::Auth::X509.new(user)
32+
#
33+
# @param [ Mongo::Auth::User ] user The user to authenticate.
34+
#
35+
# @since 2.0.0
36+
def initialize(user)
37+
@user = user
38+
end
2539

2640
# Log the user in on the given connection.
2741
#
@@ -39,6 +53,19 @@ def login(connection)
3953
reply = connection.dispatch([ conversation.continue(reply) ])
4054
conversation.finalize(reply)
4155
end
56+
57+
private
58+
59+
# If we are on MongoDB 2.6 and higher, we *always* authorize against the
60+
# admin database. Otherwise for 2.4 and lower we authorize against the
61+
# auth source provided. The logic for that is encapsulated in the User class.
62+
def auth_database(connection)
63+
if connection.features.write_command_enabled?
64+
Database::ADMIN
65+
else
66+
user.auth_source
67+
end
68+
end
4269
end
4370
end
4471
end

lib/mongo/auth/executable.rb

Lines changed: 0 additions & 52 deletions
This file was deleted.

lib/mongo/auth/ldap.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,26 @@ module Auth
2121
#
2222
# @since 2.0.0
2323
class LDAP
24-
include Executable
2524

2625
# The authentication mechinism string.
2726
#
2827
# @since 2.0.0
2928
MECHANISM = 'PLAIN'.freeze
3029

30+
# @return [ Mongo::Auth::User ] The user to authenticate.
31+
attr_reader :user
32+
33+
# Instantiate a new authenticator.
34+
#
35+
# @example Create the authenticator.
36+
# Mongo::Auth::LDAP.new(user)
37+
#
38+
# @param [ Mongo::Auth::User ] user The user to authenticate.
39+
#
40+
# @since 2.0.0
41+
def initialize(user)
42+
@user = user
43+
end
3144
# Log the user in on the given connection.
3245
#
3346
# @example Log the user in.

lib/mongo/auth/scram.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,27 @@ module Auth
2121
#
2222
# @since 2.0.0
2323
class SCRAM
24-
include Executable
2524

2625
# The authentication mechinism string.
2726
#
2827
# @since 2.0.0
2928
MECHANISM = 'SCRAM-SHA-1'.freeze
3029

30+
# @return [ Mongo::Auth::User ] The user to authenticate.
31+
attr_reader :user
32+
33+
# Instantiate a new authenticator.
34+
#
35+
# @example Create the authenticator.
36+
# Mongo::Auth::SCRAM.new(user)
37+
#
38+
# @param [ Mongo::Auth::User ] user The user to authenticate.
39+
#
40+
# @since 2.0.0
41+
def initialize(user)
42+
@user = user
43+
end
44+
3145
# Log the user in on the given connection.
3246
#
3347
# @example Log the user in.

lib/mongo/auth/scram/conversation.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Conversation
111111
def continue(reply)
112112
validate_first_message!(reply)
113113
Protocol::Query.new(
114-
Database::ADMIN,
114+
user.auth_source,
115115
Database::COMMAND,
116116
CLIENT_CONTINUE_MESSAGE.merge(payload: client_final_message, conversationId: id),
117117
limit: -1
@@ -133,7 +133,7 @@ def continue(reply)
133133
def finalize(reply)
134134
validate_final_message!(reply)
135135
Protocol::Query.new(
136-
Database::ADMIN,
136+
user.auth_source,
137137
Database::COMMAND,
138138
CLIENT_CONTINUE_MESSAGE.merge(payload: client_empty_message, conversationId: id),
139139
limit: -1
@@ -151,7 +151,7 @@ def finalize(reply)
151151
# @since 2.0.0
152152
def start
153153
Protocol::Query.new(
154-
Database::ADMIN,
154+
user.auth_source,
155155
Database::COMMAND,
156156
CLIENT_FIRST_MESSAGE.merge(payload: client_first_message, mechanism: SCRAM::MECHANISM),
157157
limit: -1

lib/mongo/auth/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ def hashed_password
134134
#
135135
# @since 2.0.0
136136
def initialize(options)
137-
@auth_source = options[:auth_source] || options[:database] || Database::ADMIN
138137
@database = options[:database] || Database::ADMIN
138+
@auth_source = options[:auth_source] || @database
139139
@name = options[:user]
140140
@password = options[:password] || options[:pwd]
141141
@mechanism = options[:auth_mech] || :mongodb_cr

lib/mongo/auth/x509.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,27 @@ module Auth
2121
#
2222
# @since 2.0.0
2323
class X509
24-
include Executable
2524

2625
# The authentication mechinism string.
2726
#
2827
# @since 2.0.0
2928
MECHANISM = 'MONGODB-X509'.freeze
3029

30+
# @return [ Mongo::Auth::User ] The user to authenticate.
31+
attr_reader :user
32+
33+
# Instantiate a new authenticator.
34+
#
35+
# @example Create the authenticator.
36+
# Mongo::Auth::X509.new(user)
37+
#
38+
# @param [ Mongo::Auth::User ] user The user to authenticate.
39+
#
40+
# @since 2.0.0
41+
def initialize(user)
42+
@user = user
43+
end
44+
3145
# Log the user in on the given connection.
3246
#
3347
# @example Log the user in.

0 commit comments

Comments
 (0)