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

57 lines
1.2 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
2017-11-07 15:32:33 +00:00
ALLOWED_AUTHENTICATION_HOSTS = %w[
app.vagrantup.com
2017-11-07 15:32:33 +00:00
atlas.hashicorp.com
vagrantcloud.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)
replace = u.host == server_uri.host
if !replace
2017-11-07 15:32:33 +00:00
if ALLOWED_AUTHENTICATION_HOSTS.include?(u.host) &&
ALLOWED_AUTHENTICATION_HOSTS.include?(server_uri.host)
replace = true
end
end
if replace
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