258 lines
8.5 KiB
Rust
258 lines
8.5 KiB
Rust
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
//! Team sign-in without Authelia (T4): the setup link makes the founding admin, invites make
|
|
//! members, a session cookie identifies them, members cannot do admin things, and browser
|
|
//! writes need the UI header. Runs on Windows and Linux (control in this process, no edge).
|
|
|
|
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
|
|
|
use std::net::SocketAddr;
|
|
use std::time::Duration;
|
|
|
|
use ianitor_control::Config;
|
|
use ianitor_control::team::Team;
|
|
use ianitor_testkit::more::{JsonResp, tcp_json};
|
|
use ianitor_testkit::proc::{TempDir, free_port, local, wait_for_port};
|
|
use serde_json::{Value, json};
|
|
|
|
fn s(p: &std::path::Path) -> String {
|
|
p.to_string_lossy().replace('\\', "/")
|
|
}
|
|
|
|
async fn start() -> (TempDir, SocketAddr, tokio::sync::oneshot::Sender<()>) {
|
|
let dir = TempDir::new("team");
|
|
std::fs::create_dir_all(dir.join("state")).unwrap();
|
|
let api = local(free_port());
|
|
let text = format!(
|
|
"listen = \"{api}\"\nstate_dir = \"{st}\"\n\n[routes]\nrepo = \"{repo}\"\n\n[edge]\nconfig = \"{d}/edge.json\"\nshare_revocations = \"{d}/rev.json\"\n\n[caddy]\nconfig = \"{d}/caddy.json\"\n\n[compile]\nhttp_listen = \"127.0.0.1:{h}\"\nhttps_listen = \"127.0.0.1:{hs}\"\ncert_dir = \"{d}\"\nstate_dir = \"{d}\"\naccess_log = \"{d}/a.log\"\ncaddy_credentials_dir = \"{d}\"\n",
|
|
st = s(&dir.join("state")),
|
|
repo = s(&dir.join("routes.git")),
|
|
d = s(dir.path()),
|
|
h = free_port(),
|
|
hs = free_port(),
|
|
);
|
|
std::fs::write(dir.join("control.toml"), text).unwrap();
|
|
let cfg = Config::load(&dir.join("control.toml")).unwrap();
|
|
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
|
|
tokio::spawn(ianitor_control::serve(cfg, async {
|
|
let _ = rx.await;
|
|
}));
|
|
assert!(wait_for_port(api, Duration::from_secs(20)));
|
|
(dir, api, tx)
|
|
}
|
|
|
|
async fn req(api: SocketAddr, m: &str, p: &str, h: &[(&str, &str)], b: Option<Value>) -> JsonResp {
|
|
tcp_json(api, m, p, h, b.as_ref()).await.unwrap()
|
|
}
|
|
|
|
fn cookie_of(r: &JsonResp) -> String {
|
|
let set = r
|
|
.headers
|
|
.iter()
|
|
.find(|(n, _)| n == "set-cookie")
|
|
.map(|(_, v)| v.clone())
|
|
.expect("a Set-Cookie");
|
|
for part in ["HttpOnly", "Secure", "SameSite=Strict", "Path=/"] {
|
|
assert!(set.contains(part), "{set}");
|
|
}
|
|
assert!(set.starts_with("__Host-ianitor=ise_"), "{set}");
|
|
set.split(';').next().unwrap().to_owned()
|
|
}
|
|
|
|
async fn sign_in(api: SocketAddr, code: &str, name: &str) -> (JsonResp, String) {
|
|
let r = req(
|
|
api,
|
|
"POST",
|
|
"/api/v1/session",
|
|
&[("X-Ianitor-UI", "1"), ("Sec-Fetch-Site", "same-origin")],
|
|
Some(json!({"code": code, "name": name, "device": "Laptop"})),
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 200, "{}", r.text);
|
|
let c = cookie_of(&r);
|
|
(r, c)
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
|
async fn a_setup_link_then_invites_sign_a_team_in() {
|
|
let (dir, api, _stop) = start().await;
|
|
let (setup, _) = Team::new(&dir.join("state")).setup_code().unwrap();
|
|
|
|
// The door refuses bad codes, other sites and pages that are not the UI.
|
|
for (headers, code) in [
|
|
(vec![("X-Ianitor-UI", "1")], "isc_wrong"),
|
|
(vec![], setup.as_str()),
|
|
(
|
|
vec![("X-Ianitor-UI", "1"), ("Sec-Fetch-Site", "cross-site")],
|
|
setup.as_str(),
|
|
),
|
|
] {
|
|
let r = req(
|
|
api,
|
|
"POST",
|
|
"/api/v1/session",
|
|
&headers,
|
|
Some(json!({"code": code})),
|
|
)
|
|
.await;
|
|
assert!(
|
|
r.status == 401 || r.status == 403,
|
|
"{headers:?}: {}",
|
|
r.text
|
|
);
|
|
assert!(!r.headers.iter().any(|(n, _)| n == "set-cookie"));
|
|
}
|
|
|
|
let (r, mark) = sign_in(api, &setup, "Mark").await;
|
|
assert_eq!(r.json["member"]["role"], "admin");
|
|
let ov = req(api, "GET", "/api/v1/overview", &[("Cookie", &mark)], None).await;
|
|
assert_eq!(ov.status, 200, "{}", ov.text);
|
|
assert_eq!(ov.json["viewer"]["via"], "session");
|
|
assert_eq!(ov.json["viewer"]["name"], "Mark");
|
|
// The setup link worked once.
|
|
let r = req(
|
|
api,
|
|
"POST",
|
|
"/api/v1/session",
|
|
&[("X-Ianitor-UI", "1")],
|
|
Some(json!({"code": setup})),
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 401, "{}", r.text);
|
|
|
|
// A browser write without the UI header is refused (CSRF), with it it works.
|
|
let r = req(
|
|
api,
|
|
"POST",
|
|
"/api/v1/team/invites",
|
|
&[("Cookie", &mark)],
|
|
Some(json!({"name": "Petra"})),
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 403, "{}", r.text);
|
|
assert_eq!(r.json["error"]["code"], "request.csrf");
|
|
let r = req(
|
|
api,
|
|
"POST",
|
|
"/api/v1/team/invites",
|
|
&[("Cookie", &mark), ("X-Ianitor-UI", "1")],
|
|
Some(json!({"name": "Petra", "role": "member"})),
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 201, "{}", r.text);
|
|
let code = r.json["link_fragment"]
|
|
.as_str()
|
|
.unwrap()
|
|
.strip_prefix("#signin=")
|
|
.unwrap()
|
|
.to_owned();
|
|
|
|
// Petra is a member: hosts yes, tokens, the team and the engine no.
|
|
let (_, petra) = sign_in(api, &code, "Not Petra").await;
|
|
let team = req(api, "GET", "/api/v1/team", &[("Cookie", &petra)], None).await;
|
|
assert_eq!(team.json["me"]["name"], "Petra", "{}", team.text);
|
|
for (m, p, b) in [
|
|
("GET", "/api/v1/tokens", None),
|
|
("POST", "/api/v1/tokens", Some(json!({"name": "x"}))),
|
|
(
|
|
"POST",
|
|
"/api/v1/team/invites",
|
|
Some(json!({"name": "Eve", "role": "admin"})),
|
|
),
|
|
("POST", "/api/v1/engine", Some(json!({"target": "caddy"}))),
|
|
] {
|
|
let r = req(api, m, p, &[("Cookie", &petra), ("X-Ianitor-UI", "1")], b).await;
|
|
assert_eq!(r.status, 403, "{m} {p}: {}", r.text);
|
|
assert_eq!(r.json["error"]["code"], "team.admin_only", "{m} {p}");
|
|
}
|
|
let r = req(
|
|
api,
|
|
"POST",
|
|
"/api/v1/hosts",
|
|
&[("Cookie", &petra), ("X-Ianitor-UI", "1")],
|
|
Some(
|
|
json!({"host": {"hostname": "wiki.example.test", "upstreams": ["http://10.0.0.2:80"]}}),
|
|
),
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 202, "{}", r.text);
|
|
|
|
// A member records their Tessera root; the list shows its match code.
|
|
let root = "BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc";
|
|
let r = req(
|
|
api,
|
|
"PUT",
|
|
"/api/v1/team/me/root",
|
|
&[("Cookie", &petra), ("X-Ianitor-UI", "1")],
|
|
Some(json!({"tessera_root": root})),
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 200, "{}", r.text);
|
|
let team = req(api, "GET", "/api/v1/team", &[("Cookie", &mark)], None).await;
|
|
let p = team.json["members"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.find(|m| m["name"] == "Petra")
|
|
.unwrap()
|
|
.clone();
|
|
assert_eq!(p["match_code"], "016 810");
|
|
|
|
// Removing Petra signs her out; a forged cookie gets nothing; the last admin stays.
|
|
let id = p["id"].as_str().unwrap();
|
|
let r = req(
|
|
api,
|
|
"DELETE",
|
|
&format!("/api/v1/team/members/{id}"),
|
|
&[("Cookie", &mark), ("X-Ianitor-UI", "1")],
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 200, "{}", r.text);
|
|
let r = req(api, "GET", "/api/v1/overview", &[("Cookie", &petra)], None).await;
|
|
assert_eq!(r.status, 401, "{}", r.text);
|
|
let r = req(
|
|
api,
|
|
"GET",
|
|
"/api/v1/overview",
|
|
&[("Cookie", "__Host-ianitor=ise_forged")],
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 401);
|
|
let me = team.json["members"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.find(|m| m["name"] == "Mark")
|
|
.unwrap()["id"]
|
|
.as_str()
|
|
.unwrap()
|
|
.to_owned();
|
|
let r = req(
|
|
api,
|
|
"DELETE",
|
|
&format!("/api/v1/team/members/{me}"),
|
|
&[("Cookie", &mark), ("X-Ianitor-UI", "1")],
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 409, "{}", r.text);
|
|
assert_eq!(r.json["error"]["code"], "team.last_admin");
|
|
|
|
// Signing out ends this browser's session.
|
|
let r = req(
|
|
api,
|
|
"POST",
|
|
"/api/v1/session/end",
|
|
&[("Cookie", &mark), ("X-Ianitor-UI", "1")],
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(r.status, 200, "{}", r.text);
|
|
let r = req(api, "GET", "/api/v1/overview", &[("Cookie", &mark)], None).await;
|
|
assert_eq!(r.status, 401);
|
|
}
|