2018-02-15 00:53:11 +01:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
use super::User;
|
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
2018-02-10 01:00:55 +01:00
|
|
|
#[table_name = "devices"]
|
2018-02-15 00:40:34 +01:00
|
|
|
#[belongs_to(User, foreign_key = "user_uuid")]
|
2018-02-10 01:00:55 +01:00
|
|
|
#[primary_key(uuid)]
|
|
|
|
pub struct Device {
|
|
|
|
pub uuid: String,
|
|
|
|
pub created_at: NaiveDateTime,
|
|
|
|
pub updated_at: NaiveDateTime,
|
|
|
|
|
|
|
|
pub user_uuid: String,
|
|
|
|
|
|
|
|
pub name: String,
|
|
|
|
/// https://github.com/bitwarden/core/tree/master/src/Core/Enums
|
2019-05-20 21:12:41 +02:00
|
|
|
pub atype: i32,
|
2018-02-10 01:00:55 +01:00
|
|
|
pub push_token: Option<String>,
|
|
|
|
|
|
|
|
pub refresh_token: String,
|
2018-06-01 15:08:03 +02:00
|
|
|
|
|
|
|
pub twofactor_remember: Option<String>,
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Device {
|
2019-05-20 21:12:41 +02:00
|
|
|
pub fn new(uuid: String, user_uuid: String, name: String, atype: i32) -> Self {
|
2018-02-10 01:00:55 +01:00
|
|
|
let now = Utc::now().naive_utc();
|
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
Self {
|
2018-02-10 01:00:55 +01:00
|
|
|
uuid,
|
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
|
|
|
|
|
|
|
user_uuid,
|
|
|
|
name,
|
2019-05-20 21:12:41 +02:00
|
|
|
atype,
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
push_token: None,
|
|
|
|
refresh_token: String::new(),
|
2018-06-01 15:08:03 +02:00
|
|
|
twofactor_remember: None,
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 21:46:50 +02:00
|
|
|
pub fn refresh_twofactor_remember(&mut self) -> String {
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::crypto;
|
2018-12-30 23:34:31 +01:00
|
|
|
use data_encoding::BASE64;
|
2018-06-01 15:08:03 +02:00
|
|
|
|
2018-07-12 21:46:50 +02:00
|
|
|
let twofactor_remember = BASE64.encode(&crypto::get_random(vec![0u8; 180]));
|
|
|
|
self.twofactor_remember = Some(twofactor_remember.clone());
|
|
|
|
|
|
|
|
twofactor_remember
|
2018-06-01 15:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_twofactor_remember(&mut self) {
|
|
|
|
self.twofactor_remember = None;
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:01:55 +02:00
|
|
|
pub fn refresh_tokens(&mut self, user: &super::User, orgs: Vec<super::UserOrganization>) -> (String, i64) {
|
2018-02-10 01:00:55 +01:00
|
|
|
// If there is no refresh token, we create one
|
|
|
|
if self.refresh_token.is_empty() {
|
2018-12-07 02:05:45 +01:00
|
|
|
use crate::crypto;
|
2018-12-30 23:34:31 +01:00
|
|
|
use data_encoding::BASE64URL;
|
2018-02-10 01:00:55 +01:00
|
|
|
|
|
|
|
self.refresh_token = BASE64URL.encode(&crypto::get_random_64());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 21:12:41 +02: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 22:01:55 +02:00
|
|
|
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
// Create the JWT claims struct, to send to the client
|
2019-01-19 21:36:34 +01:00
|
|
|
use crate::auth::{encode_jwt, LoginJWTClaims, DEFAULT_VALIDITY, JWT_LOGIN_ISSUER};
|
|
|
|
let claims = LoginJWTClaims {
|
2018-02-10 01:00:55 +01:00
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + *DEFAULT_VALIDITY).timestamp(),
|
2019-01-19 21:36:34 +01:00
|
|
|
iss: JWT_LOGIN_ISSUER.to_string(),
|
2018-02-10 01:00:55 +01:00
|
|
|
sub: user.uuid.to_string(),
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
premium: true,
|
|
|
|
name: user.name.to_string(),
|
|
|
|
email: user.email.to_string(),
|
|
|
|
email_verified: true,
|
2018-04-24 22:01:55 +02:00
|
|
|
|
|
|
|
orgowner,
|
|
|
|
orgadmin,
|
|
|
|
orguser,
|
2018-12-09 17:58:38 +01:00
|
|
|
orgmanager,
|
2018-04-24 22:01:55 +02:00
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
sstamp: user.security_stamp.to_string(),
|
|
|
|
device: self.uuid.to_string(),
|
|
|
|
scope: vec!["api".into(), "offline_access".into()],
|
|
|
|
amr: vec!["Application".into()],
|
|
|
|
};
|
|
|
|
|
|
|
|
(encode_jwt(&claims), DEFAULT_VALIDITY.num_seconds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
use crate::db::schema::devices;
|
|
|
|
use crate::db::DbConn;
|
2018-02-10 01:00:55 +01:00
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-02-10 01:00:55 +01:00
|
|
|
/// Database methods
|
|
|
|
impl Device {
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
|
2018-02-15 01:07:57 +01:00
|
|
|
self.updated_at = Utc::now().naive_utc();
|
2018-02-10 01:00:55 +01:00
|
|
|
|
2018-12-12 22:15:54 +01:00
|
|
|
crate::util::retry(
|
2018-12-30 23:34:31 +01:00
|
|
|
|| diesel::replace_into(devices::table).values(&*self).execute(&**conn),
|
2018-12-12 22:15:54 +01:00
|
|
|
10,
|
|
|
|
)
|
2018-12-19 21:52:53 +01:00
|
|
|
.map_res("Error saving device")
|
2018-02-10 01:00:55 +01: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(devices::table.filter(devices::uuid.eq(self.uuid)))
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error removing device")
|
2018-10-12 16:20:10 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-10-12 16:20:10 +02:00
|
|
|
for device in Self::find_by_user(user_uuid, &conn) {
|
|
|
|
device.delete(&conn)?;
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
2018-10-12 16:20:10 +02:00
|
|
|
Ok(())
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
|
2018-02-10 01:00:55 +01:00
|
|
|
devices::table
|
|
|
|
.filter(devices::uuid.eq(uuid))
|
2018-12-30 23:34:31 +01:00
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 00:40:34 +01:00
|
|
|
pub fn find_by_refresh_token(refresh_token: &str, conn: &DbConn) -> Option<Self> {
|
2018-02-10 01:00:55 +01:00
|
|
|
devices::table
|
|
|
|
.filter(devices::refresh_token.eq(refresh_token))
|
2018-12-30 23:34:31 +01:00
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|
2018-02-15 19:05:57 +01:00
|
|
|
|
|
|
|
pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
devices::table
|
|
|
|
.filter(devices::user_uuid.eq(user_uuid))
|
2018-12-30 23:34:31 +01:00
|
|
|
.load::<Self>(&**conn)
|
|
|
|
.expect("Error loading devices")
|
2018-02-15 19:05:57 +01:00
|
|
|
}
|
2018-02-10 01:00:55 +01:00
|
|
|
}
|