2019-08-03 18:47:52 +02:00
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-07-12 21:46:50 +02:00
|
|
|
|
2019-08-03 18:47:52 +02:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::db::DbConn;
|
|
|
|
use crate::db::schema::twofactor;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-07-12 21:46:50 +02:00
|
|
|
use super::User;
|
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
|
|
|
#[table_name = "twofactor"]
|
|
|
|
#[belongs_to(User, foreign_key = "user_uuid")]
|
|
|
|
#[primary_key(uuid)]
|
|
|
|
pub struct TwoFactor {
|
|
|
|
pub uuid: String,
|
|
|
|
pub user_uuid: String,
|
2019-05-20 21:12:41 +02:00
|
|
|
pub atype: i32,
|
2018-07-12 21:46:50 +02:00
|
|
|
pub enabled: bool,
|
|
|
|
pub data: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2019-02-08 18:45:07 +01:00
|
|
|
#[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,
|
|
|
|
|
|
|
|
// These are implementation details
|
|
|
|
U2fRegisterChallenge = 1000,
|
|
|
|
U2fLoginChallenge = 1001,
|
2019-08-03 18:47:52 +02:00
|
|
|
EmailVerificationChallenge = 1002,
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json_list(&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 {
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn save(&self, conn: &DbConn) -> EmptyResult {
|
2018-07-12 21:46:50 +02:00
|
|
|
diesel::replace_into(twofactor::table)
|
|
|
|
.values(self)
|
|
|
|
.execute(&**conn)
|
2018-12-19 21:52:53 +01:00
|
|
|
.map_res("Error saving twofactor")
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn delete(self, conn: &DbConn) -> EmptyResult {
|
2018-12-30 23:34:31 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
twofactor::table
|
|
|
|
.filter(twofactor::user_uuid.eq(user_uuid))
|
2019-05-20 21:12:41 +02:00
|
|
|
.filter(twofactor::atype.lt(1000)) // Filter implementation types
|
2018-12-30 23:34:31 +01:00
|
|
|
.load::<Self>(&**conn)
|
|
|
|
.expect("Error loading twofactor")
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 21:12:41 +02:00
|
|
|
pub fn find_by_user_and_type(user_uuid: &str, atype: i32, conn: &DbConn) -> Option<Self> {
|
2018-07-12 21:46:50 +02:00
|
|
|
twofactor::table
|
|
|
|
.filter(twofactor::user_uuid.eq(user_uuid))
|
2019-05-20 21:12:41 +02:00
|
|
|
.filter(twofactor::atype.eq(atype))
|
2018-12-30 23:34:31 +01:00
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
2018-07-12 21:46:50 +02:00
|
|
|
}
|
2018-12-30 23:34:31 +01:00
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-12-30 23:34:31 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|