@@ -20,6 +20,7 @@ class SCRAM
2020 # the client and server.
2121 #
2222 # @since 2.0.0
23+ # @api private
2324 class Conversation
2425
2526 # The base client continue message.
@@ -103,13 +104,14 @@ class Conversation
103104 #
104105 # @param [ Protocol::Message ] reply The reply of the previous
105106 # message.
106- # @param [ Mongo::Server::Connection ] connection The connection being authenticated.
107+ # @param [ Server::Connection ] connection The connection being
108+ # authenticated.
107109 #
108- # @return [ Protocol::Query ] The next message to send.
110+ # @return [ Protocol::Message ] The next message to send.
109111 #
110112 # @since 2.0.0
111- def continue ( reply , connection = nil )
112- validate_first_message! ( reply )
113+ def continue ( reply , connection )
114+ validate_first_message! ( reply , connection . server )
113115
114116 # The salted password needs to be calculated now; otherwise, if the
115117 # client key is cached from a previous authentication, the salt in the
@@ -118,7 +120,10 @@ def continue(reply, connection = nil)
118120 salted_password
119121
120122 if connection && connection . features . op_msg_enabled?
121- selector = CLIENT_CONTINUE_MESSAGE . merge ( payload : client_final_message , conversationId : id )
123+ selector = CLIENT_CONTINUE_MESSAGE . merge (
124+ payload : client_final_message ,
125+ conversationId : id ,
126+ )
122127 selector [ Protocol ::Msg ::DATABASE_IDENTIFIER ] = user . auth_source
123128 cluster_time = connection . mongos? && connection . cluster_time
124129 selector [ Operation ::CLUSTER_TIME ] = cluster_time if cluster_time
@@ -127,29 +132,32 @@ def continue(reply, connection = nil)
127132 Protocol ::Query . new (
128133 user . auth_source ,
129134 Database ::COMMAND ,
130- CLIENT_CONTINUE_MESSAGE . merge ( payload : client_final_message , conversationId : id ) ,
131- limit : -1
135+ CLIENT_CONTINUE_MESSAGE . merge (
136+ payload : client_final_message ,
137+ conversationId : id ,
138+ ) ,
139+ limit : -1 ,
132140 )
133141 end
134142 end
135143
136144 # Finalize the SCRAM conversation. This is meant to be iterated until
137145 # the provided reply indicates the conversation is finished.
138146 #
139- # @example Finalize the conversation.
140- # conversation.finalize(reply)
141- #
142147 # @param [ Protocol::Message ] reply The reply of the previous
143148 # message.
144- # @param [ Mongo:: Server::Connection ] connection The connection being authenticated.
149+ # @param [ Server::Connection ] connection The connection being authenticated.
145150 #
146151 # @return [ Protocol::Query ] The next message to send.
147152 #
148153 # @since 2.0.0
149- def finalize ( reply , connection = nil )
150- validate_final_message! ( reply )
154+ def finalize ( reply , connection )
155+ validate_final_message! ( reply , connection . server )
151156 if connection && connection . features . op_msg_enabled?
152- selector = CLIENT_CONTINUE_MESSAGE . merge ( payload : client_empty_message , conversationId : id )
157+ selector = CLIENT_CONTINUE_MESSAGE . merge (
158+ payload : client_empty_message ,
159+ conversationId : id ,
160+ )
153161 selector [ Protocol ::Msg ::DATABASE_IDENTIFIER ] = user . auth_source
154162 cluster_time = connection . mongos? && connection . cluster_time
155163 selector [ Operation ::CLUSTER_TIME ] = cluster_time if cluster_time
@@ -158,24 +166,24 @@ def finalize(reply, connection = nil)
158166 Protocol ::Query . new (
159167 user . auth_source ,
160168 Database ::COMMAND ,
161- CLIENT_CONTINUE_MESSAGE . merge ( payload : client_empty_message , conversationId : id ) ,
162- limit : -1
169+ CLIENT_CONTINUE_MESSAGE . merge (
170+ payload : client_empty_message ,
171+ conversationId : id ,
172+ ) ,
173+ limit : -1 ,
163174 )
164175 end
165176 end
166177
167178 # Start the SCRAM conversation. This returns the first message that
168179 # needs to be sent to the server.
169180 #
170- # @example Start the conversation.
171- # conversation.start
172- #
173- # @param [ Mongo::Server::Connection ] connection The connection being authenticated.
181+ # @param [ Server::Connection ] connection The connection being authenticated.
174182 #
175183 # @return [ Protocol::Query ] The first SCRAM conversation message.
176184 #
177185 # @since 2.0.0
178- def start ( connection = nil )
186+ def start ( connection )
179187 if connection && connection . features . op_msg_enabled?
180188 selector = CLIENT_FIRST_MESSAGE . merge (
181189 payload : client_first_message , mechanism : full_mechanism )
@@ -189,7 +197,7 @@ def start(connection = nil)
189197 Database ::COMMAND ,
190198 CLIENT_FIRST_MESSAGE . merge (
191199 payload : client_first_message , mechanism : full_mechanism ) ,
192- limit : -1
200+ limit : -1 ,
193201 )
194202 end
195203 end
@@ -505,23 +513,24 @@ def compare_digest(a, b)
505513 check == 0
506514 end
507515
508- def validate_final_message! ( reply )
509- validate! ( reply )
516+ def validate_final_message! ( reply , server )
517+ validate! ( reply , server )
510518 unless compare_digest ( verifier , server_signature )
511519 raise Error ::InvalidSignature . new ( verifier , server_signature )
512520 end
513521 end
514522
515- def validate_first_message! ( reply )
516- validate! ( reply )
523+ def validate_first_message! ( reply , server )
524+ validate! ( reply , server )
517525 raise Error ::InvalidNonce . new ( nonce , rnonce ) unless rnonce . start_with? ( nonce )
518526 end
519527
520- def validate! ( reply )
528+ def validate! ( reply , server )
521529 if reply . documents [ 0 ] [ Operation ::Result ::OK ] != 1
522530 raise Unauthorized . new ( user ,
523531 used_mechanism : full_mechanism ,
524532 message : reply . documents [ 0 ] [ 'errmsg' ] ,
533+ server : server ,
525534 )
526535 end
527536 @reply = reply
0 commit comments