Class: Mongo::Auth::SCRAM::Conversation

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/auth/scram/conversation.rb

Overview

Defines behaviour around a single SCRAM-SHA-1 conversation between the client and server.

Since:

  • 2.0.0

Constant Summary

CLIENT_CONTINUE_MESSAGE =

The base client continue message.

Since:

  • 2.0.0

{ saslContinue: 1 }.freeze
CLIENT_FIRST_MESSAGE =

The base client first message.

Since:

  • 2.0.0

{ saslStart: 1, autoAuthorize: 1 }.freeze
CLIENT_KEY =

The client key string.

Since:

  • 2.0.0

'Client Key'.freeze
DIGEST =

The digest to use for encryption.

Since:

  • 2.0.0

OpenSSL::Digest::SHA1.new.freeze
DONE =

The key for the done field in the responses.

Since:

  • 2.0.0

'done'.freeze
ID =

The conversation id field.

Since:

  • 2.0.0

'conversationId'.freeze
ITERATIONS =

The iterations key in the responses.

Since:

  • 2.0.0

/i=(\d+)/.freeze
PAYLOAD =

The payload field.

Since:

  • 2.0.0

'payload'.freeze
RNONCE =

The rnonce key in the responses.

Since:

  • 2.0.0

/r=([^,]*)/.freeze
SALT =

The salt key in the responses.

Since:

  • 2.0.0

/s=([^,]*)/.freeze
SERVER_KEY =

The server key string.

Since:

  • 2.0.0

'Server Key'.freeze
VERIFIER =

The server signature verifier in the response.

Since:

  • 2.0.0

/v=([^,]*)/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Conversation

Create the new conversation.

Examples:

Create the new conversation.

Conversation.new(user)

Parameters:

  • user (Auth::User)

    The user to converse about.

Since:

  • 2.0.0



181
182
183
184
# File 'lib/mongo/auth/scram/conversation.rb', line 181

def initialize(user)
  @user = user
  @nonce = SecureRandom.base64
end

Instance Attribute Details

#nonceString (readonly)

Returns nonce The initial user nonce.

Returns:

  • (String)

    nonce The initial user nonce.

Since:

  • 2.0.0



89
90
91
# File 'lib/mongo/auth/scram/conversation.rb', line 89

def nonce
  @nonce
end

#replyProtocol::Reply (readonly)

Returns reply The current reply in the conversation.

Returns:

Since:

  • 2.0.0



93
94
95
# File 'lib/mongo/auth/scram/conversation.rb', line 93

def reply
  @reply
end

#userUser (readonly)

Returns user The user for the conversation.

Returns:

  • (User)

    user The user for the conversation.

Since:

  • 2.0.0



96
97
98
# File 'lib/mongo/auth/scram/conversation.rb', line 96

def user
  @user
end

Instance Method Details

#continue(reply) ⇒ Protocol::Query

Continue the SCRAM conversation. This sends the client final message to the server after setting the reply from the previous server communication.

Examples:

Continue the conversation.

conversation.continue(reply)

Parameters:

Returns:

Since:

  • 2.0.0



111
112
113
114
115
116
117
118
119
# File 'lib/mongo/auth/scram/conversation.rb', line 111

def continue(reply)
  validate_first_message!(reply)
  Protocol::Query.new(
    user.auth_source,
    Database::COMMAND,
    CLIENT_CONTINUE_MESSAGE.merge(payload: client_final_message, conversationId: id),
    limit: -1
  )
end

#finalize(reply) ⇒ Protocol::Query

Finalize the SCRAM conversation. This is meant to be iterated until the provided reply indicates the conversation is finished.

Examples:

Finalize the conversation.

conversation.finalize(reply)

Parameters:

Returns:

Since:

  • 2.0.0



133
134
135
136
137
138
139
140
141
# File 'lib/mongo/auth/scram/conversation.rb', line 133

def finalize(reply)
  validate_final_message!(reply)
  Protocol::Query.new(
    user.auth_source,
    Database::COMMAND,
    CLIENT_CONTINUE_MESSAGE.merge(payload: client_empty_message, conversationId: id),
    limit: -1
  )
end

#idInteger

Get the id of the conversation.

Examples:

Get the id of the conversation.

conversation.id

Returns:

  • (Integer)

    The conversation id.

Since:

  • 2.0.0



169
170
171
# File 'lib/mongo/auth/scram/conversation.rb', line 169

def id
  reply.documents[0][ID]
end

#startProtocol::Query

Start the SCRAM conversation. This returns the first message that needs to be send to the server.

Examples:

Start the conversation.

conversation.start

Returns:

Since:

  • 2.0.0



152
153
154
155
156
157
158
159
# File 'lib/mongo/auth/scram/conversation.rb', line 152

def start
  Protocol::Query.new(
    user.auth_source,
    Database::COMMAND,
    CLIENT_FIRST_MESSAGE.merge(payload: client_first_message, mechanism: SCRAM::MECHANISM),
    limit: -1
  )
end