Module: Conjur::Authn

Defined in:
lib/conjur/authn.rb

Class Method Summary collapse

Class Method Details

.ask_for_credentials(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/conjur/authn.rb', line 74

def ask_for_credentials(options = {})
  raise "No credentials provided or found" if options[:noask]


  # also use stderr here, because we might be prompting for a password as part
  # of a command like user:create that we'd want to send to a file.
  require 'highline'
  require 'conjur/api'

  hl = HighLine.new $stdin, $stderr

  user = options[:username] || hl.ask("Enter your username to log into Conjur: ")
  pass = options[:password] || hl.ask("Please enter your password (it will not be echoed): "){ |q| q.echo = false }

  api_key = if cas_server = options[:"cas-server"]
    Conjur::API.(user, pass, cas_server)
  else
    Conjur::API.(user, pass)
  end
  @credentials = [user, api_key]
end

.authenticate(options = {}) ⇒ Object



33
34
35
36
# File 'lib/conjur/authn.rb', line 33

def authenticate(options = {})
  require 'conjur/api'
  Conjur::API.authenticate(*get_credentials(options))
end

.connect(cls = nil, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/conjur/authn.rb', line 96

def connect(cls = nil, options = {})
  if cls.nil?
    require 'conjur/api'
    require 'conjur/base'
    cls = Conjur::API
  end
  cls.new_from_key(*get_credentials(options))
end

.delete_credentials ⇒ Object



38
39
40
41
# File 'lib/conjur/authn.rb', line 38

def delete_credentials
  netrc.delete host
  netrc.save
end

.fetch_credentials(options = {}) ⇒ Object



63
64
65
66
# File 'lib/conjur/authn.rb', line 63

def fetch_credentials(options = {})
  ask_for_credentials(options)
  write_credentials
end

.get_credentials(options = {}) ⇒ Object



55
56
57
# File 'lib/conjur/authn.rb', line 55

def get_credentials(options = {})
  @credentials ||= (read_credentials || fetch_credentials(options))
end

.host ⇒ Object



43
44
45
# File 'lib/conjur/authn.rb', line 43

def host
  Conjur::Authn::API.host
end

.login(options = {}) ⇒ Object



28
29
30
31
# File 'lib/conjur/authn.rb', line 28

def (options = {})
  delete_credentials
  get_credentials(options)
end

.netrc ⇒ Object



47
48
49
50
51
52
53
# File 'lib/conjur/authn.rb', line 47

def netrc
  args = []
  if path = Conjur::Config[:netrc_path]
    args.unshift(path)
  end
  @netrc ||= Netrc.read(*args)
end

.read_credentials ⇒ Object



59
60
61
# File 'lib/conjur/authn.rb', line 59

def read_credentials
  netrc[host]
end

.write_credentials ⇒ Object



68
69
70
71
72
# File 'lib/conjur/authn.rb', line 68

def write_credentials
  netrc[host] = @credentials
  netrc.save
  @credentials
end