2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-07-12 21:46:50 +02:00
|
|
|
|
2021-10-25 10:36:05 +02:00
|
|
|
use crate::{api::EmptyResult, db::DbConn, error::MapResult};
|
2019-08-03 18:47:52 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_object! {
|
2022-05-04 21:13:05 +02:00
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
2022-05-20 23:39:47 +02:00
|
|
|
#[diesel(table_name = twofactor)]
|
|
|
|
#[diesel(primary_key(uuid))]
|
2020-08-18 17:15:44 +02:00
|
|
|
pub struct TwoFactor {
|
|
|
|
pub uuid: String,
|
|
|
|
pub user_uuid: String,
|
|
|
|
pub atype: i32,
|
|
|
|
pub enabled: bool,
|
|
|
|
pub data: String,
|
2024-03-17 22:04:37 +01:00
|
|
|
pub last_used: i64,
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2020-05-03 17:24:51 +02:00
|
|
|
#[derive(num_derive::FromPrimitive)]
|
2018-07-12 21:46:50 +02:00
|
|
|
pub enum TwoFactorType {
|
|
|
|
Authenticator = 0,
|
|
|
|
Email = 1,
|
|
|
|
Duo = 2,
|
|
|
|
YubiKey = 3,
|
|
|
|
U2f = 4,
|
|
|
|
Remember = 5,
|
|
|
|
OrganizationDuo = 6,
|
2021-06-07 23:34:00 +02:00
|
|
|
Webauthn = 7,
|
2018-07-12 21:46:50 +02:00
|
|
|
|
|
|
|
// These are implementation details
|
|
|
|
U2fRegisterChallenge = 1000,
|
|
|
|
U2fLoginChallenge = 1001,
|
2019-08-03 18:47:52 +02:00
|
|
|
EmailVerificationChallenge = 1002,
|
2021-06-07 23:34:00 +02:00
|
|
|
WebauthnRegisterChallenge = 1003,
|
|
|
|
WebauthnLoginChallenge = 1004,
|
2023-11-12 22:15:44 +01:00
|
|
|
|
|
|
|
// Special type for Protected Actions verification via email
|
|
|
|
ProtectedActions = 2000,
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl TwoFactor {
|
2019-05-20 21:12:41 +02:00
|
|
|
pub fn new(user_uuid: String, atype: TwoFactorType, data: String) -> Self {
|
2018-07-12 21:46:50 +02:00
|
|
|
Self {
|
2018-12-07 14:32:40 +01:00
|
|
|
uuid: crate::util::get_uuid(),
|
2018-07-12 21:46:50 +02:00
|
|
|
user_uuid,
|
2019-05-20 21:12:41 +02:00
|
|
|
atype: atype as i32,
|
2018-07-12 21:46:50 +02:00
|
|
|
enabled: true,
|
|
|
|
data,
|
2019-10-10 17:32:20 +02:00
|
|
|
last_used: 0,
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json(&self) -> Value {
|
2018-07-12 21:46:50 +02:00
|
|
|
json!({
|
|
|
|
"Enabled": self.enabled,
|
|
|
|
"Key": "", // This key and value vary
|
|
|
|
"Object": "twoFactorAuthenticator" // This value varies
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:36:35 +02:00
|
|
|
pub fn to_json_provider(&self) -> Value {
|
2018-07-12 21:46:50 +02:00
|
|
|
json!({
|
|
|
|
"Enabled": self.enabled,
|
2019-05-20 21:12:41 +02:00
|
|
|
"Type": self.atype,
|
2018-07-12 21:46:50 +02:00
|
|
|
"Object": "twoFactorProvider"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl TwoFactor {
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn save(&self, conn: &mut DbConn) -> EmptyResult {
|
2020-09-22 12:13:02 +02:00
|
|
|
db_run! { conn:
|
2020-08-18 17:15:44 +02:00
|
|
|
sqlite, mysql {
|
2020-09-22 12:13:02 +02:00
|
|
|
match diesel::replace_into(twofactor::table)
|
2020-08-18 17:15:44 +02:00
|
|
|
.values(TwoFactorDb::to_db(self))
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
{
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
|
|
|
|
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
|
|
|
|
diesel::update(twofactor::table)
|
|
|
|
.filter(twofactor::uuid.eq(&self.uuid))
|
|
|
|
.set(TwoFactorDb::to_db(self))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving twofactor")
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}.map_res("Error saving twofactor")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
let value = TwoFactorDb::to_db(self);
|
|
|
|
// We need to make sure we're not going to violate the unique constraint on user_uuid and atype.
|
|
|
|
// This happens automatically on other DBMS backends due to replace_into(). PostgreSQL does
|
|
|
|
// not support multiple constraints on ON CONFLICT clauses.
|
|
|
|
diesel::delete(twofactor::table.filter(twofactor::user_uuid.eq(&self.user_uuid)).filter(twofactor::atype.eq(&self.atype)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error deleting twofactor for insert")?;
|
2019-09-12 22:12:22 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
diesel::insert_into(twofactor::table)
|
|
|
|
.values(&value)
|
|
|
|
.on_conflict(twofactor::uuid)
|
|
|
|
.do_update()
|
|
|
|
.set(&value)
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
.map_res("Error saving twofactor")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete(self, conn: &mut DbConn) -> EmptyResult {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(twofactor::table.filter(twofactor::uuid.eq(self.uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error deleting twofactor")
|
|
|
|
}}
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
twofactor::table
|
|
|
|
.filter(twofactor::user_uuid.eq(user_uuid))
|
|
|
|
.filter(twofactor::atype.lt(1000)) // Filter implementation types
|
|
|
|
.load::<TwoFactorDb>(conn)
|
|
|
|
.expect("Error loading twofactor")
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user_and_type(user_uuid: &str, atype: i32, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
twofactor::table
|
|
|
|
.filter(twofactor::user_uuid.eq(user_uuid))
|
|
|
|
.filter(twofactor::atype.eq(atype))
|
|
|
|
.first::<TwoFactorDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
2018-12-30 23:34:31 +01:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_user(user_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(twofactor::table.filter(twofactor::user_uuid.eq(user_uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error deleting twofactors")
|
|
|
|
}}
|
2018-12-18 18:52:58 +01:00
|
|
|
}
|
2021-06-07 23:34:00 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn migrate_u2f_to_webauthn(conn: &mut DbConn) -> EmptyResult {
|
2021-06-07 23:34:00 +02:00
|
|
|
let u2f_factors = db_run! { conn: {
|
|
|
|
twofactor::table
|
|
|
|
.filter(twofactor::atype.eq(TwoFactorType::U2f as i32))
|
|
|
|
.load::<TwoFactorDb>(conn)
|
|
|
|
.expect("Error loading twofactor")
|
|
|
|
.from_db()
|
|
|
|
}};
|
|
|
|
|
2022-03-27 17:25:04 +02:00
|
|
|
use crate::api::core::two_factor::webauthn::U2FRegistration;
|
2021-06-07 23:34:00 +02:00
|
|
|
use crate::api::core::two_factor::webauthn::{get_webauthn_registrations, WebauthnRegistration};
|
|
|
|
use webauthn_rs::proto::*;
|
|
|
|
|
|
|
|
for mut u2f in u2f_factors {
|
|
|
|
let mut regs: Vec<U2FRegistration> = serde_json::from_str(&u2f.data)?;
|
|
|
|
// If there are no registrations or they are migrated (we do the migration in batch so we can consider them all migrated when the first one is)
|
|
|
|
if regs.is_empty() || regs[0].migrated == Some(true) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
let (_, mut webauthn_regs) = get_webauthn_registrations(&u2f.user_uuid, conn).await?;
|
2021-06-07 23:34:00 +02:00
|
|
|
|
|
|
|
// If the user already has webauthn registrations saved, don't overwrite them
|
|
|
|
if !webauthn_regs.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for reg in &mut regs {
|
|
|
|
let x: [u8; 32] = reg.reg.pub_key[1..33].try_into().unwrap();
|
|
|
|
let y: [u8; 32] = reg.reg.pub_key[33..65].try_into().unwrap();
|
|
|
|
|
|
|
|
let key = COSEKey {
|
|
|
|
type_: COSEAlgorithm::ES256,
|
|
|
|
key: COSEKeyType::EC_EC2(COSEEC2Key {
|
|
|
|
curve: ECDSACurve::SECP256R1,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_reg = WebauthnRegistration {
|
|
|
|
id: reg.id,
|
|
|
|
migrated: true,
|
|
|
|
name: reg.name.clone(),
|
|
|
|
credential: Credential {
|
|
|
|
counter: reg.counter,
|
|
|
|
verified: false,
|
|
|
|
cred: key,
|
|
|
|
cred_id: reg.reg.key_handle.clone(),
|
|
|
|
registration_policy: UserVerificationPolicy::Discouraged,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
webauthn_regs.push(new_reg);
|
|
|
|
|
|
|
|
reg.migrated = Some(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
u2f.data = serde_json::to_string(®s)?;
|
2021-11-16 17:07:55 +01:00
|
|
|
u2f.save(conn).await?;
|
2021-06-07 23:34:00 +02:00
|
|
|
|
|
|
|
TwoFactor::new(u2f.user_uuid.clone(), TwoFactorType::Webauthn, serde_json::to_string(&webauthn_regs)?)
|
2021-11-16 17:07:55 +01:00
|
|
|
.save(conn)
|
|
|
|
.await?;
|
2021-06-07 23:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-18 18:52:58 +01:00
|
|
|
}
|