2019-04-24 21:52:08 +00:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
2021-02-21 23:07:06 +00:00
|
|
|
_ "embed" // Embed users_database.template.yml.
|
2022-10-17 11:31:23 +00:00
|
|
|
"errors"
|
2019-04-24 21:52:08 +00:00
|
|
|
"fmt"
|
2020-06-17 06:25:35 +00:00
|
|
|
"os"
|
2022-10-17 11:31:23 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-12-04 22:37:08 +00:00
|
|
|
"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"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileUserProvider is a provider reading details from a file.
|
|
|
|
type FileUserProvider struct {
|
2022-10-17 11:31:23 +00:00
|
|
|
config *schema.FileAuthenticationBackend
|
2022-12-04 22:37:08 +00:00
|
|
|
hash algorithm.Hash
|
2022-10-17 11:31:23 +00:00
|
|
|
database *FileUserDatabase
|
|
|
|
mutex *sync.Mutex
|
|
|
|
timeoutReload time.Time
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileUserProvider creates a new instance of FileUserProvider.
|
2022-10-17 10:51:59 +00:00
|
|
|
func NewFileUserProvider(config *schema.FileAuthenticationBackend) (provider *FileUserProvider) {
|
2019-04-24 21:52:08 +00:00
|
|
|
return &FileUserProvider{
|
2022-10-17 11:31:23 +00:00
|
|
|
config: config,
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
timeoutReload: time.Now().Add(-1 * time.Second),
|
2023-05-23 19:57:53 +00:00
|
|
|
database: NewFileUserDatabase(config.Path, config.Search.Email, config.Search.CaseInsensitive),
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 11:31:23 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
// CheckUserPassword checks if provided password matches for the given user.
|
|
|
|
func (p *FileUserProvider) CheckUserPassword(username string, password string) (match bool, err error) {
|
|
|
|
var details DatabaseUserDetails
|
2020-05-08 03:38:22 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if details, err = p.database.GetUserDetails(username); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-05-08 03:38:22 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if details.Disabled {
|
|
|
|
return false, ErrUserNotFound
|
2019-12-27 17:09:57 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
return details.Digest.MatchAdvanced(password)
|
2019-12-27 17:09:57 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
// GetDetails retrieve the groups a user belongs to.
|
|
|
|
func (p *FileUserProvider) GetDetails(username string) (details *UserDetails, err error) {
|
|
|
|
var d DatabaseUserDetails
|
2020-06-17 06:25:35 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if d, err = p.database.GetUserDetails(username); err != nil {
|
|
|
|
return nil, err
|
2020-06-17 06:25:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if d.Disabled {
|
|
|
|
return nil, ErrUserNotFound
|
2020-06-17 06:25:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
return d.ToUserDetails(), nil
|
2020-06-17 06:25:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
// UpdatePassword update the password of the given user.
|
|
|
|
func (p *FileUserProvider) UpdatePassword(username string, newPassword string) (err error) {
|
|
|
|
var details DatabaseUserDetails
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if details, err = p.database.GetUserDetails(username); err != nil {
|
|
|
|
return err
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if details.Disabled {
|
|
|
|
return ErrUserNotFound
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if details.Digest, err = p.hash.Hash(newPassword); err != nil {
|
|
|
|
return err
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
p.database.SetUserDetails(details.Username, &details)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-10-17 11:31:23 +00:00
|
|
|
p.mutex.Lock()
|
|
|
|
|
|
|
|
p.setTimeoutReload(time.Now())
|
|
|
|
|
|
|
|
p.mutex.Unlock()
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if err = p.database.Save(); err != nil {
|
|
|
|
return err
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
2020-05-01 22:32:09 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
return nil
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
// 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")
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
return fmt.Errorf("one or more errors occurred checking the authentication database")
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if p.hash, err = NewFileCryptoHashFromConfig(p.config.Password); err != nil {
|
2020-05-08 03:38:22 +00:00
|
|
|
return err
|
2020-03-06 01:38:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 19:57:53 +00:00
|
|
|
if p.database == nil {
|
|
|
|
p.database = NewFileUserDatabase(p.config.Path, p.config.Search.Email, p.config.Search.CaseInsensitive)
|
|
|
|
}
|
2020-05-08 03:38:22 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if err = p.database.Load(); err != nil {
|
2020-03-06 01:38:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-10-17 11:31:23 +00:00
|
|
|
func (p *FileUserProvider) setTimeoutReload(now time.Time) {
|
|
|
|
p.timeoutReload = now.Add(time.Second / 2)
|
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
// NewFileCryptoHashFromConfig returns a crypt.Hash given a valid configuration.
|
2022-12-04 22:37:08 +00:00
|
|
|
func NewFileCryptoHashFromConfig(config schema.Password) (hash algorithm.Hash, err error) {
|
2022-10-17 10:51:59 +00:00
|
|
|
switch config.Algorithm {
|
|
|
|
case hashArgon2, "":
|
2022-12-04 22:37:08 +00:00
|
|
|
hash, err = argon2.New(
|
|
|
|
argon2.WithVariantName(config.Argon2.Variant),
|
|
|
|
argon2.WithT(config.Argon2.Iterations),
|
|
|
|
argon2.WithM(uint32(config.Argon2.Memory)),
|
|
|
|
argon2.WithP(config.Argon2.Parallelism),
|
|
|
|
argon2.WithK(config.Argon2.KeyLength),
|
|
|
|
argon2.WithS(config.Argon2.SaltLength),
|
|
|
|
)
|
2022-10-17 10:51:59 +00:00
|
|
|
case hashSHA2Crypt:
|
2022-12-04 22:37:08 +00:00
|
|
|
hash, err = shacrypt.New(
|
|
|
|
shacrypt.WithVariantName(config.SHA2Crypt.Variant),
|
|
|
|
shacrypt.WithIterations(config.SHA2Crypt.Iterations),
|
|
|
|
shacrypt.WithSaltLength(config.SHA2Crypt.SaltLength),
|
|
|
|
)
|
2022-10-17 10:51:59 +00:00
|
|
|
case hashPBKDF2:
|
2022-12-04 22:37:08 +00:00
|
|
|
hash, err = pbkdf2.New(
|
|
|
|
pbkdf2.WithVariantName(config.PBKDF2.Variant),
|
|
|
|
pbkdf2.WithIterations(config.PBKDF2.Iterations),
|
|
|
|
pbkdf2.WithSaltLength(config.PBKDF2.SaltLength),
|
|
|
|
)
|
2022-10-17 10:51:59 +00:00
|
|
|
case hashSCrypt:
|
2022-12-04 22:37:08 +00:00
|
|
|
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),
|
|
|
|
)
|
2022-10-17 10:51:59 +00:00
|
|
|
case hashBCrypt:
|
2022-12-04 22:37:08 +00:00
|
|
|
hash, err = bcrypt.New(
|
|
|
|
bcrypt.WithVariantName(config.BCrypt.Variant),
|
|
|
|
bcrypt.WithIterations(config.BCrypt.Cost),
|
|
|
|
)
|
2022-10-17 10:51:59 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("algorithm '%s' is unknown", config.Algorithm)
|
|
|
|
}
|
|
|
|
|
2022-12-04 22:37:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize hash settings: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
if err = hash.Validate(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to validate hash settings: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash, nil
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-09-17 09:53:59 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-17 10:51:59 +00:00
|
|
|
|
|
|
|
//go:embed users_database.template.yml
|
|
|
|
var userYAMLTemplate []byte
|