siyuan-note/siyuan · error

invalid publish service token

Error message

invalid publish service token

What it means

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.

Solutions

  1. Discard the stale token and re-authenticate: obtain a fresh JWT from the current publish account
  2. Check errors.Is(err, model.ErrInvalidPublishServiceToken) to distinguish this case from other JWT failures and trigger re-login
  3. If you control account lifecycle, re-issue tokens to clients whenever publish accounts are re-initialized

Example fix

// before
claims, err := ParseJWT(token) // treat all failures the same
// after
claims, err := ParseJWT(token)
if errors.Is(err, model.ErrInvalidPublishServiceToken) {
    token = reissuePublishToken() // stale publish token
}
Defensive patterns

Strategy: try-catch

Type guard

function isStalePublishToken(err) { return errors.Is(err, model.ErrInvalidPublishServiceToken); }

Try / catch

claims, err := ParseJWT(token)
if err != nil {
    if errors.Is(err, ErrInvalidPublishServiceToken) {
        token = refreshPublishToken() // re-authenticate against current publish account
        claims, err = ParseJWT(token)
    } else {
        return err // other JWT failure
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/b1555bda9ba9eae3. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/auth.go:80

	// publishSessionTTL 发布服务会话空闲过期时长,超过后需要重新认证
	publishSessionTTL = 7 * 24 * time.Hour
	// publishSessionGlobalCap 发布服务会话全局上限,超出后淘汰最久未活跃的会话
	publishSessionGlobalCap = 4096
	// publishSessionPerAccountCap 单账户会话上限,超出后淘汰该账户最久未活跃的会话
	publishSessionPerAccountCap = 32
)

var (
	accountsMap  = AccountsMap{}
	accountsLock = sync.RWMutex{}
	sessionsMap  = map[string]*PublishSession{}
	sessionLock  = sync.Mutex{}

	jwtKey     = make([]byte, 32)
	jwtKeyOnce sync.Once

	ErrInvalidPublishServiceToken = errors.New("invalid publish service token")
)

func InitJwtKey() {
	jwtKeyOnce.Do(func() {
		err := refreshJwtKey()
		if err != nil {
			logging.LogFatalf(logging.ExitCodeFatal, "initialize JWT signing key failed: %s", err)
		}
	})
}

func refreshJwtKey() error {
	if _, err := rand.Read(jwtKey); err != nil {
		logging.LogErrorf("generate JWT signing key failed: %s", err)
		return err
	}
	return nil
}

View on GitHub (pinned to 9f775e8a12)