matrix_sdk_base/response_processors/
verification.rs

1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use ruma::{
16    RoomId,
17    events::{
18        AnySyncMessageLikeEvent, AnySyncTimelineEvent, SyncMessageLikeEvent,
19        room::message::MessageType,
20    },
21};
22
23use super::e2ee::E2EE;
24use crate::Result;
25
26/// Process the given event as a verification event if it is a candidate. The
27/// event must be decrypted.
28///
29/// **Note**: If the supplied event is an `m.room.message` event with
30/// `msgtype: m.key.verification.request`, then the device information for
31/// the sending user must be up-to-date before calling this method
32/// (otherwise, the request will be ignored). It is hard to guarantee this
33/// is the case, but you can maximize your chances by explicitly making a
34/// request for this user's device info by calling
35/// [`OlmMachine::query_keys_for_users`], sending the request, and
36/// processing the response with [`OlmMachine::mark_request_as_sent`].
37pub async fn process_if_relevant(
38    event: &AnySyncTimelineEvent,
39    e2ee: E2EE<'_>,
40    room_id: &RoomId,
41) -> Result<()> {
42    if !e2ee.verification_is_allowed {
43        return Ok(());
44    }
45
46    let Some(olm) = e2ee.olm_machine else {
47        return Ok(());
48    };
49
50    let AnySyncTimelineEvent::MessageLike(event) = event else {
51        return Ok(());
52    };
53
54    match event {
55        // This is an original (i.e. non-redacted) `m.room.message` event and its
56        // content is a verification request…
57        AnySyncMessageLikeEvent::RoomMessage(SyncMessageLikeEvent::Original(original_event))
58            if matches!(&original_event.content.msgtype, MessageType::VerificationRequest(_)) => {}
59
60        // … or this is verification request event.
61        AnySyncMessageLikeEvent::KeyVerificationReady(_)
62        | AnySyncMessageLikeEvent::KeyVerificationStart(_)
63        | AnySyncMessageLikeEvent::KeyVerificationCancel(_)
64        | AnySyncMessageLikeEvent::KeyVerificationAccept(_)
65        | AnySyncMessageLikeEvent::KeyVerificationKey(_)
66        | AnySyncMessageLikeEvent::KeyVerificationMac(_)
67        | AnySyncMessageLikeEvent::KeyVerificationDone(_) => {}
68
69        _ => {
70            // No need to handle those other event types.
71            return Ok(());
72        }
73    }
74
75    Ok(olm.receive_verification_event(&event.clone().into_full_event(room_id.to_owned())).await?)
76}