Skip to content

Commit

Permalink
Update authentication handling with client wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisroberts committed Nov 11, 2024
1 parent 20bc230 commit 37b2946
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
Vagrant.require "uri"
Vagrant.require "vagrant/util/credential_scrubber"

require_relative "./add_authentication"

require Vagrant.source_root.join("plugins/commands/cloud/client/client")
require_relative "./add_authentication"

# Similar to AddAuthentication this middleware will add authentication for interacting
# with Vagrant cloud. It does this by adding Authentication headers to a
Expand Down Expand Up @@ -54,6 +53,8 @@ def call(env)
else
env[:downloader].headers << "Authorization: Bearer #{token}"
end
else
@logger.debug("Not adding authentication header, host mismatch #{target_url.host} != #{server_uri.host}")
end

env[:downloader]
Expand Down
18 changes: 13 additions & 5 deletions plugins/commands/cloud/client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ def self.reset!
def initialize(env)
@logger = Log4r::Logger.new("vagrant::cloud::client")
@env = env
@client = VagrantCloud::Client.new(
access_token: token,
url_base: api_server_url
)
if !defined?(@@client)
@@client = VagrantCloud::Client.new(
access_token: token,
url_base: api_server_url
)
end
@client = @@client
end

# Removes the token, effectively logging the user out.
Expand All @@ -54,7 +57,8 @@ def clear_token
#
# @return [Boolean]
def logged_in?
return false if !client.access_token
return false if !client&.access_token

Vagrant::Util::CredentialScrubber.sensitive(client.access_token)

with_error_handling do
Expand Down Expand Up @@ -128,6 +132,10 @@ def store_token(token)
#
# @return [String]
def token
# If the client is defined, use the client for the access token
# to allow proper token generation if required
return client.access_token if client && !client.access_token.nil?

if present?(ENV["VAGRANT_CLOUD_TOKEN"]) && token_path.exist?
# Only show warning if it has not been previously shown
if !defined?(@@double_token_warning)
Expand Down

0 comments on commit 37b2946

Please sign in to comment.