123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Auth::User

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/mongo/auth/user.rb,
lib/mongo/auth/user/view.rb

Overview

Represents a user in MongoDB.

Since:

  • 2.0.0

Constant Summary

::Mongo::Loggable - Included

PREFIX

Class Method Summary

Instance Attribute Summary

Instance Method Summary

::Mongo::Loggable - Included

#log_debug

Convenience method to log debug messages with the standard prefix.

#log_error

Convenience method to log error messages with the standard prefix.

#log_fatal

Convenience method to log fatal messages with the standard prefix.

#log_info

Convenience method to log info messages with the standard prefix.

#log_warn

Convenience method to log warn messages with the standard prefix.

#logger

Get the logger instance.

#_mongo_log_prefix, #format_message

Constructor Details

.new(options) ⇒ User

Create the new user.

Examples:

Create a new user.

Mongo::Auth::User.new(options)

Parameters:

  • options (Hash)

    The options to create the user from.

Options Hash (options):

  • :auth_source (String)

    The authorization database or external source.

  • :database (String)

    The database the user is authorized for.

  • :user (String)

    The user name.

  • :password (String)

    The user’s password.

  • :pwd (String)

    Legacy option for the user’s password. If :password and :pwd are both specified, :password takes precedence.

  • :auth_mech (Symbol)

    The authorization mechanism.

  • roles (Array<String>, Array<Hash>)

    The user roles.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 163

def initialize(options)
  @database = options[:database] || Database::ADMIN
  @auth_source = options[:auth_source] || self.class.default_auth_source(options)
  @name = options[:user]
  @password = options[:password] || options[:pwd]
  @mechanism = options[:auth_mech]
  if @mechanism
    # Since the driver must select an authentication class for
    # the specified mechanism, mechanisms that the driver does not
    # know about, and cannot translate to an authentication class,
    # need to be rejected.
    unless @mechanism.is_a?(Symbol)
      # Although we documented auth_mech option as being a symbol, we
      # have not enforced this; warn, reject in lint mode
      if Lint.enabled?
        raise Error::LintError, "Auth mechanism #{@mechanism.inspect} must be specified as a symbol"
      else
        log_warn("Auth mechanism #{@mechanism.inspect} should be specified as a symbol")
        @mechanism = @mechanism.to_sym
      end
    end
    unless Auth::SOURCES.key?(@mechanism)
      raise InvalidMechanism.new(options[:auth_mech])
    end
  end
  @auth_mech_properties = options[:auth_mech_properties] || {}
  @roles = options[:roles] || []
end

Class Method Details

.default_auth_source(options) (private)

This method is for internal use only.

Generate default auth source based on the ::Mongo::URI and options

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 213

def self.default_auth_source(options)
  case options[:auth_mech]
  when :aws, :gssapi, :mongodb_x509
    '$external'
  when :plain
    options[:database] || '$external'
  else
    options[:database] || Database::ADMIN
  end
end

Instance Attribute Details

#auth_mech_propertiesHash (readonly)

Returns:

  • (Hash)

    The authentication mechanism properties.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 37

attr_reader :auth_mech_properties

#auth_sourceString (readonly)

Returns:

  • (String)

    The authorization source, either a database or external name.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 31

attr_reader :auth_source

#databaseString (readonly)

Returns:

  • (String)

    The database the user is created in.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 34

attr_reader :database

#mechanismSymbol (readonly)

Returns:

  • (Symbol)

    The authorization mechanism.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 40

attr_reader :mechanism

#nameString (readonly)

Returns:

  • (String)

    The username.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 43

attr_reader :name

#passwordString (readonly)

Returns:

  • (String)

    The cleartext password.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 46

attr_reader :password

#rolesArray<String> (readonly)

Returns:

  • (Array<String>)

    roles The user roles.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 49

attr_reader :roles

Instance Method Details

#==(other) ⇒ true, false

Determine if this user is equal to another.

Examples:

Check user equality.

user == other

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 69

def ==(other)
  return false unless other.is_a?(User)
  name == other.name && database == other.database && password == other.password
end

#auth_key(nonce) ⇒ String

Get an authentication key for the user based on a nonce from the server.

Examples:

Get the authentication key.

user.auth_key(nonce)

Parameters:

  • nonce (String)

    The response from the server.

Returns:

  • (String)

    The authentication key.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 85

def auth_key(nonce)
  Digest::MD5.hexdigest("#{nonce}#{name}#{hashed_password}")
end

#encoded_nameString

Get the UTF-8 encoded name with escaped special characters for use with SCRAM authorization.

Examples:

Get the encoded name.

user.encoded_name

Returns:

  • (String)

    The encoded user name.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 98

def encoded_name
  name.encode(BSON::UTF8).gsub('=','=3D').gsub(',','=2C')
end

#hashString

Get the hash key for the user.

Examples:

Get the hash key.

user.hash

Returns:

  • (String)

    The user hash key.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 110

def hash
  [ name, database, password ].hash
end

#hashed_passwordString

Get the user’s hashed password for SCRAM-SHA-1.

Examples:

Get the user’s hashed password.

user.hashed_password

Returns:

  • (String)

    The hashed password.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 122

def hashed_password
  unless password
    raise Error::MissingPassword
  end

  @hashed_password ||= Digest::MD5.hexdigest("#{name}:mongo:#{password}").encode(BSON::UTF8)
end

#options

This method is for internal use only.

::Mongo::Loggable requires an options attribute. We don’t have any options hence provide this as a stub.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 55

def options
  {}
end

#sasl_prepped_password

This method is for internal use only.

Get the user’s stringprepped password for SCRAM-SHA-256.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 133

def sasl_prepped_password
  unless password
    raise Error::MissingPassword
  end

  @sasl_prepped_password ||= StringPrep.prepare(password,
    StringPrep::Profiles::SASL::MAPPINGS,
    StringPrep::Profiles::SASL::PROHIBITED,
    normalize: true, bidi: true).encode(BSON::UTF8)
end

#specHash

Get the specification for the user, used in creation.

Examples:

Get the user’s specification.

user.spec

Returns:

  • (Hash)

    The user spec.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user.rb', line 200

def spec
  {roles: roles}.tap do |spec|
    if password
      spec[:pwd] = password
    end
  end
end