|
| 1 | +require 'securerandom' |
| 2 | +require 'base64' |
| 3 | + |
| 4 | +module Kafka |
| 5 | + |
| 6 | + class SaslScramAuthenticator |
| 7 | + MECHANISMS = { |
| 8 | + sha256: { |
| 9 | + mechanism: 'SHA-256' |
| 10 | + }, |
| 11 | + sha512: { |
| 12 | + mechanism: 'SHA-512' |
| 13 | + } |
| 14 | + }.freeze |
| 15 | + |
| 16 | + VALID_MECHANISMS = %w{sha256 sha512}.freeze |
| 17 | + |
| 18 | + def initialize(username, password, mechanism: 'sha256', logger: nil, connection:) |
| 19 | + unless VALID_MECHANISMS.include?(mechanism) |
| 20 | + raise Kafka::SaslScramError, "SCRAM mechanism #{mechanism} is not supported." |
| 21 | + end |
| 22 | + @username = username |
| 23 | + @password = password |
| 24 | + @mechanism = MECHANISMS[mechanism.to_sym][:mechanism] |
| 25 | + @logger = logger |
| 26 | + @connection = connection |
| 27 | + end |
| 28 | + |
| 29 | + def authenticate! |
| 30 | + response = @connection.send_request(Kafka::Protocol::SaslHandshakeRequest.new('SCRAM-' + @mechanism)) |
| 31 | + |
| 32 | + unless response.error_code == 0 && response.enabled_mechanisms.include?('SCRAM-' + @mechanism) |
| 33 | + raise Kafka::SaslScramError, "SCRAM-#{@mechanism} is not supported." |
| 34 | + end |
| 35 | + |
| 36 | + log_debug "Authenticating #{@username} with SASL SCRAM #{@mechanism}" |
| 37 | + |
| 38 | + @encoder = @connection.encoder |
| 39 | + @decoder = @connection.decoder |
| 40 | + |
| 41 | + begin |
| 42 | + msg = first_message |
| 43 | + log_debug "Sending first client SASL SCRAM message: #{msg}" |
| 44 | + @encoder.write_bytes(msg) |
| 45 | + |
| 46 | + @server_first_message = @decoder.bytes |
| 47 | + log_debug "Received first server SASL SCRAM message: #{@server_first_message}" |
| 48 | + |
| 49 | + msg = final_message |
| 50 | + log_debug "Sending final client SASL SCRAM message: #{msg}" |
| 51 | + @encoder.write_bytes(msg) |
| 52 | + |
| 53 | + response = parse_response(@decoder.bytes) |
| 54 | + log_debug "Received last server SASL SCRAM message: #{response}" |
| 55 | + |
| 56 | + raise FailedScramAuthentication, response['e'] if response['e'] |
| 57 | + raise FailedScramAuthentication, 'Invalid server signature' if response['v'] != server_signature |
| 58 | + rescue EOFError => e |
| 59 | + raise FailedScramAuthentication, e.message |
| 60 | + end |
| 61 | + log_debug "SASL SCRAM authentication successful" |
| 62 | + end |
| 63 | + |
| 64 | + private |
| 65 | + |
| 66 | + def log_debug(str) |
| 67 | + @logger.debug str if @logger |
| 68 | + end |
| 69 | + |
| 70 | + def first_message |
| 71 | + "n,,#{first_message_bare}" |
| 72 | + end |
| 73 | + |
| 74 | + def first_message_bare |
| 75 | + "n=#{encoded_username},r=#{nonce}" |
| 76 | + end |
| 77 | + |
| 78 | + def final_message_without_proof |
| 79 | + "c=biws,r=#{rnonce}" |
| 80 | + end |
| 81 | + |
| 82 | + def final_message |
| 83 | + "#{final_message_without_proof},p=#{client_proof}" |
| 84 | + end |
| 85 | + |
| 86 | + def server_data |
| 87 | + parse_response(@server_first_message) |
| 88 | + end |
| 89 | + |
| 90 | + def rnonce |
| 91 | + server_data['r'] |
| 92 | + end |
| 93 | + |
| 94 | + def salt |
| 95 | + Base64.strict_decode64(server_data['s']) |
| 96 | + end |
| 97 | + |
| 98 | + def iterations |
| 99 | + server_data['i'].to_i |
| 100 | + end |
| 101 | + |
| 102 | + def auth_message |
| 103 | + msg = [first_message_bare, @server_first_message, final_message_without_proof].join(',') |
| 104 | + end |
| 105 | + |
| 106 | + def salted_password |
| 107 | + hi(@password, salt, iterations) |
| 108 | + end |
| 109 | + |
| 110 | + def client_key |
| 111 | + hmac(salted_password, 'Client Key') |
| 112 | + end |
| 113 | + |
| 114 | + def stored_key |
| 115 | + h(client_key) |
| 116 | + end |
| 117 | + |
| 118 | + def server_key |
| 119 | + hmac(salted_password, 'Server Key') |
| 120 | + end |
| 121 | + |
| 122 | + def client_signature |
| 123 | + hmac(stored_key, auth_message) |
| 124 | + end |
| 125 | + |
| 126 | + def server_signature |
| 127 | + Base64.strict_encode64(hmac(server_key, auth_message)) |
| 128 | + end |
| 129 | + |
| 130 | + def client_proof |
| 131 | + Base64.strict_encode64(xor(client_key, client_signature)) |
| 132 | + end |
| 133 | + |
| 134 | + def h(str) |
| 135 | + digest.digest(str) |
| 136 | + end |
| 137 | + |
| 138 | + def hi(str, salt, iterations) |
| 139 | + OpenSSL::PKCS5.pbkdf2_hmac( |
| 140 | + str, |
| 141 | + salt, |
| 142 | + iterations, |
| 143 | + digest.size, |
| 144 | + digest |
| 145 | + ) |
| 146 | + end |
| 147 | + |
| 148 | + def hmac(data, key) |
| 149 | + OpenSSL::HMAC.digest(digest, data, key) |
| 150 | + end |
| 151 | + |
| 152 | + def xor(first, second) |
| 153 | + first.bytes.zip(second.bytes).map { |(a, b)| (a ^ b).chr }.join('') |
| 154 | + end |
| 155 | + |
| 156 | + def parse_response(data) |
| 157 | + data.split(',').map { |s| s.split('=', 2) }.to_h |
| 158 | + end |
| 159 | + |
| 160 | + def encoded_username |
| 161 | + safe_str(@username.encode(Encoding::UTF_8)) |
| 162 | + end |
| 163 | + |
| 164 | + def nonce |
| 165 | + @nonce ||= SecureRandom.urlsafe_base64(32) |
| 166 | + end |
| 167 | + |
| 168 | + def digest |
| 169 | + @digest ||= case @mechanism |
| 170 | + when 'SHA-256' |
| 171 | + OpenSSL::Digest::SHA256.new.freeze |
| 172 | + when 'SHA-512' |
| 173 | + OpenSSL::Digest::SHA512.new.freeze |
| 174 | + else |
| 175 | + raise StandardError, "Unknown mechanism '#{@mechanism}'" |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + def safe_str(val) |
| 180 | + val.gsub('=', '=3D').gsub(',', '=2C') |
| 181 | + end |
| 182 | + end |
| 183 | +end |
0 commit comments