Class: Mongo::Auth::CR::Conversation

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

Overview

Defines behaviour around a single MONGODB-CR conversation between the client and server.

Since:

  • 2.0.0

Constant Summary

LOGIN =

The login message base.

Since:

  • 2.0.0

{ authenticate: 1 }.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, "admin")

Parameters:

  • user (Auth::User)

    The user to converse about.

Since:

  • 2.0.0



123
124
125
# File 'lib/mongo/auth/cr/conversation.rb', line 123

def initialize(user)
  @user = user
end

Instance Attribute Details

#databaseString (readonly)

Returns database The database to authenticate against.

Returns:

  • (String)

    database The database to authenticate against.

Since:

  • 2.0.0



35
36
37
# File 'lib/mongo/auth/cr/conversation.rb', line 35

def database
  @database
end

#nonceString (readonly)

Returns nonce The initial auth nonce.

Returns:

  • (String)

    nonce The initial auth nonce.

Since:

  • 2.0.0



38
39
40
# File 'lib/mongo/auth/cr/conversation.rb', line 38

def nonce
  @nonce
end

#replyProtocol::Message (readonly)

Returns reply The current reply in the conversation.

Returns:

Since:

  • 2.0.0



32
33
34
# File 'lib/mongo/auth/cr/conversation.rb', line 32

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



41
42
43
# File 'lib/mongo/auth/cr/conversation.rb', line 41

def user
  @user
end

Instance Method Details

#continue(reply, connection = nil) ⇒ Protocol::Query

Continue the CR 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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mongo/auth/cr/conversation.rb', line 57

def continue(reply, connection = nil)
  validate!(reply)
  if connection && connection.features.op_msg_enabled?
    selector = LOGIN.merge(user: user.name, nonce: nonce, key: user.auth_key(nonce))
    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([:none], {}, selector)
  else
    Protocol::Query.new(
      user.auth_source,
      Database::COMMAND,
      LOGIN.merge(user: user.name, nonce: nonce, key: user.auth_key(nonce)),
      limit: -1
    )
  end
end

#finalize(reply, connection = nil) ⇒ Protocol::Query

Finalize the CR 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



87
88
89
# File 'lib/mongo/auth/cr/conversation.rb', line 87

def finalize(reply, connection = nil)
  validate!(reply)
end

#start(connection = nil) ⇒ Protocol::Query

Start the CR 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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mongo/auth/cr/conversation.rb', line 100

def start(connection = nil)
  if connection && connection.features.op_msg_enabled?
    selector = Auth::GET_NONCE.merge(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([:none], {}, selector)
  else
    Protocol::Query.new(
      user.auth_source,
      Database::COMMAND,
      Auth::GET_NONCE,
      limit: -1)
  end
end