Module: Conjur::Authn

Defined in:
lib/conjur/authn.rb

Class Method Summary collapse

Class Method Details

.ask_for_credentials(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/conjur/authn.rb', line 48

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.
  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



12
13
14
# File 'lib/conjur/authn.rb', line 12

def authenticate(options = {})
  Conjur::API.authenticate(*get_credentials(options))
end

.connect(cls = Conjur::API, options = {}) ⇒ Object



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

def connect(cls = Conjur::API, options = {})
  cls.new_from_key(*get_credentials(options))
end

.delete_credentials ⇒ Object



16
17
18
19
# File 'lib/conjur/authn.rb', line 16

def delete_credentials
  netrc.delete host
  netrc.save
end

.fetch_credentials(options = {}) ⇒ Object



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

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

.get_credentials(options = {}) ⇒ Object



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

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

.host ⇒ Object



21
22
23
# File 'lib/conjur/authn.rb', line 21

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

.login(options = {}) ⇒ Object



7
8
9
10
# File 'lib/conjur/authn.rb', line 7

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

.netrc ⇒ Object



25
26
27
# File 'lib/conjur/authn.rb', line 25

def netrc
  @netrc ||= Netrc.read
end

.read_credentials ⇒ Object



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

def read_credentials
  netrc[host]
end

.write_credentials ⇒ Object



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

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