vagrant/plugins/commands/login/middleware/add_authentication.rb

65 lines
1.5 KiB
Ruby
Raw Normal View History

require "cgi"
2014-12-09 01:42:00 +00:00
require "uri"
require_relative "../client"
module VagrantPlugins
module LoginCommand
class AddAuthentication
REPLACEMENT_HOSTS = [
"app.vagrantup.com".freeze,
"atlas.hashicorp.com".freeze
2017-11-07 15:32:33 +00:00
].freeze
TARGET_HOST = "vagrantcloud.com".freeze
CUSTOM_HOST_NOTIFY_WAIT = 5
2014-12-09 01:42:00 +00:00
def initialize(app, env)
@app = app
LoginCommand::Plugin.init!
2014-12-09 01:42:00 +00:00
end
def call(env)
client = Client.new(env[:env])
token = client.token
env[:box_urls].map! do |url|
u = URI.parse(url)
if u.host != TARGET_HOST && REPLACEMENT_HOSTS.include?(u.host)
u.host = TARGET_HOST
end
u.to_s
end
server_uri = URI.parse(Vagrant.server_url.to_s)
if token && !server_uri.host.to_s.empty?
if server_uri.host != TARGET_HOST
env[:ui].warn(I18n.t("login_command.middleware.authentication.different_target",
custom_host: server_uri.host, known_host: TARGET_HOST) + "\n")
sleep CUSTOM_HOST_NOTIFY_WAIT
end
2014-12-09 01:42:00 +00:00
env[:box_urls].map! do |url|
u = URI.parse(url)
if u.host == server_uri.host
q = CGI.parse(u.query || "")
current = q["access_token"]
if current && current.empty?
q["access_token"] = token
end
u.query = URI.encode_www_form(q)
2014-12-09 01:42:00 +00:00
end
u.to_s
end
end
@app.call(env)
end.freeze
2014-12-09 01:42:00 +00:00
end
end
end