2015-11-18 18:43:29 +00:00
|
|
|
require "cgi"
|
2014-12-09 01:42:00 +00:00
|
|
|
require "uri"
|
|
|
|
|
|
|
|
require_relative "../client"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module LoginCommand
|
|
|
|
class AddAuthentication
|
2015-11-18 18:43:29 +00:00
|
|
|
VCLOUD = "vagrantcloud.com".freeze
|
|
|
|
ATLAS = "atlas.hashicorp.com".freeze
|
|
|
|
|
2014-12-09 01:42:00 +00:00
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
client = Client.new(env[:env])
|
|
|
|
token = client.token
|
|
|
|
|
|
|
|
if token && Vagrant.server_url
|
|
|
|
server_uri = URI.parse(Vagrant.server_url)
|
|
|
|
|
|
|
|
env[:box_urls].map! do |url|
|
|
|
|
u = URI.parse(url)
|
2014-12-12 22:53:05 +00:00
|
|
|
replace = u.host == server_uri.host
|
2015-11-18 18:43:29 +00:00
|
|
|
|
2014-12-12 22:53:05 +00:00
|
|
|
if !replace
|
|
|
|
# We need this in here for the transition we made from
|
|
|
|
# Vagrant Cloud to Atlas. This preserves access tokens
|
|
|
|
# appending to both without leaking access tokens to
|
|
|
|
# unsavory URLs.
|
2017-05-22 16:42:31 +00:00
|
|
|
if (u.host == VCLOUD && server_uri.host == ATLAS) ||
|
|
|
|
(u.host == ATLAS && server_uri.host == VCLOUD)
|
2015-11-18 18:43:29 +00:00
|
|
|
replace = true
|
|
|
|
end
|
2014-12-12 22:53:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if replace
|
2015-11-18 18:43:29 +00:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|