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



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

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
39
# File 'lib/cloudcover/commands/simple_auth.rb', line 23

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

#contextObject



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

def context
  @opts[:context]
end

#context_messageObject



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

def context_message
  context || DEFAULT_CONTEXT
end

#date_formatObject



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

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

#file_based?Boolean

Returns:

  • (Boolean)


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

def file_based?
  @opts[:f]
end

#file_creds(creds_path) ⇒ Object

Raises:



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

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

#formatted_date(time) ⇒ Object



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

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

#get_credentialsObject



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

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)


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

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

#loginObject



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

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

#passwordObject



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

def password
  @credentials[:password]
end

#send_to_slack(msg) ⇒ Object



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

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::GlobalConfig.slack[var]}) if Cloudcover::GlobalConfig.slack[var]
  end

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

#usernameObject



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

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