Class: Mongo::Auth::SCRAM

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

Overview

Defines behaviour for SCRAM authentication.

Since:

  • 2.0.0

Defined Under Namespace

Classes: Conversation

Constant Summary collapse

SCRAM_SHA_1_MECHANISM =

The authentication mechanism string for SCRAM-SHA-1.

Since:

  • 2.6.0

'SCRAM-SHA-1'.freeze
SCRAM_SHA_256_MECHANISM =

The authentication mechanism string for SCRAM-SHA-256.

Since:

  • 2.6.0

'SCRAM-SHA-256'.freeze
MECHANISMS =

Map the user-specified authentication mechanism to the proper names of the mechanisms.

Since:

  • 2.6.0

{
  scram: SCRAM_SHA_1_MECHANISM,
  scram256: SCRAM_SHA_256_MECHANISM
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ SCRAM

Instantiate a new authenticator.

Examples:

Create the authenticator.

Mongo::Auth::SCRAM.new(user)

Parameters:

Since:

  • 2.0.0



55
56
57
# File 'lib/mongo/auth/scram.rb', line 55

def initialize(user)
  @user = user
end

Instance Attribute Details

#userMongo::Auth::User (readonly)

Returns The user to authenticate.

Returns:

Since:

  • 2.0.0



45
46
47
# File 'lib/mongo/auth/scram.rb', line 45

def user
  @user
end

Instance Method Details

#login(connection, mechanism = nil) ⇒ Protocol::Message

Log the user in on the given connection.

Examples:

Log the user in.

user.(connection)

Parameters:

  • connection (Mongo::Connection)

    The connection to log into. on.

  • mechanism (String) (defaults to: nil)

    The auth mechanism to use (either 'SCRAM-SHA-1' or 'SCRAM-SHA-256');

Returns:

Since:

  • 2.0.0



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mongo/auth/scram.rb', line 72

def (connection, mechanism = nil)
  mechanism ||= user.mechanism || :scram
  conversation = Conversation.new(user, MECHANISMS[mechanism])
  reply = connection.dispatch([ conversation.start(connection) ])
  connection.update_cluster_time(Operation::Result.new(reply))
  reply = connection.dispatch([ conversation.continue(reply, connection) ])
  connection.update_cluster_time(Operation::Result.new(reply))
  until reply.documents[0][Conversation::DONE]
    reply = connection.dispatch([ conversation.finalize(reply, connection) ])
    connection.update_cluster_time(Operation::Result.new(reply))
  end
  reply
end