2019-11-16 10:38:21 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2021-11-23 09:45:38 +00:00
|
|
|
"context"
|
2019-11-16 10:38:21 +00:00
|
|
|
"database/sql"
|
2021-11-23 09:45:38 +00:00
|
|
|
"errors"
|
2019-11-16 19:50:58 +00:00
|
|
|
"fmt"
|
2019-11-16 10:38:21 +00:00
|
|
|
"time"
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-07-16 05:56:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/authentication"
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
|
|
|
"github.com/authelia/authelia/v4/internal/models"
|
2019-11-16 10:38:21 +00:00
|
|
|
)
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// NewSQLProvider generates a generic SQLProvider to be used with other SQL provider NewUp's.
|
|
|
|
func NewSQLProvider(name, driverName, dataSourceName string) (provider SQLProvider) {
|
|
|
|
db, err := sqlx.Open(driverName, dataSourceName)
|
2020-03-04 23:25:52 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
provider = SQLProvider{
|
|
|
|
name: name,
|
|
|
|
driverName: driverName,
|
|
|
|
db: db,
|
|
|
|
log: logging.Logger(),
|
|
|
|
errOpen: err,
|
2019-11-16 19:50:58 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
sqlInsertAuthenticationAttempt: fmt.Sprintf(queryFmtInsertAuthenticationLogEntry, tableAuthenticationLogs),
|
|
|
|
sqlSelectAuthenticationAttemptsByUsername: fmt.Sprintf(queryFmtSelect1FAAuthenticationLogEntryByUsername, tableAuthenticationLogs),
|
2019-11-16 19:50:58 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
sqlInsertIdentityVerification: fmt.Sprintf(queryFmtInsertIdentityVerification, tableIdentityVerification),
|
|
|
|
sqlDeleteIdentityVerification: fmt.Sprintf(queryFmtDeleteIdentityVerification, tableIdentityVerification),
|
|
|
|
sqlSelectExistsIdentityVerification: fmt.Sprintf(queryFmtSelectExistsIdentityVerification, tableIdentityVerification),
|
2019-11-16 19:50:58 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
sqlUpsertTOTPConfig: fmt.Sprintf(queryFmtUpsertTOTPConfiguration, tableTOTPConfigurations),
|
|
|
|
sqlDeleteTOTPConfig: fmt.Sprintf(queryFmtDeleteTOTPConfiguration, tableTOTPConfigurations),
|
|
|
|
sqlSelectTOTPConfig: fmt.Sprintf(queryFmtSelectTOTPConfiguration, tableTOTPConfigurations),
|
2019-11-16 19:50:58 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
sqlUpsertU2FDevice: fmt.Sprintf(queryFmtUpsertU2FDevice, tableU2FDevices),
|
|
|
|
sqlSelectU2FDevice: fmt.Sprintf(queryFmtSelectU2FDevice, tableU2FDevices),
|
2020-07-16 05:56:08 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
sqlUpsertPreferred2FAMethod: fmt.Sprintf(queryFmtUpsertPreferred2FAMethod, tableUserPreferences),
|
|
|
|
sqlSelectPreferred2FAMethod: fmt.Sprintf(queryFmtSelectPreferred2FAMethod, tableUserPreferences),
|
|
|
|
sqlSelectUserInfo: fmt.Sprintf(queryFmtSelectUserInfo, tableTOTPConfigurations, tableU2FDevices, tableUserPreferences),
|
2019-11-16 10:38:21 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
sqlInsertMigration: fmt.Sprintf(queryFmtInsertMigration, tableMigrations),
|
|
|
|
sqlSelectMigrations: fmt.Sprintf(queryFmtSelectMigrations, tableMigrations),
|
|
|
|
sqlSelectLatestMigration: fmt.Sprintf(queryFmtSelectLatestMigration, tableMigrations),
|
2019-11-16 10:38:21 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
sqlFmtRenameTable: queryFmtRenameTable,
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
return provider
|
|
|
|
}
|
2020-07-16 05:56:08 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// SQLProvider is a storage provider persisting data in a SQL database.
|
|
|
|
type SQLProvider struct {
|
|
|
|
db *sqlx.DB
|
|
|
|
log *logrus.Logger
|
|
|
|
name string
|
|
|
|
driverName string
|
|
|
|
errOpen error
|
|
|
|
|
|
|
|
// Table: authentication_logs.
|
|
|
|
sqlInsertAuthenticationAttempt string
|
|
|
|
sqlSelectAuthenticationAttemptsByUsername string
|
|
|
|
|
|
|
|
// Table: identity_verification_tokens.
|
|
|
|
sqlInsertIdentityVerification string
|
|
|
|
sqlDeleteIdentityVerification string
|
|
|
|
sqlSelectExistsIdentityVerification string
|
|
|
|
|
|
|
|
// Table: totp_configurations.
|
|
|
|
sqlUpsertTOTPConfig string
|
|
|
|
sqlDeleteTOTPConfig string
|
|
|
|
sqlSelectTOTPConfig string
|
|
|
|
|
|
|
|
// Table: u2f_devices.
|
|
|
|
sqlUpsertU2FDevice string
|
|
|
|
sqlSelectU2FDevice string
|
|
|
|
|
|
|
|
// Table: user_preferences.
|
|
|
|
sqlUpsertPreferred2FAMethod string
|
|
|
|
sqlSelectPreferred2FAMethod string
|
|
|
|
sqlSelectUserInfo string
|
|
|
|
|
|
|
|
// Table: migrations.
|
|
|
|
sqlInsertMigration string
|
|
|
|
sqlSelectMigrations string
|
|
|
|
sqlSelectLatestMigration string
|
|
|
|
|
|
|
|
// Utility.
|
|
|
|
sqlSelectExistingTables string
|
|
|
|
sqlFmtRenameTable string
|
|
|
|
}
|
2020-07-16 05:56:08 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// StartupCheck implements the provider startup check interface.
|
|
|
|
func (p *SQLProvider) StartupCheck() (err error) {
|
|
|
|
if p.errOpen != nil {
|
|
|
|
return p.errOpen
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// TODO: Decide if this is needed, or if it should be configurable.
|
|
|
|
for i := 0; i < 19; i++ {
|
|
|
|
err = p.db.Ping()
|
|
|
|
if err == nil {
|
|
|
|
break
|
2020-07-16 05:56:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-07-16 05:56:08 +00:00
|
|
|
return err
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
p.log.Infof("Storage schema is being checked for updates")
|
2020-07-16 05:56:08 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
ctx := context.Background()
|
2020-07-16 05:56:08 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
err = p.SchemaMigrate(ctx, true, SchemaLatest)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
switch err {
|
|
|
|
case ErrSchemaAlreadyUpToDate:
|
|
|
|
p.log.Infof("Storage schema is already up to date")
|
|
|
|
return nil
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// SavePreferred2FAMethod save the preferred method for 2FA to the database.
|
|
|
|
func (p *SQLProvider) SavePreferred2FAMethod(ctx context.Context, username string, method string) (err error) {
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpsertPreferred2FAMethod, username, method)
|
2020-07-16 05:56:08 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
return err
|
2020-07-16 05:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadPreferred2FAMethod load the preferred method for 2FA from the database.
|
2021-11-23 09:45:38 +00:00
|
|
|
func (p *SQLProvider) LoadPreferred2FAMethod(ctx context.Context, username string) (method string, err error) {
|
|
|
|
err = p.db.GetContext(ctx, &method, p.sqlSelectPreferred2FAMethod, username)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
switch err {
|
|
|
|
case sql.ErrNoRows:
|
|
|
|
return "", nil
|
|
|
|
case nil:
|
|
|
|
return method, err
|
|
|
|
default:
|
2019-11-16 10:38:21 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// LoadUserInfo loads the models.UserInfo from the database.
|
|
|
|
func (p *SQLProvider) LoadUserInfo(ctx context.Context, username string) (info models.UserInfo, err error) {
|
|
|
|
err = p.db.GetContext(ctx, &info, p.sqlSelectUserInfo, username, username, username)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return info, nil
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpsertPreferred2FAMethod, username, authentication.PossibleMethods[0])
|
|
|
|
if err != nil {
|
|
|
|
return models.UserInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.db.GetContext(ctx, &info, p.sqlSelectUserInfo, username, username, username)
|
|
|
|
if err != nil {
|
|
|
|
return models.UserInfo{}, err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
return info, nil
|
|
|
|
default:
|
|
|
|
return models.UserInfo{}, err
|
|
|
|
}
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// SaveIdentityVerification save an identity verification record to the database.
|
|
|
|
func (p *SQLProvider) SaveIdentityVerification(ctx context.Context, verification models.IdentityVerification) (err error) {
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlInsertIdentityVerification, verification.Token)
|
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// RemoveIdentityVerification remove an identity verification record from the database.
|
|
|
|
func (p *SQLProvider) RemoveIdentityVerification(ctx context.Context, token string) (err error) {
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlDeleteIdentityVerification, token)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// FindIdentityVerification checks if an identity verification record is in the database and active.
|
|
|
|
func (p *SQLProvider) FindIdentityVerification(ctx context.Context, jti string) (found bool, err error) {
|
|
|
|
err = p.db.GetContext(ctx, &found, p.sqlSelectExistsIdentityVerification, jti)
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
return found, nil
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// SaveTOTPConfiguration save a TOTP config of a given user in the database.
|
|
|
|
func (p *SQLProvider) SaveTOTPConfiguration(ctx context.Context, config models.TOTPConfiguration) (err error) {
|
|
|
|
// TODO: Encrypt config.Secret here.
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpsertTOTPConfig,
|
|
|
|
config.Username,
|
|
|
|
config.Algorithm,
|
|
|
|
config.Digits,
|
|
|
|
config.Period,
|
|
|
|
config.Secret,
|
|
|
|
)
|
2019-11-16 10:38:21 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// DeleteTOTPConfiguration delete a TOTP secret from the database given a username.
|
|
|
|
func (p *SQLProvider) DeleteTOTPConfiguration(ctx context.Context, username string) (err error) {
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlDeleteTOTPConfig, username)
|
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// LoadTOTPConfiguration load a TOTP secret given a username from the database.
|
|
|
|
func (p *SQLProvider) LoadTOTPConfiguration(ctx context.Context, username string) (config *models.TOTPConfiguration, err error) {
|
|
|
|
config = &models.TOTPConfiguration{}
|
|
|
|
|
|
|
|
err = p.db.QueryRowxContext(ctx, p.sqlSelectTOTPConfig, username).StructScan(config)
|
|
|
|
if err != nil {
|
2019-11-16 10:38:21 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2021-11-23 09:45:38 +00:00
|
|
|
return nil, ErrNoTOTPSecret
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
return nil, err
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// TODO: Decrypt config.Secret here.
|
|
|
|
return config, nil
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// SaveU2FDevice saves a registered U2F device.
|
|
|
|
func (p *SQLProvider) SaveU2FDevice(ctx context.Context, device models.U2FDevice) (err error) {
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpsertU2FDevice, device.Username, device.KeyHandle, device.PublicKey)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// LoadU2FDevice loads a U2F device registration for a given username.
|
|
|
|
func (p *SQLProvider) LoadU2FDevice(ctx context.Context, username string) (device *models.U2FDevice, err error) {
|
|
|
|
device = &models.U2FDevice{
|
|
|
|
Username: username,
|
2019-11-17 01:05:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
err = p.db.GetContext(ctx, device, p.sqlSelectU2FDevice, username)
|
2019-11-17 01:05:46 +00:00
|
|
|
if err != nil {
|
2021-11-23 09:45:38 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, ErrNoU2FDeviceHandle
|
|
|
|
}
|
2019-11-17 01:05:46 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
return nil, err
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
2019-11-16 19:50:58 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
return device, nil
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendAuthenticationLog append a mark to the authentication log.
|
2021-11-23 09:45:38 +00:00
|
|
|
func (p *SQLProvider) AppendAuthenticationLog(ctx context.Context, attempt models.AuthenticationAttempt) (err error) {
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlInsertAuthenticationAttempt, attempt.Time, attempt.Successful, attempt.Username)
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// LoadAuthenticationLogs retrieve the latest failed authentications from the authentication log.
|
|
|
|
func (p *SQLProvider) LoadAuthenticationLogs(ctx context.Context, username string, fromDate time.Time, limit, page int) (attempts []models.AuthenticationAttempt, err error) {
|
|
|
|
rows, err := p.db.QueryxContext(ctx, p.sqlSelectAuthenticationAttemptsByUsername, fromDate, username, limit, limit*page)
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
defer func() {
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
p.log.Errorf(logFmtErrClosingConn, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
attempts = make([]models.AuthenticationAttempt, 0, limit)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
for rows.Next() {
|
2021-11-23 09:45:38 +00:00
|
|
|
var attempt models.AuthenticationAttempt
|
2019-11-16 10:38:21 +00:00
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
err = rows.StructScan(&attempt)
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
attempts = append(attempts, attempt)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
return attempts, nil
|
|
|
|
}
|