mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
8aade7f40e
* added regulation validator * made regulations find_time and ban_time values duration notation strings * added DefaultRegulationConfiguration for the validator * made session expiration and inactivity values duration notation strings * TOTP period does not need to be converted because adjustment should be discouraged * moved TOTP defaults to DefaultTOTPConfiguration and removed the consts * arranged the root config validator in configuration file order * adjusted tests for the changes * moved duration notation docs to root of configuration * added references to duration notation where applicable * project wide gofmt and goimports: * run gofmt * run goimports -local github.com/authelia/authelia -w on all files * Make jwt_secret error uniform and add tests * now at 100% coverage for internal/configuration/validator/configuration.go
76 lines
3.2 KiB
Go
76 lines
3.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
_ "github.com/go-sql-driver/mysql" // Load the MySQL Driver used in the connection string.
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
"github.com/authelia/authelia/internal/logging"
|
|
)
|
|
|
|
// MySQLProvider is a MySQL provider
|
|
type MySQLProvider struct {
|
|
SQLProvider
|
|
}
|
|
|
|
// NewMySQLProvider a MySQL provider
|
|
func NewMySQLProvider(configuration schema.MySQLStorageConfiguration) *MySQLProvider {
|
|
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)
|
|
}
|
|
|
|
provider := MySQLProvider{
|
|
SQLProvider{
|
|
sqlCreateUserPreferencesTable: SQLCreateUserPreferencesTable,
|
|
sqlCreateIdentityVerificationTokensTable: SQLCreateIdentityVerificationTokensTable,
|
|
sqlCreateTOTPSecretsTable: SQLCreateTOTPSecretsTable,
|
|
sqlCreateU2FDeviceHandlesTable: SQLCreateU2FDeviceHandlesTable,
|
|
sqlCreateAuthenticationLogsTable: SQLCreateAuthenticationLogsTable,
|
|
|
|
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),
|
|
sqlDeleteTOTPSecret: fmt.Sprintf("DELETE FROM %s WHERE username=?", totpSecretsTableName),
|
|
|
|
sqlGetU2FDeviceHandleByUsername: fmt.Sprintf("SELECT keyHandle, publicKey FROM %s WHERE username=?", u2fDeviceHandlesTableName),
|
|
sqlUpsertU2FDeviceHandle: fmt.Sprintf("REPLACE INTO %s (username, keyHandle, publicKey) VALUES (?, ?, ?)", u2fDeviceHandlesTableName),
|
|
|
|
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),
|
|
},
|
|
}
|
|
if err := provider.initialize(db); err != nil {
|
|
logging.Logger().Fatalf("Unable to initialize SQL database: %v", err)
|
|
}
|
|
return &provider
|
|
}
|