2018-02-14 23:53:11 +00:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2019-11-25 05:28:49 +00:00
|
|
|
use crate::CONFIG;
|
2018-02-14 23:40:34 +00:00
|
|
|
|
2020-08-18 15:15:44 +00:00
|
|
|
db_object! {
|
2022-05-04 19:13:05 +00:00
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
2022-05-20 21:39:47 +00:00
|
|
|
#[diesel(table_name = devices)]
|
|
|
|
#[diesel(treat_none_as_null = true)]
|
|
|
|
#[diesel(primary_key(uuid, user_uuid))]
|
2020-08-18 15:15:44 +00:00
|
|
|
pub struct Device {
|
|
|
|
pub uuid: String,
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
|
|
|
|
pub user_uuid: String,
|
|
|
|
|
|
|
|
pub name: String,
|
2023-06-11 11:28:18 +00:00
|
|
|
pub atype: i32, // https://github.com/bitwarden/server/blob/master/src/Core/Enums/DeviceType.cs
|
|
|
|
pub push_uuid: Option<String>,
|
2020-08-18 15:15:44 +00:00
|
|
|
pub push_token: Option<String>,
|
|
|
|
|
|
|
|
pub refresh_token: String,
|
|
|
|
|
|
|
|
pub twofactor_remember: Option<String>,
|
|
|
|
}
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Device {
|
2019-05-20 19:12:41 +00:00
|
|
|
pub fn new(uuid: String, user_uuid: String, name: String, atype: i32) -> Self {
|
2018-02-10 00:00:55 +00:00
|
|
|
let now = Utc::now().naive_utc();
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
Self {
|
2018-02-10 00:00:55 +00:00
|
|
|
uuid,
|
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
|
|
|
|
|
|
|
user_uuid,
|
|
|
|
name,
|
2019-05-20 19:12:41 +00:00
|
|
|
atype,
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2023-06-11 11:28:18 +00:00
|
|
|
push_uuid: None,
|
2018-02-10 00:00:55 +00:00
|
|
|
push_token: None,
|
|
|
|
refresh_token: String::new(),
|
2018-06-01 13:08:03 +00:00
|
|
|
twofactor_remember: None,
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:46:50 +00:00
|
|
|
pub fn refresh_twofactor_remember(&mut self) -> String {
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::crypto;
|
2018-12-30 22:34:31 +00:00
|
|
|
use data_encoding::BASE64;
|
2018-06-01 13:08:03 +00:00
|
|
|
|
2022-11-11 09:55:04 +00:00
|
|
|
let twofactor_remember = crypto::encode_random_bytes::<180>(BASE64);
|
2018-07-12 19:46:50 +00:00
|
|
|
self.twofactor_remember = Some(twofactor_remember.clone());
|
|
|
|
|
|
|
|
twofactor_remember
|
2018-06-01 13:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_twofactor_remember(&mut self) {
|
|
|
|
self.twofactor_remember = None;
|
|
|
|
}
|
|
|
|
|
2022-01-21 05:50:58 +00:00
|
|
|
pub fn refresh_tokens(
|
|
|
|
&mut self,
|
|
|
|
user: &super::User,
|
|
|
|
orgs: Vec<super::UserOrganization>,
|
|
|
|
scope: Vec<String>,
|
|
|
|
) -> (String, i64) {
|
2018-02-10 00:00:55 +00:00
|
|
|
// If there is no refresh token, we create one
|
|
|
|
if self.refresh_token.is_empty() {
|
2018-12-07 01:05:45 +00:00
|
|
|
use crate::crypto;
|
2018-12-30 22:34:31 +00:00
|
|
|
use data_encoding::BASE64URL;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2022-11-11 09:55:04 +00:00
|
|
|
self.refresh_token = crypto::encode_random_bytes::<64>(BASE64URL);
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the expiration of the device and the last update date
|
|
|
|
let time_now = Utc::now().naive_utc();
|
|
|
|
self.updated_at = time_now;
|
|
|
|
|
2019-05-20 19:12:41 +00:00
|
|
|
let orgowner: Vec<_> = orgs.iter().filter(|o| o.atype == 0).map(|o| o.org_uuid.clone()).collect();
|
|
|
|
let orgadmin: Vec<_> = orgs.iter().filter(|o| o.atype == 1).map(|o| o.org_uuid.clone()).collect();
|
|
|
|
let orguser: Vec<_> = orgs.iter().filter(|o| o.atype == 2).map(|o| o.org_uuid.clone()).collect();
|
|
|
|
let orgmanager: Vec<_> = orgs.iter().filter(|o| o.atype == 3).map(|o| o.org_uuid.clone()).collect();
|
2018-04-24 20:01:55 +00:00
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
// Create the JWT claims struct, to send to the client
|
2021-03-27 14:26:32 +00:00
|
|
|
use crate::auth::{encode_jwt, LoginJwtClaims, DEFAULT_VALIDITY, JWT_LOGIN_ISSUER};
|
|
|
|
let claims = LoginJwtClaims {
|
2018-02-10 00:00:55 +00:00
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + *DEFAULT_VALIDITY).timestamp(),
|
2019-01-19 20:36:34 +00:00
|
|
|
iss: JWT_LOGIN_ISSUER.to_string(),
|
2022-07-10 14:39:38 +00:00
|
|
|
sub: user.uuid.clone(),
|
2018-04-24 20:01:55 +00:00
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
premium: true,
|
2022-07-10 14:39:38 +00:00
|
|
|
name: user.name.clone(),
|
|
|
|
email: user.email.clone(),
|
2019-11-25 05:28:49 +00:00
|
|
|
email_verified: !CONFIG.mail_enabled() || user.verified_at.is_some(),
|
2018-04-24 20:01:55 +00:00
|
|
|
|
|
|
|
orgowner,
|
|
|
|
orgadmin,
|
|
|
|
orguser,
|
2018-12-09 16:58:38 +00:00
|
|
|
orgmanager,
|
2018-04-24 20:01:55 +00:00
|
|
|
|
2022-07-10 14:39:38 +00:00
|
|
|
sstamp: user.security_stamp.clone(),
|
|
|
|
device: self.uuid.clone(),
|
2022-01-21 05:50:58 +00:00
|
|
|
scope,
|
2018-02-10 00:00:55 +00:00
|
|
|
amr: vec!["Application".into()],
|
|
|
|
};
|
|
|
|
|
|
|
|
(encode_jwt(&claims), DEFAULT_VALIDITY.num_seconds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
use crate::db::DbConn;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
/// Database methods
|
|
|
|
impl Device {
|
2022-05-20 21:39:47 +00:00
|
|
|
pub async fn save(&mut self, conn: &mut DbConn) -> EmptyResult {
|
2019-09-12 20:12:22 +00:00
|
|
|
self.updated_at = Utc::now().naive_utc();
|
|
|
|
|
2021-03-27 14:26:32 +00:00
|
|
|
db_run! { conn:
|
2020-08-18 15:15:44 +00:00
|
|
|
sqlite, mysql {
|
|
|
|
crate::util::retry(
|
|
|
|
|| diesel::replace_into(devices::table).values(DeviceDb::to_db(self)).execute(conn),
|
|
|
|
10,
|
|
|
|
).map_res("Error saving device")
|
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
let value = DeviceDb::to_db(self);
|
|
|
|
crate::util::retry(
|
2022-03-03 20:00:10 +00:00
|
|
|
|| diesel::insert_into(devices::table).values(&value).on_conflict((devices::uuid, devices::user_uuid)).do_update().set(&value).execute(conn),
|
2020-08-18 15:15:44 +00:00
|
|
|
10,
|
|
|
|
).map_res("Error saving device")
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 21:39:47 +00:00
|
|
|
pub async fn delete_all_by_user(user_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2020-08-18 15:15:44 +00:00
|
|
|
db_run! { conn: {
|
2022-03-03 20:00:10 +00:00
|
|
|
diesel::delete(devices::table.filter(devices::user_uuid.eq(user_uuid)))
|
2020-08-18 15:15:44 +00:00
|
|
|
.execute(conn)
|
2022-03-03 20:00:10 +00:00
|
|
|
.map_res("Error removing devices for user")
|
2020-08-18 15:15:44 +00:00
|
|
|
}}
|
2018-10-12 14:20:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 21:39:47 +00:00
|
|
|
pub async fn find_by_uuid_and_user(uuid: &str, user_uuid: &str, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 15:15:44 +00:00
|
|
|
db_run! { conn: {
|
|
|
|
devices::table
|
|
|
|
.filter(devices::uuid.eq(uuid))
|
2022-03-03 20:00:10 +00:00
|
|
|
.filter(devices::user_uuid.eq(user_uuid))
|
2020-08-18 15:15:44 +00:00
|
|
|
.first::<DeviceDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2023-06-11 11:28:18 +00:00
|
|
|
pub async fn find_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
|
|
|
db_run! { conn: {
|
|
|
|
devices::table
|
|
|
|
.filter(devices::user_uuid.eq(user_uuid))
|
|
|
|
.load::<DeviceDb>(conn)
|
|
|
|
.expect("Error loading devices")
|
|
|
|
.from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn find_by_uuid(uuid: &str, conn: &mut DbConn) -> Option<Self> {
|
|
|
|
db_run! { conn: {
|
|
|
|
devices::table
|
|
|
|
.filter(devices::uuid.eq(uuid))
|
|
|
|
.first::<DeviceDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn clear_push_token_by_uuid(uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
|
|
|
db_run! { conn: {
|
|
|
|
diesel::update(devices::table)
|
|
|
|
.filter(devices::uuid.eq(uuid))
|
|
|
|
.set(devices::push_token.eq::<Option<String>>(None))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing push token")
|
|
|
|
}}
|
|
|
|
}
|
2022-05-20 21:39:47 +00:00
|
|
|
pub async fn find_by_refresh_token(refresh_token: &str, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 15:15:44 +00:00
|
|
|
db_run! { conn: {
|
|
|
|
devices::table
|
|
|
|
.filter(devices::refresh_token.eq(refresh_token))
|
|
|
|
.first::<DeviceDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
2018-02-15 18:05:57 +00:00
|
|
|
|
2022-05-20 21:39:47 +00:00
|
|
|
pub async fn find_latest_active_by_user(user_uuid: &str, conn: &mut DbConn) -> Option<Self> {
|
2020-11-30 21:00:51 +00:00
|
|
|
db_run! { conn: {
|
|
|
|
devices::table
|
|
|
|
.filter(devices::user_uuid.eq(user_uuid))
|
|
|
|
.order(devices::updated_at.desc())
|
|
|
|
.first::<DeviceDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
|
|
|
}
|
2023-06-11 11:28:18 +00:00
|
|
|
pub async fn find_push_device_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
|
|
|
db_run! { conn: {
|
|
|
|
devices::table
|
|
|
|
.filter(devices::user_uuid.eq(user_uuid))
|
|
|
|
.filter(devices::push_token.is_not_null())
|
|
|
|
.load::<DeviceDb>(conn)
|
|
|
|
.expect("Error loading push devices")
|
|
|
|
.from_db()
|
|
|
|
}}
|
|
|
|
}
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|