Module: Moped::Authenticatable

Included in:
Connection
Defined in:
lib/moped/authenticatable.rb

Overview

Provides behaviour to nodes around authentication.

Since:

  • 2.0.0

Instance Method Summary collapse

Instance Method Details

#apply_credentials(logins) ⇒ Object

Apply authentication credentials.

Examples:

Apply the authentication credentials.

node.apply_credentials({ "moped_test" => [ "user", "pass" ]})

Parameters:

  • credentials (Hash)

    The authentication credentials in the form: { database_name: [ user, password ]}

Returns:

  • (Object)

    The authenticated object.

Since:

  • 2.0.0



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/moped/authenticatable.rb', line 20

def apply_credentials(logins)
  unless credentials == logins
    logouts = credentials.keys - logins.keys
    logouts.each do |database|
      logout(database)
    end
    logins.each do |database, (username, password)|
      unless credentials[database] == [ username, password ]
        (database, username, password)
      end
    end
    @original_credentials = credentials.dup
  end
  self
end

#credentialsHash

Get the applied credentials.

Examples:

Get the applied credentials.

node.credentials

Returns:

  • (Hash)

    The credentials.

Since:

  • 2.0.0



44
45
46
# File 'lib/moped/authenticatable.rb', line 44

def credentials
  @credentials ||= {}
end

#login(database, username, password) ⇒ Array

Login the user to the provided database with the supplied password.

Examples:

Login the user to the database.

node.("moped_test", "user", "pass")

Parameters:

  • database (String)

    The database name.

  • username (String)

    The username.

  • password (String)

    The password.

Returns:

  • (Array)

    The username and password.

Raises:

Since:

  • 2.0.0



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/moped/authenticatable.rb', line 62

def (database, username, password)
  getnonce = Protocol::Command.new(database, getnonce: 1)
  self.write([getnonce])
  reply = self.receive_replies([getnonce]).first
  if getnonce.failure?(reply)
    return
  end
  result = getnonce.results(reply)

  authenticate = Protocol::Commands::Authenticate.new(database, username, password, result["nonce"])
  self.write([ authenticate ])
  document = self.read.documents.first

  unless result["ok"] == 1
    # See if we had connectivity issues so we can retry
    e = Errors::PotentialReconfiguration.new(authenticate, document)
    if e.reconfiguring_replica_set?
      raise Errors::ReplicaSetReconfigured.new(e.command, e.details)
    elsif e.connection_failure?
      raise Errors::ConnectionFailure.new(e.inspect)
    end

    raise Errors::AuthenticationFailure.new(authenticate, document)
  end
  credentials[database] = [username, password]
end

#logout(database) ⇒ Array

Logout the user from the provided database.

Examples:

Logout from the provided database.

node.logout("moped_test")

Parameters:

  • database (String)

    The database name.

Returns:

  • (Array)

    The username and password.

Since:

  • 2.0.0



99
100
101
102
103
104
105
106
107
# File 'lib/moped/authenticatable.rb', line 99

def logout(database)
  command = Protocol::Command.new(database, logout: 1)
  self.write([command])
  reply = self.receive_replies([command]).first
  if command.failure?(reply)
    return
  end
  credentials.delete(database)
end