Sha256: f5920244bc3e35f40bdceac92a02bcb43d007cbcf9e2d35b3202f6f38194d44c

Contents?: true

Size: 1.06 KB

Versions: 7

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require 'uri'

module MicrosoftKiotaAbstractions
  # Maintains a list of valid hosts and allows authentication providers to check whether
  # a host is valid before authenticating a request
  class AllowedHostsValidator
    # creates a new AllocatedHostsValidator with provided values
    def initialize(allowed_hosts)
      @allowed_hosts = {}
      allowed_hosts.each { |host| @allowed_hosts[host.downcase] = true }
    end

    # sets the list of valid hosts with provided value (val)
    def allowed_hosts=(val)
      @allowed_hosts = {}
      val.each { |host| @allowed_hosts[host.downcase] = true }
    end

    # checks whether the provided host is valid
    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)

      @allowed_hosts.key? parsed_url.host.downcase

    rescue URI::InvalidURIError
      false
    end

    # gets the list of valid hosts
    attr_reader :allowed_hosts
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
microsoft_kiota_abstractions-0.14.4 lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb
microsoft_kiota_abstractions-0.14.3 lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb
microsoft_kiota_abstractions-0.14.2 lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb
microsoft_kiota_abstractions-0.14.1 lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb
microsoft_kiota_abstractions-0.14.0 lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb
microsoft_kiota_abstractions-0.13.0 lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb
microsoft_kiota_abstractions-0.12.0 lib/microsoft_kiota_abstractions/authentication/allowed_hosts_validator.rb