chiark / gitweb /
AssetUrlKey: wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 3 May 2021 22:39:18 +0000 (23:39 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 4 May 2021 11:18:52 +0000 (12:18 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bundles.rs
src/global.rs
src/prelude.rs

index 7af582a8be5eca97d6ed55f92ed3ce4c08163101..bd84f29497e935f1c3eef5c687e79a77cd0ff6fd 100644 (file)
@@ -47,6 +47,48 @@ hformat_as_display!{Id}
 
 const BUNDLES_MAX: Index = Index(64);
 
+#[derive(Clone)]
+pub enum AssetUrlKey {
+  Dummy,
+  Y(AssetUrlKeyRaw),
+}
+type AssetUrlKeyRaw = [u8; 32];
+impl Debug for AssetUrlKey {
+  #[throws(fmt::Error)]
+  fn fmt(&self, f: &mut Formatter) {
+    use AssetUrlKey::*;
+    match self {
+      Y(_) => write!(f, "AssetUrlKey::Y{{..}}")?,
+      Dummy => write!(f, "AssetUrlKey::Dummy")?,
+    }
+  }
+}
+impl AssetUrlKey {
+  #[throws(IE)]
+  pub fn new_random() -> AssetUrlKey {
+    let mut buf: AssetUrlKeyRaw = default();
+    let mut rng: rand::rngs::ThreadRng = thread_rng();
+    rand::RngCore::try_fill_bytes(&mut rng, &mut buf)
+      .context("generate new AssetUrlKey")?;
+    AssetUrlKey::Y(buf)
+  }
+}
+pub type AssetUrlToken = digest::Output<Digester>;
+impl AssetUrlKey {
+  pub fn token<V>(&self, what: &str, v: V) -> AssetUrlToken
+  where V: Serialize {
+    let k = match self {
+      AssetUrlKey::Y(k) => k,
+      _ => panic!("dummy AssetUrlKey being used!"),
+    };
+    let mut dw = DigestWrite::sink();
+    write!(dw, "{}\0", what).unwrap();
+    dw.write(&k[..]).unwrap();
+    rmp_serde::encode::write(&mut dw, &v).expect("serialize failed!");
+    dw.finish().0
+  }
+}
+
 #[derive(Copy,Clone,Debug,Hash,PartialEq,Eq,Ord,PartialOrd)]
 #[derive(Serialize,Deserialize)]
 pub struct Id { pub index: Index, pub kind: Kind, }
index c35a7d5c1770a2b3c207fed852a3fe45ecd52e25..22c02f30e6461885f82da2c92696839354b87e1a 100644 (file)
@@ -57,6 +57,7 @@ pub struct Instance {
   pub acl: LoadedAcl<TablePermission>,
   pub links: Arc<LinksTable>,
   pub bundle_list: MgmtBundleList, // copy for easy access
+  pub asset_url_key: AssetUrlKey,
 }
 
 pub struct PlayerRecord {
@@ -345,6 +346,7 @@ impl Instance {
       tokens_clients: default(),
       links: default(),
       bundle_list: default(),
+      asset_url_key: AssetUrlKey::new_random()?,
     };
 
     let c = InstanceContainer {
@@ -1158,6 +1160,7 @@ impl InstanceGuard<'_> {
       tokens_clients: default(),
       tokens_players: default(),
       bundle_list: default(), // set by load_game_bundles
+      asset_url_key: AssetUrlKey::Dummy,
     };
 
     let b = InstanceBundles::load_game_bundles(&mut g)?;
index 1f45a80114478f5cc466664c2a9533f5a3c08f1d..315830f0018a953c06020f0d9e8a078270d4a6a6 100644 (file)
@@ -123,6 +123,7 @@ pub use crate::accounts::*;
 pub use crate::authproofs::{self, Authorisation, Unauthorised};
 pub use crate::authproofs::AuthorisationSuperuser;
 pub use crate::bundles::{self, InstanceBundles, MgmtBundleListExt};
+pub use crate::bundles::{AssetUrlKey, AssetUrlToken};
 pub use crate::commands::{AccessTokenInfo, AccessTokenReport, MgmtError};
 pub use crate::commands::{MgmtCommand, MgmtResponse};
 pub use crate::commands::{MgmtGameInstruction, MgmtGameResponse};