Skip to content

Claims ​

Claims ​

go
type Claims struct {
    UserID      string         `json:"user_id,omitempty"`
    Username    string         `json:"username,omitempty"`
    Role        string         `json:"role,omitempty"`
    Permissions []string       `json:"permissions,omitempty"`
    Scopes      []string       `json:"scopes,omitempty"`
    Extra       map[string]any `json:"extra,omitempty"`
    SessionID   string         `json:"session_id,omitempty"`
    ClientID    string         `json:"client_id,omitempty"`
    RegisteredClaims
}

Built-in Claims structure containing common business fields and standard JWT fields.

struct

Fields ​

FieldTypeDescription
UserIDstringUser ID
UsernamestringUsername
RolestringRole
Permissions[]stringPermission list
Scopes[]stringScope list
Extramap[string]anyCustom extension fields
SessionIDstringSession ID
ClientIDstringClient ID
RegisteredClaimsRegisteredClaimsStandard JWT fields

Validation Rules ​

The Validate() method checks that at least one of UserID or Username is non-empty.

Deep field validation runs only when a token is created. The validation path calls Validate() (required fields) and sanitizes the standard claim strings. The deep rules are:

RuleLimit
String field lengthAt most 256 bytes after UTF-8 encoding; about 85 Chinese characters, or 256 ASCII characters
Array field sizeMax 100 items
Extra field countMax 50 keys
Extra value typesOnly string and []string allowed; nested maps and other types are rejected
Control charactersCharacters other than tab, newline, and carriage return are rejected
Injection pattern detectionRejected when containing HTML/SQL/path traversal patterns

Array values in Extra read back from Parse as []any, not the []string that was written. Read them with a []any assertion, then convert each item to string.

Methods ​

MethodSignatureDescription
GetRegisteredClaimsfunc (c *Claims) GetRegisteredClaims() *RegisteredClaimsReturns the embedded standard fields
Validatefunc (c *Claims) Validate() errorChecks that at least one of UserID or Username is non-empty
MarshalJSONfunc (c *Claims) MarshalJSON() ([]byte, error)Implements json.Marshaler; field order and omitempty behavior match encoding/json
UnmarshalJSONfunc (c *Claims) UnmarshalJSON(data []byte) errorImplements json.Unmarshaler; runs a fast scan first and falls back to encoding/json when needed
AppendJSONfunc (c *Claims) AppendJSON(dst []byte) ([]byte, error)Advanced API; appends the encoding to dst and returns the extended slice
UnmarshalFastJSONfunc (c *Claims) UnmarshalFastJSON(data []byte) errorAdvanced API; runs the fast scan over a decoded JWT payload

MarshalJSON / UnmarshalJSON are called automatically by encoding/json and by the library's signing and parsing pipelines; regular business code rarely needs to call them.

Advanced Use Only

AppendJSON and UnmarshalFastJSON are Advanced APIs intended for allocation-sensitive paths. AppendJSON produces the same bytes as json.Marshal(*Claims) and only appends — it never rewrites existing bytes in dst. When UnmarshalFastJSON returns a non-nil error, the caller must re-decode with encoding/json; the receiver's contents are undefined at that point. Regular code should use MarshalJSON / UnmarshalJSON instead.


RegisteredClaims ​

go
type RegisteredClaims struct {
    Issuer    string        `json:"iss,omitempty"`
    Subject   string        `json:"sub,omitempty"`
    Audience  StringOrSlice `json:"aud,omitempty"`
    ExpiresAt NumericDate   `json:"exp"`
    NotBefore NumericDate   `json:"nbf"`
    IssuedAt  NumericDate   `json:"iat"`
    ID        string        `json:"jti,omitempty"`
    TokenType string        `json:"token_type,omitempty"`
}

Standard JWT registered claims (RFC 7519).

struct

Fields ​

FieldTypeJSON TagDescription
IssuerstringissIssuer
SubjectstringsubSubject
AudienceStringOrSliceaudAudience
ExpiresAtNumericDateexpExpiration time
NotBeforeNumericDatenbfNot-before time
IssuedAtNumericDateiatIssued-at time
IDstringjtiToken ID
TokenTypestringtoken_typeToken type (access or refresh; see Token Type Constants)