From d40aa8c7c6a12214f0fac1d8e3ea94414c78e0ab Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Thu, 15 Aug 2024 10:04:11 -0600 Subject: [PATCH] verificationhelper: add function to dismiss verification request without cancelling it Signed-off-by: Sumner Evans --- client.go | 21 +++++++++++++++++++ crypto/verificationhelper/reciprocate.go | 7 +++++-- crypto/verificationhelper/sas.go | 12 +++++++---- .../verificationhelper/verificationhelper.go | 18 +++++++++++++--- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 750e3c25..636b355f 100644 --- a/client.go +++ b/client.go @@ -35,16 +35,37 @@ type CryptoHelper interface { } type VerificationHelper interface { + // Init initializes the helper. This should be called before any other + // methods. Init(context.Context) error + + // StartVerification starts an interactive verification flow with the given + // user via a to-device event. StartVerification(ctx context.Context, to id.UserID) (id.VerificationTransactionID, error) + // StartInRoomVerification starts an interactive verification flow with the + // given user in the given room. StartInRoomVerification(ctx context.Context, roomID id.RoomID, to id.UserID) (id.VerificationTransactionID, error) + + // AcceptVerification accepts a verification request. AcceptVerification(ctx context.Context, txnID id.VerificationTransactionID) error + // DismissVerification dismisses a verification request. This will not send + // a cancellation to the other device. This method should only be called + // *before* the request has been accepted and will error otherwise. + DismissVerification(ctx context.Context, txnID id.VerificationTransactionID) error + // CancelVerification cancels a verification request. This method should + // only be called *after* the request has been accepted, although it will + // not error if called beforehand. CancelVerification(ctx context.Context, txnID id.VerificationTransactionID, code event.VerificationCancelCode, reason string) error + // HandleScannedQRData handles the data from a QR code scan. HandleScannedQRData(ctx context.Context, data []byte) error + // ConfirmQRCodeScanned confirms that our QR code has been scanned. ConfirmQRCodeScanned(ctx context.Context, txnID id.VerificationTransactionID) error + // StartSAS starts a SAS verification flow. StartSAS(ctx context.Context, txnID id.VerificationTransactionID) error + // ConfirmSAS indicates that the user has confirmed that the SAS matches + // SAS shown on the other user's device. ConfirmSAS(ctx context.Context, txnID id.VerificationTransactionID) error } diff --git a/crypto/verificationhelper/reciprocate.go b/crypto/verificationhelper/reciprocate.go index 2ea0a0ed..21276218 100644 --- a/crypto/verificationhelper/reciprocate.go +++ b/crypto/verificationhelper/reciprocate.go @@ -183,8 +183,11 @@ func (vh *VerificationHelper) HandleScannedQRData(ctx context.Context, data []by return nil } -// ConfirmQRCodeScanned confirms that our QR code has been scanned and sends the -// m.key.verification.done event to the other device. +// ConfirmQRCodeScanned confirms that our QR code has been scanned and sends +// the m.key.verification.done event to the other device for the given +// transaction ID. The transaction ID should be one received via the +// VerificationRequested callback in [RequiredCallbacks] or the +// [StartVerification] or [StartInRoomVerification] functions. func (vh *VerificationHelper) ConfirmQRCodeScanned(ctx context.Context, txnID id.VerificationTransactionID) error { log := vh.getLog(ctx).With(). Str("verification_action", "confirm QR code scanned"). diff --git a/crypto/verificationhelper/sas.go b/crypto/verificationhelper/sas.go index bf8c6050..e28ec405 100644 --- a/crypto/verificationhelper/sas.go +++ b/crypto/verificationhelper/sas.go @@ -28,9 +28,10 @@ import ( "maunium.net/go/mautrix/id" ) -// StartSAS starts a SAS verification flow. The transaction ID should be the -// transaction ID of a verification request that was received via the -// VerificationRequested callback in [RequiredCallbacks]. +// StartSAS starts a SAS verification flow for the given transaction ID. The +// transaction ID should be one received via the VerificationRequested callback +// in [RequiredCallbacks] or the [StartVerification] or +// [StartInRoomVerification] functions. func (vh *VerificationHelper) StartSAS(ctx context.Context, txnID id.VerificationTransactionID) error { log := vh.getLog(ctx).With(). Str("verification_action", "accept verification"). @@ -81,7 +82,10 @@ func (vh *VerificationHelper) StartSAS(ctx context.Context, txnID id.Verificatio } // ConfirmSAS indicates that the user has confirmed that the SAS matches SAS -// shown on the other user's device. +// shown on the other user's device for the given transaction ID. The +// transaction ID should be one received via the VerificationRequested callback +// in [RequiredCallbacks] or the [StartVerification] or +// [StartInRoomVerification] functions. func (vh *VerificationHelper) ConfirmSAS(ctx context.Context, txnID id.VerificationTransactionID) error { log := vh.getLog(ctx).With(). Str("verification_action", "confirm SAS"). diff --git a/crypto/verificationhelper/verificationhelper.go b/crypto/verificationhelper/verificationhelper.go index 2719ea78..f4e5e2f5 100644 --- a/crypto/verificationhelper/verificationhelper.go +++ b/crypto/verificationhelper/verificationhelper.go @@ -482,9 +482,21 @@ func (vh *VerificationHelper) AcceptVerification(ctx context.Context, txnID id.V return vh.generateAndShowQRCode(ctx, txn) } -// CancelVerification cancels a verification request. The transaction ID should -// be the transaction ID of a verification request that was received via the -// VerificationRequested callback in [RequiredCallbacks]. +// DismissVerification dismisses the verification request with the given +// transaction ID. The transaction ID should be one received via the +// VerificationRequested callback in [RequiredCallbacks] or the +// [StartVerification] or [StartInRoomVerification] functions. +func (vh *VerificationHelper) DismissVerification(ctx context.Context, txnID id.VerificationTransactionID) error { + vh.activeTransactionsLock.Lock() + defer vh.activeTransactionsLock.Unlock() + delete(vh.activeTransactions, txnID) + return nil +} + +// DismissVerification cancels the verification request with the given +// transaction ID. The transaction ID should be one received via the +// VerificationRequested callback in [RequiredCallbacks] or the +// [StartVerification] or [StartInRoomVerification] functions. func (vh *VerificationHelper) CancelVerification(ctx context.Context, txnID id.VerificationTransactionID, code event.VerificationCancelCode, reason string) error { vh.activeTransactionsLock.Lock() defer vh.activeTransactionsLock.Unlock()