|
| 1 | +//! Path-scoped visibility management API. Owner-only, mirrors `api/protect.rs`. |
| 2 | +
|
| 3 | +use axum::extract::{Extension, Path, State}; |
| 4 | +use axum::http::StatusCode; |
| 5 | +use axum::Json; |
| 6 | +use serde::Deserialize; |
| 7 | + |
| 8 | +use crate::auth::AuthenticatedDid; |
| 9 | +use crate::db::VisibilityMode; |
| 10 | +use crate::error::{AppError, Result}; |
| 11 | +use crate::state::AppState; |
| 12 | + |
| 13 | +#[derive(Deserialize)] |
| 14 | +pub struct SetVisibilityRequest { |
| 15 | + pub path_glob: String, |
| 16 | + /// "a" or "b"; defaults to "b" if absent or unrecognized. |
| 17 | + #[serde(default)] |
| 18 | + pub mode: Option<String>, |
| 19 | + #[serde(default)] |
| 20 | + pub reader_dids: Vec<String>, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Deserialize)] |
| 24 | +pub struct RemoveVisibilityRequest { |
| 25 | + pub path_glob: String, |
| 26 | +} |
| 27 | + |
| 28 | +fn require_owner(record: &crate::db::RepoRecord, caller: &str) -> Result<()> { |
| 29 | + let owner_short = record |
| 30 | + .owner_did |
| 31 | + .split(':') |
| 32 | + .next_back() |
| 33 | + .unwrap_or(&record.owner_did); |
| 34 | + if caller != record.owner_did && caller != owner_short { |
| 35 | + return Err(AppError::BadRequest( |
| 36 | + "only the repo owner can manage visibility".into(), |
| 37 | + )); |
| 38 | + } |
| 39 | + Ok(()) |
| 40 | +} |
| 41 | + |
| 42 | +/// PUT /api/v1/repos/{owner}/{repo}/visibility |
| 43 | +pub async fn set_visibility( |
| 44 | + State(state): State<AppState>, |
| 45 | + Extension(auth): Extension<AuthenticatedDid>, |
| 46 | + Path((owner, repo)): Path<(String, String)>, |
| 47 | + Json(req): Json<SetVisibilityRequest>, |
| 48 | +) -> Result<(StatusCode, Json<serde_json::Value>)> { |
| 49 | + let record = state |
| 50 | + .db |
| 51 | + .get_repo(&owner, &repo) |
| 52 | + .await? |
| 53 | + .ok_or_else(|| AppError::RepoNotFound(format!("{owner}/{repo}")))?; |
| 54 | + require_owner(&record, &auth.0)?; |
| 55 | + |
| 56 | + let mode = match req.mode.as_deref() { |
| 57 | + Some("a") => VisibilityMode::A, |
| 58 | + _ => VisibilityMode::B, |
| 59 | + }; |
| 60 | + |
| 61 | + // Mode A hides existence and is only coherent for the whole repo; a subtree |
| 62 | + // cannot hide its existence without rewriting git history (see spec). |
| 63 | + if mode == VisibilityMode::A && req.path_glob != "/" { |
| 64 | + return Err(AppError::BadRequest( |
| 65 | + "mode 'a' (hide) is only allowed for the whole repo (path_glob '/'); use mode 'b' for subtrees".into(), |
| 66 | + )); |
| 67 | + } |
| 68 | + |
| 69 | + // An empty reader_dids list is valid and intentional: the owner is always |
| 70 | + // allowed by visibility_check, so a "/" rule with no readers is exactly the |
| 71 | + // whole-repo "private to owner only" case. |
| 72 | + state |
| 73 | + .db |
| 74 | + .set_visibility_rule(&record.id, &req.path_glob, mode, &req.reader_dids, &auth.0) |
| 75 | + .await?; |
| 76 | + |
| 77 | + let subtree_warning = req.path_glob != "/"; |
| 78 | + tracing::info!( |
| 79 | + repo = %repo, caller = %auth.0, path_glob = %req.path_glob, mode = %mode.as_str(), |
| 80 | + subtree_pending = subtree_warning, |
| 81 | + "visibility rule set" |
| 82 | + ); |
| 83 | + |
| 84 | + Ok(( |
| 85 | + StatusCode::CREATED, |
| 86 | + Json(serde_json::json!({ |
| 87 | + "status": "set", |
| 88 | + "repo": format!("{owner}/{repo}"), |
| 89 | + "path_glob": req.path_glob, |
| 90 | + "mode": mode.as_str(), |
| 91 | + "reader_dids": req.reader_dids, |
| 92 | + "subtree_clone_enforcement_pending": subtree_warning, |
| 93 | + })), |
| 94 | + )) |
| 95 | +} |
| 96 | + |
| 97 | +/// DELETE /api/v1/repos/{owner}/{repo}/visibility |
| 98 | +pub async fn remove_visibility( |
| 99 | + State(state): State<AppState>, |
| 100 | + Extension(auth): Extension<AuthenticatedDid>, |
| 101 | + Path((owner, repo)): Path<(String, String)>, |
| 102 | + Json(req): Json<RemoveVisibilityRequest>, |
| 103 | +) -> Result<Json<serde_json::Value>> { |
| 104 | + let record = state |
| 105 | + .db |
| 106 | + .get_repo(&owner, &repo) |
| 107 | + .await? |
| 108 | + .ok_or_else(|| AppError::RepoNotFound(format!("{owner}/{repo}")))?; |
| 109 | + require_owner(&record, &auth.0)?; |
| 110 | + |
| 111 | + state |
| 112 | + .db |
| 113 | + .remove_visibility_rule(&record.id, &req.path_glob) |
| 114 | + .await?; |
| 115 | + |
| 116 | + tracing::info!( |
| 117 | + repo = %repo, caller = %auth.0, path_glob = %req.path_glob, |
| 118 | + "visibility rule removed" |
| 119 | + ); |
| 120 | + |
| 121 | + Ok(Json(serde_json::json!({ |
| 122 | + "status": "removed", |
| 123 | + "repo": format!("{owner}/{repo}"), |
| 124 | + "path_glob": req.path_glob, |
| 125 | + }))) |
| 126 | +} |
| 127 | + |
| 128 | +/// GET /api/v1/repos/{owner}/{repo}/visibility |
| 129 | +pub async fn list_visibility( |
| 130 | + State(state): State<AppState>, |
| 131 | + Extension(auth): Extension<AuthenticatedDid>, |
| 132 | + Path((owner, repo)): Path<(String, String)>, |
| 133 | +) -> Result<Json<serde_json::Value>> { |
| 134 | + let record = state |
| 135 | + .db |
| 136 | + .get_repo(&owner, &repo) |
| 137 | + .await? |
| 138 | + .ok_or_else(|| AppError::RepoNotFound(format!("{owner}/{repo}")))?; |
| 139 | + require_owner(&record, &auth.0)?; |
| 140 | + |
| 141 | + let rules = state.db.list_visibility_rules(&record.id).await?; |
| 142 | + let rules_json: Vec<_> = rules |
| 143 | + .into_iter() |
| 144 | + .map(|r| { |
| 145 | + serde_json::json!({ |
| 146 | + "path_glob": r.path_glob, |
| 147 | + "mode": r.mode.as_str(), |
| 148 | + "reader_dids": r.reader_dids, |
| 149 | + "created_by": r.created_by, |
| 150 | + }) |
| 151 | + }) |
| 152 | + .collect(); |
| 153 | + |
| 154 | + Ok(Json(serde_json::json!({ |
| 155 | + "repo": format!("{owner}/{repo}"), |
| 156 | + "count": rules_json.len(), |
| 157 | + "rules": rules_json, |
| 158 | + }))) |
| 159 | +} |
0 commit comments