Class: MicrosoftKiotaAbstractions::AllowedHostsValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb

Overview

Maintains a list of valid hosts and allows authentication providers to check whether a host is valid before authenticating a request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allowed_hosts) ⇒ AllowedHostsValidator

creates a new AllocatedHostsValidator with provided values



10
11
12
13
# File 'lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb', line 10

def initialize(allowed_hosts)
  @allowed_hosts = {}
  allowed_hosts.each { |host| @allowed_hosts[host.downcase] = true }
end

Instance Attribute Details

#allowed_hostsObject

gets the list of valid hosts



39
40
41
# File 'lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb', line 39

def allowed_hosts
  @allowed_hosts
end

Instance Method Details

#url_host_valid?(url) ⇒ Boolean

checks whether the provided host is valid

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb', line 22

def url_host_valid?(url)
  return true if @allowed_hosts.empty?

  parsed_url = URI(url)
  return false if parsed_url.host.nil?

  return false unless parsed_url.is_a?(URI::HTTPS)

  hostname = parsed_url.host.downcase
  return true if @allowed_hosts.key? hostname

  @allowed_hosts.any? { |allowed_host, _| allowed_host.start_with?('.') && hostname.end_with?(allowed_host) }
rescue URI::InvalidURIError
  false
end