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
|
2017-11-07 15:32:33 +00:00
|
|
|
ALLOWED_AUTHENTICATION_HOSTS = %w[
|
2017-11-07 15:35:16 +00:00
|
|
|
app.vagrantup.com
|
2017-11-07 15:32:33 +00:00
|
|
|
atlas.hashicorp.com
|
|
|
|
vagrantcloud.com
|
|
|
|
].freeze
|
2015-11-18 18:43:29 +00:00
|
|
|
|
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
|
2017-11-07 15:32:33 +00:00
|
|
|
if ALLOWED_AUTHENTICATION_HOSTS.include?(u.host) &&
|
|
|
|
ALLOWED_AUTHENTICATION_HOSTS.include?(server_uri.host)
|
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
|