MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / extract_payload

Function extract_payload

crates/client-api/src/auth.rs:343–376  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

341 // Test that extracting a JWT payload from a valid token gets the json representation.
342 #[tokio::test]
343 async fn extract_payload() -> Result<(), anyhow::Error> {
344 let kp = JwtKeys::generate()?;
345
346 let dummy_audience: Box<str> = "spacetimedb".into();
347 let claims = TokenClaims {
348 issuer: "localhost".into(),
349 subject: "test-subject".into(),
350 audience: [dummy_audience.clone()].into(),
351 extra: None,
352 };
353 let (_, token) = claims.encode_and_sign(&kp.private)?;
354 let st_creds = SpacetimeCreds::from_signed_token(token);
355 let payload = st_creds
356 .extract_jwt_payload_string()
357 .ok_or_else(|| anyhow::anyhow!("Failed to extract JWT payload"))?;
358 // Make sure it is valid json.
359 let parsed: serde_json::Value = serde_json::from_str(&payload)?;
360 assert_eq!(parsed.get("iss").unwrap().as_str().unwrap(), &*claims.issuer);
361 assert_eq!(parsed.get("sub").unwrap().as_str().unwrap(), &*claims.subject);
362 assert_eq!(
363 parsed.get("aud").unwrap().as_array().unwrap()[0].as_str().unwrap(),
364 &*dummy_audience
365 );
366 let as_object = parsed
367 .as_object()
368 .ok_or_else(|| anyhow::anyhow!("Failed to parse JWT payload as object"))?;
369 let keys: HashSet<String> = as_object.keys().map(|s| s.to_string()).collect();
370 let expected_keys = vec!["iss", "sub", "aud", "iat", "exp", "hex_identity"]
371 .into_iter()
372 .map(|s| s.to_string())
373 .collect::<HashSet<String>>();
374 assert_eq!(keys, expected_keys);
375 Ok(())
376 }
377
378 #[tokio::test]
379 async fn authorization_rejection_custom_uses_display_message() -> Result<(), anyhow::Error> {

Callers

nothing calls this directly

Calls 11

from_strFunction · 0.85
encode_and_signMethod · 0.80
generateFunction · 0.50
OkFunction · 0.50
cloneMethod · 0.45
collectMethod · 0.45
mapMethod · 0.45
keysMethod · 0.45
to_stringMethod · 0.45
into_iterMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…