2019-11-16 10:38:21 +00:00
package storage
import (
"database/sql"
"fmt"
2019-12-24 02:14:52 +00:00
"github.com/authelia/authelia/internal/configuration/schema"
"github.com/authelia/authelia/internal/logging"
2019-11-16 10:38:21 +00:00
_ "github.com/go-sql-driver/mysql" // Load the MySQL Driver used in the connection string.
)
// MySQLProvider is a MySQL provider
type MySQLProvider struct {
SQLProvider
}
2019-11-16 19:50:58 +00:00
// NewMySQLProvider a MySQL provider
func NewMySQLProvider ( configuration schema . MySQLStorageConfiguration ) * MySQLProvider {
2019-11-16 10:38:21 +00:00
connectionString := configuration . Username
if configuration . Password != "" {
connectionString += fmt . Sprintf ( ":%s" , configuration . Password )
}
if connectionString != "" {
connectionString += "@"
}
address := configuration . Host
if configuration . Port > 0 {
address += fmt . Sprintf ( ":%d" , configuration . Port )
}
connectionString += fmt . Sprintf ( "tcp(%s)" , address )
if configuration . Database != "" {
connectionString += fmt . Sprintf ( "/%s" , configuration . Database )
}
db , err := sql . Open ( "mysql" , connectionString )
if err != nil {
logging . Logger ( ) . Fatalf ( "Unable to connect to SQL database: %v" , err )
}
2019-11-16 19:50:58 +00:00
provider := MySQLProvider {
SQLProvider {
2020-03-04 23:25:52 +00:00
sqlCreateUserPreferencesTable : SQLCreateUserPreferencesTable ,
sqlCreateIdentityVerificationTokensTable : SQLCreateIdentityVerificationTokensTable ,
sqlCreateTOTPSecretsTable : SQLCreateTOTPSecretsTable ,
sqlCreateU2FDeviceHandlesTable : SQLCreateU2FDeviceHandlesTable ,
sqlCreateAuthenticationLogsTable : SQLCreateAuthenticationLogsTable ,
2019-11-16 19:50:58 +00: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-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 ) ,
} ,
}
2019-11-16 10:38:21 +00:00
if err := provider . initialize ( db ) ; err != nil {
logging . Logger ( ) . Fatalf ( "Unable to initialize SQL database: %v" , err )
}
return & provider
}