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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
package authentication
import (
_ "embed" // Embed users_database.template.yml.
"errors"
"fmt"
"os"
"sync"
"time"
"github.com/go-crypt/crypt/algorithm"
"github.com/go-crypt/crypt/algorithm/argon2"
"github.com/go-crypt/crypt/algorithm/bcrypt"
"github.com/go-crypt/crypt/algorithm/pbkdf2"
"github.com/go-crypt/crypt/algorithm/scrypt"
"github.com/go-crypt/crypt/algorithm/shacrypt"
"github.com/authelia/authelia/v4/internal/configuration/schema"
"github.com/authelia/authelia/v4/internal/expression"
"github.com/authelia/authelia/v4/internal/logging"
)
// FileUserProvider is a provider reading details from a file.
type FileUserProvider struct {
config *schema.AuthenticationBackendFile
hash algorithm.Hash
database FileUserProviderDatabase
mutex sync.Mutex
timeoutReload time.Time
}
// NewFileUserProvider creates a new instance of FileUserProvider.
func NewFileUserProvider(config *schema.AuthenticationBackendFile) (provider *FileUserProvider) {
return &FileUserProvider{
config: config,
timeoutReload: time.Now().Add(-1 * time.Second),
database: NewFileUserDatabase(config.Path, config.Search.Email, config.Search.CaseInsensitive, getExtra(config)),
}
}
func getExtra(config *schema.AuthenticationBackendFile) (extra map[string]expression.ExtraAttribute) {
extra = make(map[string]expression.ExtraAttribute, len(config.ExtraAttributes))
if len(config.ExtraAttributes) != 0 {
for name, attribute := range config.ExtraAttributes {
extra[name] = attribute
}
}
return extra
}
// Reload the database.
func (p *FileUserProvider) Reload() (reloaded bool, err error) {
now := time.Now()
p.mutex.Lock()
defer p.mutex.Unlock()
if now.Before(p.timeoutReload) {
return false, nil
}
switch err = p.database.Load(); {
case err == nil:
p.setTimeoutReload(now)
case errors.Is(err, ErrNoContent):
return false, nil
default:
return false, fmt.Errorf("failed to reload: %w", err)
}
p.setTimeoutReload(now)
return true, nil
}
func (p *FileUserProvider) Shutdown() (err error) {
return nil
}
// CheckUserPassword checks if provided password matches for the given user.
func (p *FileUserProvider) CheckUserPassword(username string, password string) (match bool, err error) {
var details FileUserDatabaseUserDetails
if details, err = p.database.GetUserDetails(username); err != nil {
return false, err
}
if details.Disabled {
return false, ErrUserNotFound
}
return details.Password.MatchAdvanced(password)
}
// GetDetails retrieve the groups a user belongs to.
func (p *FileUserProvider) GetDetails(username string) (details *UserDetails, err error) {
var d FileUserDatabaseUserDetails
if d, err = p.database.GetUserDetails(username); err != nil {
return nil, err
}
if d.Disabled {
return nil, ErrUserNotFound
}
return d.ToUserDetails(), nil
}
func (p *FileUserProvider) GetDetailsExtended(username string) (details *UserDetailsExtended, err error) {
var d FileUserDatabaseUserDetails
if d, err = p.database.GetUserDetails(username); err != nil {
return nil, err
}
if d.Disabled {
return nil, ErrUserNotFound
}
return d.ToExtendedUserDetails(), nil
}
// UpdatePassword update the password of the given user.
func (p *FileUserProvider) UpdatePassword(username string, newPassword string) (err error) {
var details FileUserDatabaseUserDetails
if details, err = p.database.GetUserDetails(username); err != nil {
return err
}
if details.Disabled {
return ErrUserNotFound
}
var digest algorithm.Digest
if digest, err = p.hash.Hash(newPassword); err != nil {
return err
}
details.Password = schema.NewPasswordDigest(digest)
p.database.SetUserDetails(details.Username, &details)
p.mutex.Lock()
p.setTimeoutReload(time.Now())
p.mutex.Unlock()
if err = p.database.Save(); err != nil {
return err
}
return nil
}
// StartupCheck implements the startup check provider interface.
func (p *FileUserProvider) StartupCheck() (err error) {
if err = checkDatabase(p.config.Path); err != nil {
logging.Logger().WithError(err).Errorf("Error checking user authentication YAML database")
return fmt.Errorf("one or more errors occurred checking the authentication database")
}
if p.hash, err = NewFileCryptoHashFromConfig(p.config.Password); err != nil {
return err
}
if p.database == nil {
p.database = NewFileUserDatabase(p.config.Path, p.config.Search.Email, p.config.Search.CaseInsensitive, getExtra(p.config))
}
if err = p.database.Load(); err != nil {
return err
}
return nil
}
func (p *FileUserProvider) setTimeoutReload(now time.Time) {
p.timeoutReload = now.Add(time.Second / 2)
}
// NewFileCryptoHashFromConfig returns a crypt.Hash given a valid configuration.
func NewFileCryptoHashFromConfig(config schema.AuthenticationBackendFilePassword) (hash algorithm.Hash, err error) {
switch config.Algorithm {
case hashArgon2, "":
hash, err = argon2.New(
argon2.WithVariantName(config.Argon2.Variant),
argon2.WithT(config.Argon2.Iterations),
argon2.WithM(uint32(config.Argon2.Memory)), //nolint:gosec // Validated at runtime.
argon2.WithP(config.Argon2.Parallelism),
argon2.WithK(config.Argon2.KeyLength),
argon2.WithS(config.Argon2.SaltLength),
)
case hashSHA2Crypt:
hash, err = shacrypt.New(
shacrypt.WithVariantName(config.SHA2Crypt.Variant),
shacrypt.WithIterations(config.SHA2Crypt.Iterations),
shacrypt.WithSaltLength(config.SHA2Crypt.SaltLength),
)
case hashPBKDF2:
hash, err = pbkdf2.New(
pbkdf2.WithVariantName(config.PBKDF2.Variant),
pbkdf2.WithIterations(config.PBKDF2.Iterations),
pbkdf2.WithSaltLength(config.PBKDF2.SaltLength),
)
case hashSCrypt:
hash, err = scrypt.New(
scrypt.WithLN(config.SCrypt.Iterations),
scrypt.WithP(config.SCrypt.Parallelism),
scrypt.WithR(config.SCrypt.BlockSize),
scrypt.WithKeyLength(config.SCrypt.KeyLength),
scrypt.WithSaltLength(config.SCrypt.SaltLength),
)
case hashBCrypt:
hash, err = bcrypt.New(
bcrypt.WithVariantName(config.BCrypt.Variant),
bcrypt.WithIterations(config.BCrypt.Cost),
)
default:
return nil, fmt.Errorf("algorithm '%s' is unknown", config.Algorithm)
}
if err != nil {
return nil, fmt.Errorf("failed to initialize hash settings: %w", err)
}
return hash, nil
}
func checkDatabase(path string) (err error) {
if _, err = os.Stat(path); os.IsNotExist(err) {
if err = os.WriteFile(path, userYAMLTemplate, 0600); err != nil {
return fmt.Errorf("user authentication database file doesn't exist at path '%s' and could not be generated: %w", path, err)
}
return fmt.Errorf("user authentication database file doesn't exist at path '%s' and has been generated", path)
} else if err != nil {
return fmt.Errorf("error checking user authentication database file: %w", err)
}
return nil
}
//go:embed users_database.template.yml
var userYAMLTemplate []byte
|