v1.0.3

API Reference

Last updated


Every exported function, method, and type in github.com/pardnchiu/go-jwt/core (package goJwt).

Functions


SignatureDescription
func New(c Config) (*JWTAuth, error)Apply defaults, load or generate keys, connect to Redis and PING, return an instance
func GetAuthDataFromGinContext(c *gin.Context) (*Auth, bool)Read the user data stored by GinMiddleware
func GetAuthDataFromHTTPRequest(r *http.Request) (*Auth, bool)Read the user data HTTPMiddleware stored in the request context

JWTAuth Methods


SignatureDescription
func (j *JWTAuth) Create(w http.ResponseWriter, r *http.Request, auth *Auth) JWTAuthResultIssue an Access Token and Refresh ID, writing cookies and Redis; a nil auth returns 400 data_missing
func (j *JWTAuth) Verify(w http.ResponseWriter, r *http.Request) JWTAuthResultVerify the request, refreshing transparently when needed (see Verification Flow)
func (j *JWTAuth) Revoke(w http.ResponseWriter, r *http.Request) JWTAuthResultClear cookies, revoke the Access Token, expire the Refresh ID after 5 seconds (see Revocation)
func (j *JWTAuth) GinMiddleware() gin.HandlerFuncGin middleware (see Middleware)
func (j *JWTAuth) HTTPMiddleware(next http.Handler) http.Handlernet/http middleware
func (j *JWTAuth) Close() errorClose the Redis connection

JWTAuth has no exported fields; one instance can be shared across goroutines.

Configuration Types


TypeDescription
ConfigTop-level settings: Redis, File, Option, Cookie, CheckAuth
RedisHost, Port, Password, DB
FilePrivateKeyPath, PublicKeyPath
OptionKey contents, lifetimes, cookie names, MaxVersion, RefreshTTL
CookieDomain, Path, SameSite, Secure, HttpOnly (all pointers)

See Configuration for fields and defaults.

Result and Data Types


  1. type Auth struct {
  2. ID string `json:"id"`
  3. Name string `json:"name"`
  4. Email string `json:"email"`
  5. Thumbnail string `json:"thumbnail,omitempty"`
  6. Scope []string `json:"scope,omitempty"`
  7. Role string `json:"role,omitempty"`
  8. Level int `json:"level,omitempty"`
  9. }
  10. type JWTAuthResult struct {
  11. StatusCode int `json:"status_code"`
  12. Success bool `json:"success"`
  13. Data *Auth `json:"data,omitempty"`
  14. Token *TokenResult `json:"token,omitempty"`
  15. Error string `json:"error,omitempty"`
  16. ErrorTag string `json:"error_tag,omitempty"`
  17. }
  18. type TokenResult struct {
  19. Token string `json:"token"`
  20. RefreshId string `json:"refresh_id"`
  21. }

FieldWhen set
JWTAuthResult.DataOn successful Create and Verify
JWTAuthResult.TokenOn successful Create; on Verify when a refresh happened and succeeded
JWTAuthResult.ErrorTagOn failure; values in Status Codes and Error Tags

Internal State Types


These types are exported but callers rarely need them directly:
TypeDescription
RefreshDataContent of Redis refresh:<id>: Data, Version, Fingerprint, Exp, Iat, Jti (see Token Model)
RefreshIdInput hashed into the Refresh ID: ID, Name, Email, Fingerprint, Iat, Jti
PemParsed ECDSA key pair; its private and public fields are unexported, so it cannot be built or read from outside
中文