SERVER HANDSHAKES ARE WORKING

This commit is contained in:
sam 2024-01-19 00:28:02 +01:00
parent 041531e88a
commit bfb0a1d1b0
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
7 changed files with 157 additions and 42 deletions

View file

@ -30,7 +30,7 @@ pub async fn get<R: DeserializeOwned>(
self_domain: &str,
host: &str,
path: &str,
user_id: Option<&str>,
user_id: Option<String>,
) -> Result<R> {
let (signature, date) = build_signature(
private_key,
@ -61,7 +61,7 @@ pub async fn post<T: Serialize, R: DeserializeOwned>(
self_domain: &str,
host: &str,
path: &str,
user_id: Option<&str>,
user_id: Option<String>,
body: &T,
) -> Result<R> {
let body = serde_json::to_string(body)?;

View file

@ -13,7 +13,7 @@ pub fn build_signature(
host: &str,
request_path: &str,
content_length: Option<usize>,
user_id: Option<&str>,
user_id: Option<String>,
) -> (String, DateTime<Utc>) {
let mut rng = rand::thread_rng();
let signing_key = SigningKey::<Sha256>::new(private_key.clone());
@ -32,13 +32,13 @@ fn plaintext_string(
host: &str,
request_path: &str,
content_length: Option<usize>,
user_id: Option<&str>,
user_id: Option<String>,
) -> String {
let raw_time = format_date(time);
let raw_content_length = content_length
.map(|i| i.to_string())
.unwrap_or("".to_owned());
let raw_user_id = user_id.unwrap_or("");
let raw_user_id = user_id.unwrap_or("".into());
format!(
"{}:{}:{}:{}:{}",
@ -61,14 +61,14 @@ pub fn verify_signature(
host: &str, // from Host header, verify that it's actually your host
request_path: &str, // from router
content_length: Option<usize>, // from Content-Length header
user_id: Option<&str>, // from X-Foxchat-User header
user_id: Option<String>, // from X-Foxchat-User header
) -> Result<bool> {
let verifying_key = VerifyingKey::<Sha256>::new(public_key.clone());
let now = Utc::now();
if (now - Duration::minutes(1)) < time {
if (now - Duration::minutes(1)) > time {
return Err(FoxError::SignatureDateOutOfRange("request was made too long ago").into());
} else if (now + Duration::minutes(1)) > time {
} else if (now + Duration::minutes(1)) < time {
return Err(FoxError::SignatureDateOutOfRange("request was made in the future").into());
}

View file

@ -86,10 +86,30 @@ impl From<FoxError> for ApiError {
code: ErrorCode::ObjectNotFound,
message: "Object not found".into(),
},
FoxError::SignatureDateOutOfRange(s) => ApiError {
status: StatusCode::BAD_REQUEST,
code: ErrorCode::InvalidSignature,
message: format!("Signature date out of range: {}", s),
},
FoxError::ResponseNotOk => ApiError {
status: StatusCode::INTERNAL_SERVER_ERROR,
code: ErrorCode::InternalServerError,
message: "Error response from remote server".into(),
},
FoxError::InvalidServer => ApiError {
status: StatusCode::BAD_REQUEST,
code: ErrorCode::InvalidServer,
message: "Invalid domain or server".into(),
message: "Invalid remote server".into(),
},
FoxError::InvalidHeader => ApiError {
status: StatusCode::BAD_REQUEST,
code: ErrorCode::InvalidHeader,
message: "Invalid header value".into(),
},
FoxError::InvalidDate => ApiError {
status: StatusCode::BAD_REQUEST,
code: ErrorCode::InvalidHeader,
message: "Invalid date value in header".into(),
},
FoxError::MissingSignature => ApiError {
status: StatusCode::BAD_REQUEST,
@ -101,11 +121,6 @@ impl From<FoxError> for ApiError {
code: ErrorCode::InvalidSignature,
message: "Invalid signature".into(),
},
_ => ApiError {
status: StatusCode::INTERNAL_SERVER_ERROR,
code: ErrorCode::InternalServerError,
message: "Internal server error".into(),
},
}
}
}