lib/aptible/cli/agent.rb in aptible-cli-0.7.0 vs lib/aptible/cli/agent.rb in aptible-cli-0.7.1

- old
+ new

@@ -22,12 +22,10 @@ require_relative 'subcommands/ssh' require_relative 'subcommands/backup' module Aptible module CLI - TOKEN_EXPIRY_WITH_OTP = 12 * 60 * 60 # 12 hours - class Agent < Thor include Thor::Actions include Helpers::Token include Subcommands::Apps @@ -52,10 +50,12 @@ end desc 'login', 'Log in to Aptible' option :email option :password + option :lifetime, desc: 'The duration the token should be valid for ' \ + '(example usage: 24h, 1d, 600s, etc.)' option :otp_token, desc: 'A token generated by your second-factor app' def login email = options[:email] || ask('Email: ') password = options[:password] || ask('Password: ', echo: false) puts '' @@ -64,12 +64,20 @@ otp_token = options[:otp_token] token_options[:otp_token] = otp_token if otp_token begin - token_options[:expires_in] = TOKEN_EXPIRY_WITH_OTP \ - if token_options[:otp_token] + lifetime = '1w' + lifetime = '12h' if token_options[:otp_token] + lifetime = options[:lifetime] if options[:lifetime] + + duration = ChronicDuration.parse(lifetime) + if duration.nil? + fail Thor::Error, "Invalid token lifetime requested: #{lifetime}" + end + + token_options[:expires_in] = duration token = Aptible::Auth::Token.create(token_options) rescue OAuth2::Error => e if e.code == 'otp_token_required' token_options[:otp_token] = options[:otp_token] || ask('2FA Token: ') @@ -80,9 +88,15 @@ "#{e.code}" end save_token(token.access_token) puts "Token written to #{token_file}" + + lifetime_format = { units: 2, joiner: ', ' } + token_lifetime = (token.expires_at - token.created_at).round + expires_in = ChronicDuration.output(token_lifetime, lifetime_format) + puts "This token will expire after #{expires_in} " \ + '(use --lifetime to customize)' end private def deprecated(msg)