wait for unlock before responding to credentials request
This commit is contained in:
1 parent
5f1ff5e4e8
commit
009eec764e
2 files changed
+47
-5
No files matched your search
+38
-1
@@ -1,6 +1,6 @@
|
|||||||
use crate::config::AppConfig;
|
use crate::config::AppConfig;
|
||||||
use crate::db::Db;
|
use crate::db::Db;
|
||||||
use gpui_kit::Global;
|
use gpui_kit::{AsyncApp, Global};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum AppSession {
|
pub enum AppSession {
|
||||||
@@ -17,6 +17,22 @@ impl AppSession {
|
|||||||
AppSession::Empty
|
AppSession::Empty
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resolves once the session is unlocked, which is immediately if it already is.
|
||||||
|
pub async fn wait_for_unlock(cx: &AsyncApp) {
|
||||||
|
let (tx, rx) = smol::channel::bounded(1);
|
||||||
|
let _subscription = cx.update(|cx| {
|
||||||
|
cx.observe_global::<AppSession>(move |_| {
|
||||||
|
// a full channel already means "check again", so a failed send loses nothing
|
||||||
|
let _ = tx.try_send(());
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
while !cx.read_global::<AppSession, _>(|session, _| matches!(session, AppSession::Unlocked(_))) {
|
||||||
|
// the subscription owns the sender, and outlives this loop
|
||||||
|
rx.recv().await.expect("session observer was dropped");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Global for AppSession {}
|
impl Global for AppSession {}
|
||||||
@@ -25,6 +41,7 @@ impl Global for AppSession {}
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::tests::TestEnv;
|
use crate::tests::TestEnv;
|
||||||
|
use gpui_kit::TestAppContext;
|
||||||
use std::assert_matches;
|
use std::assert_matches;
|
||||||
use testresult::TestResult;
|
use testresult::TestResult;
|
||||||
|
|
||||||
@@ -44,4 +61,24 @@ mod tests {
|
|||||||
assert_matches!(session, AppSession::Locked);
|
assert_matches!(session, AppSession::Locked);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[gpui_kit::test]
|
||||||
|
fn test_wait_for_unlock(cx: &mut TestAppContext) {
|
||||||
|
let env = TestEnv::new().unwrap();
|
||||||
|
let db = env.db().unwrap();
|
||||||
|
cx.update(|cx| cx.set_global(AppSession::Locked));
|
||||||
|
|
||||||
|
let task = cx.spawn(async move |cx| AppSession::wait_for_unlock(&cx).await);
|
||||||
|
cx.run_until_parked();
|
||||||
|
assert!(!task.is_ready());
|
||||||
|
|
||||||
|
// a change that doesn't unlock shouldn't end the wait
|
||||||
|
cx.update(|cx| cx.set_global(AppSession::Locked));
|
||||||
|
cx.run_until_parked();
|
||||||
|
assert!(!task.is_ready());
|
||||||
|
|
||||||
|
cx.update(|cx| cx.set_global(AppSession::Unlocked(db)));
|
||||||
|
cx.run_until_parked();
|
||||||
|
assert!(task.is_ready());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -148,19 +148,24 @@ async fn handle_aws_get(uri: Uri, app: AsyncApp) -> Result<Response<Full<Bytes>>
|
|||||||
let params: HashMap<_, _> = form_urlencoded::parse(qs).collect();
|
let params: HashMap<_, _> = form_urlencoded::parse(qs).collect();
|
||||||
let name = params.get("name").map(|n| n.to_string());
|
let name = params.get("name").map(|n| n.to_string());
|
||||||
let session = params.get("session").map(|s| s == "true").unwrap_or(false);
|
let session = params.get("session").map(|s| s == "true").unwrap_or(false);
|
||||||
|
|
||||||
|
// we need to hang on to `name` so we can use it to retrieve the credential later
|
||||||
let req = CredentialRequest::AwsGet { name: name.clone(), session };
|
let req = CredentialRequest::AwsGet { name: name.clone(), session };
|
||||||
|
|
||||||
let (sender, receiver) = oneshot::channel::<CredentialResponse>();
|
let (sender, receiver) = oneshot::channel::<CredentialResponse>();
|
||||||
app.update_global::<PendingRequests, _>(|pending, _cx| pending.0.push_back((req.clone(), sender)));
|
app.update_global::<PendingRequests, _>(|pending, _cx| pending.0.push_back((req, sender)));
|
||||||
|
|
||||||
let resp = receiver.await.unwrap_or_else(|_| CredentialResponse::no());
|
let resp = receiver.await.unwrap_or_else(|_| CredentialResponse::no());
|
||||||
if !resp.approved {
|
if !resp.approved {
|
||||||
return Err(CreddyError::RequestDenied);
|
return Err(CreddyError::RequestDenied);
|
||||||
}
|
}
|
||||||
|
|
||||||
// eventually: let session = AppSession::wait_for_unlock().await; or something like that
|
// it's possible to approve a request while the app is still locked, so we have to account for that
|
||||||
let creds = app.read_global::<AppSession, _>(|session, cx| {
|
AppSession::wait_for_unlock(&app).await;
|
||||||
let AppSession::Unlocked(db) = session else { unreachable!() };
|
let creds = app.read_global::<AppSession, _>(|session, _cx| {
|
||||||
|
let AppSession::Unlocked(db) = session else {
|
||||||
|
unreachable!("App re-locked immediately after unlock")
|
||||||
|
};
|
||||||
db.load_aws_credential(&name.unwrap_or_else(|| "default".into()))
|
db.load_aws_credential(&name.unwrap_or_else(|| "default".into()))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user