finish extremely basic implementation of docker credentials

This commit is contained in:
2024-11-25 07:57:59 -05:00
parent 8bcdc5420a
commit 9bc9cb56c1
6 changed files with 64 additions and 26 deletions

View File

@ -7,7 +7,8 @@ use crate::clientinfo::{self, Client};
use crate::credentials::{
Credential,
CredentialRecord,
Crypto
Crypto,
DockerCredential
};
use crate::errors::*;
use crate::ipc::{Approval, AwsRequestNotification, RequestNotificationDetail, RequestResponse};
@ -55,13 +56,16 @@ async fn handle(
CliRequest::GetAwsCredential{ name, base } => get_aws_credentials(
name, base, client, app_handle, waiter
).await,
CliRequest::GetDockerCredential{ server_url } => get_docker_credentials (
CliRequest::GetDockerCredential{ server_url } => get_docker_credential (
server_url, client, app_handle, waiter
).await,
CliRequest::SaveCredential{ name, is_default, credential } => save_credential(
name, is_default, credential, app_handle
CliRequest::StoreDockerCredential(docker_credential) => store_docker_credential(
docker_credential, app_handle, waiter
).await,
CliRequest::InvokeShortcut(action) => invoke_shortcut(action).await,
CliRequest::EraseDockerCredential { server_url } => erase_docker_credential(
server_url, app_handle, waiter
).await,
CliRequest::InvokeShortcut{ action } => invoke_shortcut(action).await,
};
// doesn't make sense to send the error to the client if the client has already left
@ -106,7 +110,7 @@ async fn get_aws_credentials(
}
}
async fn get_docker_credentials(
async fn get_docker_credential(
server_url: String,
client: Client,
app_handle: AppHandle,
@ -126,24 +130,39 @@ async fn get_docker_credentials(
}
}
pub async fn save_credential(
name: String,
is_default: bool,
credential: Credential,
async fn store_docker_credential(
docker_credential: DockerCredential,
app_handle: AppHandle,
_waiter: CloseWaiter<'_>,
) -> Result<CliResponse, HandlerError> {
let state = app_handle.state::<AppState>();
// eventually ask the frontend to unlock here
// eventually ask the frontend to confirm here
// a bit weird but convenient
let random_bytes = Crypto::salt();
let id = Uuid::from_slice(&random_bytes[..16]).unwrap();
let record = CredentialRecord {
id, name, is_default, credential
id,
name: docker_credential.server_url.clone(),
is_default: false,
credential: Credential::Docker(docker_credential)
};
state.save_credential(record).await?;
Ok(CliResponse::Empty)
}
async fn erase_docker_credential(
server_url: String,
app_handle: AppHandle,
_waiter: CloseWaiter<'_>
) -> Result<CliResponse, HandlerError> {
let state = app_handle.state::<AppState>();
// eventually ask the frontend to confirm here
state.delete_credential_by_name(&server_url).await?;
Ok(CliResponse::Empty)
}