basic working version of approval screen

This commit is contained in:
jfmonty2 committed 2026-11-23 14:35:37 -05:00
1 parent 49c1e81e1d
commit 5f1ff5e4e8
9 files changed
+174 -24

No files matched your search

+57 -19
View File
@@ -1,5 +1,7 @@
use crate::credentials::{AwsCredential, AwsSessionCredential};
use crate::error::CreddyError;
use crate::session::AppSession;
use futures::channel::oneshot::{self, Sender};
use gpui_kit::{AsyncApp, Global};
use http_body_util::Full;
use hyper::body::{Bytes, Incoming};
@@ -20,6 +22,7 @@ use std::io;
use std::path::Path;
use std::time::Duration;
#[derive(Clone)]
pub enum CredentialRequest {
AwsGet {
name: Option<String>,
@@ -27,7 +30,26 @@ pub enum CredentialRequest {
}
}
pub struct PendingRequests(pub VecDeque<CredentialRequest>);
pub struct CredentialResponse {
approved: bool,
session: bool,
}
impl CredentialResponse {
pub fn yes_session() -> CredentialResponse {
CredentialResponse { approved: true, session: true }
}
pub fn yes_longterm() -> CredentialResponse {
CredentialResponse { approved: true, session: false }
}
pub fn no() -> CredentialResponse {
CredentialResponse { approved: false, session: true }
}
}
pub struct PendingRequests(pub VecDeque<(CredentialRequest, Sender<CredentialResponse>)>);
impl PendingRequests {
pub fn new() -> PendingRequests {
@@ -44,6 +66,13 @@ pub struct ErrorResponse {
message: String,
}
impl ErrorResponse {
fn into_http(&self) -> serde_json::Result<Response<Full<Bytes>>> {
let bytes = serde_json::to_vec(self)?;
Ok(Response::new(Full::new(Bytes::from(bytes))))
}
}
impl From<CreddyError> for ErrorResponse {
fn from(e: CreddyError) -> ErrorResponse {
let message = e.to_string();
@@ -101,39 +130,48 @@ async fn handle(
app: AsyncApp,
) -> Result<Response<Full<Bytes>>, Infallible> {
let (parts, _body) = req.into_parts();
let response = match (parts.method, parts.uri.path()) {
(Method::GET, "/ping") => text(StatusCode::OK, "pong"),
(Method::GET, "/aws") => handle_aws_get(parts.uri),
(Method::GET, "/aws") => handle_aws_get(parts.uri, app).await,
(_, p) => text(StatusCode::NOT_FOUND, &format!("Not found: {p}")),
};
Ok(response)
match response {
Ok(r) => Ok(r),
Err(e) => Ok(ErrorResponse::from(e).into_http().unwrap()),
}
}
fn handle_aws_get(uri: Uri) -> Response<Full<Bytes>> {
async fn handle_aws_get(uri: Uri, app: AsyncApp) -> Result<Response<Full<Bytes>>, CreddyError> {
let qs = uri.query().unwrap_or("").as_bytes();
let params: HashMap<_, _> = form_urlencoded::parse(qs).collect();
let req = CredentialRequest::AwsGet{
name: params.get("name").map(|n| n.to_string()),
session: params.get("session").map(|s| s == "true").unwrap_or(false),
};
let name = params.get("name").map(|n| n.to_string());
let session = params.get("session").map(|s| s == "true").unwrap_or(false);
let req = CredentialRequest::AwsGet { name: name.clone(), session };
// todo: actually get credentials from the rest of the app
let (sender, receiver) = oneshot::channel::<CredentialResponse>();
app.update_global::<PendingRequests, _>(|pending, _cx| pending.0.push_back((req.clone(), sender)));
let creds = AwsCredential {
id: uuid::uuid!("b448d315-fe82-41a9-9070-e0977e8dfa0c"),
name: "test".into(),
access_key_id: "AKIAWHATEVER".into(),
secret_access_key: "VERYSECRETSECUREVALUE".into(),
is_default: true,
};
let resp = receiver.await.unwrap_or_else(|_| CredentialResponse::no());
if !resp.approved {
return Err(CreddyError::RequestDenied);
}
// eventually: let session = AppSession::wait_for_unlock().await; or something like that
let creds = app.read_global::<AppSession, _>(|session, cx| {
let AppSession::Unlocked(db) = session else { unreachable!() };
db.load_aws_credential(&name.unwrap_or_else(|| "default".into()))
})?;
// todo: session credential handling
let body = serde_json::to_vec(&creds).unwrap();
Response::new(Full::new(Bytes::from(body)))
Ok(Response::new(Full::new(Bytes::from(body))))
}
fn text(status: StatusCode, body: &str) -> Response<Full<Bytes>> {
fn text(status: StatusCode, body: &str) -> Result<Response<Full<Bytes>>, CreddyError> {
let mut response = Response::new(Full::new(Bytes::from(body.to_owned())));
*response.status_mut() = status;
response
Ok(response)
}