@@ -12,7 +12,7 @@ use crate::host::wasm_common::{
1212} ;
1313use crate :: host:: AbiCall ;
1414use anyhow:: Context as _;
15- use spacetimedb_lib:: Timestamp ;
15+ use spacetimedb_lib:: { ConnectionId , Timestamp } ;
1616use spacetimedb_primitives:: { errno, ColId } ;
1717use wasmtime:: { AsContext , Caller , StoreContextMut } ;
1818
@@ -22,6 +22,7 @@ use super::{Mem, MemView, NullableMemOp, WasmError, WasmPointee, WasmPtr};
2222use instrumentation:: noop as span;
2323#[ cfg( feature = "spacetimedb-wasm-instance-env-times" ) ]
2424use instrumentation:: op as span;
25+ use spacetimedb_datastore:: locking_tx_datastore:: state_view:: StateView ;
2526
2627/// A `WasmInstanceEnv` provides the connection between a module
2728/// and the database.
@@ -1208,6 +1209,69 @@ impl WasmInstanceEnv {
12081209 } )
12091210 }
12101211
1212+ pub fn jwt_len (
1213+ caller : Caller < ' _ , Self > ,
1214+ connection_id : WasmPtr < ConnectionId > ,
1215+ out_ptr : WasmPtr < u32 > ,
1216+ ) -> RtResult < ( ) > {
1217+ log:: info!( "Calling has_jwt" ) ;
1218+ Self :: with_span ( caller, AbiCall :: JwtLength , |caller| {
1219+ let ( mem, env) = Self :: mem_env ( caller) ;
1220+ let cid = ConnectionId :: read_from ( mem, connection_id) ?;
1221+ let length = env
1222+ . instance_env
1223+ . tx
1224+ . get ( )
1225+ . unwrap ( )
1226+ . get_jwt_payload ( cid) ?
1227+ . map ( |p| p. len ( ) as u32 )
1228+ . unwrap_or ( 0u32 ) ;
1229+ length. write_to ( mem, out_ptr) ?;
1230+ Ok ( ( ) )
1231+ } )
1232+ }
1233+
1234+ pub fn get_jwt (
1235+ caller : Caller < ' _ , Self > ,
1236+ connection_id : WasmPtr < ConnectionId > ,
1237+ target_ptr : WasmPtr < u8 > ,
1238+ target_ptr_len : WasmPtr < u32 > ,
1239+ ) -> RtResult < ( ) > {
1240+ log:: info!( "Calling get_jwt" ) ;
1241+ Self :: with_span ( caller, AbiCall :: GetJwt , |caller| {
1242+ let ( mem, env) = Self :: mem_env ( caller) ;
1243+ let cid = ConnectionId :: read_from ( mem, connection_id) ?;
1244+ let jwt = match env. instance_env . tx . get ( ) . unwrap ( ) . get_jwt_payload ( cid) ? {
1245+ None => {
1246+ // Consider logging here, since this should only happen during an upgrade.
1247+ 0u32 . write_to ( mem, target_ptr_len) ?;
1248+ return Ok ( ( ) ) ;
1249+ }
1250+ Some ( jwt) => jwt,
1251+ } ;
1252+ log:: info!( "JWT payload found for connection ID: {:?}: {}" , cid. to_hex( ) , jwt) ;
1253+ let jwt_len = jwt. len ( ) ;
1254+ // Read `buffer_len`, i.e., the capacity of `buffer` pointed to by `buffer_ptr`.
1255+ let buffer_len = u32:: read_from ( mem, target_ptr_len) ?;
1256+ log:: info!( "buffer_len: {buffer_len}" ) ;
1257+ if buffer_len < jwt_len as u32 {
1258+ return Err ( anyhow:: anyhow!( "buffer too small to hold JWT payload" ) ) ;
1259+ }
1260+ log:: info!( "About to write length" ) ;
1261+ // Write the length of the JWT payload to the target pointer.
1262+ ( jwt_len as u32 ) . write_to ( mem, target_ptr_len) ?;
1263+ log:: info!( "wrote length of {jwt_len}" ) ;
1264+ log:: info!( "Byte len {}" , jwt. len( ) ) ;
1265+
1266+ // Write the JWT payload to the target pointer.
1267+ // Get a mutable view to the `buffer`.
1268+ let buffer = mem. deref_slice_mut ( target_ptr, buffer_len) ?;
1269+ buffer[ ..jwt_len] . copy_from_slice ( jwt. as_bytes ( ) ) ;
1270+ log:: info!( "wrote jwt bytes to slice" ) ;
1271+ Ok ( ( ) )
1272+ } )
1273+ }
1274+
12111275 /// Writes the identity of the module into `out = out_ptr[..32]`.
12121276 ///
12131277 /// # Traps
0 commit comments