类:Mongo::Auth::User

继承:
对象
  • 对象
显示全部
包括:
Loggable
定义于:
lib/ Mongo/auth/user.rb
lib/ Mongo/auth/user/view.rb

Overview

代表MongoDB中的用户。

由于:

  • 2.0.0

在命名空间下定义

类: 查看

常量摘要

Loggable中包含的常量

Loggable::PREFIX

实例属性摘要折叠

类方法摘要折叠

实例方法摘要折叠

Loggable中包含的方法

#log_debug#log_error#log_ Fatal#log_info#log_warn#logger

构造函数详情

#initialize (options) ⇒用户

创建新用户。

例子:

创建一个新用户。

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

参数:

  • 选项 (哈希)

    用于创建用户的选项。

选项哈希 ( options ):

  • :auth_source string

    授权数据库或外部源。

  • :database string

    用户已获得授权的数据库。

  • : user string

    用户名。

  • :password string

    用户的密码。

  • :pwd string

    用户密码的旧版选项。如果同时指定了 :password 和 :pwd,则 :password 优先。

  • :auth_mech 符号

    授权机制。

  • 角色 ( Array<String> , Array<Hash> )

    用户角色。

由于:

  • 2.0.0



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ Mongo/auth/user.rb', line 158

def 初始化(选项)
  @database = 选项[:database] || Database::ADMIN
  @auth_source = 选项[:auth_source] || self.class.default_auth_source(选项)
  @name = 选项[: user]
  @password = 选项[:password] || 选项[:pwd]
  @chanism = 选项[:auth_mech]
  if @chanism
    # 由于驱动程序必须选择身份验证类
    # 指定的机制,驱动程序没有的机制
    # 了解,但无法转换为身份验证类,
    # 需要被拒绝。
    除非 @chanism.is_a?(符号)
      # 虽然我们将 auth_mech 选项记录为一个符号,但我们
      # 尚未强制执行此操作;在 lint模式警告、拒绝
      if Lint.已启用?
        提高 错误::LintError, " Auth Mechanism #{ @ Mechanism . Inspect }必须指定为符号"
      end

      log_warn(" Auth Mechanism #{ @ Mechanism . Inspect }应指定为符号")
      @chanism = @chanism.to_sym

    end
    提高 InvalidMechanism.new(选项[:auth_mech]) 除非 auth::来源.键?(@chanism)
  end
  @auth_mech_properties = 选项[:auth_mech_properties] || {}
  @roles = 选项[:roles] || []
end

实例属性详细信息

# auth_mech_properties哈希(只读)

返回身份验证机制属性。

返回:

  • (哈希)

    身份验证机制属性。

由于:

  • 2.0.0



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

def auth_mech_properties
  @auth_mech_properties
end

# auth_sourcestring (只读)

返回 授权源,可以是数据库或外部名称。

返回:

  • ( string )

    授权源,可以是数据库或外部名称。

由于:

  • 2.0.0



29
30
31
# File 'lib/ Mongo/auth/user.rb', line 29

def auth_source
  @auth_source
end

#数据库string (只读)

返回创建用户的数据库。

返回:

  • ( string )

    在其中创建用户的数据库。

由于:

  • 2.0.0



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

def database
  @database
end

#机制符号(只读)

返回授权机制。

返回:

  • (符号)

    授权机制。

由于:

  • 2.0.0



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

def 机制
  @chanism
end

# namestring (readonly)

返回 用户名。

返回:

  • ( string )

    用户名。

由于:

  • 2.0.0



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

def 名称
  @name
end

#密码string (只读)

返回 明文密码。

返回:

  • ( string )

    明文密码。

由于:

  • 2.0.0



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

def 密码
  @password
end

# rolesArray<String> (只读)

返回角色 用户角色。

返回:

  • ( Array<String> )

    roles 用户角色。

由于:

  • 2.0.0



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

def 角色
  @roles
end

类方法详细信息

default_auth_source (options) ⇒ 对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

根据 URI 和选项生成默认身份验证源

由于:

  • 2.0.0



203
204
205
206
207
208
209
210
211
212
# File 'lib/ Mongo/auth/user.rb', line 203

def self.default_auth_source(选项)
  案例 选项[:auth_mech]
  when :aws, :gssapi, :mongodb_x 509
    ' $external '
  when :plain
    选项[:database] || ' $external '
  else
    选项[:database] || Database::ADMIN
  end
end

实例方法详细信息

# == (other) ⇒ true , false

确定此用户是否与另一个用户相同。

例子:

检查用户相等性。

user == other

参数:

  • 其他 ( Object )

    要比较的对象。

返回:

  • ( true , false )

    如果对象相等。

由于:

  • 2.0.0



67
68
69
70
71
# File 'lib/ Mongo/auth/user.rb', line 67

def ==(其他)
  return false 除非 其他.is_a?(user)

  名称 == 其他.名称 && database == 其他.database && 密码 == 其他.密码
end

# auth_key (nonce) ⇒ string

根据服务器的随机数获取用户的身份验证密钥。

例子:

获取身份验证密钥。

user.auth_key(nonce)

参数:

  • 随机数 ( string )

    来自服务器的响应。

返回:

  • ( string )

    身份验证密钥。

由于:

  • 2.0.0



84
85
86
# File 'lib/ Mongo/auth/user.rb', line 84

def auth_key(随机数)
  摘要::MD5.hexdigest(" #{ nonce } #{ name } #{ hashed_password } ")
end

# Encoded_namestring

获取带有转义特殊字符的 UTF- 8编码名称,以用于SCRAM授权。

例子:

获取编码后的名称。

user.encoded_name

返回:

  • ( string )

    编码后的用户名。

由于:

  • 2.0.0



97
98
99
# File 'lib/ Mongo/auth/user.rb', line 97

def Encoded_name
  名称.encode(BSON::UTF8).gsub(' = ', '=3D').gsub(' , ', ' = 2 C ')
end

#哈希string

获取用户的哈希键。

例子:

获取哈希键。

user.hash

返回:

  • ( string )

    用户哈希键。

由于:

  • 2.0.0



109
110
111
# File 'lib/ Mongo/auth/user.rb', line 109

def 哈希
  [ 名称, database, 密码 ].哈希
end

# hashed_passwordstring

获取用户的SCRAM -SHA-1哈希密码。

例子:

获取用户的哈希密码。

user.hashed_password

返回:

  • ( string )

    哈希密码。

引发:

由于:

  • 2.0.0



121
122
123
124
125
# File 'lib/ Mongo/auth/user.rb', line 121

def hashed_password
  提高 错误::MissingPassword 除非 密码

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

# options对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

Loggable 需要一个选项属性。我们没有任何选项,因此将其作为存根提供。

由于:

  • 2.0.0



53
54
55
# File 'lib/ Mongo/auth/user.rb', line 53

def 选项
  {}
end

# sasl_prepped_password对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

获取适用于SCRAM-SHA-256 的用户字符串准备好的密码。

引发:

由于:

  • 2.0.0



130
131
132
133
134
135
136
137
# File 'lib/ Mongo/auth/user.rb', line 130

def sasl_prepped_password
  提高 错误::MissingPassword 除非 密码

  @sasl_prepped_password ||= StringPrep.准备(密码,
                                                StringPrep::个人资料::sasl::mappings,
                                                StringPrep::个人资料::sasl::禁止,
                                                规范化: true, bidi: true).encode(BSON::UTF8)
end

# spec哈希

获取创建时使用的用户规范。

例子:

获取用户的规范。

user.spec

返回:

  • (哈希)

    用户规范。

由于:

  • 2.0.0



194
195
196
197
198
# File 'lib/ Mongo/auth/user.rb', line 194

def spec
  { 角色: 角色 }.点击 do |spec|
    spec[:pwd] = 密码 if 密码
  end
end