2019-04-24 21:52:08 +00:00
package storage
import (
"database/sql"
2019-11-16 19:50:58 +00:00
"fmt"
2019-04-24 21:52:08 +00:00
2021-06-22 00:45:33 +00:00
_ "modernc.org/sqlite" // Load the SQLite Driver used in the connection string.
2019-04-24 21:52:08 +00:00
)
2020-05-02 05:06:39 +00:00
// SQLiteProvider is a SQLite3 provider.
2019-04-24 21:52:08 +00:00
type SQLiteProvider struct {
2019-11-16 10:38:21 +00:00
SQLProvider
2019-04-24 21:52:08 +00:00
}
2020-05-02 05:06:39 +00:00
// NewSQLiteProvider constructs a SQLite provider.
2019-04-24 21:52:08 +00:00
func NewSQLiteProvider ( path string ) * SQLiteProvider {
2019-11-16 19:50:58 +00:00
provider := SQLiteProvider {
SQLProvider {
2020-07-16 05:56:08 +00:00
name : "sqlite" ,
sqlUpgradesCreateTableStatements : sqlUpgradeCreateTableStatements ,
sqlUpgradesCreateTableIndexesStatements : sqlUpgradesCreateTableIndexesStatements ,
2020-03-04 23:25:52 +00:00
2020-07-16 05:56:08 +00:00
sqlGetPreferencesByUsername : fmt . Sprintf ( "SELECT second_factor_method FROM %s WHERE username=?" , userPreferencesTableName ) ,
sqlUpsertSecondFactorPreference : fmt . Sprintf ( "REPLACE INTO %s (username, second_factor_method) VALUES (?, ?)" , userPreferencesTableName ) ,
2019-11-16 19:50:58 +00:00
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-07 17:14:26 +00:00
sqlDeleteTOTPSecret : fmt . Sprintf ( "DELETE FROM %s WHERE username=?" , totpSecretsTableName ) ,
2019-11-16 19:50:58 +00:00
2019-11-17 01:05:46 +00: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-16 19:50:58 +00: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 ) ,
2020-07-16 05:56:08 +00:00
sqlGetExistingTables : "SELECT name FROM sqlite_master WHERE type='table'" ,
sqlConfigSetValue : fmt . Sprintf ( "REPLACE INTO %s (category, key_name, value) VALUES (?, ?, ?)" , configTableName ) ,
sqlConfigGetValue : fmt . Sprintf ( "SELECT value FROM %s WHERE category=? AND key_name=?" , configTableName ) ,
2019-11-16 19:50:58 +00:00
} ,
}
2020-07-16 05:56:08 +00:00
2021-06-22 00:45:33 +00:00
db , err := sql . Open ( "sqlite" , path )
2020-07-16 05:56:08 +00:00
if err != nil {
provider . log . Fatalf ( "Unable to create SQL database %s: %s" , path , err )
}
2019-11-16 10:38:21 +00:00
if err := provider . initialize ( db ) ; err != nil {
2020-07-16 05:56:08 +00:00
provider . log . Fatalf ( "Unable to initialize SQL database %s: %s" , path , err )
2019-04-24 21:52:08 +00:00
}
2020-05-05 19:35:32 +00:00
2019-11-16 10:38:21 +00:00
return & provider
2019-04-24 21:52:08 +00:00
}