handle withdraw/revive qr code actions

This commit is contained in:
B. Petersen
2021-06-23 14:28:25 +02:00
committed by bjoern
parent 3f00a6efbe
commit 8ac2bd0298
4 changed files with 97 additions and 2 deletions

View File

@@ -110,6 +110,16 @@ pub enum LotState {
/// text1=error string
QrError = 400,
QrWithdrawVerifyContact = 500,
/// text1=groupname
QrWithdrawVerifyGroup = 502,
QrReviveVerifyContact = 510,
/// text1=groupname
QrReviveVerifyGroup = 512,
// Message States
MsgInFresh = 10,
MsgInNoticed = 13,

View File

@@ -6,7 +6,7 @@ use percent_encoding::percent_decode_str;
use serde::Deserialize;
use std::collections::BTreeMap;
use crate::chat::{self, ChatIdBlocked};
use crate::chat::{self, get_chat_id_by_grpid, ChatIdBlocked};
use crate::config::Config;
use crate::constants::Blocked;
use crate::contact::{addr_normalize, may_be_valid_addr, Contact, Origin};
@@ -16,6 +16,7 @@ use crate::log::LogExt;
use crate::lot::{Lot, LotState};
use crate::message::Message;
use crate::peerstate::Peerstate;
use crate::token;
const OPENPGP4FPR_SCHEME: &str = "OPENPGP4FPR:"; // yes: uppercase
const DCACCOUNT_SCHEME: &str = "DCACCOUNT:";
@@ -189,6 +190,27 @@ async fn decode_openpgp(context: &Context, qr: &str) -> Lot {
lot.fingerprint = Some(fingerprint);
lot.invitenumber = invitenumber;
lot.auth = auth;
// scanning own qr-code offers withdraw/revive instead of secure-join
if context.is_self_addr(&addr).await.unwrap_or_default() {
if let Some(ref invitenumber) = lot.invitenumber {
lot.state =
if token::exists(context, token::Namespace::InviteNumber, &*invitenumber).await
{
if lot.state == LotState::QrAskVerifyContact {
LotState::QrWithdrawVerifyContact
} else {
LotState::QrWithdrawVerifyGroup
}
} else {
if lot.state == LotState::QrAskVerifyContact {
LotState::QrReviveVerifyContact
} else {
LotState::QrReviveVerifyGroup
}
}
}
}
} else {
return format_err!("Missing address").into();
}
@@ -275,7 +297,8 @@ async fn set_account_from_qr(context: &Context, qr: &str) -> Result<(), Error> {
}
pub async fn set_config_from_qr(context: &Context, qr: &str) -> Result<(), Error> {
match check_qr(context, qr).await.state {
let lot = check_qr(context, qr).await;
match lot.state {
LotState::QrAccount => set_account_from_qr(context, qr).await,
LotState::QrWebrtcInstance => {
let val = decode_webrtc_instance(context, qr).text2;
@@ -284,6 +307,47 @@ pub async fn set_config_from_qr(context: &Context, qr: &str) -> Result<(), Error
.await?;
Ok(())
}
LotState::QrWithdrawVerifyContact | LotState::QrWithdrawVerifyGroup => {
token::delete(
context,
token::Namespace::InviteNumber,
lot.invitenumber.unwrap_or_default().as_str(),
)
.await?;
token::delete(
context,
token::Namespace::Auth,
lot.auth.unwrap_or_default().as_str(),
)
.await?;
Ok(())
}
LotState::QrReviveVerifyContact | LotState::QrReviveVerifyGroup => {
let chat_id = if lot.state == LotState::QrReviveVerifyContact {
None
} else {
Some(
get_chat_id_by_grpid(context, &lot.text2.unwrap_or_default())
.await?
.0,
)
};
token::save(
context,
token::Namespace::InviteNumber,
chat_id,
lot.invitenumber.unwrap_or_default(),
)
.await;
token::save(
context,
token::Namespace::Auth,
chat_id,
lot.auth.unwrap_or_default(),
)
.await;
Ok(())
}
_ => bail!("qr code does not contain config: {}", qr),
}
}

View File

@@ -111,3 +111,14 @@ pub async fn exists(context: &Context, namespace: Namespace, token: &str) -> boo
.await
.unwrap_or_default()
}
pub async fn delete(context: &Context, namespace: Namespace, token: &str) -> Result<()> {
context
.sql
.execute(
"DELETE FROM tokens WHERE namespc=? AND token=?;",
paramsv![namespace, token],
)
.await?;
Ok(())
}