Skip to content

Commit

Permalink
Fix post headers
Browse files Browse the repository at this point in the history
  • Loading branch information
tzskp1 committed Jan 8, 2025
1 parent 5bee504 commit 8e26325
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
17 changes: 17 additions & 0 deletions agent/src/nodex/utils/studio_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ impl StudioClient {
self._post(path, body.into()).await
}

async fn _post_binary(&self, path: &str, body: Body) -> anyhow::Result<reqwest::Response> {
let mut headers = HeaderMap::new();
headers.insert(
reqwest::header::CONTENT_TYPE,
HeaderValue::from_static("binary/octet-stream"),
);
self._post_common(path, body, headers).await
}

pub async fn post_binary(
&self,
path: &str,
body: impl Into<Body>,
) -> anyhow::Result<reqwest::Response> {
self._post_binary(path, body.into()).await
}

pub async fn send_device_info(
&self,
path: &str,
Expand Down
9 changes: 6 additions & 3 deletions agent/src/services/studio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl Studio {
}

#[inline]
async fn relay_to_studio_via_cbor<T: serde::Serialize + std::fmt::Debug + PartialEq>(
async fn relay_to_studio_via_cbor<T: serde::Serialize + std::fmt::Debug>(
&self,
path: &str,
request: T,
Expand All @@ -252,9 +252,12 @@ impl Studio {
let my_keyring = self.did_accessor.get_my_keyring();
let signing_key = &my_keyring.sign_metrics.get_secret_key();
let token = protocol::cbor::signature::Token::new(my_did);
let request = protocol::cbor::signature::WithTokenImpl { inner: request, token };
let request = protocol::cbor::signature::WithToken {
inner: request,
token,
};
let payload = protocol::cbor::signature::sign_message(signing_key, &request)?;
let res = self.http_client.post(path, payload).await?;
let res = self.http_client.post_binary(path, payload).await?;
let status = res.status();
let json: Value = res.json().await.context("Failed to read response body")?;
let message = json
Expand Down
38 changes: 11 additions & 27 deletions protocol/src/cbor/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use validator::Validate;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct Token {
did: String,
pub did: String,
#[serde(with = "ts_seconds")]
exp: DateTime<Utc>,
pub exp: DateTime<Utc>,
}

impl Token {
Expand All @@ -25,28 +25,12 @@ impl Token {
}
}

pub trait WithToken {
fn get_token(&self) -> &Token;
}

#[derive(Serialize, Deserialize, PartialEq, Validate, Debug)]
pub struct WithTokenImpl<T>
where
T: PartialEq + std::fmt::Debug
{
#[derive(Serialize, Deserialize, Validate, Debug)]
pub struct WithToken<T> {
pub token: Token,
pub inner: T,
}

impl<T> WithToken for WithTokenImpl<T>
where
T: PartialEq + std::fmt::Debug
{
fn get_token(&self) -> &Token {
&self.token
}
}

#[derive(Debug, thiserror::Error)]
pub enum SignMessageError {
#[error(transparent)]
Expand All @@ -57,9 +41,9 @@ pub enum SignMessageError {
Cose(CoseError),
}

pub fn sign_message<M: Serialize + WithToken>(
pub fn sign_message<M: Serialize>(
signing_key: &SigningKey,
message: &M,
message: &WithToken<M>,
) -> Result<Vec<u8>, SignMessageError> {
let mut c = Cursor::new(Vec::new());
ciborium::into_writer(message, &mut c).map_err(SignMessageError::Cbor)?;
Expand Down Expand Up @@ -105,20 +89,20 @@ pub async fn verify_message<M, R>(
did_repository: &R,
key_type: &str,
data: &[u8],
) -> Result<M, DecodeMessageError<R::FindIdentifierError>>
) -> Result<WithToken<M>, DecodeMessageError<R::FindIdentifierError>>
where
R: DidRepository,
M: DeserializeOwned + WithToken,
M: DeserializeOwned,
{
let sign1 = coset::CoseSign1::from_slice(data).map_err(DecodeMessageError::Cose)?;
let payload = sign1
.payload
.as_ref()
.ok_or(DecodeMessageError::PayloadEmpty)?;
let message: M =
let message: WithToken<M> =
ciborium::from_reader(Cursor::new(payload)).map_err(DecodeMessageError::Cbor)?;
let document = did_repository
.find_identifier(&message.get_token().did)
.find_identifier(&message.token.did)
.await
.map_err(DecodeMessageError::GetDidDocument)?
.ok_or(DecodeMessageError::NotFoundPubkey)?
Expand All @@ -127,7 +111,7 @@ where
.get_key(key_type)
.ok_or(DecodeMessageError::NotFoundPubkey)?;
let pubkey: VerifyingKey = pubkey.try_into().map_err(DecodeMessageError::GetPubkey)?;
if message.get_token().exp < Utc::now() {
if message.token.exp < Utc::now() {
return Err(DecodeMessageError::Expired);
}
sign1.verify_signature(b"", |sig, data| {
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/cbor/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use validator::Validate;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct TimeValue(
#[serde(with = "ts_milliseconds")] pub DateTime<Utc>,
pub f32,
);

#[derive(Serialize, Deserialize, PartialEq, Debug, Validate)]
#[derive(Serialize, Deserialize, Debug, Validate)]
pub struct CustomMetric {
#[validate(length(min = 1))]
pub key: String,
Expand Down

0 comments on commit 8e26325

Please sign in to comment.