Module: Pmux::LogView::AuthHelper

Defined in:
lib/pmux-logview/auth_helper.rb

Class Method Summary collapse

Class Method Details

.authenticated(request, auth) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/pmux-logview/auth_helper.rb', line 37

def self.authenticated request, auth
  auth ||=  Rack::Auth::Basic::Request.new(request.env)
  if auth.provided? && auth.basic? && auth.credentials
    user, pass = auth.credentials
    if @auth_db && @auth_db[user] && @auth_db[user]["pass"] && @auth_db[user]["pass"] == pass
      return user
    end
  end
  return nil
end

.check_auth!(request, response, auth, password_file_path) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/pmux-logview/auth_helper.rb', line 48

def self.check_auth! request, response, auth, password_file_path
  user = self.authenticated(request, auth)
  if user.nil?
    response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
    throw(:halt, [401, "Unauthorized"])
  end
  return user
end

.init(password_file_path) ⇒ Object



8
9
10
11
# File 'lib/pmux-logview/auth_helper.rb', line 8

def self.init password_file_path
  @logger = LoggerWrapper.instance()
  self.load_config(password_file_path) if @auth_db.nil?
end

.load_config(password_file_path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pmux-logview/auth_helper.rb', line 17

def self.load_config password_file_path
  @auth_db = {} if @auth_db.nil?
  begin
    stat = File.stat(password_file_path)
    mode = "%o" % stat.mode
    if mode[-3, 3] != "600"
      @logger.warn("password file permission is not 600 (#{password_file_path})")
    end
    new_auth_db = YAML.load_file(password_file_path)
  rescue Errno::ENOENT
    @logger.warn("not found password file (#{password_file_path})")
  rescue Errno::EACCES
    @logger.warn("can not access password file (#{password_file_path})")
  rescue Exception => e
    @logger.warn("error occurred in password loading: #{e}")
    @logger.warn(e.backtrace.join("\n"))
  end
    @auth_db = new_auth_db
end

.update(password_file_path) ⇒ Object



13
14
15
# File 'lib/pmux-logview/auth_helper.rb', line 13

def self.update password_file_path
  self.load_config(password_file_path)
end