0
Fork 0
mirror of https://github.com/dani-garcia/vaultwarden.git synced 2025-04-01 02:42:49 -05:00

add /api/devices endpoints

This commit is contained in:
Stefan Melmuk 2025-01-22 12:25:34 +01:00
parent d1dee04615
commit 12be28544e
No known key found for this signature in database
GPG key ID: 817020C608FE9C09
2 changed files with 43 additions and 4 deletions

View file

@ -30,6 +30,7 @@ pub fn routes() -> Vec<rocket::Route> {
profile,
put_profile,
post_profile,
put_avatar,
get_public_keys,
post_keys,
post_password,
@ -42,9 +43,8 @@ pub fn routes() -> Vec<rocket::Route> {
post_verify_email_token,
post_delete_recover,
post_delete_recover_token,
post_device_token,
delete_account,
post_delete_account,
delete_account,
revision_date,
password_hint,
prelogin,
@ -52,7 +52,9 @@ pub fn routes() -> Vec<rocket::Route> {
api_key,
rotate_api_key,
get_known_device,
put_avatar,
get_all_devices,
get_device,
post_device_token,
put_device_token,
put_clear_device_token,
post_clear_device_token,
@ -1068,6 +1070,29 @@ impl<'r> FromRequest<'r> for KnownDevice {
}
}
#[get("/devices")]
async fn get_all_devices(headers: Headers, mut conn: DbConn) -> JsonResult {
let devices = Device::find_by_user(&headers.user.uuid, &mut conn).await;
Ok(Json(json!({
"data": devices
.iter()
.map(|device| {
device.to_json()
}).collect::<Vec<Value>>(),
"continuationToken": null,
"object": "list"
})))
}
#[get("/devices/identifier/<device_id>")]
async fn get_device(device_id: DeviceId, headers: Headers, mut conn: DbConn) -> JsonResult {
let Some(device) = Device::find_by_uuid_and_user(&device_id, &headers.user.uuid, &mut conn).await else {
err!("No device found");
};
Ok(Json(device.to_json()))
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct PushToken {

View file

@ -1,8 +1,9 @@
use chrono::{NaiveDateTime, Utc};
use derive_more::{Display, From};
use serde_json::Value;
use super::UserId;
use crate::{crypto, CONFIG};
use crate::{crypto, util::format_date, CONFIG};
use macros::IdFromParam;
db_object! {
@ -49,6 +50,19 @@ impl Device {
}
}
pub fn to_json(&self) -> Value {
json!({
"id": self.uuid,
"name": self.name,
"type": self.atype,
"identifier": self.push_uuid,
"creationDate": format_date(&self.created_at),
"isTrusted": false,
"devicePendingAuthRequest": null,
"object":"device"
})
}
pub fn refresh_twofactor_remember(&mut self) -> String {
use data_encoding::BASE64;
let twofactor_remember = crypto::encode_random_bytes::<180>(BASE64);