Merge pull request #8876 from mitchellh/vagrantcloud-login-description

command/login: Add description to created token
This commit is contained in:
Justin Campbell 2017-08-14 11:49:22 -04:00 committed by GitHub
commit 715b447892
3 changed files with 33 additions and 10 deletions

View File

@ -40,15 +40,24 @@ module VagrantPlugins
# Login logs a user in and returns the token for that user. The token # Login logs a user in and returns the token for that user. The token
# is _not_ stored unless {#store_token} is called. # is _not_ stored unless {#store_token} is called.
# #
# @param [String] user # @param [String] username_or_email
# @param [String] pass # @param [String] password
# @param [String] description
# @return [String] token The access token, or nil if auth failed. # @return [String] token The access token, or nil if auth failed.
def login(user, pass) def login(username_or_email, password, description: nil)
@logger.info("Logging in '#{user}'") @logger.info("Logging in '#{username_or_email}'")
with_error_handling do with_error_handling do
url = "#{Vagrant.server_url}/api/v1/authenticate" url = "#{Vagrant.server_url}/api/v1/authenticate"
request = { "user" => { "login" => user, "password" => pass } } request = {
user: {
login: username_or_email,
password: password
},
token: {
description: description
}
}
proxy = nil proxy = nil
proxy ||= ENV["HTTPS_PROXY"] || ENV["https_proxy"] proxy ||= ENV["HTTPS_PROXY"] || ENV["https_proxy"]

View File

@ -1,3 +1,5 @@
require 'socket'
module VagrantPlugins module VagrantPlugins
module LoginCommand module LoginCommand
class Command < Vagrant.plugin("2", "command") class Command < Vagrant.plugin("2", "command")
@ -50,6 +52,7 @@ module VagrantPlugins
# Ask for the username # Ask for the username
login = nil login = nil
password = nil password = nil
description = nil
while !login while !login
login = @env.ui.ask("Vagrant Cloud Username: ") login = @env.ui.ask("Vagrant Cloud Username: ")
end end
@ -58,7 +61,14 @@ module VagrantPlugins
password = @env.ui.ask("Password (will be hidden): ", echo: false) password = @env.ui.ask("Password (will be hidden): ", echo: false)
end end
token = @client.login(login, password) description_default = "Vagrant login from #{Socket.gethostname}"
while !description
description =
@env.ui.ask("Token description (Defaults to #{description_default.inspect}): ")
end
description = description_default if description.empty?
token = @client.login(login, password, description: description)
if !token if !token
@env.ui.error(I18n.t("login_command.invalid_login")) @env.ui.error(I18n.t("login_command.invalid_login"))
return 1 return 1

View File

@ -61,6 +61,9 @@ describe VagrantPlugins::LoginCommand::Client do
"login" => "foo", "login" => "foo",
"password" => "bar", "password" => "bar",
}, },
"token" => {
"description" => "Token description"
}
} }
response = { response = {
@ -76,7 +79,8 @@ describe VagrantPlugins::LoginCommand::Client do
with(body: JSON.dump(request), headers: headers). with(body: JSON.dump(request), headers: headers).
to_return(status: 200, body: JSON.dump(response)) to_return(status: 200, body: JSON.dump(response))
expect(subject.login("foo", "bar")).to eq("baz") expect(subject.login("foo", "bar", description: "Token description"))
.to eq("baz")
end end
it "returns nil on bad login" do it "returns nil on bad login" do