Class: Ezframe::Auth

Inherits:
Object show all
Defined in:
lib/ezframe/auth.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account) ⇒ Auth

Returns a new instance of Auth.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ezframe/auth.rb', line 69

def initialize()
  self. = 
  auth_conf = Config[:auth]
  dataset = DB.dataset(auth_conf[:table])
  if .is_a?(Integer)
    @user = dataset.where(id: ).first
  else
    @user = dataset.where(auth_conf[:user].to_sym => ).first
  end
  unless @user
    EzLog.error "Auth.initialize: This user does not exist: #{}"
    return
  end
  self.password = @user[auth_conf[:password].to_sym]
  @user.delete(:password)
end

Class Attribute Details

.userObject

Returns the value of attribute user.



4
5
6
# File 'lib/ezframe/auth.rb', line 4

def user
  @user
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



67
68
69
# File 'lib/ezframe/auth.rb', line 67

def 
  @account
end

#idObject

Returns the value of attribute id.



67
68
69
# File 'lib/ezframe/auth.rb', line 67

def id
  @id
end

#passwordObject

Returns the value of attribute password.



67
68
69
# File 'lib/ezframe/auth.rb', line 67

def password
  @password
end

#userObject

Returns the value of attribute user.



67
68
69
# File 'lib/ezframe/auth.rb', line 67

def user
  @user
end

Class Method Details

.authenticate(env, account, pass) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ezframe/auth.rb', line 40

def authenticate(env, , pass)
  return nil if !pass || pass.strip.empty?
  EzLog.debug("Auth.self.authenticate: account=#{}, pass=#{pass}")
  auth_conf = Config[:auth]
  user_data = DB.dataset(auth_conf[:table]).where(auth_conf[:user].to_sym =>  ).first
  if user_data
    EzLog.info "Auth: self.authenticate: has user: #{@user}"
  else
    EzLog.info "Auth.self.authenticate: this user does not exist: #{}"
    return nil
  end
  db_pass = user_data[auth_conf[:password].to_sym]
  user_data.delete(:password)
  return nil if !db_pass || db_pass.strip.length < 8
  bcrypt = BCrypt::Password.new(db_pass)
  if bcrypt == pass
    env['rack.session'][:user] = user_data[:id]
    @user = user_data
    EzLog.debug("Auth.self.authenticate: success: password match!")
    return true
  else
    EzLog.debug("Auth.self.authenticate: failure: password mismatch")
  end
  return nil
end

.get(account) ⇒ Object



36
37
38
# File 'lib/ezframe/auth.rb', line 36

def get()
  new()
end

.initObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ezframe/auth.rb', line 6

def init
  Warden::Manager.serialize_into_session do |auth|
    # EzLog.info "serialize_into: #{auth.inspect}"
    auth.user[:id]
  end
  Warden::Manager.serialize_from_session do ||
    # EzLog.info "serialize_from: account = #{account}"
    inst = Auth.get()
    # EzLog.info "inst = #{inst.inspect}"
    inst
  end
  Warden::Strategies.add(:mystrategy) do
    def valid?
      # EzLog.info "valid?"
      params["account"] || params["password"]
    end

    def authenticate!
      EzLog.info "mystrategy.authenticate!: user=#{user}, params=#{params}"
      if Auth.authenticate(env, params["account"], params["password"])
        EzLog.info "mystrategy.authenticate!: success: user=#{user}"
        success!(Auth.get(params["account"]))
      else
        EzLog.info "mystrategy.authenticate!: failed: user=#{user}"
        fail!(Message[:login_failure])
      end
    end
  end 
end

Instance Method Details

#inspectObject



86
87
88
# File 'lib/ezframe/auth.rb', line 86

def inspect
  "account=#{@account}, user=#{@user}, id=#{@id}"
end