Added custom auth support
windows / build (push) Successful in 5m0s

This commit is contained in:
Torbjörn Molin committed 2026-11-09 12:56:54 +01:00
1 parent ae055be9cc
commit 1e7f8678eb
9 files changed
+377 -97

No files matched your search

+16 -19
View File
@@ -8,7 +8,9 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH};
use reqlite_core::collection::{self, Collection};
use reqlite_core::env::{self, Environments};
use reqlite_core::export::curl::to_curl;
use reqlite_core::folder::{self, auth_mode, same_auth, with_inherited_auth, AuthMode, FolderAuth, FolderSettings};
use reqlite_core::folder::{
self, auth_mode, same_auth, with_inherited_auth, AuthHeader, AuthMode, FolderAuth, FolderSettings,
};
use reqlite_core::http::{HttpError, Response};
use reqlite_core::httpfile::{render_request, HttpFile};
use reqlite_core::import::curl::{from_curl, looks_like_curl};
@@ -154,7 +156,7 @@ pub enum Action {
/// `target` picks the auth to inherit when the folder has none.
ConsolidateAuth {
dir: PathBuf,
target: Option<String>,
target: Option<AuthHeader>,
},
ApplyConsolidateAuth(ConsolidateAuth),
@@ -320,13 +322,7 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
}
Action::HeadersEdited(id, headers) => edit(state, id, |r| r.headers = headers),
Action::BodyEdited(id, body) => edit(state, id, |r| r.body = body),
Action::AuthEdited(id, mode) => edit(state, id, |r| {
match &mode {
AuthMode::Own(v) => r.set_header("Authorization", v.as_str()),
AuthMode::NoAuth | AuthMode::Inherit => r.remove_header("Authorization"),
}
folder::set_no_auth(r, mode == AuthMode::NoAuth);
}),
Action::AuthEdited(id, mode) => edit(state, id, |r| folder::set_auth_mode(r, &mode)),
Action::SetNoRedirect(id, on) => edit(state, id, |r| r.directives.no_redirect = on),
Action::PickBodyFile(id) => fx.push(Effect::PickBodyFile(id)),
Action::BodyFilePicked(id, path) => {
@@ -1434,14 +1430,14 @@ fn slug_name(title: &str) -> String {
}
/// Works out what "Consolidate auth" on `dir` would do: which requests can
/// drop their own Authorization header and inherit `dir`'s auth instead.
/// drop their own auth header and inherit `dir`'s auth instead.
/// When `dir` has no auth of its own, `pick` (or what it already inherits,
/// or the most used header) becomes its auth.
pub fn plan_consolidation(state: &AppState, dir: &Path, pick: Option<String>) -> Option<ConsolidateAuth> {
pub fn plan_consolidation(state: &AppState, dir: &Path, pick: Option<AuthHeader>) -> Option<ConsolidateAuth> {
let c = state.collection_of(dir)?;
let requests: Vec<_> = c.all_requests().filter(|(p, _, _)| p.starts_with(dir)).collect();
let mut choices: Vec<(String, usize)> = Vec::new();
let mut choices: Vec<(AuthHeader, usize)> = Vec::new();
for (_, _, r) in &requests {
if let AuthMode::Own(v) = auth_mode(r) {
match choices.iter_mut().find(|(c, _)| same_auth(c, &v)) {
@@ -1549,7 +1545,7 @@ enum AuthFix {
/// Drop its own header so it inherits.
Inherit,
/// Pin the auth it sends today (a header, or `# @no-auth` for `None`).
Keep(Option<String>),
Keep(Option<AuthHeader>),
}
fn apply_consolidation(state: &mut AppState, plan: ConsolidateAuth, fx: &mut Vec<Effect>) {
@@ -1583,14 +1579,15 @@ fn apply_consolidation(state: &mut AppState, plan: ConsolidateAuth, fx: &mut Vec
// The file may have changed since the plan was made.
match (&edit, auth_mode(&r)) {
(AuthFix::Inherit, AuthMode::Own(_)) => {
r.remove_header("Authorization");
folder::set_auth_mode(&mut r, &AuthMode::Inherit);
dropped += 1;
}
(AuthFix::Keep(before), AuthMode::Inherit) => {
match before {
Some(v) => r.set_header("Authorization", v.as_str()),
None => folder::set_no_auth(&mut r, true),
}
let mode = match before {
Some(h) => AuthMode::Own(h.clone()),
None => AuthMode::NoAuth,
};
folder::set_auth_mode(&mut r, &mode);
pinned += 1;
}
_ => continue,
@@ -1621,7 +1618,7 @@ fn apply_consolidation(state: &mut AppState, plan: ConsolidateAuth, fx: &mut Vec
settings.auth = FolderAuth::Header(target.clone());
ops.push(FileOp::Write(folder::settings_path(&plan.dir), settings.to_json()));
c.folders.insert(plan.dir.clone(), settings);
msg.push(txt::consolidate::set_folder(target));
msg.push(txt::consolidate::set_folder(&target.to_string()));
}
}
if dropped > 0 {