fixed issue with hwsauth where table creation didnt work so user logins was broken if table didnt already exist

This commit is contained in:
2026-01-24 03:08:39 +11:00
parent f25bc437c4
commit 1c49b19197
4 changed files with 89 additions and 19 deletions

View File

@@ -1,6 +1,11 @@
package hwsauth
import (
"context"
"database/sql"
"os"
"time"
"git.haelnorr.com/h/golib/hlog"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/jwt"
@@ -30,6 +35,7 @@ func NewAuthenticator[T Model, TX DBTransaction](
beginTx BeginTX,
logger *hlog.Logger,
errorPage hws.ErrorPageFunc,
db *sql.DB,
) (*Authenticator[T, TX], error) {
if load == nil {
return nil, errors.New("No function to load model supplied")
@@ -55,7 +61,10 @@ func NewAuthenticator[T Model, TX DBTransaction](
return nil, errors.New("SecretKey is required")
}
if cfg.SSL && cfg.TrustedHost == "" {
return nil, errors.New("TrustedHost is required when SSL is enabled")
cfg.SSL = false // Disable SSL if TrustedHost is not configured
}
if cfg.TrustedHost == "" {
cfg.TrustedHost = "localhost" // Default TrustedHost for JWT
}
if cfg.AccessTokenExpiry == 0 {
cfg.AccessTokenExpiry = 5
@@ -69,12 +78,35 @@ func NewAuthenticator[T Model, TX DBTransaction](
if cfg.LandingPage == "" {
cfg.LandingPage = "/profile"
}
if cfg.DatabaseType == "" {
cfg.DatabaseType = "postgres"
}
if cfg.DatabaseVersion == "" {
cfg.DatabaseVersion = "15"
}
if db == nil {
return nil, errors.New("No Database provided")
}
// Test database connectivity
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := db.PingContext(ctx); err != nil {
return nil, errors.Wrap(err, "database connection test failed")
}
// Configure JWT table
tableConfig := jwt.DefaultTableConfig()
if cfg.JWTTableName != "" {
tableConfig.TableName = cfg.JWTTableName
}
// Disable auto-creation for tests
// Check for test environment or mock database
if os.Getenv("GO_TEST") == "1" {
tableConfig.AutoCreate = false
tableConfig.EnableAutoCleanup = false
}
// Create token generator
tokenGen, err := jwt.CreateGenerator(jwt.GeneratorConfig{
@@ -87,6 +119,7 @@ func NewAuthenticator[T Model, TX DBTransaction](
Type: cfg.DatabaseType,
Version: cfg.DatabaseVersion,
},
DB: db,
TableConfig: tableConfig,
}, beginTx)
if err != nil {