crypto: Allow accepting key request while specifying our supported methods

master
Damir Jelić 2021-06-14 17:44:51 +02:00
parent 80fac4bfa4
commit 29bba0b2ca
1 changed files with 43 additions and 17 deletions

View File

@ -266,11 +266,19 @@ impl VerificationRequest {
} }
} }
/// Accept the verification request. /// Accept the verification request signaling that our client supports the
pub fn accept(&self) -> Option<OutgoingVerificationRequest> { /// given verification methods.
///
/// # Arguments
///
/// * `methods` - The methods that we should advertise as supported by us.
pub fn accept_with_methods(
&self,
methods: Vec<VerificationMethod>,
) -> Option<OutgoingVerificationRequest> {
let mut inner = self.inner.lock().unwrap(); let mut inner = self.inner.lock().unwrap();
inner.accept().map(|c| match c { inner.accept(methods).map(|c| match c {
OutgoingContent::ToDevice(content) => { OutgoingContent::ToDevice(content) => {
ToDeviceRequest::new(&self.other_user(), inner.other_device_id(), content).into() ToDeviceRequest::new(&self.other_user(), inner.other_device_id(), content).into()
} }
@ -280,6 +288,19 @@ impl VerificationRequest {
}) })
} }
/// Accept the verification request.
///
/// This method will accept the request and signal that it supports the
/// `m.sas.v1`, the `m.qr_code.show.v1`, and `m.reciprocate.v1` method.
///
/// If QR code scanning should be supported or QR code showing shouldn't be
/// supported the [`accept_with_methods()`] method should be used instead.
///
/// [`accept_with_methods()`]: #method.accept_with_methods
pub fn accept(&self) -> Option<OutgoingVerificationRequest> {
self.accept_with_methods(SUPPORTED_METHODS.to_vec())
}
/// Cancel the verification request /// Cancel the verification request
pub fn cancel(&self) -> Option<OutgoingVerificationRequest> { pub fn cancel(&self) -> Option<OutgoingVerificationRequest> {
let mut inner = self.inner.lock().unwrap(); let mut inner = self.inner.lock().unwrap();
@ -400,9 +421,9 @@ impl InnerRequest {
} }
} }
fn accept(&mut self) -> Option<OutgoingContent> { fn accept(&mut self, methods: Vec<VerificationMethod>) -> Option<OutgoingContent> {
if let InnerRequest::Requested(s) = self { if let InnerRequest::Requested(s) = self {
let (state, content) = s.clone().accept(); let (state, content) = s.clone().accept(methods);
*self = InnerRequest::Ready(state); *self = InnerRequest::Ready(state);
Some(content) Some(content)
@ -512,7 +533,7 @@ impl RequestState<Created> {
account, account,
other_user_id: other_user_id.to_owned(), other_user_id: other_user_id.to_owned(),
private_cross_signing_identity: private_identity, private_cross_signing_identity: private_identity,
state: Created { methods: SUPPORTED_METHODS.to_vec() }, state: Created { our_methods: SUPPORTED_METHODS.to_vec() },
verification_cache: cache, verification_cache: cache,
store, store,
flow_id: flow_id.to_owned().into(), flow_id: flow_id.to_owned().into(),
@ -541,7 +562,8 @@ impl RequestState<Created> {
store: self.store, store: self.store,
other_user_id: self.other_user_id, other_user_id: self.other_user_id,
state: Ready { state: Ready {
methods: content.methods().to_owned(), their_methods: content.methods().to_owned(),
our_methods: self.state.our_methods,
other_device_id: content.from_device().into(), other_device_id: content.from_device().into(),
}, },
} }
@ -550,14 +572,14 @@ impl RequestState<Created> {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Created { struct Created {
/// The verification methods supported by the sender. /// The verification methods supported by us.
pub methods: Vec<VerificationMethod>, pub our_methods: Vec<VerificationMethod>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Requested { struct Requested {
/// The verification methods supported by the sender. /// The verification methods supported by the sender.
pub methods: Vec<VerificationMethod>, pub their_methods: Vec<VerificationMethod>,
/// The device id of the device that responded to the verification request. /// The device id of the device that responded to the verification request.
pub other_device_id: DeviceIdBox, pub other_device_id: DeviceIdBox,
@ -582,13 +604,13 @@ impl RequestState<Requested> {
flow_id: flow_id.to_owned().into(), flow_id: flow_id.to_owned().into(),
other_user_id: sender.clone(), other_user_id: sender.clone(),
state: Requested { state: Requested {
methods: content.methods().to_owned(), their_methods: content.methods().to_owned(),
other_device_id: content.from_device().into(), other_device_id: content.from_device().into(),
}, },
} }
} }
fn accept(self) -> (RequestState<Ready>, OutgoingContent) { fn accept(self, methods: Vec<VerificationMethod>) -> (RequestState<Ready>, OutgoingContent) {
let state = RequestState { let state = RequestState {
account: self.account.clone(), account: self.account.clone(),
store: self.store, store: self.store,
@ -597,7 +619,8 @@ impl RequestState<Requested> {
flow_id: self.flow_id.clone(), flow_id: self.flow_id.clone(),
other_user_id: self.other_user_id, other_user_id: self.other_user_id,
state: Ready { state: Ready {
methods: SUPPORTED_METHODS.to_vec(), their_methods: self.state.their_methods,
our_methods: methods.clone(),
other_device_id: self.state.other_device_id.clone(), other_device_id: self.state.other_device_id.clone(),
}, },
}; };
@ -606,7 +629,7 @@ impl RequestState<Requested> {
FlowId::ToDevice(i) => { FlowId::ToDevice(i) => {
AnyToDeviceEventContent::KeyVerificationReady(ReadyToDeviceEventContent::new( AnyToDeviceEventContent::KeyVerificationReady(ReadyToDeviceEventContent::new(
self.account.device_id().to_owned(), self.account.device_id().to_owned(),
SUPPORTED_METHODS.to_vec(), methods,
i.to_owned(), i.to_owned(),
)) ))
.into() .into()
@ -615,7 +638,7 @@ impl RequestState<Requested> {
r.to_owned(), r.to_owned(),
AnyMessageEventContent::KeyVerificationReady(ReadyEventContent::new( AnyMessageEventContent::KeyVerificationReady(ReadyEventContent::new(
self.account.device_id().to_owned(), self.account.device_id().to_owned(),
SUPPORTED_METHODS.to_vec(), methods,
Relation::new(e.to_owned()), Relation::new(e.to_owned()),
)), )),
) )
@ -628,8 +651,11 @@ impl RequestState<Requested> {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Ready { struct Ready {
/// The verification methods supported by the sender. /// The verification methods supported by the other side.
pub methods: Vec<VerificationMethod>, pub their_methods: Vec<VerificationMethod>,
/// The verification methods supported by the us.
pub our_methods: Vec<VerificationMethod>,
/// The device id of the device that responded to the verification request. /// The device id of the device that responded to the verification request.
pub other_device_id: DeviceIdBox, pub other_device_id: DeviceIdBox,