Class: Mysql::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql/authenticator.rb,
lib/mysql/authenticator/sha256_password.rb,
lib/mysql/authenticator/caching_sha2_password.rb,
lib/mysql/authenticator/mysql_native_password.rb

Overview

authenticator

Defined Under Namespace

Classes: CachingSha2Password, DummyPlugin, MysqlNativePassword, Sha256Password

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol) ⇒ Authenticator

Returns a new instance of Authenticator.



23
24
25
# File 'lib/mysql/authenticator.rb', line 23

def initialize(protocol)
  @protocol = protocol
end

Class Method Details

.plugin_class(plugin) ⇒ Object

Parameters:

  • plugin (String)

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mysql/authenticator.rb', line 7

def self.plugin_class(plugin)
  return @plugins[plugin] if @plugins[plugin]

  raise ClientError, "invalid plugin name: #{plugin}" unless plugin.match?(/\A\w+\z/)
  begin
    require_relative "authenticator/#{plugin}"
  rescue LoadError
    return nil
  end
  class_name = plugin.gsub(/(?:^|_)(.)/){$1.upcase}
  raise ClientError, "#{class_name} is undefined" unless self.const_defined? class_name
  klass = self.const_get(class_name)
  @plugins[plugin] = klass
  return klass
end

Instance Method Details

#authenticate(user, passwd, db, scramble, plugin_name, connect_attrs) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mysql/authenticator.rb', line 37

def authenticate(user, passwd, db, scramble, plugin_name, connect_attrs)
  plugin = (get(plugin_name) || DummyPlugin).new(@protocol)
  pkt = plugin.authenticate(passwd, scramble) do |hashed|
    @protocol.write Protocol::AuthenticationPacket.serialize(@protocol.client_flags, 1024**3, @protocol.charset.number, user, hashed, db, plugin.name, connect_attrs)
  end
  while true
    res = Protocol::AuthenticationResultPacket.parse(pkt)
    case res.result
    when 0  # OK
      break
    when 2  # multi factor auth
      raise ClientError, 'multi factor authentication is not supported'
    when 254  # change auth plugin
      plugin = get!(res.auth_plugin).new(@protocol)
      pkt = plugin.authenticate(passwd, res.scramble) do |hashed|
        if passwd.nil? || passwd.empty?
          @protocol.write "\0"
        else
          @protocol.write hashed
        end
      end
    else
      raise ClientError, "invalid packet: #{pkt}"
    end
  end
end

#get(plugin) ⇒ Object

Parameters:

  • plugin (String)


28
29
30
# File 'lib/mysql/authenticator.rb', line 28

def get(plugin)
  self.class.plugin_class(plugin)
end

#get!(plugin) ⇒ Object

Parameters:

  • plugin (String)


33
34
35
# File 'lib/mysql/authenticator.rb', line 33

def get!(plugin)
  get(plugin) or raise ClientError, "unknown plugin: #{plugin}"
end