v1.0.3

Configuration

Last updated


Every setting passes through goJwt.New(goJwt.Config{...}); the library reads no environment variables. Key settings are covered in Key Management.

Config


FieldTypeJSON tagRequiredDescription
RedisRedisredisYesRedis connection settings
File*FilefileNoPEM key file paths
Option*OptionparameterNoToken parameters; nil applies every default
Cookie*CookiecookieNoCookie attribute overrides; nil keeps the defaults
CheckAuthfunc(Auth) (bool, error)not serializedNoCalled on every refresh; returning false or an error rejects the refresh (see Transparent Refresh)

The JSON tag of Option is parameter; use that key when loading the config from JSON.

Redis


FieldTypeDescription
HoststringHost address
PortintPort
PasswordstringPassword, optional
DBintDatabase index

New() builds a go-redis client for Host:Port with Password and DB, then sends PING; a failed PING makes New() return an error.

Option


FieldTypeDefaultDescription
PrivateKeystring—ECDSA private key PEM
PublicKeystring—ECDSA public key PEM
AccessTokenExpirestime.Duration15 * time.MinuteLifetime of the Access Token and its JTI
RefreshIdExpirestime.Duration7 * 24 * time.HourRefresh ID lifetime
AccessTokenCookieKeystringaccess_tokenAccess Token cookie name
RefreshIdCookieKeystringrefresh_idRefresh ID cookie name; also the JWT claim name holding the Refresh ID
MaxVersionint5Maximum re-sign-only refreshes before a full reissue
RefreshTTLfloat640.5Full reissue once the Refresh ID's remaining TTL drops below RefreshIdExpires × RefreshTTL

Zero or negative values fall back to the defaults. Verify() reads the Refresh ID claim under the current RefreshIdCookieKey with an unchecked type assertion, so after changing it, a still-valid Access Token issued under the old name panics inside Verify() (under gin.Default() the recovery middleware answers 500; a plain net/http server aborts that request). Change it together with a key rotation, or wait until every Access Token issued under the old name has expired.

FieldTypeDefault
Domain*stringunset (host-only)
Path*string/
SameSite*http.SameSitehttp.SameSiteLaxMode
Secure*boolfalse
HttpOnly*booltrue

Only non-nil fields override the defaults; they apply to the access_token, refresh_id, and conn.device.id cookies.

Full Example


  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "time"
  6. goJwt "github.com/pardnchiu/go-jwt/core"
  7. )
  8. func main() {
  9. domain := "example.com"
  10. secure := true
  11. sameSite := http.SameSiteStrictMode
  12. auth, err := goJwt.New(goJwt.Config{
  13. Redis: goJwt.Redis{Host: "redis.internal", Port: 6379, Password: "secret", DB: 1},
  14. File: &goJwt.File{
  15. PrivateKeyPath: "/etc/app/keys/private-key.pem",
  16. PublicKeyPath: "/etc/app/keys/public-key.pem",
  17. },
  18. Option: &goJwt.Option{
  19. AccessTokenExpires: 10 * time.Minute,
  20. RefreshIdExpires: 30 * 24 * time.Hour,
  21. MaxVersion: 10,
  22. RefreshTTL: 0.3,
  23. },
  24. Cookie: &goJwt.Cookie{
  25. Domain: &domain,
  26. Secure: &secure,
  27. SameSite: &sameSite,
  28. },
  29. })
  30. if err != nil {
  31. log.Fatalf("failed to init go-jwt: %v", err)
  32. }
  33. defer auth.Close()
  34. }

Logging


New() points the package-level slog logger to syslog (priority LOG_INFO|LOG_LOCAL0, tag goCron, JSON format) and falls back to stderr (text format) when syslog is unreachable. Filter syslog by the goCron tag to find go-jwt entries.
中文