Class: Mongo::Auth::User

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/auth/user.rb,
lib/mongo/auth/user/view.rb

Overview

Represents a user in MongoDB.

Since:

  • 2.0.0

Defined Under Namespace

Classes: View

Constant Summary

COLLECTION =

The users collection for the database.

Since:

  • 2.0.0

'system.users'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(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.

  • :auth_mech (Symbol)

    The authorization mechanism.

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

    The user roles.

Since:

  • 2.0.0



136
137
138
139
140
141
142
143
144
# File 'lib/mongo/auth/user.rb', line 136

def initialize(options)
  @database = options[:database] || Database::ADMIN
  @auth_source = options[:auth_source] || @database
  @name = options[:user]
  @password = options[:password] || options[:pwd]
  @mechanism = options[:auth_mech] || :mongodb_cr
  @auth_mech_properties = options[:auth_mech_properties] || {}
  @roles = options[:roles] || []
end

Instance Attribute Details

#auth_mech_propertiesHash (readonly)

Returns The authentication mechanism properties.

Returns:

  • (Hash)

    The authentication mechanism properties.

Since:

  • 2.0.0



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

def auth_mech_properties
  @auth_mech_properties
end

#auth_sourceString (readonly)

Returns The authorization source, either a database or external name.

Returns:

  • (String)

    The authorization source, either a database or external name.

Since:

  • 2.0.0



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

def auth_source
  @auth_source
end

#databaseString (readonly)

Returns The database the user is created in.

Returns:

  • (String)

    The database the user is created in.

Since:

  • 2.0.0



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

def database
  @database
end

#mechanismSymbol (readonly)

Returns The authorization mechanism.

Returns:

  • (Symbol)

    The authorization mechanism.

Since:

  • 2.0.0



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

def mechanism
  @mechanism
end

#nameString (readonly)

Returns The username.

Returns:

  • (String)

    The username.

Since:

  • 2.0.0



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

def name
  @name
end

#passwordString (readonly)

Returns The cleartext password.

Returns:

  • (String)

    The cleartext password.

Since:

  • 2.0.0



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

def password
  @password
end

#rolesArray<String> (readonly)

Returns roles The user roles.

Returns:

  • (Array<String>)

    roles The user roles.

Since:

  • 2.0.0



50
51
52
# File 'lib/mongo/auth/user.rb', line 50

def roles
  @roles
end

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



62
63
64
65
# File 'lib/mongo/auth/user.rb', line 62

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



78
79
80
# File 'lib/mongo/auth/user.rb', line 78

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



91
92
93
# File 'lib/mongo/auth/user.rb', line 91

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



103
104
105
# File 'lib/mongo/auth/user.rb', line 103

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

#hashed_passwordString

Get the user's hashed password.

Examples:

Get the user's hashed password.

user.hashed_password

Returns:

  • (String)

    The hashed password.

Since:

  • 2.0.0



115
116
117
# File 'lib/mongo/auth/user.rb', line 115

def hashed_password
  @hashed_password ||= Digest::MD5.hexdigest("#{name}:mongo:#{password}").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



154
155
156
# File 'lib/mongo/auth/user.rb', line 154

def spec
  { pwd: hashed_password, roles: roles }
end