# JWT Package [![Go Reference](https://pkg.go.dev/badge/git.haelnorr.com/h/golib/jwt.svg)](https://pkg.go.dev/git.haelnorr.com/h/golib/jwt) 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 ```bash go get git.haelnorr.com/h/golib/jwt ``` ## Quick Start ```go package main import ( "context" "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() // Create a transaction getter function txGetter := func(ctx context.Context) (jwt.DBTransaction, error) { return db.Begin() } // 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", DB: db, DBType: jwt.DatabaseType{ Type: jwt.DatabasePostgreSQL, Version: "15", }, TableConfig: jwt.DefaultTableConfig(), }, txGetter) if err != nil { panic(err) } // Generate tokens accessToken, _, _ := gen.NewAccess(42, true, false) refreshToken, _, _ := gen.NewRefresh(42, false) // Validate token tx, _ := db.Begin() token, _ := gen.ValidateAccess(tx, accessToken) // Revoke token token.Revoke(tx) tx.Commit() } ``` ## Documentation Comprehensive documentation is available in the [Wiki](https://git.haelnorr.com/h/golib/wiki/JWT). ### Key Topics - [Configuration](https://git.haelnorr.com/h/golib/wiki/JWT#configuration) - [Token Generation](https://git.haelnorr.com/h/golib/wiki/JWT#token-generation) - [Token Validation](https://git.haelnorr.com/h/golib/wiki/JWT#token-validation) - [Token Revocation](https://git.haelnorr.com/h/golib/wiki/JWT#token-revocation) - [Cleanup](https://git.haelnorr.com/h/golib/wiki/JWT#cleanup) - [Using with ORMs](https://git.haelnorr.com/h/golib/wiki/JWT#using-with-orms) ## 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.