mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
refactor(token): rustify
This commit is contained in:
committed by
holger krekel
parent
96e02af0da
commit
8eee449305
@@ -19,7 +19,7 @@ use crate::param::*;
|
|||||||
use crate::peerstate::*;
|
use crate::peerstate::*;
|
||||||
use crate::qr::check_qr;
|
use crate::qr::check_qr;
|
||||||
use crate::stock::StockMessage;
|
use crate::stock::StockMessage;
|
||||||
use crate::token::*;
|
use crate::token;
|
||||||
use crate::types::*;
|
use crate::types::*;
|
||||||
|
|
||||||
pub const NON_ALPHANUMERIC_WITHOUT_DOT: &AsciiSet = &NON_ALPHANUMERIC.remove(b'.');
|
pub const NON_ALPHANUMERIC_WITHOUT_DOT: &AsciiSet = &NON_ALPHANUMERIC.remove(b'.');
|
||||||
@@ -80,8 +80,8 @@ pub fn dc_get_securejoin_qr(context: &Context, group_chat_id: uint32_t) -> Optio
|
|||||||
let fingerprint: String;
|
let fingerprint: String;
|
||||||
|
|
||||||
ensure_secret_key_exists(context).ok();
|
ensure_secret_key_exists(context).ok();
|
||||||
let invitenumber = dc_token_lookup_or_new(context, DC_TOKEN_INVITENUMBER, group_chat_id);
|
let invitenumber = token::lookup_or_new(context, token::Namespace::InviteNumber, group_chat_id);
|
||||||
let auth = dc_token_lookup_or_new(context, DC_TOKEN_AUTH, group_chat_id);
|
let auth = token::lookup_or_new(context, token::Namespace::Auth, group_chat_id);
|
||||||
let self_addr = match context.sql.get_config(context, "configured_addr") {
|
let self_addr = match context.sql.get_config(context, "configured_addr") {
|
||||||
Some(addr) => addr,
|
Some(addr) => addr,
|
||||||
None => {
|
None => {
|
||||||
@@ -387,7 +387,7 @@ pub fn handle_securejoin_handshake(
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if !dc_token_exists(context, DC_TOKEN_INVITENUMBER, &invitenumber) {
|
if !token::exists(context, token::Namespace::InviteNumber, &invitenumber) {
|
||||||
warn!(context, 0, "Secure-join denied (bad invitenumber).",);
|
warn!(context, 0, "Secure-join denied (bad invitenumber).",);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -507,7 +507,7 @@ pub fn handle_securejoin_handshake(
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if !dc_token_exists(context, DC_TOKEN_AUTH, &auth_0) {
|
if !token::exists(context, token::Namespace::Auth, &auth_0) {
|
||||||
could_not_establish_secure_connection(context, contact_chat_id, "Auth invalid.");
|
could_not_establish_secure_connection(context, contact_chat_id, "Auth invalid.");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
46
src/token.rs
46
src/token.rs
@@ -1,56 +1,58 @@
|
|||||||
|
//! Functions to read/write token from/to the database. A token is any string associated with a key.
|
||||||
|
|
||||||
|
use deltachat_derive::*;
|
||||||
|
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::dc_tools::*;
|
use crate::dc_tools::*;
|
||||||
use crate::sql;
|
use crate::sql;
|
||||||
|
|
||||||
// Token namespaces
|
// Token namespaces
|
||||||
#[allow(non_camel_case_types)]
|
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive, ToSql, FromSql)]
|
||||||
type dc_tokennamespc_t = usize;
|
#[repr(i32)]
|
||||||
pub const DC_TOKEN_AUTH: dc_tokennamespc_t = 110;
|
pub enum Namespace {
|
||||||
pub const DC_TOKEN_INVITENUMBER: dc_tokennamespc_t = 100;
|
Unknown = 0,
|
||||||
|
Auth = 110,
|
||||||
|
InviteNumber = 100,
|
||||||
|
}
|
||||||
|
|
||||||
// Functions to read/write token from/to the database. A token is any string associated with a key.
|
impl Default for Namespace {
|
||||||
|
fn default() -> Self {
|
||||||
|
Namespace::Unknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn dc_token_save(context: &Context, namespc: dc_tokennamespc_t, foreign_id: u32) -> String {
|
pub fn save(context: &Context, namespace: Namespace, foreign_id: u32) -> String {
|
||||||
// foreign_id may be 0
|
// foreign_id may be 0
|
||||||
let token = dc_create_id();
|
let token = dc_create_id();
|
||||||
sql::execute(
|
sql::execute(
|
||||||
context,
|
context,
|
||||||
&context.sql,
|
&context.sql,
|
||||||
"INSERT INTO tokens (namespc, foreign_id, token, timestamp) VALUES (?, ?, ?, ?);",
|
"INSERT INTO tokens (namespc, foreign_id, token, timestamp) VALUES (?, ?, ?, ?);",
|
||||||
params![namespc as i32, foreign_id as i32, &token, time()],
|
params![namespace, foreign_id as i32, &token, time()],
|
||||||
)
|
)
|
||||||
.ok();
|
.ok();
|
||||||
token
|
token
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dc_token_lookup(
|
pub fn lookup(context: &Context, namespace: Namespace, foreign_id: u32) -> Option<String> {
|
||||||
context: &Context,
|
|
||||||
namespc: dc_tokennamespc_t,
|
|
||||||
foreign_id: u32,
|
|
||||||
) -> Option<String> {
|
|
||||||
context.sql.query_row_col::<_, String>(
|
context.sql.query_row_col::<_, String>(
|
||||||
context,
|
context,
|
||||||
"SELECT token FROM tokens WHERE namespc=? AND foreign_id=?;",
|
"SELECT token FROM tokens WHERE namespc=? AND foreign_id=?;",
|
||||||
params![namespc as i32, foreign_id as i32],
|
params![namespace, foreign_id as i32],
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dc_token_lookup_or_new(
|
pub fn lookup_or_new(context: &Context, namespace: Namespace, foreign_id: u32) -> String {
|
||||||
context: &Context,
|
lookup(context, namespace, foreign_id).unwrap_or_else(|| save(context, namespace, foreign_id))
|
||||||
namespc: dc_tokennamespc_t,
|
|
||||||
foreign_id: u32,
|
|
||||||
) -> String {
|
|
||||||
dc_token_lookup(context, namespc, foreign_id)
|
|
||||||
.unwrap_or_else(|| dc_token_save(context, namespc, foreign_id))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dc_token_exists(context: &Context, namespc: dc_tokennamespc_t, token: &str) -> bool {
|
pub fn exists(context: &Context, namespace: Namespace, token: &str) -> bool {
|
||||||
context
|
context
|
||||||
.sql
|
.sql
|
||||||
.exists(
|
.exists(
|
||||||
"SELECT id FROM tokens WHERE namespc=? AND token=?;",
|
"SELECT id FROM tokens WHERE namespc=? AND token=?;",
|
||||||
params![namespc as i32, token],
|
params![namespace, token],
|
||||||
)
|
)
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user