"Fossies" - the Fresh Open Source Software Archive

Member "vagrant-2.2.14/plugins/commands/cloud/auth/middleware/add_authentication.rb" (20 Nov 2020, 2073 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.source_root.join("plugins/commands/cloud/client/client")
    5 
    6 module VagrantPlugins
    7   module CloudCommand
    8     class AddAuthentication
    9       REPLACEMENT_HOSTS = [
   10         "app.vagrantup.com".freeze,
   11         "atlas.hashicorp.com".freeze
   12       ].freeze
   13       TARGET_HOST = "vagrantcloud.com".freeze
   14       CUSTOM_HOST_NOTIFY_WAIT = 5
   15 
   16       def self.custom_host_notified!
   17         @_host_notify = true
   18       end
   19 
   20       def self.custom_host_notified?
   21         if defined?(@_host_notify)
   22           @_host_notify
   23         else
   24           false
   25         end
   26       end
   27 
   28       def initialize(app, env)
   29         @app = app
   30         CloudCommand::Plugin.init!
   31       end
   32 
   33       def call(env)
   34         client = Client.new(env[:env])
   35         token  = client.token
   36 
   37         env[:box_urls].map! do |url|
   38           begin
   39             u = URI.parse(url)
   40             if u.host != TARGET_HOST && REPLACEMENT_HOSTS.include?(u.host)
   41               u.host = TARGET_HOST
   42               u.to_s
   43             else
   44               url
   45             end
   46           rescue URI::Error
   47             url
   48           end
   49         end
   50 
   51         server_uri = URI.parse(Vagrant.server_url.to_s)
   52 
   53         if token && !server_uri.host.to_s.empty?
   54           env[:box_urls].map! do |url|
   55             u = URI.parse(url)
   56 
   57             if u.host == server_uri.host
   58               if server_uri.host != TARGET_HOST && !self.class.custom_host_notified?
   59                 env[:ui].warn(I18n.t("cloud_command.middleware.authentication.different_target",
   60                   custom_host: server_uri.host, known_host: TARGET_HOST) + "\n")
   61                 sleep CUSTOM_HOST_NOTIFY_WAIT
   62                 self.class.custom_host_notified!
   63               end
   64 
   65               q = CGI.parse(u.query || "")
   66 
   67               current = q["access_token"]
   68               if current && current.empty?
   69                 q["access_token"] = token
   70               end
   71 
   72               u.query = URI.encode_www_form(q)
   73             end
   74 
   75             u.to_s
   76           end
   77         end
   78 
   79         @app.call(env)
   80       end.freeze
   81     end
   82   end
   83 end