{"record":{"id":"1f442786a9228508","repo":"clockworklabs/SpacetimeDB","slug":"token-does-not-look-like-a-json-web-token-token","errorCode":null,"errorMessage":"Token does not look like a JSON web token: {token}","messagePattern":"Token does not look like a JSON web token: (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/cli/src/util.rs","lineNumber":338,"sourceCode":"    if force {\n        println!(\"Skipping confirmation due to --yes\");\n        return Ok(true);\n    }\n    let mut input = String::new();\n    print!(\"{prompt} [y/N]\");\n    std::io::stdout().flush()?;\n    std::io::stdin().read_line(&mut input)?;\n    let input = input.trim().to_lowercase();\n    Ok(input == \"y\" || input == \"yes\")\n}\n\npub fn decode_identity(token: &String) -> anyhow::Result<String> {\n    // Here, we manually extract and decode the claims from the json web token.\n    // We do this without using the `jsonwebtoken` crate because it doesn't seem to have a way to skip signature verification.\n    // But signature verification would require getting the public key from a server, and we don't necessarily want to do that.\n    let token_parts: Vec<_> = token.split('.').collect();\n    if token_parts.len() != 3 {\n        return Err(anyhow::anyhow!(\"Token does not look like a JSON web token: {token}\"));\n    }\n    let decoded_bytes = BASE_64_STD_NO_PAD.decode(token_parts[1])?;\n    let decoded_string = String::from_utf8(decoded_bytes)?;\n\n    let claims_data: IncomingClaims = serde_json::from_str(decoded_string.as_str())?;\n    let claims_data: SpacetimeIdentityClaims = claims_data.try_into()?;\n\n    Ok(claims_data.identity.to_string())\n}\n\npub async fn get_login_token_or_log_in(\n    config: &mut Config,\n    target_server: Option<&str>,\n    interactive: bool,\n) -> anyhow::Result<String> {\n    if let Some(token) = config.spacetimedb_token() {\n        return Ok(token.clone());\n    }","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/cli/src/util.rs#L320-L356","documentation":"decode_identity manually splits the login token on '.' and expects exactly three JWT segments (header.payload.signature) so it can base64-decode the claims without verifying the signature. Any token that is not that shape — truncated, an opaque key, garbage — fails this check before decoding starts.","triggerScenarios":"The token read from CLI config or an env override is not a 3-part JWT: hand-edited token file, truncated paste into SPACETIME_TOKEN-style variables, a refresh/session id used where the JWT is expected, or a partially-written token after an interrupted `spacetime login`.","commonSituations":"Scripting CI auth and storing the wrong secret; clipboard copies that drop characters; switching between auth systems while stale state remains.","solutions":["Log in again to mint a clean token: `spacetime logout && spacetime login`","Unset any token overriding env vars so the CLI uses its own config","If scripting, write the token verbatim from the login flow without trimming or line-wrapping"],"exampleFix":"# before\nexport SPACETIMEDB_TOKEN=\"eyJhbGciOi...\"   # truncated paste\n\n# after\nspacetime logout && spacetime login            # fresh, complete JWT stored","handlingStrategy":"type-guard","validationCode":"// Rust: check JWT shape before calling decode_identity\nfn looks_like_jwt(token: &str) -> bool {\n    let parts: Vec<&str> = token.split('.').collect();\n    parts.len() == 3 && parts.iter().all(|p| !p.is_empty())\n}","typeGuard":"fn is_jwt_shaped(token: &str) -> std::result::Result<&str, String> {\n    match token.split('.').count() {\n        3 => Ok(token),\n        n => Err(format!(\"expected 3 JWT segments, found {n}\")),\n    }\n}","tryCatchPattern":null,"preventionTips":["Never hand-edit or retype tokens; always transfer them programmatically from the login flow","If auth fails after env changes, suspect an overridden token variable and re-login to reset state"],"tags":["auth","jwt","token","cli"],"backgroundTag":"jwt-malformed-token","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}