siyuan-note/siyuan · error

decode OIDC claims failed

Error message

decode OIDC claims failed: %w

What it means

After verifying the ID token, Exchange decodes its claims into a map[string]any via idToken.Claims(&claims); a decoding failure (unexpected claim types the JSON decoder cannot unmarshal, malformed payload) is wrapped as "decode OIDC claims failed". The token was cryptographically valid but its claim payload could not be materialized for the caller.

Solutions

  1. Inspect the ID token payload (jwt.io or equivalent) to find which claim breaks decoding, then adjust IdP claim configuration (e.g. fix a custom claim's type).
  2. Update/align the OIDC library versions on both sides if a claim-type incompatibility is suspected.
  3. Log the wrapped cause and report the non-conformant IdP behavior; work around by normalizing claims at the IdP (e.g. Keycloak mapper) to standard JSON types.
  4. Retry the sign-in with a different account/issuer to confirm whether the problem is account-specific claim data.

Example fix

// before
claims := map[string]any{}
if err = idToken.Claims(&claims); err != nil {
    return nil, fmt.Errorf("decode OIDC claims failed: %w", err) // diagnosing: dump token payload
}
// after
claims := map[string]any{}
if err = idToken.Claims(&claims); err != nil {
    // inspect raw token: log rawIDToken payload (never the secret) to find the offending claim,
    // then fix the IdP claim mapper to emit standard JSON types
    return nil, fmt.Errorf("decode OIDC claims failed: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

parts := strings.Split(rawIDToken, ".")
if len(parts) != 3 {
    return errors.New("ID token is not a well-formed JWT")
}

Try / catch

claims, err := provider.Exchange(ctx, code, verifier, nonce)
if err != nil {
    if strings.Contains(err.Error(), "decode OIDC claims failed") {
        // inspect the token payload and the IdP claim mappers; report non-conformant claim types
    }
    return err
}

Prevention

When it happens

Trigger: Calling Exchange when idToken.Claims fails to decode the verified token's JSON payload into map[string]any — e.g. a claim value with a type the decoder rejects, or a corrupted/abnormal token payload from a non-conformant IdP.

Common situations: IdPs emitting exotic claim values (very large numbers, nested structures conflicting with the target type); middleware rewriting tokens; malformed JSON in tokens from non-conformant providers; rarely, library/version incompatibilities in claim handling.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/oidc_provider/provider.go:112

		return nil, fmt.Errorf("exchange OIDC authorization code failed: %w", err)
	}
	if p.kind == conf.OIDCProviderGitHub {
		return exchangeGitHubClaims(ctx, token)
	}
	rawIDToken, ok := token.Extra("id_token").(string)
	if !ok || rawIDToken == "" {
		return nil, errors.New("OIDC response does not contain an ID token")
	}
	idToken, err := p.verifier.Verify(ctx, rawIDToken)
	if err != nil {
		return nil, fmt.Errorf("verify OIDC ID token failed: %w", err)
	}
	if idToken.Nonce != nonce {
		return nil, errors.New("OIDC nonce does not match")
	}
	claims := map[string]any{}
	if err = idToken.Claims(&claims); err != nil {
		return nil, fmt.Errorf("decode OIDC claims failed: %w", err)
	}
	return claims, nil
}

func newGitHub(config *conf.OIDC, redirectURL string) *Provider {
	scopes := append([]string{}, config.Scopes...)
	if len(scopes) == 0 || isDefaultOIDCScopes(scopes) {
		scopes = []string{"read:user", "user:email"}
	} else {
		filtered := scopes[:0]
		for _, scope := range scopes {
			if scope != oidc.ScopeOpenID && scope != "profile" && scope != "email" {
				filtered = append(filtered, scope)
			}
		}
		scopes = filtered
		if !contains(scopes, "read:user") {
			scopes = append([]string{"read:user"}, scopes...)

View on GitHub (pinned to 9f775e8a12)