Class: Gtool::Auth

Inherits:
Thor
  • Object
show all
Includes:
Util
Defined in:
lib/gtool/auth.rb

Constant Summary collapse

TOKEN_DURATION =
60 * 60 * 24 * 14

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#ask_default

Class Method Details



77
78
79
# File 'lib/gtool/auth.rb', line 77

def self.banner(task, namespace = true, subcommand = false)
  "#{basename} #{task.formatted_usage(self, true, subcommand)}"
end

.connection(options) ⇒ Object



73
74
75
# File 'lib/gtool/auth.rb', line 73

def self.connection(options)
  connection = GProv::Connection.new(settings[:domain], settings[:token], options)
end

.settingsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gtool/auth.rb', line 59

def self.settings
  settings = nil
  creds_file = "#{ENV['HOME']}/.gtool.yaml"
  begin
    File.open creds_file do |f|
      settings = YAML::load f.read
    end
  rescue Errno::ENOENT
    puts "#{creds_file} does not exist, run 'gtool auth generate'"
    exit
  end
  settings
end

Instance Method Details

#displayObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gtool/auth.rb', line 45

def display
  settings = self.class.settings

  expiration = Time.at(settings[:created]) + TOKEN_DURATION

  if Time.now > expiration
    say "created: #{settings[:created]} (expired)", :red
  else
    say "created: #{settings[:created]} (valid)", :green
    say "expires: #{expiration}", :green
  end
  say "token: #{settings[:token]}", :cyan
end

#generateObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gtool/auth.rb', line 17

def generate
  user = ask "Username:"
  %x{stty -echo}
  pass = ask "Password:"
  %x{stty echo}
  puts

  service = ask_default "apps", "Service (defaults to apps):"

  domain  = ask "Domain:"

  authject = GProv::Auth::ClientLogin.new(user, pass, service, options)
  token = authject.token

  if token.nil?
    say "Authentication failed!", :red
  else
    say "Authentication accepted, token valid till #{Time.now + TOKEN_DURATION}", :green
    credentials = {:token => token, :created => Time.now, :domain => domain}

    File.open("#{ENV['HOME']}/.gtool.yaml", "w") do |f|
      f.write(YAML.dump(credentials))
      f.chmod 0600
    end
  end
end