class Rack::RestrictAccess::RestrictFilter
Public Class Methods
new()
click to toggle source
Calls superclass method
Rack::RestrictAccess::Filter::new
# File lib/rack/restrict_access.rb, line 228 def initialize @credentials = [] super end
Public Instance Methods
credentials(*creds)
click to toggle source
# File lib/rack/restrict_access.rb, line 241 def credentials(*creds) if !creds.first.is_a?(Hash) && creds.last.is_a?(Hash) options = creds.pop end options ||= {} concat_new_attributes(args: creds, ivar: @credentials) do |credential_pair| if credential_pair.is_a? Hash creds_from_hash(credential_pair) elsif credential_pair.is_a? String creds_from_string(credential_pair, options) elsif credential_pair.is_a? Array creds_from_array(credential_pair, options) end end end
credentials_count()
click to toggle source
# File lib/rack/restrict_access.rb, line 268 def credentials_count @credentials.length end
credentials_match?(creds_hash)
click to toggle source
# File lib/rack/restrict_access.rb, line 258 def credentials_match?(creds_hash) @credentials.any? do |saved_hash| saved_u = saved_hash[:username] saved_p = saved_hash[:password] given_u = creds_hash[:username] given_p = creds_hash[:password] !(given_u =~ saved_u).nil? && !(given_p =~ saved_p).nil? end end
restricts_ip?(ip)
click to toggle source
# File lib/rack/restrict_access.rb, line 237 def restricts_ip?(ip) applies_to_ip?(ip) end
restricts_resource?(path)
click to toggle source
# File lib/rack/restrict_access.rb, line 233 def restricts_resource?(path) applies_to_resource?(path) end
Private Instance Methods
cred_to_regexp(cred)
click to toggle source
# File lib/rack/restrict_access.rb, line 273 def cred_to_regexp(cred) if cred.respond_to? :to_str /^#{Regexp.escape(cred)}$/ elsif cred.respond_to? :match cred else raise TypeError, cred end end
creds_from_array(array)
click to toggle source
# File lib/rack/restrict_access.rb, line 290 def creds_from_array(array) array.map do |el| creds_from_string(el, options) end end
creds_from_hash(hash)
click to toggle source
# File lib/rack/restrict_access.rb, line 283 def creds_from_hash(hash) { username: cred_to_regexp(hash[:username]), password: cred_to_regexp(hash[:password]) } end
creds_from_string(string, options = {})
click to toggle source
# File lib/rack/restrict_access.rb, line 296 def creds_from_string(string, options = {}) string = string.to_str credentials_delimiter = options.fetch(:credentials_delimiter, ',') credential_pair_delimiter = options.fetch(:credential_pair_delimiter, ';') creds = string.split(credential_pair_delimiter) creds.map do |str| cred_pair = str.split(credentials_delimiter) { username: cred_to_regexp(cred_pair[0]), password: cred_to_regexp(cred_pair[1]) } end end