{"record":{"id":"b1555bda9ba9eae3","repo":"siyuan-note/siyuan","slug":"invalid-publish-service-token","errorCode":null,"errorMessage":"invalid publish service token","messagePattern":"invalid publish service token","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/auth.go","lineNumber":80,"sourceCode":"\n\t// publishSessionTTL 发布服务会话空闲过期时长，超过后需要重新认证\n\tpublishSessionTTL = 7 * 24 * time.Hour\n\t// publishSessionGlobalCap 发布服务会话全局上限，超出后淘汰最久未活跃的会话\n\tpublishSessionGlobalCap = 4096\n\t// publishSessionPerAccountCap 单账户会话上限，超出后淘汰该账户最久未活跃的会话\n\tpublishSessionPerAccountCap = 32\n)\n\nvar (\n\taccountsMap  = AccountsMap{}\n\taccountsLock = sync.RWMutex{}\n\tsessionsMap  = map[string]*PublishSession{}\n\tsessionLock  = sync.Mutex{}\n\n\tjwtKey     = make([]byte, 32)\n\tjwtKeyOnce sync.Once\n\n\tErrInvalidPublishServiceToken = errors.New(\"invalid publish service token\")\n)\n\nfunc InitJwtKey() {\n\tjwtKeyOnce.Do(func() {\n\t\terr := refreshJwtKey()\n\t\tif err != nil {\n\t\t\tlogging.LogFatalf(logging.ExitCodeFatal, \"initialize JWT signing key failed: %s\", err)\n\t\t}\n\t})\n}\n\nfunc refreshJwtKey() error {\n\tif _, err := rand.Read(jwtKey); err != nil {\n\t\tlogging.LogErrorf(\"generate JWT signing key failed: %s\", err)\n\t\treturn err\n\t}\n\treturn nil\n}","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/auth.go#L62-L98","documentation":"ErrInvalidPublishServiceToken is a sentinel error returned by ParseJWT when a JWT is tagged as a publish-service token but fails the publish-service validity check (IsValidPublishServiceToken) — for example after publish account re-initialization invalidated old sessions. Because it is a sentinel, callers should test it with errors.Is, and it is distinct from ordinary malformed-token failures.","triggerScenarios":"Calling ParseJWT on a token whose IsPublishServiceToken claim is true while IsValidPublishServiceToken(token) returns false — i.e. the token was issued for a publish service account that has since been re-initialized or replaced.","commonSituations":"A plugin or client kept a JWT across a publish account reset; the publish account was initialized after the token was minted (covered by TestJWTLifecycle); server-side jwt key/session state refreshed between token issuance and validation.","solutions":["Discard the stale token and re-authenticate: obtain a fresh JWT from the current publish account","Check errors.Is(err, model.ErrInvalidPublishServiceToken) to distinguish this case from other JWT failures and trigger re-login","If you control account lifecycle, re-issue tokens to clients whenever publish accounts are re-initialized"],"exampleFix":"// before\nclaims, err := ParseJWT(token) // treat all failures the same\n// after\nclaims, err := ParseJWT(token)\nif errors.Is(err, model.ErrInvalidPublishServiceToken) {\n    token = reissuePublishToken() // stale publish token\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function isStalePublishToken(err) { return errors.Is(err, model.ErrInvalidPublishServiceToken); }","tryCatchPattern":"claims, err := ParseJWT(token)\nif err != nil {\n    if errors.Is(err, ErrInvalidPublishServiceToken) {\n        token = refreshPublishToken() // re-authenticate against current publish account\n        claims, err = ParseJWT(token)\n    } else {\n        return err // other JWT failure\n    }\n}","preventionTips":["Always compare with errors.Is against the sentinel, never string matching","Re-issue tokens to clients whenever publish accounts are (re-)initialized","Treat this error as 'session invalidated', not 'malformed token'"],"tags":["jwt","authentication","publish-service","sentinel-error"],"backgroundTag":"jwt-token-expired","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}