2019-11-16 10:38:21 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-11-16 19:50:58 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2019-11-16 10:38:21 +00:00
|
|
|
"time"
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
|
|
|
"github.com/authelia/authelia/v4/internal/models"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-11-16 10:38:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SQLProvider is a storage provider persisting data in a SQL database.
|
|
|
|
type SQLProvider struct {
|
2020-07-16 05:56:08 +00:00
|
|
|
db *sql.DB
|
|
|
|
log *logrus.Logger
|
|
|
|
name string
|
2019-11-16 19:50:58 +00:00
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
sqlUpgradesCreateTableStatements map[SchemaVersion]map[string]string
|
|
|
|
sqlUpgradesCreateTableIndexesStatements map[SchemaVersion][]string
|
2020-03-04 23:25:52 +00:00
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
sqlGetPreferencesByUsername string
|
|
|
|
sqlUpsertSecondFactorPreference string
|
|
|
|
|
|
|
|
sqlTestIdentityVerificationTokenExistence string
|
|
|
|
sqlInsertIdentityVerificationToken string
|
|
|
|
sqlDeleteIdentityVerificationToken string
|
|
|
|
|
|
|
|
sqlGetTOTPSecretByUsername string
|
|
|
|
sqlUpsertTOTPSecret string
|
2019-12-07 17:14:26 +00:00
|
|
|
sqlDeleteTOTPSecret string
|
2019-11-16 19:50:58 +00:00
|
|
|
|
|
|
|
sqlGetU2FDeviceHandleByUsername string
|
|
|
|
sqlUpsertU2FDeviceHandle string
|
|
|
|
|
|
|
|
sqlInsertAuthenticationLog string
|
|
|
|
sqlGetLatestAuthenticationLogs string
|
2020-07-16 05:56:08 +00:00
|
|
|
|
|
|
|
sqlGetExistingTables string
|
|
|
|
|
|
|
|
sqlConfigSetValue string
|
|
|
|
sqlConfigGetValue string
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SQLProvider) initialize(db *sql.DB) error {
|
|
|
|
p.db = db
|
2020-07-16 05:56:08 +00:00
|
|
|
p.log = logging.Logger()
|
2019-11-16 10:38:21 +00:00
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
return p.upgrade()
|
|
|
|
}
|
2019-11-16 10:38:21 +00:00
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
func (p *SQLProvider) getSchemaBasicDetails() (version SchemaVersion, tables []string, err error) {
|
|
|
|
rows, err := p.db.Query(p.sqlGetExistingTables)
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
2020-07-16 05:56:08 +00:00
|
|
|
return version, tables, err
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var table string
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&table)
|
|
|
|
if err != nil {
|
|
|
|
return version, tables, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tables = append(tables, table)
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
if utils.IsStringInSlice(configTableName, tables) {
|
|
|
|
rows, err := p.db.Query(p.sqlConfigGetValue, "schema", "version")
|
|
|
|
if err != nil {
|
|
|
|
return version, tables, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(&version)
|
|
|
|
if err != nil {
|
|
|
|
return version, tables, err
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
return version, tables, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SQLProvider) upgrade() error {
|
|
|
|
p.log.Debug("Storage schema is being checked to verify it is up to date")
|
|
|
|
|
|
|
|
version, tables, err := p.getSchemaBasicDetails()
|
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
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
if version < storageSchemaCurrentVersion {
|
|
|
|
p.log.Debugf("Storage schema is v%d, latest is v%d", version, storageSchemaCurrentVersion)
|
|
|
|
|
|
|
|
tx, err := p.db.Begin()
|
2020-03-04 23:25:52 +00:00
|
|
|
if err != nil {
|
2020-07-16 05:56:08 +00:00
|
|
|
return err
|
2020-03-04 23:25:52 +00:00
|
|
|
}
|
2020-07-16 05:56:08 +00:00
|
|
|
|
|
|
|
switch version {
|
|
|
|
case 0:
|
|
|
|
err := p.upgradeSchemaToVersion001(tx, tables)
|
|
|
|
if err != nil {
|
|
|
|
return p.handleUpgradeFailure(tx, 1, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
err := tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.log.Infof("Storage schema upgrade to v%d completed", storageSchemaCurrentVersion)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p.log.Debug("Storage schema is up to date")
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
func (p *SQLProvider) handleUpgradeFailure(tx *sql.Tx, version SchemaVersion, err error) error {
|
|
|
|
rollbackErr := tx.Rollback()
|
|
|
|
formattedErr := fmt.Errorf("%s%d: %v", storageSchemaUpgradeErrorText, version, err)
|
|
|
|
|
|
|
|
if rollbackErr != nil {
|
|
|
|
return fmt.Errorf("rollback error occurred: %v (inner error %v)", rollbackErr, formattedErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return formattedErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadPreferred2FAMethod load the preferred method for 2FA from the database.
|
2020-01-05 23:03:16 +00:00
|
|
|
func (p *SQLProvider) LoadPreferred2FAMethod(username string) (string, error) {
|
2020-05-05 19:35:32 +00:00
|
|
|
var method string
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
rows, err := p.db.Query(p.sqlGetPreferencesByUsername, username)
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-04-09 01:05:17 +00:00
|
|
|
defer rows.Close()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-04-09 01:05:17 +00:00
|
|
|
if !rows.Next() {
|
|
|
|
return "", nil
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-04-09 01:05:17 +00:00
|
|
|
err = rows.Scan(&method)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-04-09 01:05:17 +00:00
|
|
|
return method, err
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
// SavePreferred2FAMethod save the preferred method for 2FA to the database.
|
2020-01-05 23:03:16 +00:00
|
|
|
func (p *SQLProvider) SavePreferred2FAMethod(username string, method string) error {
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err := p.db.Exec(p.sqlUpsertSecondFactorPreference, username, method)
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
// FindIdentityVerificationToken look for an identity verification token in the database.
|
2019-11-16 10:38:21 +00:00
|
|
|
func (p *SQLProvider) FindIdentityVerificationToken(token string) (bool, error) {
|
2019-11-16 19:50:58 +00:00
|
|
|
var found bool
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
err := p.db.QueryRow(p.sqlTestIdentityVerificationTokenExistence, token).Scan(&found)
|
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
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
// SaveIdentityVerificationToken save an identity verification token in the database.
|
2019-11-16 10:38:21 +00:00
|
|
|
func (p *SQLProvider) SaveIdentityVerificationToken(token string) error {
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err := p.db.Exec(p.sqlInsertIdentityVerificationToken, token)
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
// RemoveIdentityVerificationToken remove an identity verification token from the database.
|
2019-11-16 10:38:21 +00:00
|
|
|
func (p *SQLProvider) RemoveIdentityVerificationToken(token string) error {
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err := p.db.Exec(p.sqlDeleteIdentityVerificationToken, token)
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
// SaveTOTPSecret save a TOTP secret of a given user in the database.
|
2019-11-16 10:38:21 +00:00
|
|
|
func (p *SQLProvider) SaveTOTPSecret(username string, secret string) error {
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err := p.db.Exec(p.sqlUpsertTOTPSecret, username, secret)
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
// LoadTOTPSecret load a TOTP secret given a username from the database.
|
2019-11-16 10:38:21 +00:00
|
|
|
func (p *SQLProvider) LoadTOTPSecret(username string) (string, error) {
|
|
|
|
var secret string
|
2019-11-16 19:50:58 +00:00
|
|
|
if err := p.db.QueryRow(p.sqlGetTOTPSecretByUsername, username).Scan(&secret); err != nil {
|
2019-11-16 10:38:21 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-12-07 11:18:22 +00:00
|
|
|
return "", ErrNoTOTPSecret
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
return secret, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 05:56:08 +00:00
|
|
|
// DeleteTOTPSecret delete a TOTP secret from the database given a username.
|
2019-12-07 17:14:26 +00:00
|
|
|
func (p *SQLProvider) DeleteTOTPSecret(username string) error {
|
|
|
|
_, err := p.db.Exec(p.sqlDeleteTOTPSecret, username)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
// SaveU2FDeviceHandle save a registered U2F device registration blob.
|
2019-11-17 01:05:46 +00:00
|
|
|
func (p *SQLProvider) SaveU2FDeviceHandle(username string, keyHandle []byte, publicKey []byte) error {
|
|
|
|
_, err := p.db.Exec(p.sqlUpsertU2FDeviceHandle,
|
|
|
|
username,
|
|
|
|
base64.StdEncoding.EncodeToString(keyHandle),
|
|
|
|
base64.StdEncoding.EncodeToString(publicKey))
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadU2FDeviceHandle load a U2F device registration blob for a given username.
|
2019-11-17 01:05:46 +00:00
|
|
|
func (p *SQLProvider) LoadU2FDeviceHandle(username string) ([]byte, []byte, error) {
|
|
|
|
var keyHandleBase64, publicKeyBase64 string
|
|
|
|
if err := p.db.QueryRow(p.sqlGetU2FDeviceHandleByUsername, username).Scan(&keyHandleBase64, &publicKeyBase64); err != nil {
|
2019-11-16 10:38:21 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-11-17 01:05:46 +00:00
|
|
|
return nil, nil, ErrNoU2FDeviceHandle
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-17 01:05:46 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyHandle, err := base64.StdEncoding.DecodeString(keyHandleBase64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := base64.StdEncoding.DecodeString(publicKeyBase64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
2019-11-16 19:50:58 +00:00
|
|
|
|
2019-11-17 01:05:46 +00:00
|
|
|
return keyHandle, publicKey, nil
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendAuthenticationLog append a mark to the authentication log.
|
|
|
|
func (p *SQLProvider) AppendAuthenticationLog(attempt models.AuthenticationAttempt) error {
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err := p.db.Exec(p.sqlInsertAuthenticationLog, attempt.Username, attempt.Successful, attempt.Time.Unix())
|
2019-11-16 10:38:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadLatestAuthenticationLogs retrieve the latest marks from the authentication log.
|
|
|
|
func (p *SQLProvider) LoadLatestAuthenticationLogs(username string, fromDate time.Time) ([]models.AuthenticationAttempt, error) {
|
2020-05-05 19:35:32 +00:00
|
|
|
var t int64
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
rows, err := p.db.Query(p.sqlGetLatestAuthenticationLogs, fromDate.Unix(), username)
|
2019-11-16 10:38:21 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
attempts := make([]models.AuthenticationAttempt, 0, 10)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-16 10:38:21 +00:00
|
|
|
for rows.Next() {
|
|
|
|
attempt := models.AuthenticationAttempt{
|
|
|
|
Username: username,
|
|
|
|
}
|
|
|
|
err = rows.Scan(&attempt.Successful, &t)
|
|
|
|
attempt.Time = time.Unix(t, 0)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|