2018-02-14 23:53:11 +00:00
|
|
|
use chrono::{NaiveDateTime, Utc};
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
use super::User;
|
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
2018-02-10 00:00:55 +00:00
|
|
|
#[table_name = "devices"]
|
2018-02-14 23:40:34 +00:00
|
|
|
#[belongs_to(User, foreign_key = "user_uuid")]
|
2018-02-10 00:00:55 +00: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
|
|
|
|
pub type_: i32,
|
|
|
|
pub push_token: Option<String>,
|
|
|
|
|
|
|
|
pub refresh_token: String,
|
2018-06-01 13:08:03 +00:00
|
|
|
|
|
|
|
pub twofactor_remember: Option<String>,
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Device {
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn new(uuid: String, user_uuid: String, name: String, type_: 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,
|
|
|
|
type_,
|
|
|
|
|
|
|
|
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-06-01 13:08:03 +00:00
|
|
|
pub fn refresh_twofactor_remember(&mut self) {
|
|
|
|
use data_encoding::BASE64;
|
|
|
|
use crypto;
|
|
|
|
|
|
|
|
self.twofactor_remember = Some(BASE64.encode(&crypto::get_random(vec![0u8; 180])));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_twofactor_remember(&mut self) {
|
|
|
|
self.twofactor_remember = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-24 20:01:55 +00:00
|
|
|
pub fn refresh_tokens(&mut self, user: &super::User, orgs: Vec<super::UserOrganization>) -> (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() {
|
|
|
|
use data_encoding::BASE64URL;
|
|
|
|
use crypto;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-04-24 20:01:55 +00:00
|
|
|
|
|
|
|
let orgowner: Vec<_> = orgs.iter().filter(|o| o.type_ == 0).map(|o| o.org_uuid.clone()).collect();
|
|
|
|
let orgadmin: Vec<_> = orgs.iter().filter(|o| o.type_ == 1).map(|o| o.org_uuid.clone()).collect();
|
|
|
|
let orguser: Vec<_> = orgs.iter().filter(|o| o.type_ == 2).map(|o| o.org_uuid.clone()).collect();
|
|
|
|
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
// Create the JWT claims struct, to send to the client
|
|
|
|
use auth::{encode_jwt, JWTClaims, DEFAULT_VALIDITY, JWT_ISSUER};
|
|
|
|
let claims = JWTClaims {
|
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + *DEFAULT_VALIDITY).timestamp(),
|
|
|
|
iss: JWT_ISSUER.to_string(),
|
|
|
|
sub: user.uuid.to_string(),
|
2018-04-24 20:01:55 +00:00
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
premium: true,
|
|
|
|
name: user.name.to_string(),
|
|
|
|
email: user.email.to_string(),
|
|
|
|
email_verified: true,
|
2018-04-24 20:01:55 +00:00
|
|
|
|
|
|
|
orgowner,
|
|
|
|
orgadmin,
|
|
|
|
orguser,
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
sstamp: user.security_stamp.to_string(),
|
|
|
|
device: self.uuid.to_string(),
|
|
|
|
scope: vec!["api".into(), "offline_access".into()],
|
|
|
|
amr: vec!["Application".into()],
|
|
|
|
};
|
|
|
|
|
2018-04-24 20:01:55 +00:00
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
(encode_jwt(&claims), DEFAULT_VALIDITY.num_seconds())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
use db::DbConn;
|
|
|
|
use db::schema::devices;
|
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl Device {
|
2018-02-15 00:07:57 +00:00
|
|
|
pub fn save(&mut self, conn: &DbConn) -> bool {
|
|
|
|
self.updated_at = Utc::now().naive_utc();
|
2018-02-10 00:00:55 +00:00
|
|
|
|
|
|
|
match diesel::replace_into(devices::table)
|
2018-02-15 00:07:57 +00:00
|
|
|
.values(&*self)
|
2018-02-10 00:00:55 +00:00
|
|
|
.execute(&**conn) {
|
|
|
|
Ok(1) => true, // One row inserted
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete(self, conn: &DbConn) -> bool {
|
|
|
|
match diesel::delete(devices::table.filter(
|
|
|
|
devices::uuid.eq(self.uuid)))
|
|
|
|
.execute(&**conn) {
|
|
|
|
Ok(1) => true, // One row deleted
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
|
2018-02-10 00:00:55 +00:00
|
|
|
devices::table
|
|
|
|
.filter(devices::uuid.eq(uuid))
|
2018-02-14 23:40:34 +00:00
|
|
|
.first::<Self>(&**conn).ok()
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 23:40:34 +00:00
|
|
|
pub fn find_by_refresh_token(refresh_token: &str, conn: &DbConn) -> Option<Self> {
|
2018-02-10 00:00:55 +00:00
|
|
|
devices::table
|
|
|
|
.filter(devices::refresh_token.eq(refresh_token))
|
2018-02-14 23:40:34 +00:00
|
|
|
.first::<Self>(&**conn).ok()
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
2018-02-15 18:05:57 +00:00
|
|
|
|
|
|
|
pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
devices::table
|
|
|
|
.filter(devices::user_uuid.eq(user_uuid))
|
|
|
|
.load::<Self>(&**conn).expect("Error loading devices")
|
|
|
|
}
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|