Class: Mongo::Auth::User
- Inherits:
-
Object
- Object
- Mongo::Auth::User
- Defined in:
- lib/mongo/auth/user.rb,
lib/mongo/auth/user/view.rb
Overview
Represents a user in MongoDB.
Defined Under Namespace
Classes: View
Constant Summary collapse
- COLLECTION =
The users collection for the database.
'system.users'.freeze
Instance Attribute Summary collapse
-
#auth_mech_properties ⇒ Hash
readonly
The authentication mechanism properties.
-
#auth_source ⇒ String
readonly
The authorization source, either a database or external name.
-
#database ⇒ String
readonly
The database the user is created in.
-
#mechanism ⇒ Symbol
readonly
The authorization mechanism.
-
#name ⇒ String
readonly
The username.
-
#password ⇒ String
readonly
The cleartext password.
-
#roles ⇒ Array<String>
readonly
Roles The user roles.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Determine if this user is equal to another.
-
#auth_key(nonce) ⇒ String
Get an authentication key for the user based on a nonce from the server.
-
#encoded_name ⇒ String
Get the UTF-8 encoded name with escaped special characters for use with SCRAM authorization.
-
#hash ⇒ String
Get the hash key for the user.
-
#hashed_password ⇒ String
Get the user’s hashed password.
-
#initialize(options) ⇒ User
constructor
Create the new user.
-
#spec ⇒ Hash
Get the specification for the user, used in creation.
Constructor Details
#initialize(options) ⇒ User
Create the new user.
136 137 138 139 140 141 142 143 144 |
# File 'lib/mongo/auth/user.rb', line 136 def initialize() @database = [:database] || Database::ADMIN @auth_source = [:auth_source] || @database @name = [:user] @password = [:password] || [:pwd] @mechanism = [:auth_mech] || :mongodb_cr @auth_mech_properties = [:auth_mech_properties] || {} @roles = [:roles] || [] end |
Instance Attribute Details
#auth_mech_properties ⇒ Hash (readonly)
Returns The authentication mechanism properties.
38 39 40 |
# File 'lib/mongo/auth/user.rb', line 38 def auth_mech_properties @auth_mech_properties end |
#auth_source ⇒ String (readonly)
Returns The authorization source, either a database or external name.
32 33 34 |
# File 'lib/mongo/auth/user.rb', line 32 def auth_source @auth_source end |
#database ⇒ String (readonly)
Returns The database the user is created in.
35 36 37 |
# File 'lib/mongo/auth/user.rb', line 35 def database @database end |
#mechanism ⇒ Symbol (readonly)
Returns The authorization mechanism.
41 42 43 |
# File 'lib/mongo/auth/user.rb', line 41 def mechanism @mechanism end |
#name ⇒ String (readonly)
Returns The username.
44 45 46 |
# File 'lib/mongo/auth/user.rb', line 44 def name @name end |
#password ⇒ String (readonly)
Returns The cleartext password.
47 48 49 |
# File 'lib/mongo/auth/user.rb', line 47 def password @password end |
#roles ⇒ Array<String> (readonly)
Returns roles The user roles.
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.
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.
78 79 80 |
# File 'lib/mongo/auth/user.rb', line 78 def auth_key(nonce) Digest::MD5.hexdigest("#{nonce}#{name}#{hashed_password}") end |
#encoded_name ⇒ String
Get the UTF-8 encoded name with escaped special characters for use with SCRAM authorization.
91 92 93 |
# File 'lib/mongo/auth/user.rb', line 91 def encoded_name name.encode(BSON::UTF8).gsub('=','=3D').gsub(',','=2C') end |
#hash ⇒ String
Get the hash key for the user.
103 104 105 |
# File 'lib/mongo/auth/user.rb', line 103 def hash [ name, database, password ].hash end |
#hashed_password ⇒ String
Get the user’s hashed password.
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 |
#spec ⇒ Hash
Get the specification for the user, used in creation.
154 155 156 |
# File 'lib/mongo/auth/user.rb', line 154 def spec { pwd: hashed_password, roles: roles } end |