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"
|
|
|
|
|
2019-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/models"
|
2019-11-16 10:38:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SQLProvider is a storage provider persisting data in a SQL database.
|
|
|
|
type SQLProvider struct {
|
|
|
|
db *sql.DB
|
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
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SQLProvider) initialize(db *sql.DB) error {
|
|
|
|
p.db = db
|
|
|
|
|
2019-12-19 03:56:16 +00:00
|
|
|
_, err := db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (username VARCHAR(100) PRIMARY KEY, second_factor_method VARCHAR(11))", preferencesTableName))
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (token VARCHAR(512))", identityVerificationTokensTableName))
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (username VARCHAR(100) PRIMARY KEY, secret VARCHAR(64))", totpSecretsTableName))
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-17 01:05:46 +00:00
|
|
|
// keyHandle and publicKey are stored in base64 format
|
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (username VARCHAR(100) PRIMARY KEY, keyHandle TEXT, publicKey TEXT)", u2fDeviceHandlesTableName))
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (username VARCHAR(100), successful BOOL, time INTEGER)", authenticationLogsTableName))
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE INDEX IF NOT EXISTS time ON %s (time);", authenticationLogsTableName))
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE INDEX IF NOT EXISTS username ON %s (username);", authenticationLogsTableName))
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-05 23:03:16 +00:00
|
|
|
// LoadPreferred2FAMethod load the preferred method for 2FA from sqlite db.
|
|
|
|
func (p *SQLProvider) LoadPreferred2FAMethod(username string) (string, error) {
|
2019-11-16 19:50:58 +00:00
|
|
|
rows, err := p.db.Query(p.sqlGetPreferencesByUsername, username)
|
2019-11-16 10:38:21 +00:00
|
|
|
defer rows.Close()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if rows.Next() {
|
|
|
|
var method string
|
|
|
|
err = rows.Scan(&method)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return method, nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2020-01-05 23:03:16 +00:00
|
|
|
// SavePreferred2FAMethod save the preferred method for 2FA in sqlite db.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindIdentityVerificationToken look for an identity verification token in DB.
|
|
|
|
func (p *SQLProvider) FindIdentityVerificationToken(token string) (bool, error) {
|
2019-11-16 19:50:58 +00:00
|
|
|
var found bool
|
|
|
|
err := p.db.QueryRow(p.sqlTestIdentityVerificationTokenExistence, token).Scan(&found)
|
2019-11-16 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-11-16 19:50:58 +00:00
|
|
|
return found, nil
|
2019-11-16 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveIdentityVerificationToken save an identity verification token in DB.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveIdentityVerificationToken remove an identity verification token from the DB.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveTOTPSecret save a TOTP secret of a given user.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTOTPSecret load a TOTP secret given a username.
|
|
|
|
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
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return secret, nil
|
|
|
|
}
|
|
|
|
|
2019-12-07 17:14:26 +00:00
|
|
|
// DeleteTOTPSecret delete a TOTP secret given a username.
|
|
|
|
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))
|
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
|
|
|
}
|
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) {
|
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)
|
|
|
|
for rows.Next() {
|
|
|
|
attempt := models.AuthenticationAttempt{
|
|
|
|
Username: username,
|
|
|
|
}
|
|
|
|
var t int64
|
|
|
|
err = rows.Scan(&attempt.Successful, &t)
|
|
|
|
attempt.Time = time.Unix(t, 0)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
attempts = append(attempts, attempt)
|
|
|
|
}
|
|
|
|
return attempts, nil
|
|
|
|
}
|