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.
|
2019-04-24 21:52:08 +00:00
|
|
|
"fmt"
|
2020-06-17 06:25:35 +00:00
|
|
|
"os"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
"github.com/go-crypt/crypt"
|
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 10:51:59 +00:00
|
|
|
config *schema.FileAuthenticationBackend
|
|
|
|
hash crypt.Hash
|
|
|
|
database *FileUserDatabase
|
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 10:51:59 +00:00
|
|
|
config: config,
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 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
|
|
|
}
|
|
|
|
|
2022-10-17 10:51:59 +00:00
|
|
|
p.database = NewFileUserDatabase(p.config.Path)
|
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 10:51:59 +00:00
|
|
|
// NewFileCryptoHashFromConfig returns a crypt.Hash given a valid configuration.
|
|
|
|
func NewFileCryptoHashFromConfig(config schema.Password) (hash crypt.Hash, err error) {
|
|
|
|
switch config.Algorithm {
|
|
|
|
case hashArgon2, "":
|
|
|
|
hash = crypt.NewArgon2Hash().
|
|
|
|
WithVariant(crypt.NewArgon2Variant(config.Argon2.Variant)).
|
|
|
|
WithT(config.Argon2.Iterations).
|
|
|
|
WithM(config.Argon2.Memory).
|
|
|
|
WithP(config.Argon2.Parallelism).
|
|
|
|
WithK(config.Argon2.KeyLength).
|
|
|
|
WithS(config.Argon2.SaltLength)
|
|
|
|
case hashSHA2Crypt:
|
|
|
|
hash = crypt.NewSHA2CryptHash().
|
|
|
|
WithVariant(crypt.NewSHA2CryptVariant(config.SHA2Crypt.Variant)).
|
|
|
|
WithRounds(config.SHA2Crypt.Iterations).
|
|
|
|
WithSaltLength(config.SHA2Crypt.SaltLength)
|
|
|
|
case hashPBKDF2:
|
|
|
|
hash = crypt.NewPBKDF2Hash().
|
|
|
|
WithVariant(crypt.NewPBKDF2Variant(config.PBKDF2.Variant)).
|
|
|
|
WithIterations(config.PBKDF2.Iterations).
|
|
|
|
WithSaltLength(config.PBKDF2.SaltLength)
|
|
|
|
case hashSCrypt:
|
|
|
|
hash = crypt.NewScryptHash().
|
|
|
|
WithLN(config.SCrypt.Iterations).
|
|
|
|
WithP(config.SCrypt.Parallelism).
|
|
|
|
WithR(config.SCrypt.BlockSize)
|
|
|
|
case hashBCrypt:
|
|
|
|
hash = crypt.NewBcryptHash().
|
|
|
|
WithVariant(crypt.NewBcryptVariant(config.BCrypt.Variant)).
|
|
|
|
WithCost(config.BCrypt.Cost)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("algorithm '%s' is unknown", config.Algorithm)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|