class Net::IMAP::SASL::Authenticators
SASL 验证器的注册表
注册的验证器必须响应 #new 或 #call(例如,类或 Proc),接收任何凭据和选项,并返回一个验证器实例。返回的对象代表一个单独的身份验证交换,并且不得重复用于多个身份验证尝试。
验证器实例对象必须响应 #process,接收服务器的挑战并返回客户端的响应。可选地,它还可以响应 #initial_response? 和 #done?。当 #initial_response? 返回 true 时,首次调用 #process 时可以使用 nil。当 #done? 返回 false 时,表示交换未完成,如果交换过早终止,则应引发异常。
有关示例,请参阅 PlainAuthenticator、XOAuth2Authenticator 和 ScramSHA1Authenticator 的源代码。
公共类方法
创建一个新的 Authenticators 注册表。
通常不直接实例化此类。使用 SASL.authenticators 来重用默认的全局注册表。
当 use_defaults 为 false 时,注册表将以空状态启动。当 use_deprecated 为 false 时,默认情况下将不包含已弃用的验证器。
# File net-imap-0.5.4/lib/net/imap/sasl/authenticators.rb, line 36 def initialize(use_defaults: true, use_deprecated: true) @authenticators = {} return unless use_defaults add_authenticator "Anonymous" add_authenticator "External" add_authenticator "OAuthBearer" add_authenticator "Plain" add_authenticator "Scram-SHA-1" add_authenticator "Scram-SHA-256" add_authenticator "XOAuth2" return unless use_deprecated add_authenticator "Login" # deprecated add_authenticator "Cram-MD5" # deprecated add_authenticator "Digest-MD5" # deprecated end
将机制名称规范化为大写字符串,并将下划线转换为破折号。
# File net-imap-0.5.4/lib/net/imap/sasl/authenticators.rb, line 26 def self.normalize_name(mechanism) -(mechanism.to_s.upcase.tr(?_, ?-)) end
公共实例方法
为 authenticator 注册一个验证器供使用。mechanism 是由 authenticator_class 实现的 SASL 机制的名称(例如,"PLAIN")。
如果 mechanism 指的是现有验证器,则将替换旧的验证器。
当只给出一个参数时,验证器类将从 Net::IMAP::SASL::#{name}Authenticator 延迟加载(保留大小写,并删除非字母数字字符)。
# File net-imap-0.5.4/lib/net/imap/sasl/authenticators.rb, line 71 def add_authenticator(name, authenticator = nil) authenticator ||= begin class_name = "#{name.gsub(/[^a-zA-Z0-9]/, "")}Authenticator".to_sym auth_class = nil ->(*creds, **props, &block) { auth_class ||= Net::IMAP::SASL.const_get(class_name) auth_class.new(*creds, **props, &block) } end key = Authenticators.normalize_name(name) @authenticators[key] = authenticator end
使用为 mechanism 注册的验证器构建验证器实例。返回的对象代表一个单独的身份验证交换,并且不得重复用于多个身份验证尝试。
所有参数(除了 mechanism)都会转发到已注册验证器的 #new 或 #call 方法。每个验证器都必须记录自己的参数。
- 注意
-
此方法仅供连接协议代码内部使用。协议客户端用户应参阅其客户端的文档,例如
Net::IMAP#authenticate。
# File net-imap-0.5.4/lib/net/imap/sasl/authenticators.rb, line 111 def authenticator(mechanism, ...) key = Authenticators.normalize_name(mechanism) auth = @authenticators.fetch(key) do raise ArgumentError, 'unknown auth type - "%s"' % key end auth.respond_to?(:new) ? auth.new(...) : auth.call(...) end
# File net-imap-0.5.4/lib/net/imap/sasl/authenticators.rb, line 90 def mechanism?(name) key = Authenticators.normalize_name(name) @authenticators.key?(key) end
返回所有已注册的 SASL 机制的名称。
# File net-imap-0.5.4/lib/net/imap/sasl/authenticators.rb, line 53 def names; @authenticators.keys end
删除为 name 注册的验证器
# File net-imap-0.5.4/lib/net/imap/sasl/authenticators.rb, line 85 def remove_authenticator(name) key = Authenticators.normalize_name(name) @authenticators.delete(key) end