//! # Token module //! //! Functions to read/write token from/to the database. A token is any string associated with a key. //! //! Tokens are used in countermitm verification protocols. use anyhow::Result; use deltachat_derive::{FromSql, ToSql}; use crate::chat::ChatId; use crate::context::Context; use crate::dc_tools::{dc_create_id, time}; /// Token namespace #[derive( Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, ToSql, FromSql, )] #[repr(u32)] pub enum Namespace { Unknown = 0, Auth = 110, InviteNumber = 100, } impl Default for Namespace { fn default() -> Self { Namespace::Unknown } } /// Creates a new token and saves it into the database. /// /// Returns created token. pub async fn save(context: &Context, namespace: Namespace, foreign_id: Option) -> String { let token = dc_create_id(); match foreign_id { Some(foreign_id) => context .sql .execute( "INSERT INTO tokens (namespc, foreign_id, token, timestamp) VALUES (?, ?, ?, ?);", paramsv![namespace, foreign_id, token, time()], ) .await .ok(), None => context .sql .execute( "INSERT INTO tokens (namespc, token, timestamp) VALUES (?, ?, ?);", paramsv![namespace, token, time()], ) .await .ok(), }; token } pub async fn lookup( context: &Context, namespace: Namespace, chat: Option, ) -> Result> { let token = match chat { Some(chat_id) => { context .sql .query_get_value( "SELECT token FROM tokens WHERE namespc=? AND foreign_id=?;", paramsv![namespace, chat_id], ) .await? } // foreign_id is declared as `INTEGER DEFAULT 0` in the schema. None => { context .sql .query_get_value( "SELECT token FROM tokens WHERE namespc=? AND foreign_id=0;", paramsv![namespace], ) .await? } }; Ok(token) } pub async fn lookup_or_new( context: &Context, namespace: Namespace, foreign_id: Option, ) -> String { if let Ok(Some(token)) = lookup(context, namespace, foreign_id).await { return token; } save(context, namespace, foreign_id).await } pub async fn exists(context: &Context, namespace: Namespace, token: &str) -> bool { context .sql .exists( "SELECT COUNT(*) FROM tokens WHERE namespc=? AND token=?;", paramsv![namespace, token], ) .await .unwrap_or_default() }