add credentials entry view

This commit is contained in:
Joseph Montanaro 2022-12-13 21:50:34 -08:00
parent 9055fa41aa
commit 67705aa2d1
6 changed files with 83 additions and 33 deletions

View File

@ -36,7 +36,7 @@ pub fn get_clients(local_port: u16) -> Result<Vec<Client>, ClientInfoError> {
let mut clients = Vec::new();
let mut sys = System::new();
for p in get_associated_pids(local_port)? {
let pid = Pid::from(p as i32);
let pid = Pid::from(p as usize);
sys.refresh_process(pid);
let proc = sys.process(pid)
.ok_or(ClientInfoError::PidNotFound)?;

View File

@ -1,7 +1,7 @@
use serde::{Serialize, Deserialize};
use tauri::State;
use crate::state::{AppState, Session};
use crate::state::{AppState, Session, Credentials};
#[derive(Clone, Serialize, Deserialize)]
@ -43,8 +43,7 @@ pub fn respond(response: RequestResponse, app_state: State<'_, AppState>) -> Res
pub async fn unlock(passphrase: String, app_state: State<'_, AppState>) -> Result<(), String> {
app_state.decrypt(&passphrase)
.await
.map_err(|e| e.to_string())?;
Ok(())
.map_err(|e| e.to_string())
}
@ -58,3 +57,14 @@ pub fn get_session_status(app_state: State<'_, AppState>) -> String {
}
}
#[tauri::command]
pub async fn save_credentials(
credentials: Credentials,
passphrase: String,
app_state: State<'_, AppState>
) -> Result<(), String> {
app_state.save_creds(credentials, &passphrase)
.await
.map_err(|e| e.to_string())
}

View File

@ -1,6 +1,6 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use std::str::FromStr;
@ -14,31 +14,24 @@ mod storage;
fn main() {
let initial_state = match state::AppState::new() {
Ok(state) => state,
Err(e) => {eprintln!("{}", e); return;}
};
let initial_state = match state::AppState::new() {
Ok(state) => state,
Err(e) => {eprintln!("{}", e); return;}
};
tauri::Builder::default()
.manage(initial_state)
.invoke_handler(tauri::generate_handler![
ipc::unlock,
ipc::respond,
ipc::get_session_status,
])
.setup(|app| {
let addr = std::net::SocketAddrV4::from_str("127.0.0.1:12345").unwrap();
tauri::async_runtime::spawn(server::serve(addr, app.handle()));
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
// let addr = std::net::SocketAddrV4::from_str("127.0.0.1:12345").unwrap();
// let rt = Runtime::new().unwrap();
// rt.block_on(http::serve(addr)).unwrap();
// let creds = std::fs::read_to_string("credentials.json").unwrap();
// storage::save(&creds, "correct horse battery staple");
tauri::Builder::default()
.manage(initial_state)
.invoke_handler(tauri::generate_handler![
ipc::unlock,
ipc::respond,
ipc::get_session_status,
ipc::save_credentials,
])
.setup(|app| {
let addr = std::net::SocketAddrV4::from_str("127.0.0.1:12345").unwrap();
tauri::async_runtime::spawn(server::serve(addr, app.handle()));
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View File

@ -21,13 +21,19 @@ use crate::errors::*;
#[serde(untagged)]
pub enum Credentials {
LongLived {
#[serde(rename = "AccessKeyId")]
access_key_id: String,
#[serde(rename = "SecretAccessKey")]
secret_access_key: String,
},
ShortLived {
#[serde(rename = "AccessKeyId")]
access_key_id: String,
#[serde(rename = "SecretAccessKey")]
secret_access_key: String,
#[serde(rename = "Token")]
token: String,
#[serde(rename = "Expiration")]
expiration: String,
},
}

View File

@ -7,7 +7,7 @@
const dispatch = createEventDispatcher();
async function approve() {
let status = await invoke('get-session-status');
let status = await invoke('get_session_status');
if (status === 'unlocked') {
dispatch('navigate', {target: 'ShowApproved'});
}

View File

@ -0,0 +1,41 @@
<script>
import { onMount, createEventDispatcher } from 'svelte';
import { invoke } from '@tauri-apps/api/tauri';
export let appState;
const dispatch = createEventDispatcher;
let AccessKeyId, SecretAccessKey, passphrase
async function save() {
try {
console.log('Saving credentials.');
let credentials = {AccessKeyId, SecretAccessKey};
console.log(credentials);
await invoke('save_credentials', {credentials, passphrase});
if (appState.currentRequest) {
dispatch('navigate', {target: 'Approve'})
}
else {
dispatch('navigate', {target: Home})
}
}
catch (e) {
console.log("Error saving credentials:", e);
}
}
</script>
<form action="#" on:submit|preventDefault="{save}">
<div class="text-gray-200">AWS Access Key ID</div>
<input class="text-gray-200 bg-zinc-800" type="text" bind:value="{AccessKeyId}" />
<div class="text-gray-200">AWS Secret Access Key</div>
<input class="text-gray-200 bg-zinc-800" type="text" bind:value="{SecretAccessKey}" />
<div class="text-gray-200">Passphrase</div>
<input class="text-gray-200 bg-zinc-800" type="text" bind:value="{passphrase}" />
<input class="text-gray-200" type="submit" />
</form>