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>
JWT Package
JWT (JSON Web Token) generation and validation with database-backed token revocation support.
Features
- 🔐 Access and refresh token generation
- ✅ Token validation with expiration checking
- 🚫 Token revocation via database blacklist
- 🗄️ Multi-database support (PostgreSQL, MySQL, SQLite, MariaDB)
- 🔧 Compatible with database/sql, GORM, and Bun
- 🤖 Automatic table creation and management
- 🧹 Database-native automatic cleanup
- 🔄 Token freshness tracking
- 💾 "Remember me" functionality
Installation
go get git.haelnorr.com/h/golib/jwt
Quick Start
package main
import (
"database/sql"
"git.haelnorr.com/h/golib/jwt"
_ "github.com/lib/pq"
)
func main() {
// Open database
db, _ := sql.Open("postgres", "postgres://user:pass@localhost/db")
defer db.Close()
// Wrap database connection
dbConn := jwt.NewDBConnection(db)
// Create token generator
gen, err := jwt.CreateGenerator(jwt.GeneratorConfig{
AccessExpireAfter: 15, // 15 minutes
RefreshExpireAfter: 1440, // 24 hours
FreshExpireAfter: 5, // 5 minutes
TrustedHost: "example.com",
SecretKey: "your-secret-key",
DBConn: dbConn,
DBType: jwt.DatabaseType{
Type: jwt.DatabasePostgreSQL,
Version: "15",
},
TableConfig: jwt.DefaultTableConfig(),
})
if err != nil {
panic(err)
}
// Generate tokens
accessToken, _, _ := gen.NewAccess(42, true, false)
refreshToken, _, _ := gen.NewRefresh(42, false)
// Validate token
tx, _ := dbConn.BeginTx(context.Background(), nil)
token, _ := gen.ValidateAccess(tx, accessToken)
// Revoke token
token.Revoke(tx)
tx.Commit()
}
Documentation
Comprehensive documentation is available in the Wiki.
Key Topics
Supported Databases
- PostgreSQL
- MySQL
- MariaDB
- SQLite
License
See LICENSE file in the repository root.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.