mirror of
https://github.com/JoshiCodes/knock.git
synced 2026-12-04 15:09:41 +01:00
OAuth flows can be cancelled; discovery no longer waits for a pending login
- Only login, logout, refresh and revoke queue behind each other; discovery, test, userinfo and introspection run at once - Cancel in the OAuth panel (and Cancel on the request) stops the flow, frees the callback port and the login lock - Undefined variables in issuer, endpoints or client id are reported before any request is sent - Keyring commands run off the UI thread, so a KWallet prompt cannot freeze the window - One click on a collection opens its settings; the chevron folds it - Until an environment is picked, local (or the first one) is active; the environment editor says when the shown one is not active
This commit is contained in:
@@ -169,6 +169,22 @@ impl<'a> Flow<'a> {
|
||||
if c.grant == Grant::AuthorizationCode && c.issuer.is_empty() && c.authorize_url.is_empty() {
|
||||
problems.push("the authorization code flow needs an authorize URL or an issuer".to_string());
|
||||
}
|
||||
// An undefined variable leaves `{{BASE}}` in a URL; fail here with the
|
||||
// reason instead of sending a request to a broken address.
|
||||
for (label, v) in [
|
||||
("issuer", &c.issuer),
|
||||
("authorize URL", &c.authorize_url),
|
||||
("token URL", &c.token_url),
|
||||
("device URL", &c.device_url),
|
||||
("client id", &c.client_id),
|
||||
] {
|
||||
if let Some(start) = v.find("{{") {
|
||||
let name = v[start + 2..].split("}}").next().unwrap_or("").trim();
|
||||
problems.push(format!(
|
||||
"the {label} uses {{{{{name}}}}}, which is not defined. Pick the environment that defines it (Ctrl+E) or define it in the collection"
|
||||
));
|
||||
}
|
||||
}
|
||||
if c.grant == Grant::Password && c.username.is_empty() {
|
||||
problems.push("username is empty".to_string());
|
||||
}
|
||||
@@ -823,6 +839,10 @@ pub async fn action(cfg: &OauthConfig, key: &str, hooks: &dyn FlowHooks, what: A
|
||||
let mut f = Flow { cfg, hooks, http: Http::new(cfg.verify_tls), steps: Vec::new(), warnings: Vec::new(), meta: None };
|
||||
if matches!(what, Action::Discovery) {
|
||||
DISCOVERY.lock().unwrap().get_or_insert_with(HashMap::new).remove(&cfg.issuer);
|
||||
if cfg.issuer.contains("{{") {
|
||||
f.check_config();
|
||||
return FlowResult { steps: f.steps, warnings: f.warnings, ..Default::default() };
|
||||
}
|
||||
f.discover().await;
|
||||
return FlowResult { steps: f.steps, warnings: f.warnings, ..Default::default() };
|
||||
}
|
||||
|
||||
@@ -214,3 +214,23 @@ async fn errors_are_explained() {
|
||||
assert!(r.steps.iter().any(|s| s.id == "token" && s.summary.contains("invalid_grant") && s.status == StepStatus::Warning));
|
||||
assert_eq!(r.token.as_deref(), Some("at-cc"), "{:#?}", r.steps);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn undefined_variable_in_issuer_is_explained() {
|
||||
let c = cfg(serde_json::json!({ "type": "oidc", "issuer": "{{BASE}}/realms/default/", "client_id": "knock" }));
|
||||
let hooks = TestHooks::default();
|
||||
let r = oauth::action(&c, &c.store_key("c", None), &hooks, oauth::Action::Discovery).await;
|
||||
let failed = r.steps.iter().find(|s| s.status == StepStatus::Error).expect("error step");
|
||||
assert!(failed.summary.contains("{{BASE}}"), "{}", failed.summary);
|
||||
assert!(!r.steps.iter().any(|s| s.exchange.is_some()), "no request may be sent");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn issuer_with_trailing_slash_discovers() {
|
||||
let st = Arc::new(Mutex::new(Provider::default()));
|
||||
let base = start_provider(st).await;
|
||||
let c = cfg(serde_json::json!({ "type": "oauth2", "issuer": format!("{base}/"), "client_id": "knock" }));
|
||||
let hooks = TestHooks::default();
|
||||
let r = oauth::action(&c, &c.store_key("c2", None), &hooks, oauth::Action::Discovery).await;
|
||||
assert!(r.steps.iter().any(|s| s.id == "discovery" && s.status == StepStatus::Ok), "{:#?}", r.steps);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user