2019-04-25 04:52:08 +07:00
package storage
import (
"database/sql"
2019-11-17 02:50:58 +07:00
"fmt"
2019-04-25 04:52:08 +07:00
_ "github.com/mattn/go-sqlite3" // Load the SQLite Driver used in the connection string.
2020-04-05 19:37:21 +07:00
"github.com/authelia/authelia/internal/logging"
2019-04-25 04:52:08 +07:00
)
2020-05-02 12:06:39 +07:00
// SQLiteProvider is a SQLite3 provider.
2019-04-25 04:52:08 +07:00
type SQLiteProvider struct {
2019-11-16 17:38:21 +07:00
SQLProvider
2019-04-25 04:52:08 +07:00
}
2020-05-02 12:06:39 +07:00
// NewSQLiteProvider constructs a SQLite provider.
2019-04-25 04:52:08 +07:00
func NewSQLiteProvider ( path string ) * SQLiteProvider {
db , err := sql . Open ( "sqlite3" , path )
if err != nil {
2019-11-16 17:38:21 +07:00
logging . Logger ( ) . Fatalf ( "Unable to create SQLite database %s: %s" , path , err )
2019-04-25 04:52:08 +07:00
}
2019-11-17 02:50:58 +07:00
provider := SQLiteProvider {
SQLProvider {
2020-03-05 06:25:52 +07:00
sqlCreateUserPreferencesTable : SQLCreateUserPreferencesTable ,
sqlCreateIdentityVerificationTokensTable : SQLCreateIdentityVerificationTokensTable ,
sqlCreateTOTPSecretsTable : SQLCreateTOTPSecretsTable ,
sqlCreateU2FDeviceHandlesTable : SQLCreateU2FDeviceHandlesTable ,
sqlCreateAuthenticationLogsTable : fmt . Sprintf ( "CREATE TABLE IF NOT EXISTS %s (username VARCHAR(100), successful BOOL, time INTEGER)" , authenticationLogsTableName ) ,
sqlCreateAuthenticationLogsUserTimeIndex : fmt . Sprintf ( "CREATE INDEX IF NOT EXISTS usr_time_idx ON %s (username, time)" , authenticationLogsTableName ) ,
2019-11-17 02:50:58 +07:00
sqlGetPreferencesByUsername : fmt . Sprintf ( "SELECT second_factor_method FROM %s WHERE username=?" , preferencesTableName ) ,
sqlUpsertSecondFactorPreference : fmt . Sprintf ( "REPLACE INTO %s (username, second_factor_method) VALUES (?, ?)" , preferencesTableName ) ,
sqlTestIdentityVerificationTokenExistence : fmt . Sprintf ( "SELECT EXISTS (SELECT * FROM %s WHERE token=?)" , identityVerificationTokensTableName ) ,
sqlInsertIdentityVerificationToken : fmt . Sprintf ( "INSERT INTO %s (token) VALUES (?)" , identityVerificationTokensTableName ) ,
sqlDeleteIdentityVerificationToken : fmt . Sprintf ( "DELETE FROM %s WHERE token=?" , identityVerificationTokensTableName ) ,
sqlGetTOTPSecretByUsername : fmt . Sprintf ( "SELECT secret FROM %s WHERE username=?" , totpSecretsTableName ) ,
sqlUpsertTOTPSecret : fmt . Sprintf ( "REPLACE INTO %s (username, secret) VALUES (?, ?)" , totpSecretsTableName ) ,
2019-12-08 00:14:26 +07:00
sqlDeleteTOTPSecret : fmt . Sprintf ( "DELETE FROM %s WHERE username=?" , totpSecretsTableName ) ,
2019-11-17 02:50:58 +07:00
2019-11-17 08:05:46 +07:00
sqlGetU2FDeviceHandleByUsername : fmt . Sprintf ( "SELECT keyHandle, publicKey FROM %s WHERE username=?" , u2fDeviceHandlesTableName ) ,
sqlUpsertU2FDeviceHandle : fmt . Sprintf ( "REPLACE INTO %s (username, keyHandle, publicKey) VALUES (?, ?, ?)" , u2fDeviceHandlesTableName ) ,
2019-11-17 02:50:58 +07:00
sqlInsertAuthenticationLog : fmt . Sprintf ( "INSERT INTO %s (username, successful, time) VALUES (?, ?, ?)" , authenticationLogsTableName ) ,
sqlGetLatestAuthenticationLogs : fmt . Sprintf ( "SELECT successful, time FROM %s WHERE time>? AND username=? ORDER BY time DESC" , authenticationLogsTableName ) ,
} ,
}
2019-11-16 17:38:21 +07:00
if err := provider . initialize ( db ) ; err != nil {
logging . Logger ( ) . Fatalf ( "Unable to initialize SQLite database %s: %s" , path , err )
2019-04-25 04:52:08 +07:00
}
2020-05-06 02:35:32 +07:00
2019-11-16 17:38:21 +07:00
return & provider
2019-04-25 04:52:08 +07:00
}