Class: Cloudcover::SimpleAuth
- Inherits:
-
Object
- Object
- Cloudcover::SimpleAuth
- Defined in:
- lib/cloudcover/commands/simple_auth.rb
Constant Summary collapse
- DEFAULT_CONTEXT =
"AUTH"- InvalidCredsFile =
Class.new(StandardError)
Instance Method Summary collapse
- #app_id ⇒ Object
- #auth_response(auth, msg) ⇒ Object
- #context ⇒ Object
- #context_message ⇒ Object
- #date_format ⇒ Object
- #file_based? ⇒ Boolean
- #file_creds(creds_path) ⇒ Object
- #formatted_date(time) ⇒ Object
- #get_credentials ⇒ Object
- #has_application_access? ⇒ Boolean
-
#initialize(opts, path) ⇒ SimpleAuth
constructor
A new instance of SimpleAuth.
- #login ⇒ Object
- #password ⇒ Object
- #send_to_slack(msg) ⇒ Object
- #username ⇒ Object
- #verify_user ⇒ Object
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_id ⇒ Object
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 |
#context ⇒ Object
96 97 98 |
# File 'lib/cloudcover/commands/simple_auth.rb', line 96 def context @opts[:context] end |
#context_message ⇒ Object
100 101 102 |
# File 'lib/cloudcover/commands/simple_auth.rb', line 100 def context || DEFAULT_CONTEXT end |
#date_format ⇒ Object
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
92 93 94 |
# File 'lib/cloudcover/commands/simple_auth.rb', line 92 def file_based? @opts[:f] end |
#file_creds(creds_path) ⇒ Object
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_credentials ⇒ Object
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
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 |
#login ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/cloudcover/commands/simple_auth.rb', line 70 def login @okta.login(username, password) if @okta.logged_in? @user_id = @okta.login_id true else false end end |
#password ⇒ Object
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 |
#username ⇒ Object
62 63 64 |
# File 'lib/cloudcover/commands/simple_auth.rb', line 62 def username @credentials[:username] end |
#verify_user ⇒ Object
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 login 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 |