Skip to content

Commit

Permalink
move migration into change_version functions
Browse files Browse the repository at this point in the history
  • Loading branch information
boxdot committed Feb 19, 2025
1 parent b2c7752 commit 9804bf2
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 101 deletions.
22 changes: 0 additions & 22 deletions apiclient/src/qs_api/api_migrations.rs

This file was deleted.

46 changes: 22 additions & 24 deletions apiclient/src/qs_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

use api_migrations::migrate_qs_process_response;
use http::StatusCode;
use mls_assist::openmls::prelude::KeyPackage;
use phnxtypes::{
Expand All @@ -20,19 +19,20 @@ use phnxtypes::{
identifiers::{QsClientId, QsUserId},
messages::{
client_qs::{
ClientKeyPackageParams, ClientKeyPackageResponse, ClientToQsMessageTbs,
CreateClientRecordResponse, CreateUserRecordResponse, DeleteClientRecordParams,
DeleteUserRecordParams, DequeueMessagesParams, DequeueMessagesResponse,
EncryptionKeyResponse, KeyPackageParams, KeyPackageResponseIn, QsProcessResponseIn,
ClientKeyPackageParams, ClientKeyPackageResponse, CreateClientRecordResponse,
CreateUserRecordResponse, DeleteClientRecordParams, DeleteUserRecordParams,
DequeueMessagesParams, DequeueMessagesResponse, EncryptionKeyResponse,
KeyPackageParams, KeyPackageResponseIn, QsProcessResponseIn,
QsVersionedProcessResponseIn, UpdateClientRecordParams, UpdateUserRecordParams,
VersionError,
VersionError, SUPPORTED_QS_API_VERSIONS,
},
client_qs_out::{
ClientToQsMessageOut, ClientToQsMessageTbsOut, CreateClientRecordParamsOut,
CreateUserRecordParamsOut, PublishKeyPackagesParamsOut, QsRequestParamsOut,
QsVersionedRequestParamsOut,
},
push_token::EncryptedPushToken,
ApiVersion, FriendshipToken,
FriendshipToken,
},
};
use thiserror::Error;
Expand All @@ -45,7 +45,6 @@ use crate::{

pub mod ws;

mod api_migrations;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -82,7 +81,10 @@ impl ApiClient {
) -> Result<QsProcessResponseIn, QsRequestError> {
let api_version = self.negotiated_versions().qs_api_version();

let message = sign_params(request_params, &token_or_signing_key, api_version)?;
let request_params =
QsVersionedRequestParamsOut::with_version(request_params, api_version)?;
let message = sign_params(request_params, &token_or_signing_key)?;

let endpoint = self.build_url(Protocol::Http, ENDPOINT_QS);
let response = send_qs_message(&self.client, &endpoint, &message).await?;

Expand All @@ -91,14 +93,17 @@ impl ApiClient {
return process_response(response).await;
};

let supported_versions = ClientToQsMessageTbs::SUPPORTED_API_VERSIONS;
let supported_versions = SUPPORTED_QS_API_VERSIONS;
let accepted_version = negotiate_api_version(accepted_versions, supported_versions)
.ok_or_else(|| VersionError::new(api_version, supported_versions.to_vec()))?;
self.negotiated_versions()
.set_qs_api_version(accepted_version);

let request_params = message.into_payload().into_unversioned_params();
let message = sign_params(request_params, &token_or_signing_key, accepted_version)?;
let (request_params, _) = message
.into_payload()
.into_body()
.change_version(accepted_version)?;
let message = sign_params(request_params, &token_or_signing_key)?;

let response = send_qs_message(&self.client, &endpoint, &message).await?;
process_response(response).await
Expand Down Expand Up @@ -387,8 +392,9 @@ async fn process_response(
if status.is_success() {
let bytes = response.bytes().await.map_err(QsRequestError::Reqwest)?;
let qs_response = QsVersionedProcessResponseIn::tls_deserialize_exact_bytes(&bytes)
.map_err(QsRequestError::Tls)?;
migrate_qs_process_response(qs_response)
.map_err(QsRequestError::Tls)?
.into_unversioned()?;
Ok(qs_response)
} else {
let error = response
.text()
Expand Down Expand Up @@ -416,18 +422,10 @@ async fn send_qs_message(
}

fn sign_params<T: SigningKeyBehaviour>(
request_params: QsRequestParamsOut,
request_params: QsVersionedRequestParamsOut,
token_or_signing_key: &AuthenticationMethod<'_, T>,
api_version: ApiVersion,
) -> Result<ClientToQsMessageOut, QsRequestError> {
let tbs = ClientToQsMessageTbsOut::with_api_version(api_version, request_params).ok_or_else(
|| {
VersionError::new(
api_version,
ClientToQsMessageTbs::SUPPORTED_API_VERSIONS.to_vec(),
)
},
)?;
let tbs = ClientToQsMessageTbsOut::new(request_params);
let message = match token_or_signing_key {
AuthenticationMethod::Token(token) => ClientToQsMessageOut::from_token(tbs, token.clone()),
AuthenticationMethod::SigningKey(signing_key) => tbs
Expand Down
4 changes: 2 additions & 2 deletions apiclient/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use http::{HeaderMap, StatusCode};
use phnxtypes::{
messages::{client_qs::ClientToQsMessageTbs, ApiVersion},
messages::{client_qs::CURRENT_QS_API_VERSION, ApiVersion},
ACCEPTED_API_VERSIONS_HEADER,
};
use tracing::error;
Expand All @@ -24,7 +24,7 @@ pub(crate) struct NegotiatedApiVersions {
impl NegotiatedApiVersions {
pub(crate) fn new() -> Self {
Self {
qs_api_version: AtomicU64::new(ClientToQsMessageTbs::CURRENT_API_VERSION.value()),
qs_api_version: AtomicU64::new(CURRENT_QS_API_VERSION.value()),
}
}

Expand Down
20 changes: 0 additions & 20 deletions backend/src/qs/client_api/api_migrations.rs

This file was deleted.

8 changes: 5 additions & 3 deletions backend/src/qs/client_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use phnxtypes::{

use super::{client_record::QsClientRecord, queue::Queue, user_record::UserRecord, Qs};

mod api_migrations;
pub(crate) mod client_records;
pub(crate) mod key_packages;
pub(crate) mod user_records;
Expand Down Expand Up @@ -72,7 +71,7 @@ impl Qs {
})?,
};

let request_params = api_migrations::migrate_qs_request_params(request_params)?;
let (request_params, from_version) = request_params.into_unversioned()?;

let response = match request_params {
QsRequestParams::CreateUser(params) => {
Expand Down Expand Up @@ -115,7 +114,10 @@ impl Qs {
}
};

Ok(QsVersionedProcessResponse::Alpha(response))
Ok(QsVersionedProcessResponse::with_version(
response,
from_version,
)?)
}

/// Retrieve messages the given number of messages, starting with
Expand Down
59 changes: 45 additions & 14 deletions types/src/messages/client_qs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use tls_codec::{
DeserializeBytes, Serialize, TlsDeserializeBytes, TlsSerialize, TlsSize, TlsVarInt,
};

pub const CURRENT_QS_API_VERSION: ApiVersion = ApiVersion::new(1).unwrap();

pub const SUPPORTED_QS_API_VERSIONS: &[ApiVersion] = &[CURRENT_QS_API_VERSION];

use crate::{
crypto::{
ear::keys::KeyPackageEarKey,
Expand Down Expand Up @@ -323,6 +327,17 @@ impl QsVersionedRequestParams {
QsVersionedRequestParams::Alpha(_) => ApiVersion::new(1).expect("infallible"),
}
}

pub fn into_unversioned(self) -> Result<(QsRequestParams, ApiVersion), VersionError> {
let version = self.version();
let params = match self {
QsVersionedRequestParams::Alpha(params) => params,
QsVersionedRequestParams::Other(version) => {
return Err(VersionError::new(version, SUPPORTED_QS_API_VERSIONS))
}
};
Ok((params, version))
}
}

impl tls_codec::Size for QsVersionedRequestParams {
Expand Down Expand Up @@ -373,10 +388,10 @@ pub struct VersionError {
}

impl VersionError {
pub fn new(version: ApiVersion, supported_versions: Vec<ApiVersion>) -> Self {
pub fn new(version: ApiVersion, supported_versions: impl Into<Vec<ApiVersion>>) -> Self {
Self {
version,
supported_versions,
supported_versions: supported_versions.into(),
}
}

Expand All @@ -386,17 +401,12 @@ impl VersionError {
}

impl ClientToQsMessageTbs {
pub const CURRENT_API_VERSION: ApiVersion = ApiVersion::new(1).unwrap();

pub const SUPPORTED_API_VERSIONS: &[ApiVersion] = &[Self::CURRENT_API_VERSION];

pub(crate) fn sender(&self) -> Result<QsSender, VersionError> {
match &self.body {
QsVersionedRequestParams::Alpha(params) => Ok(params.sender()),
QsVersionedRequestParams::Other(version) => Err(VersionError::new(
*version,
Self::SUPPORTED_API_VERSIONS.to_vec(),
)),
QsVersionedRequestParams::Other(version) => {
Err(VersionError::new(*version, SUPPORTED_QS_API_VERSIONS))
}
}
}
}
Expand Down Expand Up @@ -448,9 +458,19 @@ pub enum QsVersionedProcessResponse {
}

impl QsVersionedProcessResponse {
pub fn version(&self) -> TlsVarInt {
pub fn with_version(
response: QsProcessResponse,
version: ApiVersion,
) -> Result<Self, VersionError> {
match version.value() {
1 => Ok(Self::Alpha(response)),
_ => Err(VersionError::new(version, SUPPORTED_QS_API_VERSIONS)),
}
}

pub fn version(&self) -> ApiVersion {
match self {
QsVersionedProcessResponse::Alpha(_) => TlsVarInt::new(1).expect("infallible"),
QsVersionedProcessResponse::Alpha(_) => ApiVersion::new(1).expect("infallible"),
}
}
}
Expand All @@ -459,7 +479,8 @@ impl tls_codec::Size for QsVersionedProcessResponse {
fn tls_serialized_len(&self) -> usize {
match self {
QsVersionedProcessResponse::Alpha(qs_process_response) => {
self.version().tls_serialized_len() + qs_process_response.tls_serialized_len()
self.version().tls_value().tls_serialized_len()
+ qs_process_response.tls_serialized_len()
}
}
}
Expand All @@ -469,7 +490,8 @@ impl tls_codec::Serialize for QsVersionedProcessResponse {
fn tls_serialize<W: io::Write>(&self, writer: &mut W) -> Result<usize, tls_codec::Error> {
match self {
QsVersionedProcessResponse::Alpha(params) => {
Ok(self.version().tls_serialize(writer)? + params.tls_serialize(writer)?)
Ok(self.version().tls_value().tls_serialize(writer)?
+ params.tls_serialize(writer)?)
}
}
}
Expand Down Expand Up @@ -501,6 +523,15 @@ impl QsVersionedProcessResponseIn {
QsVersionedProcessResponseIn::Alpha(_) => ApiVersion::new(1).expect("infallible"),
}
}

pub fn into_unversioned(self) -> Result<QsProcessResponseIn, VersionError> {
match self {
QsVersionedProcessResponseIn::Alpha(response) => Ok(response),
QsVersionedProcessResponseIn::Other(version) => {
Err(VersionError::new(version, SUPPORTED_QS_API_VERSIONS))
}
}
}
}

impl tls_codec::Size for QsVersionedProcessResponseIn {
Expand Down
Loading

0 comments on commit 9804bf2

Please sign in to comment.