Simplified the database layer by removing custom interface wrappers and using standard library *sql.DB and *sql.Tx types directly. Changes: - Removed DBConnection and DBTransaction interfaces from database.go - Removed NewDBConnection() wrapper function - Updated TokenGenerator to use *sql.DB instead of DBConnection - Updated all validation and revocation methods to accept *sql.Tx - Updated TableManager to work with *sql.DB directly - Updated all tests to use db.Begin() instead of custom wrappers - Fixed GeneratorConfig.DB field (was DBConn) - Updated documentation in doc.go with correct API usage Benefits: - Simpler API with fewer abstractions - Works directly with database/sql standard library - Compatible with GORM (via gormDB.DB()) and Bun (share same *sql.DB) - Easier to understand and maintain - No unnecessary wrapper layers Breaking changes: - GeneratorConfig.DBConn renamed to GeneratorConfig.DB - Removed NewDBConnection() function - pass *sql.DB directly - ValidateAccess/ValidateRefresh now accept *sql.Tx instead of DBTransaction - Token.Revoke/CheckNotRevoked now accept *sql.Tx instead of DBTransaction 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package hwsauth
|
|
|
|
import (
|
|
"context"
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"net/http"
|
|
"slices"
|
|
"time"
|
|
)
|
|
|
|
func (auth *Authenticator[T]) Authenticate() hws.Middleware {
|
|
return auth.server.NewMiddleware(auth.authenticate())
|
|
}
|
|
|
|
func (auth *Authenticator[T]) authenticate() hws.MiddlewareFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) (*http.Request, *hws.HWSError) {
|
|
if slices.Contains(auth.ignoredPaths, r.URL.Path) {
|
|
return r, nil
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
// Start the transaction
|
|
tx, err := auth.conn.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return nil, &hws.HWSError{Message: "Unable to start transaction", StatusCode: http.StatusServiceUnavailable, Error: err}
|
|
}
|
|
model, err := auth.getAuthenticatedUser(tx, w, r)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
auth.logger.Debug().
|
|
Str("remote_addr", r.RemoteAddr).
|
|
Err(err).
|
|
Msg("Failed to authenticate user")
|
|
return r, nil
|
|
}
|
|
tx.Commit()
|
|
authContext := setAuthenticatedModel(r.Context(), model)
|
|
newReq := r.WithContext(authContext)
|
|
return newReq, nil
|
|
}
|
|
}
|