forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.rb
115 lines (80 loc) · 1.98 KB
/
auth.rb
1
2
3
4
5
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
35
36
37
38
39
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Auth
include ApplicationLib
=begin
checks if a given user can login. Checks for
- valid user
- active state
- max failed logins
result = Auth.can_login?(user)
returns
result = true | false
=end
def self.can_login?(user)
return false if !user.is_a?(User)
return false if !user.active?
return true if !user.max_login_failed?
Rails.logger.info "Max login failed reached for user #{user.login}."
false
end
=begin
checks if a given user and password match against multiple auth backends
- valid user
- active state
- max failed logins
result = Auth.valid?(user, password)
returns
result = true | false
=end
def self.valid?(user, password)
# try to login against configure auth backends
backends.any? do |config|
next if !backend_validates?(
config: config,
user: user,
password: password,
)
Rails.logger.info "Authentication against #{config[:adapter]} for user #{user.login} ok."
# remember last login date
user.update_last_login
true
end
end
=begin
returns a list of all Auth backend configurations
result = Auth.backends
returns
result = [
{
adapter: 'Auth::Internal',
},
{
adapter: 'Auth::Developer',
},
...
]
=end
def self.backends
# use std. auth backends
config = [
{
adapter: 'Auth::Internal',
},
{
adapter: 'Auth::Developer',
},
]
# added configured backends
Setting.where(area: 'Security::Authentication').each do |setting|
next if setting.state_current[:value].blank?
config.push setting.state_current[:value]
end
config
end
def self.backend_validates?(config:, user:, password:)
return false if !config[:adapter]
instance = config[:adapter].constantize.new(config)
instance.valid?(user, password)
end
private_class_method :backend_validates?
end