|
| 1 | +//! Community-scoped authentication allowlist persistence. |
| 2 | +//! |
| 3 | +//! This store is distinct from NIP-43 relay membership. Membership backfill |
| 4 | +//! orchestration remains with the relay-membership invariant owner. |
| 5 | +
|
| 6 | +use buzz_core::CommunityId; |
| 7 | +use buzz_datastore_tracing::datastore_span; |
| 8 | +use chrono::{DateTime, Utc}; |
| 9 | +use sqlx::Row; |
| 10 | + |
| 11 | +use crate::error::Result; |
| 12 | +use crate::Db; |
| 13 | + |
| 14 | +/// An entry in the pubkey allowlist. |
| 15 | +#[derive(Debug, Clone)] |
| 16 | +pub struct AllowlistEntry { |
| 17 | + /// The allowed pubkey. |
| 18 | + pub pubkey: Vec<u8>, |
| 19 | + /// Who added this entry. |
| 20 | + pub added_by: Vec<u8>, |
| 21 | + /// When the entry was added. |
| 22 | + pub added_at: DateTime<Utc>, |
| 23 | + /// Optional note. |
| 24 | + pub note: Option<String>, |
| 25 | +} |
| 26 | + |
| 27 | +impl Db { |
| 28 | + /// Check if a pubkey is in the allowlist for `community`. |
| 29 | + #[datastore_span(name = "is_pubkey_allowed", system = "postgresql")] |
| 30 | + pub async fn is_pubkey_allowed(&self, community: CommunityId, pubkey: &[u8]) -> Result<bool> { |
| 31 | + let row = sqlx::query( |
| 32 | + "SELECT COUNT(*) as cnt FROM pubkey_allowlist WHERE community_id = $1 AND pubkey = $2", |
| 33 | + ) |
| 34 | + .bind(community.as_uuid()) |
| 35 | + .bind(pubkey) |
| 36 | + .fetch_one(&self.pool) |
| 37 | + .await?; |
| 38 | + let cnt: i64 = row.try_get("cnt")?; |
| 39 | + Ok(cnt > 0) |
| 40 | + } |
| 41 | + |
| 42 | + /// Check if the community allowlist has any entries (i.e. is enforcement active). |
| 43 | + #[datastore_span(name = "has_allowlist_entries", system = "postgresql")] |
| 44 | + pub async fn has_allowlist_entries(&self, community: CommunityId) -> Result<bool> { |
| 45 | + let row = |
| 46 | + sqlx::query("SELECT COUNT(*) as cnt FROM pubkey_allowlist WHERE community_id = $1") |
| 47 | + .bind(community.as_uuid()) |
| 48 | + .fetch_one(&self.pool) |
| 49 | + .await?; |
| 50 | + let cnt: i64 = row.try_get("cnt")?; |
| 51 | + Ok(cnt > 0) |
| 52 | + } |
| 53 | + |
| 54 | + /// Add a pubkey to the community allowlist. |
| 55 | + #[datastore_span(name = "add_to_allowlist", system = "postgresql")] |
| 56 | + pub async fn add_to_allowlist( |
| 57 | + &self, |
| 58 | + community: CommunityId, |
| 59 | + pubkey: &[u8], |
| 60 | + added_by: &[u8], |
| 61 | + note: Option<&str>, |
| 62 | + ) -> Result<bool> { |
| 63 | + let result = sqlx::query( |
| 64 | + "INSERT INTO pubkey_allowlist (community_id, pubkey, added_by, note) VALUES ($1, $2, $3, $4) \ |
| 65 | + ON CONFLICT DO NOTHING", |
| 66 | + ) |
| 67 | + .bind(community.as_uuid()) |
| 68 | + .bind(pubkey) |
| 69 | + .bind(added_by) |
| 70 | + .bind(note) |
| 71 | + .execute(&self.pool) |
| 72 | + .await?; |
| 73 | + Ok(result.rows_affected() > 0) |
| 74 | + } |
| 75 | + |
| 76 | + /// Remove a pubkey from the community allowlist. |
| 77 | + #[datastore_span(name = "remove_from_allowlist", system = "postgresql")] |
| 78 | + pub async fn remove_from_allowlist( |
| 79 | + &self, |
| 80 | + community: CommunityId, |
| 81 | + pubkey: &[u8], |
| 82 | + ) -> Result<bool> { |
| 83 | + let result = |
| 84 | + sqlx::query("DELETE FROM pubkey_allowlist WHERE community_id = $1 AND pubkey = $2") |
| 85 | + .bind(community.as_uuid()) |
| 86 | + .bind(pubkey) |
| 87 | + .execute(&self.pool) |
| 88 | + .await?; |
| 89 | + Ok(result.rows_affected() > 0) |
| 90 | + } |
| 91 | + |
| 92 | + /// List all pubkeys in the community allowlist. |
| 93 | + #[datastore_span(name = "list_allowlist", system = "postgresql")] |
| 94 | + pub async fn list_allowlist(&self, community: CommunityId) -> Result<Vec<AllowlistEntry>> { |
| 95 | + let rows = sqlx::query( |
| 96 | + "SELECT pubkey, added_by, added_at, note FROM pubkey_allowlist WHERE community_id = $1 ORDER BY added_at DESC", |
| 97 | + ) |
| 98 | + .bind(community.as_uuid()) |
| 99 | + .fetch_all(&self.pool) |
| 100 | + .await?; |
| 101 | + |
| 102 | + let mut out = Vec::with_capacity(rows.len()); |
| 103 | + for row in rows { |
| 104 | + out.push(AllowlistEntry { |
| 105 | + pubkey: row.try_get("pubkey")?, |
| 106 | + added_by: row.try_get("added_by")?, |
| 107 | + added_at: row.try_get("added_at")?, |
| 108 | + note: row.try_get("note")?, |
| 109 | + }); |
| 110 | + } |
| 111 | + Ok(out) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +#[cfg(test)] |
| 116 | +mod tests { |
| 117 | + use super::*; |
| 118 | + use sqlx::PgPool; |
| 119 | + use uuid::Uuid; |
| 120 | + |
| 121 | + const TEST_DB_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz"; // sadscan:disable np.postgres.1 -- local test-only credentials |
| 122 | + |
| 123 | + async fn setup_db() -> Db { |
| 124 | + let database_url = |
| 125 | + std::env::var("TEST_DATABASE_URL").unwrap_or_else(|_| TEST_DB_URL.into()); |
| 126 | + let pool = PgPool::connect(&database_url) |
| 127 | + .await |
| 128 | + .expect("connect to test DB"); |
| 129 | + Db::from_pool(pool) |
| 130 | + } |
| 131 | + |
| 132 | + async fn make_community(pool: &PgPool) -> Uuid { |
| 133 | + let id = Uuid::new_v4(); |
| 134 | + let host = format!("communities-of-channels-{}.example", id.simple()); |
| 135 | + sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)") |
| 136 | + .bind(id) |
| 137 | + .bind(host) |
| 138 | + .execute(pool) |
| 139 | + .await |
| 140 | + .expect("insert community"); |
| 141 | + id |
| 142 | + } |
| 143 | + |
| 144 | + #[tokio::test] |
| 145 | + #[ignore = "requires Postgres"] |
| 146 | + async fn allowlist_is_scoped_to_community() { |
| 147 | + let db = setup_db().await; |
| 148 | + let community_a = CommunityId::from_uuid(make_community(&db.pool).await); |
| 149 | + let community_b = CommunityId::from_uuid(make_community(&db.pool).await); |
| 150 | + let pubkey = [7u8; 32]; |
| 151 | + let added_by = [9u8; 32]; |
| 152 | + |
| 153 | + assert!(db |
| 154 | + .add_to_allowlist(community_a, &pubkey, &added_by, Some("a-only")) |
| 155 | + .await |
| 156 | + .expect("add allowlist row")); |
| 157 | + assert!(!db |
| 158 | + .add_to_allowlist(community_a, &pubkey, &added_by, Some("duplicate")) |
| 159 | + .await |
| 160 | + .expect("duplicate allowlist row is idempotent")); |
| 161 | + |
| 162 | + assert!( |
| 163 | + db.is_pubkey_allowed(community_a, &pubkey) |
| 164 | + .await |
| 165 | + .expect("allowlist check A"), |
| 166 | + "pubkey added to A must be allowed in A" |
| 167 | + ); |
| 168 | + assert!( |
| 169 | + !db.is_pubkey_allowed(community_b, &pubkey) |
| 170 | + .await |
| 171 | + .expect("allowlist check B"), |
| 172 | + "pubkey added only to A must not be allowed in B" |
| 173 | + ); |
| 174 | + assert!(db |
| 175 | + .has_allowlist_entries(community_a) |
| 176 | + .await |
| 177 | + .expect("A has entries")); |
| 178 | + assert!(!db |
| 179 | + .has_allowlist_entries(community_b) |
| 180 | + .await |
| 181 | + .expect("B has no entries")); |
| 182 | + |
| 183 | + let listed = db |
| 184 | + .list_allowlist(community_a) |
| 185 | + .await |
| 186 | + .expect("list A allowlist"); |
| 187 | + assert_eq!(listed.len(), 1); |
| 188 | + assert_eq!(listed[0].pubkey, pubkey); |
| 189 | + |
| 190 | + assert!( |
| 191 | + !db.remove_from_allowlist(community_b, &pubkey) |
| 192 | + .await |
| 193 | + .expect("remove from B is no-op"), |
| 194 | + "removing from B must not delete A's row" |
| 195 | + ); |
| 196 | + assert!(db |
| 197 | + .is_pubkey_allowed(community_a, &pubkey) |
| 198 | + .await |
| 199 | + .expect("A still allowed after B remove")); |
| 200 | + assert!(db |
| 201 | + .remove_from_allowlist(community_a, &pubkey) |
| 202 | + .await |
| 203 | + .expect("remove from A")); |
| 204 | + assert!(!db |
| 205 | + .is_pubkey_allowed(community_a, &pubkey) |
| 206 | + .await |
| 207 | + .expect("A not allowed after remove")); |
| 208 | + } |
| 209 | +} |
0 commit comments