"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/plugins/commands/cloud/auth/middleware/add_downloader_authentication.rb" (20 Nov 2020, 2007 Bytes) of package /linux/misc/vagrant-2.2.14.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 require "cgi"
    2 require "uri"
    3 
    4 require "vagrant/util/credential_scrubber"
    5 require_relative "./add_authentication"
    6 
    7 require Vagrant.source_root.join("plugins/commands/cloud/client/client")
    8 
    9 # Similar to AddAuthentication this middleware will add authentication for interacting
   10 # with Vagrant cloud. It does this by adding Authentication headers to a 
   11 # Vagrant::Util::Downloader object. 
   12 module VagrantPlugins
   13   module CloudCommand
   14     class AddDownloaderAuthentication <  AddAuthentication
   15 
   16       @@logger = Log4r::Logger.new("vagrant::clout::add_download_authentication")
   17 
   18       def call(env)
   19         client = Client.new(env[:env])
   20         token  = client.token
   21         Vagrant::Util::CredentialScrubber.sensitive(token)
   22 
   23         begin
   24           target_url = URI.parse(env[:downloader].source)
   25           if target_url.host != TARGET_HOST && REPLACEMENT_HOSTS.include?(target_url.host)
   26               target_url.host = TARGET_HOST
   27               env[:downloader].source = target_url.to_s
   28           end
   29         rescue URI::Error
   30           # if there is an error, use current target_url
   31         end
   32 
   33         server_uri = URI.parse(Vagrant.server_url.to_s)
   34         if token && !server_uri.host.to_s.empty?
   35           if target_url.host == server_uri.host
   36             if server_uri.host != TARGET_HOST && !self.class.custom_host_notified?
   37               env[:ui].warn(I18n.t("cloud_command.middleware.authentication.different_target",
   38                 custom_host: server_uri.host, known_host: TARGET_HOST) + "\n")
   39               sleep CUSTOM_HOST_NOTIFY_WAIT
   40               self.class.custom_host_notified!
   41             end
   42 
   43             if Array(env[:downloader].headers).any? { |h| h.include?("Authorization") }
   44               @@logger.info("Not adding an authentication header, one already found")
   45             else
   46               env[:downloader].headers << "Authorization: Bearer #{token}"
   47             end
   48           end
   49 
   50           env[:downloader]
   51         end
   52 
   53         @app.call(env)
   54       end.freeze
   55     end
   56   end
   57 end