2019-11-16 17:38:21 +07:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2021-11-23 16:45:38 +07:00
|
|
|
"context"
|
2021-11-25 08:56:58 +07:00
|
|
|
"crypto/sha256"
|
2019-11-16 17:38:21 +07:00
|
|
|
"database/sql"
|
2021-11-23 16:45:38 +07:00
|
|
|
"errors"
|
2019-11-17 02:50:58 +07:00
|
|
|
"fmt"
|
2019-11-16 17:38:21 +07:00
|
|
|
"time"
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-07-16 12:56:08 +07:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/authentication"
|
2021-12-01 19:11:29 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
|
|
|
"github.com/authelia/authelia/v4/internal/models"
|
2019-11-16 17:38:21 +07:00
|
|
|
)
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// NewSQLProvider generates a generic SQLProvider to be used with other SQL provider NewUp's.
|
2021-12-01 19:11:29 +07:00
|
|
|
func NewSQLProvider(config *schema.Configuration, name, driverName, dataSourceName string) (provider SQLProvider) {
|
2021-11-23 16:45:38 +07:00
|
|
|
db, err := sqlx.Open(driverName, dataSourceName)
|
2020-03-05 06:25:52 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
provider = SQLProvider{
|
2021-11-25 08:56:58 +07:00
|
|
|
db: db,
|
2021-12-01 19:11:29 +07:00
|
|
|
key: sha256.Sum256([]byte(config.Storage.EncryptionKey)),
|
2021-11-23 16:45:38 +07:00
|
|
|
name: name,
|
|
|
|
driverName: driverName,
|
2021-12-01 19:11:29 +07:00
|
|
|
config: config,
|
2021-11-23 16:45:38 +07:00
|
|
|
errOpen: err,
|
2021-11-25 08:56:58 +07:00
|
|
|
log: logging.Logger(),
|
2019-11-17 02:50:58 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
sqlInsertAuthenticationAttempt: fmt.Sprintf(queryFmtInsertAuthenticationLogEntry, tableAuthenticationLogs),
|
|
|
|
sqlSelectAuthenticationAttemptsByUsername: fmt.Sprintf(queryFmtSelect1FAAuthenticationLogEntryByUsername, tableAuthenticationLogs),
|
2019-11-17 02:50:58 +07:00
|
|
|
|
2021-12-04 11:34:20 +07:00
|
|
|
sqlInsertIdentityVerification: fmt.Sprintf(queryFmtInsertIdentityVerification, tableIdentityVerification),
|
|
|
|
sqlConsumeIdentityVerification: fmt.Sprintf(queryFmtConsumeIdentityVerification, tableIdentityVerification),
|
|
|
|
sqlSelectIdentityVerification: fmt.Sprintf(queryFmtSelectIdentityVerification, tableIdentityVerification),
|
2019-11-17 02:50:58 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
sqlUpsertTOTPConfig: fmt.Sprintf(queryFmtUpsertTOTPConfiguration, tableTOTPConfigurations),
|
|
|
|
sqlDeleteTOTPConfig: fmt.Sprintf(queryFmtDeleteTOTPConfiguration, tableTOTPConfigurations),
|
|
|
|
sqlSelectTOTPConfig: fmt.Sprintf(queryFmtSelectTOTPConfiguration, tableTOTPConfigurations),
|
|
|
|
sqlSelectTOTPConfigs: fmt.Sprintf(queryFmtSelectTOTPConfigurations, tableTOTPConfigurations),
|
|
|
|
|
|
|
|
sqlUpdateTOTPConfigSecret: fmt.Sprintf(queryFmtUpdateTOTPConfigurationSecret, tableTOTPConfigurations),
|
|
|
|
sqlUpdateTOTPConfigSecretByUsername: fmt.Sprintf(queryFmtUpdateTOTPConfigurationSecretByUsername, tableTOTPConfigurations),
|
2019-11-17 02:50:58 +07:00
|
|
|
|
2021-12-03 07:04:11 +07:00
|
|
|
sqlUpsertU2FDevice: fmt.Sprintf(queryFmtUpsertU2FDevice, tableU2FDevices),
|
|
|
|
sqlSelectU2FDevice: fmt.Sprintf(queryFmtSelectU2FDevice, tableU2FDevices),
|
|
|
|
sqlSelectU2FDevices: fmt.Sprintf(queryFmtSelectU2FDevices, tableU2FDevices),
|
|
|
|
|
|
|
|
sqlUpdateU2FDevicePublicKey: fmt.Sprintf(queryFmtUpdateU2FDevicePublicKey, tableU2FDevices),
|
|
|
|
sqlUpdateU2FDevicePublicKeyByUsername: fmt.Sprintf(queryFmtUpdateUpdateU2FDevicePublicKeyByUsername, tableU2FDevices),
|
2020-07-16 12:56:08 +07:00
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
sqlUpsertDuoDevice: fmt.Sprintf(queryFmtUpsertDuoDevice, tableDuoDevices),
|
|
|
|
sqlDeleteDuoDevice: fmt.Sprintf(queryFmtDeleteDuoDevice, tableDuoDevices),
|
|
|
|
sqlSelectDuoDevice: fmt.Sprintf(queryFmtSelectDuoDevice, tableDuoDevices),
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
sqlUpsertPreferred2FAMethod: fmt.Sprintf(queryFmtUpsertPreferred2FAMethod, tableUserPreferences),
|
|
|
|
sqlSelectPreferred2FAMethod: fmt.Sprintf(queryFmtSelectPreferred2FAMethod, tableUserPreferences),
|
2021-12-01 10:32:58 +07:00
|
|
|
sqlSelectUserInfo: fmt.Sprintf(queryFmtSelectUserInfo, tableTOTPConfigurations, tableU2FDevices, tableDuoDevices, tableUserPreferences),
|
2019-11-16 17:38:21 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
sqlInsertMigration: fmt.Sprintf(queryFmtInsertMigration, tableMigrations),
|
|
|
|
sqlSelectMigrations: fmt.Sprintf(queryFmtSelectMigrations, tableMigrations),
|
|
|
|
sqlSelectLatestMigration: fmt.Sprintf(queryFmtSelectLatestMigration, tableMigrations),
|
2019-11-16 17:38:21 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
sqlUpsertEncryptionValue: fmt.Sprintf(queryFmtUpsertEncryptionValue, tableEncryption),
|
|
|
|
sqlSelectEncryptionValue: fmt.Sprintf(queryFmtSelectEncryptionValue, tableEncryption),
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
sqlFmtRenameTable: queryFmtRenameTable,
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
return provider
|
|
|
|
}
|
2020-07-16 12:56:08 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// SQLProvider is a storage provider persisting data in a SQL database.
|
|
|
|
type SQLProvider struct {
|
|
|
|
db *sqlx.DB
|
2021-11-25 08:56:58 +07:00
|
|
|
key [32]byte
|
2021-11-23 16:45:38 +07:00
|
|
|
name string
|
|
|
|
driverName string
|
2021-12-03 13:29:55 +07:00
|
|
|
schema string
|
2021-12-01 19:11:29 +07:00
|
|
|
config *schema.Configuration
|
2021-11-23 16:45:38 +07:00
|
|
|
errOpen error
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
log *logrus.Logger
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// Table: authentication_logs.
|
|
|
|
sqlInsertAuthenticationAttempt string
|
|
|
|
sqlSelectAuthenticationAttemptsByUsername string
|
|
|
|
|
2021-11-30 13:58:21 +07:00
|
|
|
// Table: identity_verification.
|
2021-12-04 11:34:20 +07:00
|
|
|
sqlInsertIdentityVerification string
|
|
|
|
sqlConsumeIdentityVerification string
|
|
|
|
sqlSelectIdentityVerification string
|
2021-11-23 16:45:38 +07:00
|
|
|
|
|
|
|
// Table: totp_configurations.
|
2021-11-25 08:56:58 +07:00
|
|
|
sqlUpsertTOTPConfig string
|
|
|
|
sqlDeleteTOTPConfig string
|
|
|
|
sqlSelectTOTPConfig string
|
|
|
|
sqlSelectTOTPConfigs string
|
|
|
|
|
|
|
|
sqlUpdateTOTPConfigSecret string
|
|
|
|
sqlUpdateTOTPConfigSecretByUsername string
|
2021-11-23 16:45:38 +07:00
|
|
|
|
|
|
|
// Table: u2f_devices.
|
2021-12-03 07:04:11 +07:00
|
|
|
sqlUpsertU2FDevice string
|
|
|
|
sqlSelectU2FDevice string
|
|
|
|
sqlSelectU2FDevices string
|
|
|
|
|
|
|
|
sqlUpdateU2FDevicePublicKey string
|
|
|
|
sqlUpdateU2FDevicePublicKeyByUsername string
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2022-01-31 12:25:15 +07:00
|
|
|
// Table: duo_devices.
|
2021-12-01 10:32:58 +07:00
|
|
|
sqlUpsertDuoDevice string
|
|
|
|
sqlDeleteDuoDevice string
|
|
|
|
sqlSelectDuoDevice string
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// Table: user_preferences.
|
|
|
|
sqlUpsertPreferred2FAMethod string
|
|
|
|
sqlSelectPreferred2FAMethod string
|
|
|
|
sqlSelectUserInfo string
|
|
|
|
|
|
|
|
// Table: migrations.
|
|
|
|
sqlInsertMigration string
|
|
|
|
sqlSelectMigrations string
|
|
|
|
sqlSelectLatestMigration string
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
// Table: encryption.
|
|
|
|
sqlUpsertEncryptionValue string
|
|
|
|
sqlSelectEncryptionValue string
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// Utility.
|
|
|
|
sqlSelectExistingTables string
|
|
|
|
sqlFmtRenameTable string
|
|
|
|
}
|
2020-07-16 12:56:08 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
// Close the underlying database connection.
|
|
|
|
func (p *SQLProvider) Close() (err error) {
|
|
|
|
return p.db.Close()
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// StartupCheck implements the provider startup check interface.
|
|
|
|
func (p *SQLProvider) StartupCheck() (err error) {
|
|
|
|
if p.errOpen != nil {
|
2021-11-25 08:56:58 +07:00
|
|
|
return fmt.Errorf("error opening database: %w", p.errOpen)
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// TODO: Decide if this is needed, or if it should be configurable.
|
|
|
|
for i := 0; i < 19; i++ {
|
2021-11-25 08:56:58 +07:00
|
|
|
if err = p.db.Ping(); err == nil {
|
2021-11-23 16:45:38 +07:00
|
|
|
break
|
2020-07-16 12:56:08 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-11-25 08:56:58 +07:00
|
|
|
return fmt.Errorf("error pinging database: %w", err)
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
p.log.Infof("Storage schema is being checked for updates")
|
2020-07-16 12:56:08 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
ctx := context.Background()
|
2020-07-16 12:56:08 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
if err = p.SchemaEncryptionCheckKey(ctx, false); err != nil && !errors.Is(err, ErrSchemaEncryptionVersionUnsupported) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
err = p.SchemaMigrate(ctx, true, SchemaLatest)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
switch err {
|
|
|
|
case ErrSchemaAlreadyUpToDate:
|
|
|
|
p.log.Infof("Storage schema is already up to date")
|
|
|
|
return nil
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
default:
|
2021-11-25 08:56:58 +07:00
|
|
|
return fmt.Errorf("error during schema migrate: %w", err)
|
2021-11-23 16:45:38 +07:00
|
|
|
}
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07: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 12:56:08 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
return err
|
2020-07-16 12:56:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadPreferred2FAMethod load the preferred method for 2FA from the database.
|
2021-11-23 16:45:38 +07: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-06 02:35:32 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return method, nil
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
2021-11-23 16:45:38 +07:00
|
|
|
return "", nil
|
|
|
|
default:
|
2021-11-25 08:56:58 +07:00
|
|
|
return "", fmt.Errorf("error selecting preferred two factor method for user '%s': %w", username, err)
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
2021-11-23 16:45:38 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// LoadUserInfo loads the models.UserInfo from the database.
|
|
|
|
func (p *SQLProvider) LoadUserInfo(ctx context.Context, username string) (info models.UserInfo, err error) {
|
2021-12-01 10:32:58 +07:00
|
|
|
err = p.db.GetContext(ctx, &info, p.sqlSelectUserInfo, username, username, username, username)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return info, nil
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
2021-11-25 08:56:58 +07:00
|
|
|
if _, err = p.db.ExecContext(ctx, p.sqlUpsertPreferred2FAMethod, username, authentication.PossibleMethods[0]); err != nil {
|
|
|
|
return models.UserInfo{}, fmt.Errorf("error upserting preferred two factor method while selecting user info for user '%s': %w", username, err)
|
2021-11-23 16:45:38 +07:00
|
|
|
}
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
if err = p.db.GetContext(ctx, &info, p.sqlSelectUserInfo, username, username, username, username); err != nil {
|
2021-11-25 08:56:58 +07:00
|
|
|
return models.UserInfo{}, fmt.Errorf("error selecting user info for user '%s': %w", username, err)
|
2021-11-23 16:45:38 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
return info, nil
|
|
|
|
default:
|
2021-11-25 08:56:58 +07:00
|
|
|
return models.UserInfo{}, fmt.Errorf("error selecting user info for user '%s': %w", username, err)
|
2021-11-23 16:45:38 +07:00
|
|
|
}
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// SaveIdentityVerification save an identity verification record to the database.
|
|
|
|
func (p *SQLProvider) SaveIdentityVerification(ctx context.Context, verification models.IdentityVerification) (err error) {
|
2021-11-30 13:58:21 +07:00
|
|
|
if _, err = p.db.ExecContext(ctx, p.sqlInsertIdentityVerification,
|
2021-12-03 07:04:11 +07:00
|
|
|
verification.JTI, verification.IssuedAt, verification.IssuedIP, verification.ExpiresAt,
|
2021-11-30 13:58:21 +07:00
|
|
|
verification.Username, verification.Action); err != nil {
|
2021-11-25 08:56:58 +07:00
|
|
|
return fmt.Errorf("error inserting identity verification: %w", err)
|
|
|
|
}
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
return nil
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-12-03 07:04:11 +07:00
|
|
|
// ConsumeIdentityVerification marks an identity verification record in the database as consumed.
|
|
|
|
func (p *SQLProvider) ConsumeIdentityVerification(ctx context.Context, jti string, ip models.NullIP) (err error) {
|
|
|
|
if _, err = p.db.ExecContext(ctx, p.sqlConsumeIdentityVerification, ip, jti); err != nil {
|
2021-11-25 08:56:58 +07:00
|
|
|
return fmt.Errorf("error updating identity verification: %w", err)
|
|
|
|
}
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
return nil
|
2021-11-23 16:45:38 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// FindIdentityVerification checks if an identity verification record is in the database and active.
|
2021-11-30 13:58:21 +07:00
|
|
|
func (p *SQLProvider) FindIdentityVerification(ctx context.Context, jti string) (found bool, err error) {
|
2021-12-04 11:34:20 +07:00
|
|
|
verification := models.IdentityVerification{}
|
|
|
|
if err = p.db.GetContext(ctx, &verification, p.sqlSelectIdentityVerification, jti); err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
return false, fmt.Errorf("error selecting identity verification exists: %w", err)
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-12-04 11:34:20 +07:00
|
|
|
switch {
|
|
|
|
case verification.Consumed != nil, verification.ExpiresAt.Before(time.Now()):
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return true, nil
|
|
|
|
}
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
// SaveTOTPConfiguration save a TOTP configuration of a given user in the database.
|
2021-11-23 16:45:38 +07:00
|
|
|
func (p *SQLProvider) SaveTOTPConfiguration(ctx context.Context, config models.TOTPConfiguration) (err error) {
|
2021-11-25 08:56:58 +07:00
|
|
|
if config.Secret, err = p.encrypt(config.Secret); err != nil {
|
|
|
|
return fmt.Errorf("error encrypting the TOTP configuration secret: %v", err)
|
|
|
|
}
|
2019-11-16 17:38:21 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
if _, err = p.db.ExecContext(ctx, p.sqlUpsertTOTPConfig,
|
2021-12-01 19:11:29 +07:00
|
|
|
config.Username, config.Issuer, config.Algorithm, config.Digits, config.Period, config.Secret); err != nil {
|
2021-11-25 08:56:58 +07:00
|
|
|
return fmt.Errorf("error upserting TOTP configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
// DeleteTOTPConfiguration delete a TOTP configuration from the database given a username.
|
2021-11-23 16:45:38 +07:00
|
|
|
func (p *SQLProvider) DeleteTOTPConfiguration(ctx context.Context, username string) (err error) {
|
2021-11-25 08:56:58 +07:00
|
|
|
if _, err = p.db.ExecContext(ctx, p.sqlDeleteTOTPConfig, username); err != nil {
|
|
|
|
return fmt.Errorf("error deleting TOTP configuration: %w", err)
|
|
|
|
}
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
return nil
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
// LoadTOTPConfiguration load a TOTP configuration given a username from the database.
|
2021-11-23 16:45:38 +07:00
|
|
|
func (p *SQLProvider) LoadTOTPConfiguration(ctx context.Context, username string) (config *models.TOTPConfiguration, err error) {
|
|
|
|
config = &models.TOTPConfiguration{}
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
if err = p.db.QueryRowxContext(ctx, p.sqlSelectTOTPConfig, username).StructScan(config); err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-12-01 19:11:29 +07:00
|
|
|
return nil, ErrNoTOTPConfiguration
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
return nil, fmt.Errorf("error selecting TOTP configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Secret, err = p.decrypt(config.Secret); err != nil {
|
|
|
|
return nil, fmt.Errorf("error decrypting the TOTP secret: %v", err)
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
return config, nil
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
// LoadTOTPConfigurations load a set of TOTP configurations.
|
|
|
|
func (p *SQLProvider) LoadTOTPConfigurations(ctx context.Context, limit, page int) (configs []models.TOTPConfiguration, err error) {
|
|
|
|
rows, err := p.db.QueryxContext(ctx, p.sqlSelectTOTPConfigs, limit, limit*page)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return configs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("error selecting TOTP configurations: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
p.log.Errorf(logFmtErrClosingConn, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
configs = make([]models.TOTPConfiguration, 0, limit)
|
|
|
|
|
|
|
|
var config models.TOTPConfiguration
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
if err = rows.StructScan(&config); err != nil {
|
|
|
|
return nil, fmt.Errorf("error scanning TOTP configuration to struct: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Secret, err = p.decrypt(config.Secret); err != nil {
|
|
|
|
return nil, fmt.Errorf("error decrypting the TOTP secret: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configs = append(configs, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
return configs, nil
|
|
|
|
}
|
|
|
|
|
2021-12-03 07:04:11 +07:00
|
|
|
func (p *SQLProvider) updateTOTPConfigurationSecret(ctx context.Context, config models.TOTPConfiguration) (err error) {
|
2021-11-25 08:56:58 +07:00
|
|
|
switch config.ID {
|
|
|
|
case 0:
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpdateTOTPConfigSecretByUsername, config.Secret, config.Username)
|
|
|
|
default:
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpdateTOTPConfigSecret, config.Secret, config.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error updating TOTP configuration secret: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
// SaveU2FDevice saves a registered U2F device.
|
|
|
|
func (p *SQLProvider) SaveU2FDevice(ctx context.Context, device models.U2FDevice) (err error) {
|
2021-12-03 07:04:11 +07:00
|
|
|
if device.PublicKey, err = p.encrypt(device.PublicKey); err != nil {
|
|
|
|
return fmt.Errorf("error encrypting the U2F device public key: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-12-02 11:16:45 +07:00
|
|
|
if _, err = p.db.ExecContext(ctx, p.sqlUpsertU2FDevice, device.Username, device.Description, device.KeyHandle, device.PublicKey); err != nil {
|
|
|
|
return fmt.Errorf("error upserting U2F device: %v", err)
|
2021-11-25 08:56:58 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
return nil
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07: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) {
|
2021-12-03 07:04:11 +07:00
|
|
|
device = &models.U2FDevice{}
|
2019-11-17 08:05:46 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
if err = p.db.GetContext(ctx, device, p.sqlSelectU2FDevice, username); err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-11-23 16:45:38 +07:00
|
|
|
return nil, ErrNoU2FDeviceHandle
|
|
|
|
}
|
2019-11-17 08:05:46 +07:00
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
return nil, fmt.Errorf("error selecting U2F device: %w", err)
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
2019-11-17 02:50:58 +07:00
|
|
|
|
2021-12-03 07:04:11 +07:00
|
|
|
if device.PublicKey, err = p.decrypt(device.PublicKey); err != nil {
|
|
|
|
return nil, fmt.Errorf("error decrypting the U2F device public key: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
return device, nil
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-12-03 07:04:11 +07:00
|
|
|
// LoadU2FDevices loads U2F device registrations.
|
|
|
|
func (p *SQLProvider) LoadU2FDevices(ctx context.Context, limit, page int) (devices []models.U2FDevice, err error) {
|
|
|
|
rows, err := p.db.QueryxContext(ctx, p.sqlSelectU2FDevices, limit, limit*page)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return devices, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("error selecting U2F devices: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
p.log.Errorf(logFmtErrClosingConn, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
devices = make([]models.U2FDevice, 0, limit)
|
|
|
|
|
|
|
|
var device models.U2FDevice
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
if err = rows.StructScan(&device); err != nil {
|
|
|
|
return nil, fmt.Errorf("error scanning U2F device to struct: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if device.PublicKey, err = p.decrypt(device.PublicKey); err != nil {
|
|
|
|
return nil, fmt.Errorf("error decrypting the U2F device public key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
devices = append(devices, device)
|
|
|
|
}
|
|
|
|
|
|
|
|
return devices, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SQLProvider) updateU2FDevicePublicKey(ctx context.Context, device models.U2FDevice) (err error) {
|
|
|
|
switch device.ID {
|
|
|
|
case 0:
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpdateU2FDevicePublicKeyByUsername, device.PublicKey, device.Username)
|
|
|
|
default:
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpdateU2FDevicePublicKey, device.PublicKey, device.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error updating U2F public key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
// SavePreferredDuoDevice saves a Duo device.
|
|
|
|
func (p *SQLProvider) SavePreferredDuoDevice(ctx context.Context, device models.DuoDevice) (err error) {
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlUpsertDuoDevice, device.Username, device.Device, device.Method)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePreferredDuoDevice deletes a Duo device of a given user.
|
|
|
|
func (p *SQLProvider) DeletePreferredDuoDevice(ctx context.Context, username string) (err error) {
|
|
|
|
_, err = p.db.ExecContext(ctx, p.sqlDeleteDuoDevice, username)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadPreferredDuoDevice loads a Duo device of a given user.
|
|
|
|
func (p *SQLProvider) LoadPreferredDuoDevice(ctx context.Context, username string) (device *models.DuoDevice, err error) {
|
|
|
|
device = &models.DuoDevice{}
|
|
|
|
|
|
|
|
if err := p.db.QueryRowxContext(ctx, p.sqlSelectDuoDevice, username).StructScan(device); err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, ErrNoDuoDevice
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return device, nil
|
|
|
|
}
|
|
|
|
|
2019-11-16 17:38:21 +07:00
|
|
|
// AppendAuthenticationLog append a mark to the authentication log.
|
2021-11-23 16:45:38 +07:00
|
|
|
func (p *SQLProvider) AppendAuthenticationLog(ctx context.Context, attempt models.AuthenticationAttempt) (err error) {
|
2021-11-29 10:09:14 +07:00
|
|
|
if _, err = p.db.ExecContext(ctx, p.sqlInsertAuthenticationAttempt,
|
|
|
|
attempt.Time, attempt.Successful, attempt.Banned, attempt.Username,
|
|
|
|
attempt.Type, attempt.RemoteIP, attempt.RequestURI, attempt.RequestMethod); err != nil {
|
|
|
|
return fmt.Errorf("error inserting authentication attempt: %w", err)
|
2021-11-25 08:56:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07: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 17:38:21 +07:00
|
|
|
if err != nil {
|
2021-11-25 08:56:58 +07:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, ErrNoAuthenticationLogs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("error selecting authentication logs: %w", err)
|
2019-11-16 17:38:21 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
defer func() {
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
p.log.Errorf(logFmtErrClosingConn, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
var attempt models.AuthenticationAttempt
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
attempts = make([]models.AuthenticationAttempt, 0, limit)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-11-16 17:38:21 +07:00
|
|
|
for rows.Next() {
|
2021-11-25 08:56:58 +07:00
|
|
|
if err = rows.StructScan(&attempt); err != nil {
|
2019-11-16 17:38:21 +07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-11-16 17:38:21 +07:00
|
|
|
attempts = append(attempts, attempt)
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-11-16 17:38:21 +07:00
|
|
|
return attempts, nil
|
|
|
|
}
|