Module: Comply::CLI::Helpers::Token
Constant Summary collapse
- TOKEN_ENV_VAR =
'APTIBLE_ACCESS_TOKEN'.freeze
Instance Method Summary collapse
- #current_token_hash ⇒ Object
- #current_user_email ⇒ Object
- #fetch_token ⇒ Object
- #save_token(token) ⇒ Object
- #token_file ⇒ Object
Instance Method Details
#current_token_hash ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/comply/cli/helpers/token.rb', line 39 def current_token_hash # NOTE: older versions of the CLI did not properly create the # token_file with mode 600, which is why we update it when reading. File.chmod(0o600, token_file) JSON.parse(File.read(token_file)) rescue {} end |
#current_user_email ⇒ Object
10 11 12 13 |
# File 'lib/comply/cli/helpers/token.rb', line 10 def current_user_email @current_user_email ||= Aptible::Auth::Agent.new(token: fetch_token).me.email end |
#fetch_token ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/comply/cli/helpers/token.rb', line 15 def fetch_token @token ||= ENV[TOKEN_ENV_VAR] || current_token_hash[Aptible::Auth.configuration.root_url] return @token if @token raise Thor::Error, 'Could not read token: please run comply login ' \ "or set #{TOKEN_ENV_VAR}" end |
#save_token(token) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/comply/cli/helpers/token.rb', line 23 def save_token(token) hash = current_token_hash.merge( Aptible::Auth.configuration.root_url => token ) FileUtils.mkdir_p(File.dirname(token_file)) File.open(token_file, 'w', 0o600) do |file| file.puts hash.to_json end rescue StandardError => e m = "Could not write token to #{token_file}: #{e}. " \ 'Check filesystem permissions.' raise Thor::Error, m end |
#token_file ⇒ Object
48 49 50 |
# File 'lib/comply/cli/helpers/token.rb', line 48 def token_file File.join ENV['HOME'], '.aptible', 'tokens.json' end |