1// Copyright 2021 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.
1415//! Interactive verification for E2EE capable users and devices in Matrix.
16//!
17//! The SDK supports interactive verification of devices and users, this module
18//! contains types that model and support different verification flows.
19//!
20//! A verification flow usually starts its life as a [VerificationRequest], the
21//! request can then be accepted, or it needs to be accepted by the other side
22//! of the verification flow.
23//!
24//! Once both sides have agreed to perform the verification, and the
25//! [VerificationRequest::is_ready()] method returns true, the verification can
26//! transition into one of the supported verification flows:
27//!
28//! * [`SasVerification`] - Interactive verification using a short
29//! authentication string.
30//! * [`QrVerification`] - Interactive verification using QR codes.
3132#[cfg(feature = "qrcode")]
33mod qrcode;
34mod requests;
35mod sas;
3637use as_variant::as_variant;
38pub use matrix_sdk_base::crypto::{
39 format_emojis, AcceptSettings, AcceptedProtocols, CancelInfo, Emoji, EmojiShortAuthString,
40 SasState,
41};
42#[cfg(feature = "qrcode")]
43pub use matrix_sdk_base::crypto::{
44 matrix_sdk_qrcode::{DecodingError, EncodingError, QrVerificationData},
45 QrVerificationState, ScanError,
46};
47#[cfg(feature = "qrcode")]
48pub use qrcode::QrVerification;
49pub use requests::{VerificationRequest, VerificationRequestState};
50use ruma::RoomId;
51pub use sas::SasVerification;
5253/// An enum over the different verification types the SDK supports.
54#[derive(Debug, Clone)]
55#[non_exhaustive]
56pub enum Verification {
57/// The `m.sas.v1` verification variant.
58SasV1(SasVerification),
59#[cfg(feature = "qrcode")]
60/// The `m.qr_code.*.v1` verification variant.
61QrV1(QrVerification),
62}
6364impl Verification {
65/// Try to deconstruct this verification enum into a SAS verification.
66pub fn sas(self) -> Option<SasVerification> {
67as_variant!(self, Verification::SasV1)
68 }
6970/// Try to deconstruct this verification enum into a QR code verification.
71#[cfg(feature = "qrcode")]
72pub fn qr(self) -> Option<QrVerification> {
73as_variant!(self, Verification::QrV1)
74 }
7576/// Has this verification finished.
77pub fn is_done(&self) -> bool {
78match self {
79 Verification::SasV1(s) => s.is_done(),
80#[cfg(feature = "qrcode")]
81Verification::QrV1(qr) => qr.is_done(),
82 }
83 }
8485/// Has the verification been cancelled.
86pub fn is_cancelled(&self) -> bool {
87match self {
88 Verification::SasV1(s) => s.is_cancelled(),
89#[cfg(feature = "qrcode")]
90Verification::QrV1(qr) => qr.is_cancelled(),
91 }
92 }
9394/// Get info about the cancellation if the verification flow has been
95 /// cancelled.
96pub fn cancel_info(&self) -> Option<CancelInfo> {
97match self {
98 Verification::SasV1(s) => s.cancel_info(),
99#[cfg(feature = "qrcode")]
100Verification::QrV1(q) => q.cancel_info(),
101 }
102 }
103104/// Get our own user id.
105pub fn own_user_id(&self) -> &ruma::UserId {
106match self {
107 Verification::SasV1(v) => v.own_user_id(),
108#[cfg(feature = "qrcode")]
109Verification::QrV1(v) => v.own_user_id(),
110 }
111 }
112113/// Get the user id of the other user participating in this verification
114 /// flow.
115pub fn other_user_id(&self) -> &ruma::UserId {
116match self {
117 Verification::SasV1(v) => v.inner.other_user_id(),
118#[cfg(feature = "qrcode")]
119Verification::QrV1(v) => v.inner.other_user_id(),
120 }
121 }
122123/// Is this a verification that is verifying one of our own devices.
124pub fn is_self_verification(&self) -> bool {
125match self {
126 Verification::SasV1(v) => v.is_self_verification(),
127#[cfg(feature = "qrcode")]
128Verification::QrV1(v) => v.is_self_verification(),
129 }
130 }
131132/// Did we initiate the verification flow.
133pub fn we_started(&self) -> bool {
134match self {
135 Verification::SasV1(s) => s.we_started(),
136#[cfg(feature = "qrcode")]
137Verification::QrV1(q) => q.we_started(),
138 }
139 }
140141/// Get the room ID, if the verification is happening inside a room.
142pub fn room_id(&self) -> Option<&RoomId> {
143match self {
144 Verification::SasV1(s) => s.room_id(),
145#[cfg(feature = "qrcode")]
146Verification::QrV1(q) => q.room_id(),
147 }
148 }
149}
150151impl From<SasVerification> for Verification {
152fn from(sas: SasVerification) -> Self {
153Self::SasV1(sas)
154 }
155}
156157#[cfg(feature = "qrcode")]
158impl From<QrVerification> for Verification {
159fn from(qr: QrVerification) -> Self {
160Self::QrV1(qr)
161 }
162}