Per collection/folder auth

This commit is contained in:
Torbjörn Molin committed 2026-10-19 12:38:38 +02:00
1 parent d5e7b484fc
commit 47ee07d9ff
16 files changed
+1569 -58

No files matched your search

+99
View File
@@ -9,6 +9,7 @@ use std::time::{Instant, SystemTime};
use reqlite_core::collection::Node;
use reqlite_core::cookies::CookieJar;
use reqlite_core::env::Environments;
use reqlite_core::folder::{self, FolderAuth, FolderSettings, InheritedAuth};
use reqlite_core::http::{BodyStore, Response};
use reqlite_core::httpfile::{HttpFile, RequestKey};
use reqlite_core::params::Param;
@@ -74,6 +75,10 @@ pub enum Dialog {
Prompt(Prompt),
/// Confirm a destructive collection operation.
ConfirmDelete(DeleteTarget),
/// Editing a folder's (or the collection's) default auth.
FolderAuth(FolderAuthEdit),
/// Making requests in a folder inherit its auth.
ConsolidateAuth(ConsolidateAuth),
/// Result of a Postman import.
ImportSummary {
root: PathBuf,
@@ -82,6 +87,63 @@ pub enum Dialog {
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FolderAuthEdit {
pub dir: PathBuf,
pub auth: FolderAuth,
}
/// A request in a collection, for lists in dialogs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RequestRef {
pub file: PathBuf,
pub index: usize,
pub label: String,
}
/// The plan shown by the "Consolidate auth" dialog. Built by
/// `action::plan_consolidation`; the UI only changes the choices in it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConsolidateAuth {
pub dir: PathBuf,
/// The auth requests in `dir` should inherit.
pub target: Option<String>,
/// `dir` doesn't have `target` as its own setting yet; applying writes it.
pub set_folder: bool,
/// Values to pick `target` from (with how many requests use each), when
/// `dir` has no auth of its own.
pub choices: Vec<(String, usize)>,
/// Requests with their own Authorization header.
pub own: Vec<OwnAuth>,
/// Requests that inherit today and would send something else once
/// `target` is set on the folder.
pub affected: Vec<(RequestRef, Option<String>)>,
/// Give the affected requests their current auth explicitly (a header,
/// or `# @no-auth`) so they keep sending what they send today.
pub keep_affected: bool,
/// Requests skipped because their tab has unsaved edits.
pub skipped_dirty: Vec<RequestRef>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OwnAuth {
pub request: RequestRef,
pub value: String,
/// What the request would inherit without its header.
pub inherits: Option<String>,
/// Remove the header.
pub selected: bool,
}
impl OwnAuth {
/// Removing the header keeps the request sending the same thing.
pub fn same(&self) -> bool {
self.inherits
.as_deref()
.is_some_and(|i| folder::same_auth(i, &self.value))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DeleteTarget {
Request { file: PathBuf, index: usize, label: String },
@@ -126,6 +188,9 @@ pub struct CollectionState {
pub root: PathBuf,
pub tree: Node,
pub files: BTreeMap<PathBuf, HttpFile>,
/// Folder settings (default auth) by folder.
pub folders: BTreeMap<PathBuf, FolderSettings>,
pub folder_errors: Vec<String>,
pub envs: Environments,
pub selected_env: Option<String>,
pub env_error: Option<String>,
@@ -153,6 +218,25 @@ impl CollectionState {
pub fn contains(&self, p: &Path) -> bool {
p.starts_with(&self.root)
}
/// The auth requests in `dir` inherit from their folders.
pub fn inherited_auth(&self, dir: &Path) -> Option<InheritedAuth> {
folder::inherited_auth(&self.root, dir, |d| self.folders.get(d))
}
/// The folder's own auth setting.
pub fn folder_auth(&self, dir: &Path) -> FolderAuth {
self.folders.get(dir).map(|s| s.auth.clone()).unwrap_or_default()
}
/// `dir` relative to the root, for messages ("the collection" for the root).
pub fn folder_label(&self, dir: &Path) -> String {
if dir == self.root {
format!("the collection “{}”", self.name())
} else {
format!("the folder {}", self.relative(dir).display())
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
@@ -429,6 +513,21 @@ impl AppState {
}
}
/// The folder whose auth a tab's request inherits: its file's folder, or
/// for untitled tabs the root of the current collection.
pub fn auth_dir_for(&self, tab: &Tab) -> Option<PathBuf> {
match &tab.origin {
Some(o) => self.collection_of(&o.file).and(o.file.parent()).map(Path::to_path_buf),
None => self.current_collection().map(|c| c.root.clone()),
}
}
/// The auth a tab's request inherits from its folders.
pub fn inherited_auth_for(&self, tab: &Tab) -> Option<InheritedAuth> {
let dir = self.auth_dir_for(tab)?;
self.collection_for_tab(tab)?.inherited_auth(&dir)
}
/// Whether `name` comes from the private env file (shown masked).
pub fn is_private_var(&self, tab: &Tab, name: &str) -> bool {
self.collection_for_tab(tab).is_some_and(|c| {