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 matrix_sdk_crypto::OlmMachine;
16use ruma::{
17    events::{
18        room::message::MessageType, AnySyncMessageLikeEvent, AnySyncTimelineEvent,
19        SyncMessageLikeEvent,
20    },
21    RoomId,
22};
23
24use super::{e2ee::E2EE, Context};
25use crate::Result;
26
27/// Process the given event as a verification event if it is a candidate. The
28/// event must be decrypted.
29pub async fn process_if_relevant(
30    context: &mut Context,
31    event: &AnySyncTimelineEvent,
32    e2ee: E2EE<'_>,
33    room_id: &RoomId,
34) -> Result<()> {
35    if let AnySyncTimelineEvent::MessageLike(event) = event {
36        // That's it, we are good, the event has been decrypted successfully.
37
38        // However, let's run an additional action. Check if this is a verification
39        // event (`m.key.verification.*`), and call `verification` accordingly.
40        if match &event {
41            // This is an original (i.e. non-redacted) `m.room.message` event and its
42            // content is a verification request…
43            AnySyncMessageLikeEvent::RoomMessage(SyncMessageLikeEvent::Original(
44                original_event,
45            )) => {
46                matches!(&original_event.content.msgtype, MessageType::VerificationRequest(_))
47            }
48
49            // … or this is verification request event
50            AnySyncMessageLikeEvent::KeyVerificationReady(_)
51            | AnySyncMessageLikeEvent::KeyVerificationStart(_)
52            | AnySyncMessageLikeEvent::KeyVerificationCancel(_)
53            | AnySyncMessageLikeEvent::KeyVerificationAccept(_)
54            | AnySyncMessageLikeEvent::KeyVerificationKey(_)
55            | AnySyncMessageLikeEvent::KeyVerificationMac(_)
56            | AnySyncMessageLikeEvent::KeyVerificationDone(_) => true,
57
58            _ => false,
59        } {
60            verification(context, e2ee.verification_is_allowed, e2ee.olm_machine, event, room_id)
61                .await?;
62        }
63    }
64
65    Ok(())
66}
67
68async fn verification(
69    _context: &mut Context,
70    verification_is_allowed: bool,
71    olm_machine: Option<&OlmMachine>,
72    event: &AnySyncMessageLikeEvent,
73    room_id: &RoomId,
74) -> Result<()> {
75    if !verification_is_allowed {
76        return Ok(());
77    }
78
79    if let Some(olm) = olm_machine {
80        olm.receive_verification_event(&event.clone().into_full_event(room_id.to_owned())).await?;
81    }
82
83    Ok(())
84}