|
@@ -14,7 +14,7 @@ use tauri::{AppHandle, Emitter};
|
|
|
use x25519_dalek::{EphemeralSecret, PublicKey};
|
|
|
|
|
|
use crate::{
|
|
|
- error::{AppError, AppResult},
|
|
|
+ error::{AgentError, AgentResult},
|
|
|
model::{AuthKeyResponse, ConfirmedRegistrationRequest, HandshakeResponse},
|
|
|
state::{AppState, Registration},
|
|
|
util::EncryptedJson,
|
|
@@ -32,7 +32,7 @@ fn generate_otp() -> String {
|
|
|
|
|
|
pub async fn handshake(
|
|
|
State((_, app_handle)): State<(Arc<AppState>, AppHandle)>,
|
|
|
-) -> AppResult<Json<HandshakeResponse>> {
|
|
|
+) -> AgentResult<Json<HandshakeResponse>> {
|
|
|
Ok(Json(HandshakeResponse {
|
|
|
status: "success".to_string(),
|
|
|
__hoppscotch__agent__: true,
|
|
@@ -42,7 +42,7 @@ pub async fn handshake(
|
|
|
|
|
|
pub async fn receive_registration(
|
|
|
State((state, app_handle)): State<(Arc<AppState>, AppHandle)>,
|
|
|
-) -> AppResult<Json<serde_json::Value>> {
|
|
|
+) -> AgentResult<Json<serde_json::Value>> {
|
|
|
let otp = generate_otp();
|
|
|
|
|
|
let mut active_registration_code = state.active_registration_code.write().await;
|
|
@@ -57,7 +57,7 @@ pub async fn receive_registration(
|
|
|
|
|
|
app_handle
|
|
|
.emit("registration_received", otp)
|
|
|
- .map_err(|_| AppError::InternalServerError)?;
|
|
|
+ .map_err(|_| AgentError::InternalServerError)?;
|
|
|
|
|
|
Ok(Json(
|
|
|
json!({ "message": "Registration received and stored" }),
|
|
@@ -67,12 +67,12 @@ pub async fn receive_registration(
|
|
|
pub async fn verify_registration(
|
|
|
State((state, app_handle)): State<(Arc<AppState>, AppHandle)>,
|
|
|
Json(confirmed_registration): Json<ConfirmedRegistrationRequest>,
|
|
|
-) -> AppResult<Json<AuthKeyResponse>> {
|
|
|
+) -> AgentResult<Json<AuthKeyResponse>> {
|
|
|
state
|
|
|
.validate_registration(&confirmed_registration.registration)
|
|
|
.await
|
|
|
.then_some(())
|
|
|
- .ok_or(AppError::InvalidRegistration)?;
|
|
|
+ .ok_or(AgentError::InvalidRegistration)?;
|
|
|
|
|
|
let auth_key = Uuid::new_v4().to_string();
|
|
|
let created_at = Utc::now();
|
|
@@ -85,9 +85,9 @@ pub async fn verify_registration(
|
|
|
let their_public_key = {
|
|
|
let public_key_slice: &[u8; 32] =
|
|
|
&base16::decode(&confirmed_registration.client_public_key_b16)
|
|
|
- .map_err(|_| AppError::InvalidClientPublicKey)?[0..32]
|
|
|
+ .map_err(|_| AgentError::InvalidClientPublicKey)?[0..32]
|
|
|
.try_into()
|
|
|
- .map_err(|_| AppError::InvalidClientPublicKey)?;
|
|
|
+ .map_err(|_| AgentError::InvalidClientPublicKey)?;
|
|
|
|
|
|
PublicKey::from(public_key_slice.to_owned())
|
|
|
};
|
|
@@ -111,7 +111,7 @@ pub async fn verify_registration(
|
|
|
|
|
|
app_handle
|
|
|
.emit("authenticated", &auth_payload)
|
|
|
- .map_err(|_| AppError::InternalServerError)?;
|
|
|
+ .map_err(|_| AgentError::InternalServerError)?;
|
|
|
|
|
|
Ok(Json(AuthKeyResponse {
|
|
|
auth_key,
|
|
@@ -125,22 +125,22 @@ pub async fn run_request<T>(
|
|
|
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
|
|
headers: HeaderMap,
|
|
|
body: Bytes,
|
|
|
-) -> AppResult<EncryptedJson<ResponseWithMetadata>> {
|
|
|
+) -> AgentResult<EncryptedJson<ResponseWithMetadata>> {
|
|
|
let nonce = headers
|
|
|
.get("X-Hopp-Nonce")
|
|
|
- .ok_or(AppError::Unauthorized)?
|
|
|
+ .ok_or(AgentError::Unauthorized)?
|
|
|
.to_str()
|
|
|
- .map_err(|_| AppError::Unauthorized)?;
|
|
|
+ .map_err(|_| AgentError::Unauthorized)?;
|
|
|
|
|
|
let req: RequestWithMetadata = state
|
|
|
.validate_access_and_get_data(auth_header.token(), nonce, &body)
|
|
|
- .ok_or(AppError::Unauthorized)?;
|
|
|
+ .ok_or(AgentError::Unauthorized)?;
|
|
|
|
|
|
let req_id = req.req_id;
|
|
|
|
|
|
let reg_info = state
|
|
|
.get_registration_info(auth_header.token())
|
|
|
- .ok_or(AppError::Unauthorized)?;
|
|
|
+ .ok_or(AgentError::Unauthorized)?;
|
|
|
|
|
|
let cancel_token = tokio_util::sync::CancellationToken::new();
|
|
|
state.add_cancellation_token(req.req_id, cancel_token.clone());
|
|
@@ -164,11 +164,11 @@ pub async fn run_request<T>(
|
|
|
res = tokio::task::spawn_blocking(move || hoppscotch_relay::run_request_task(&req, cancel_token_clone)) => {
|
|
|
match res {
|
|
|
Ok(task_result) => Ok(task_result?),
|
|
|
- Err(_) => Err(AppError::InternalServerError),
|
|
|
+ Err(_) => Err(AgentError::InternalServerError),
|
|
|
}
|
|
|
},
|
|
|
_ = cancel_token.cancelled() => {
|
|
|
- Err(AppError::RequestCancelled)
|
|
|
+ Err(AgentError::RequestCancelled)
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -190,7 +190,7 @@ pub async fn run_request<T>(
|
|
|
pub async fn registered_handshake(
|
|
|
State((state, _)): State<(Arc<AppState>, AppHandle)>,
|
|
|
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
|
|
-) -> AppResult<EncryptedJson<serde_json::Value>> {
|
|
|
+) -> AgentResult<EncryptedJson<serde_json::Value>> {
|
|
|
let reg_info = state.get_registration_info(auth_header.token());
|
|
|
|
|
|
match reg_info {
|
|
@@ -198,7 +198,7 @@ pub async fn registered_handshake(
|
|
|
key_b16: reg.shared_secret_b16,
|
|
|
data: json!(true),
|
|
|
}),
|
|
|
- None => Err(AppError::Unauthorized),
|
|
|
+ None => Err(AgentError::Unauthorized),
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -206,15 +206,15 @@ pub async fn cancel_request<T>(
|
|
|
State((state, _app_handle)): State<(Arc<AppState>, T)>,
|
|
|
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
|
|
Path(req_id): Path<usize>,
|
|
|
-) -> AppResult<Json<serde_json::Value>> {
|
|
|
+) -> AgentResult<Json<serde_json::Value>> {
|
|
|
if !state.validate_access(auth_header.token()) {
|
|
|
- return Err(AppError::Unauthorized);
|
|
|
+ return Err(AgentError::Unauthorized);
|
|
|
}
|
|
|
|
|
|
if let Some((_, token)) = state.remove_cancellation_token(req_id) {
|
|
|
token.cancel();
|
|
|
Ok(Json(json!({"message": "Request cancelled successfully"})))
|
|
|
} else {
|
|
|
- Err(AppError::RequestNotFound)
|
|
|
+ Err(AgentError::RequestNotFound)
|
|
|
}
|
|
|
}
|