Class: Mongo::Auth::SCRAM::Conversation
- Inherits:
-
Object
- Object
- Mongo::Auth::SCRAM::Conversation
- Defined in:
- lib/mongo/auth/scram/conversation.rb
Overview
Defines behavior around a single SCRAM-SHA-1 conversation between the client and server.
Constant Summary collapse
- CLIENT_CONTINUE_MESSAGE =
The base client continue message.
{ saslContinue: 1 }.freeze
- CLIENT_FIRST_MESSAGE =
The base client first message.
{ saslStart: 1, autoAuthorize: 1 }.freeze
- CLIENT_KEY =
The client key string.
'Client Key'.freeze
- DONE =
The key for the done field in the responses.
'done'.freeze
- ID =
The conversation id field.
'conversationId'.freeze
- ITERATIONS =
The iterations key in the responses.
/i=(\d+)/.freeze
- MIN_ITER_COUNT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The minimum iteration count for SCRAM-SHA-256.
4096
- PAYLOAD =
The payload field.
'payload'.freeze
- RNONCE =
The rnonce key in the responses.
/r=([^,]*)/.freeze
- SALT =
The salt key in the responses.
/s=([^,]*)/.freeze
- SERVER_KEY =
The server key string.
'Server Key'.freeze
- VERIFIER =
The server signature verifier in the response.
/v=([^,]*)/.freeze
Instance Attribute Summary collapse
-
#nonce ⇒ String
readonly
Nonce The initial user nonce.
-
#reply ⇒ Protocol::Message
readonly
Reply The current reply in the conversation.
-
#user ⇒ User
readonly
User The user for the conversation.
Instance Method Summary collapse
-
#continue(reply, connection = nil) ⇒ Protocol::Query
Continue the SCRAM conversation.
-
#finalize(reply, connection = nil) ⇒ Protocol::Query
Finalize the SCRAM conversation.
-
#id ⇒ Integer
Get the id of the conversation.
-
#initialize(user, mechanism) ⇒ Conversation
constructor
Create the new conversation.
-
#start(connection = nil) ⇒ Protocol::Query
Start the SCRAM conversation.
Constructor Details
#initialize(user, mechanism) ⇒ Conversation
Create the new conversation.
218 219 220 221 222 223 |
# File 'lib/mongo/auth/scram/conversation.rb', line 218 def initialize(user, mechanism) @user = user @nonce = SecureRandom.base64 @client_key = user.send(:client_key) @mechanism = mechanism end |
Instance Attribute Details
#nonce ⇒ String (readonly)
Returns nonce The initial user nonce.
91 92 93 |
# File 'lib/mongo/auth/scram/conversation.rb', line 91 def nonce @nonce end |
#reply ⇒ Protocol::Message (readonly)
Returns reply The current reply in the conversation.
95 96 97 |
# File 'lib/mongo/auth/scram/conversation.rb', line 95 def reply @reply end |
#user ⇒ User (readonly)
Returns user The user for the conversation.
98 99 100 |
# File 'lib/mongo/auth/scram/conversation.rb', line 98 def user @user end |
Instance Method Details
#continue(reply, connection = nil) ⇒ Protocol::Query
Continue the SCRAM conversation. This sends the client final message to the server after setting the reply from the previous server communication.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mongo/auth/scram/conversation.rb', line 114 def continue(reply, connection = nil) (reply) # The salted password needs to be calculated now; otherwise, if the # client key is cached from a previous authentication, the salt in the # reply will no longer be available for when the salted password is # needed to calculate the server key. salted_password if connection && connection.features.op_msg_enabled? selector = CLIENT_CONTINUE_MESSAGE.merge(payload: , conversationId: id) selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source cluster_time = connection.mongos? && connection.cluster_time selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time Protocol::Msg.new([], {}, selector) else Protocol::Query.new( user.auth_source, Database::COMMAND, CLIENT_CONTINUE_MESSAGE.merge(payload: , conversationId: id), limit: -1 ) end end |
#finalize(reply, connection = nil) ⇒ Protocol::Query
Finalize the SCRAM conversation. This is meant to be iterated until the provided reply indicates the conversation is finished.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/mongo/auth/scram/conversation.rb', line 152 def finalize(reply, connection = nil) (reply) if connection && connection.features.op_msg_enabled? selector = CLIENT_CONTINUE_MESSAGE.merge(payload: , conversationId: id) selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source cluster_time = connection.mongos? && connection.cluster_time selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time Protocol::Msg.new([], {}, selector) else Protocol::Query.new( user.auth_source, Database::COMMAND, CLIENT_CONTINUE_MESSAGE.merge(payload: , conversationId: id), limit: -1 ) end end |
#id ⇒ Integer
Get the id of the conversation.
206 207 208 |
# File 'lib/mongo/auth/scram/conversation.rb', line 206 def id reply.documents[0][ID] end |
#start(connection = nil) ⇒ Protocol::Query
Start the SCRAM conversation. This returns the first message that needs to be send to the server.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mongo/auth/scram/conversation.rb', line 181 def start(connection = nil) if connection && connection.features.op_msg_enabled? selector = CLIENT_FIRST_MESSAGE.merge(payload: , mechanism: @mechanism) selector[Protocol::Msg::DATABASE_IDENTIFIER] = user.auth_source cluster_time = connection.mongos? && connection.cluster_time selector[Operation::CLUSTER_TIME] = cluster_time if cluster_time Protocol::Msg.new([], {}, selector) else Protocol::Query.new( user.auth_source, Database::COMMAND, CLIENT_FIRST_MESSAGE.merge(payload: , mechanism: @mechanism), limit: -1 ) end end |