Module: Mongo::Auth::StringPrep Private
Do not use. This module is for internal use only.
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Defined in: | lib/mongo/auth/stringprep.rb, lib/mongo/auth/stringprep/tables.rb, lib/mongo/auth/stringprep/profiles/sasl.rb |
Overview
This namespace contains all behavior related to string preparation (RFC 3454). It’s used to implement SCRAM-SHA-256 authentication, which is available in MongoDB server versions 4.0 and later.
Instance Method Summary
-
#prepare(data, mappings, prohibited, options = {})
Internal use only
Prepare a string given a set of mappings and prohibited character tables.
- #apply_maps(data, mappings) private Internal use only
- #check_bidi!(out) private Internal use only
- #check_prohibited!(out, prohibited) private Internal use only
- #mapping(c, mappings) private Internal use only
- #normalize!(out) private Internal use only
- #table_contains?(table, c) ⇒ Boolean private Internal use only
Instance Method Details
#apply_maps(data, mappings) (private)
# File 'lib/mongo/auth/stringprep.rb', line 64
def apply_maps(data, mappings) data.each_char.inject(+'') do |out, c| out << mapping(c.ord, mappings) end end
#check_bidi!(out) (private)
# File 'lib/mongo/auth/stringprep.rb', line 70
def check_bidi!(out) if out.each_char.any? { |c| table_contains?(Tables::C8, c) } raise Mongo::Error::FailedStringPrepValidation.new(Error::FailedStringPrepValidation::INVALID_BIDIRECTIONAL) end if out.each_char.any? { |c| table_contains?(Tables::D1, c) } if out.each_char.any? { |c| table_contains?(Tables::D2, c) } raise Mongo::Error::FailedStringPrepValidation.new(Error::FailedStringPrepValidation::INVALID_BIDIRECTIONAL) end unless table_contains?(Tables::D1, out[0]) && table_contains?(Tables::D1, out[-1]) raise Mongo::Error::FailedStringPrepValidation.new(Error::FailedStringPrepValidation::INVALID_BIDIRECTIONAL) end end end
#check_prohibited!(out, prohibited) (private)
# File 'lib/mongo/auth/stringprep.rb', line 86
def check_prohibited!(out, prohibited) out.each_char do |c| prohibited.each do |table| if table_contains?(table, c) raise Error::FailedStringPrepValidation.new(Error::FailedStringPrepValidation::PROHIBITED_CHARACTER) end end end end
#mapping(c, mappings) (private)
# File 'lib/mongo/auth/stringprep.rb', line 96
def mapping(c, mappings) m = mappings.find { |m| m.has_key?(c) } mapped = (m && m[c]) || [c] mapped.map { |i| i.chr(Encoding::UTF_8) }.join end
#normalize!(out) (private)
# File 'lib/mongo/auth/stringprep.rb', line 102
def normalize!(out) if String.method_defined?(:unicode_normalize!) out.unicode_normalize!(:nfkc) else require 'mongo/auth/stringprep/unicode_normalize/normalize' out.replace(UnicodeNormalize.normalize(out, :nfkc)) end end
#prepare(data, mappings, prohibited, options = {})
Prepare a string given a set of mappings and prohibited character tables.
# File 'lib/mongo/auth/stringprep.rb', line 54
def prepare(data, mappings, prohibited, = {}) apply_maps(data, mappings).tap do |mapped| normalize!(mapped) if [:normalize] check_prohibited!(mapped, prohibited) check_bidi!(mapped) if [:bidi] end end
#table_contains?(table, c) ⇒ Boolean
(private)
# File 'lib/mongo/auth/stringprep.rb', line 111
def table_contains?(table, c) table.any? do |r| r.member?(c.ord) end end