Class: Cloudcover::SimpleAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudcover/commands/simple_auth.rb

Constant Summary collapse

DEFAULT_CONTEXT =
"AUTH"
InvalidCredsFile =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(opts, path) ⇒ SimpleAuth

Returns a new instance of SimpleAuth.



6
7
8
9
10
11
12
# File 'lib/cloudcover/commands/simple_auth.rb', line 6

def initialize(opts,path)
  Output.say_debug("SimpleAuth class: #{self}")
  @okta = Cloudcover::Okta::Client.new
  @credentials = {}
  @opts = opts
  @creds_path = path unless path.nil?
end

Instance Method Details

#app_idObject



88
89
90
# File 'lib/cloudcover/commands/simple_auth.rb', line 88

def app_id
  @opts[:app]
end

#auth_response(auth, msg) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cloudcover/commands/simple_auth.rb', line 23

def auth_response(auth, msg)
  if auth
    if  @opts[:radius]
      p "Accept"
    else
      p msg
    end
  else
    if @opts[:radius]
      abort 'Reject'
    else
      send_to_slack(msg) if Cloudcover::Config.slack[:report_auth_failures]
      abort msg
    end
  end
end

#contextObject



96
97
98
# File 'lib/cloudcover/commands/simple_auth.rb', line 96

def context
  @opts[:context]
end

#context_messageObject



100
101
102
# File 'lib/cloudcover/commands/simple_auth.rb', line 100

def context_message
  context || DEFAULT_CONTEXT
end

#date_formatObject



84
85
86
# File 'lib/cloudcover/commands/simple_auth.rb', line 84

def date_format
  Cloudcover::Config.date_format ? Cloudcover::Config.date_format : "%a %b %e %H:%M:%S %Y"
end

#file_based?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/cloudcover/commands/simple_auth.rb', line 92

def file_based?
  @opts[:f]
end

#file_creds(creds_path) ⇒ Object

Raises:



52
53
54
55
56
# File 'lib/cloudcover/commands/simple_auth.rb', line 52

def file_creds(creds_path)
  credentials = IO.read(creds_path).split rescue {}
  raise InvalidCredsFile unless credentials.length == 2
  credentials
end

#formatted_date(time) ⇒ Object



58
59
60
# File 'lib/cloudcover/commands/simple_auth.rb', line 58

def formatted_date(time)
  Time.at(time).strftime(date_format)
end

#get_credentialsObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cloudcover/commands/simple_auth.rb', line 40

def get_credentials
  if file_based?
    Output.say_debug("Using credtials from #{@creds_path}")
    creds = file_creds(@creds_path)
    @credentials[:username] = creds.first
    @credentials[:password] = creds.last
  else
    @credentials[:username] = Output.ask("Username: ")
    @credentials[:password] = Output.ask("Password: "){ |q| q.echo = "*" }
  end
end

#has_application_access?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/cloudcover/commands/simple_auth.rb', line 80

def has_application_access?
  @okta.myApps.map{ |g| g[:appInstanceId] }.include? app_id
end

#loginObject



70
71
72
73
74
75
76
77
78
# File 'lib/cloudcover/commands/simple_auth.rb', line 70

def 
  @okta.(username, password)
  if @okta.logged_in?
    @user_id = @okta.
    true
  else
    false
  end
end

#passwordObject



66
67
68
# File 'lib/cloudcover/commands/simple_auth.rb', line 66

def password
  @credentials[:password]
end

#send_to_slack(msg) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cloudcover/commands/simple_auth.rb', line 104

def send_to_slack(msg)
  Output.say_debug "Posting to slack..."
  slackvars = [:username, :icon_url, :channel]
  payload = { text: msg }
  slackvars.each do |var|
    payload.merge!({var=> Cloudcover::Config.slack[var]}) if Cloudcover::Config.slack[var]
  end

  HTTParty.post(Cloudcover::Config.slack[:webhook],
                :body => "payload=#{payload.to_json}"
  )
end

#usernameObject



62
63
64
# File 'lib/cloudcover/commands/simple_auth.rb', line 62

def username
  @credentials[:username]
end

#verify_userObject



14
15
16
17
18
19
20
21
# File 'lib/cloudcover/commands/simple_auth.rb', line 14

def verify_user
  get_credentials
  auth_response(false, "#{formatted_date(Time.now)} - #{context_message} - Access denied, failed login for #{username}") unless 
  if app_id
    auth_response(false,"#{formatted_date(Time.now)} - #{context_message} - Access denied, #{username} does not have access to application ID `#{app_id}`") unless has_application_access?
  end
  auth_response(true, "#{formatted_date(Time.now)} - #{context_message} - Access granted for #{username}")
end