Module: Moped::Authenticatable
- Included in:
- Connection
- Defined in:
- lib/moped/authenticatable.rb
Overview
Provides behaviour to nodes around authentication.
Instance Method Summary collapse
-
#apply_credentials(logins) ⇒ Object
Apply authentication credentials.
-
#credentials ⇒ Hash
Get the applied credentials.
-
#login(database, username, password) ⇒ Array
Login the user to the provided database with the supplied password.
-
#logout(database) ⇒ Array
Logout the user from the provided database.
Instance Method Details
#apply_credentials(logins) ⇒ Object
Apply authentication credentials.
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 ] login(database, username, password) end end @original_credentials = credentials.dup end self end |
#credentials ⇒ Hash
Get the applied credentials.
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.
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 login(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.
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 |