v1.0.3

Token Model

Last updated


go-jwt manages a login session with three credentials: a short-lived Access Token, a long-lived Refresh ID, and a JTI whitelist stored in Redis.

Three Credentials


CredentialFormDefault lifetimeWhere it livesPurpose
Access TokenES256-signed JWT15 minutes (AccessTokenExpires)Cookie access_token, or Authorization: BearerAuthenticate every request
Refresh ID64-character hex string (SHA-256)7 days (RefreshIdExpires)Cookie refresh_id, or X-Refresh-ID headerObtain a new token when the Access Token expires
JTIUUID v4Same as the Access TokenRedis jti:<jti>Let the server invalidate an issued Access Token

The Refresh ID carries no data; it is the key of Redis refresh:<id>. User data, version, and device fingerprint live server-side in RefreshData.

Access Token Claims


Claims written by signJWT():
ClaimSource
id, name, email, thumbnail, scope, role, levelAuth fields
fpDevice fingerprint of this request
jtiNewly generated UUID
expIssue time + AccessTokenExpires
iat, nbfIssue time
refresh_id (name taken from RefreshIdCookieKey)Bound Refresh ID

A JWT is signed, not encrypted; anyone holding it can decode every Auth field, so keep sensitive data out.

How the Refresh ID Is Derived


createRefreshId() serializes {id, name, email, fp, iat, jti} to JSON, hashes it with SHA-256, and outputs hex. Because it includes jti and the current time, each login yields a different Refresh ID for the same user.

RefreshData


The value of Redis refresh:<id>:
FieldDescription
dataAuth
version1 at issue, +1 per refresh; the pair is fully reissued once it exceeds MaxVersion
fpDevice fingerprint at issue; must match on refresh
exp, iatTimes of the first issue; kept unchanged on refresh
jtiJTI of the most recent issue

Session Lifecycle


StageTriggerPage
IssueThe application calls Create()API Reference
VerifyEvery request, via Verify() or middlewareVerification Flow
RefreshAccess Token expired or missing while the Refresh ID is still validTransparent Refresh
RevokeThe application calls Revoke()Revocation
中文