File tree Expand file tree Collapse file tree 8 files changed +76
-61
lines changed Expand file tree Collapse file tree 8 files changed +76
-61
lines changed Original file line number Diff line number Diff line change 12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- require 'mongo/auth/executable'
16
15
require 'mongo/auth/cr'
17
16
require 'mongo/auth/ldap'
18
17
require 'mongo/auth/scram'
Original file line number Diff line number Diff line change @@ -21,7 +21,21 @@ module Auth
21
21
#
22
22
# @since 2.0.0
23
23
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
25
39
26
40
# Log the user in on the given connection.
27
41
#
@@ -39,6 +53,19 @@ def login(connection)
39
53
reply = connection . dispatch ( [ conversation . continue ( reply ) ] )
40
54
conversation . finalize ( reply )
41
55
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
42
69
end
43
70
end
44
71
end
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -21,13 +21,26 @@ module Auth
21
21
#
22
22
# @since 2.0.0
23
23
class LDAP
24
- include Executable
25
24
26
25
# The authentication mechinism string.
27
26
#
28
27
# @since 2.0.0
29
28
MECHANISM = 'PLAIN' . freeze
30
29
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
31
44
# Log the user in on the given connection.
32
45
#
33
46
# @example Log the user in.
Original file line number Diff line number Diff line change @@ -21,13 +21,27 @@ module Auth
21
21
#
22
22
# @since 2.0.0
23
23
class SCRAM
24
- include Executable
25
24
26
25
# The authentication mechinism string.
27
26
#
28
27
# @since 2.0.0
29
28
MECHANISM = 'SCRAM-SHA-1' . freeze
30
29
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
+
31
45
# Log the user in on the given connection.
32
46
#
33
47
# @example Log the user in.
Original file line number Diff line number Diff line change @@ -111,7 +111,7 @@ class Conversation
111
111
def continue ( reply )
112
112
validate_first_message! ( reply )
113
113
Protocol ::Query . new (
114
- Database :: ADMIN ,
114
+ user . auth_source ,
115
115
Database ::COMMAND ,
116
116
CLIENT_CONTINUE_MESSAGE . merge ( payload : client_final_message , conversationId : id ) ,
117
117
limit : -1
@@ -133,7 +133,7 @@ def continue(reply)
133
133
def finalize ( reply )
134
134
validate_final_message! ( reply )
135
135
Protocol ::Query . new (
136
- Database :: ADMIN ,
136
+ user . auth_source ,
137
137
Database ::COMMAND ,
138
138
CLIENT_CONTINUE_MESSAGE . merge ( payload : client_empty_message , conversationId : id ) ,
139
139
limit : -1
@@ -151,7 +151,7 @@ def finalize(reply)
151
151
# @since 2.0.0
152
152
def start
153
153
Protocol ::Query . new (
154
- Database :: ADMIN ,
154
+ user . auth_source ,
155
155
Database ::COMMAND ,
156
156
CLIENT_FIRST_MESSAGE . merge ( payload : client_first_message , mechanism : SCRAM ::MECHANISM ) ,
157
157
limit : -1
Original file line number Diff line number Diff line change @@ -134,8 +134,8 @@ def hashed_password
134
134
#
135
135
# @since 2.0.0
136
136
def initialize ( options )
137
- @auth_source = options [ :auth_source ] || options [ :database ] || Database ::ADMIN
138
137
@database = options [ :database ] || Database ::ADMIN
138
+ @auth_source = options [ :auth_source ] || @database
139
139
@name = options [ :user ]
140
140
@password = options [ :password ] || options [ :pwd ]
141
141
@mechanism = options [ :auth_mech ] || :mongodb_cr
Original file line number Diff line number Diff line change @@ -21,13 +21,27 @@ module Auth
21
21
#
22
22
# @since 2.0.0
23
23
class X509
24
- include Executable
25
24
26
25
# The authentication mechinism string.
27
26
#
28
27
# @since 2.0.0
29
28
MECHANISM = 'MONGODB-X509' . freeze
30
29
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
+
31
45
# Log the user in on the given connection.
32
46
#
33
47
# @example Log the user in.
You can’t perform that action at this time.
0 commit comments