From 3e5971b9dbfa0eabe69b682d848009741b435758 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 27 Mar 2021 15:07:26 +0000 Subject: [PATCH] Remove unnecessary result return types --- src/api/admin.rs | 14 +++++----- src/api/core/accounts.rs | 11 ++++---- src/api/core/ciphers.rs | 14 +++++----- src/api/core/folders.rs | 6 ++-- src/api/core/mod.rs | 21 +++++++------- src/api/core/organizations.rs | 50 +++++++++++++++++----------------- src/api/core/two_factor/mod.rs | 6 ++-- src/api/notifications.rs | 8 +++--- 8 files changed, 66 insertions(+), 64 deletions(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index 39fbc691..756f61c5 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -13,7 +13,7 @@ use rocket::{ use rocket_contrib::json::Json; use crate::{ - api::{ApiResult, EmptyResult, JsonResult, NumberOrString}, + api::{ApiResult, EmptyResult, NumberOrString}, auth::{decode_admin, encode_jwt, generate_admin_claims, ClientIp}, config::ConfigBuilder, db::{backup_database, models::*, DbConn, DbConnType}, @@ -291,17 +291,17 @@ fn test_smtp(data: Json, _token: AdminToken) -> EmptyResult { } #[get("/logout")] -fn logout(mut cookies: Cookies, referer: Referer) -> Result { +fn logout(mut cookies: Cookies, referer: Referer) -> Redirect { cookies.remove(Cookie::named(COOKIE_NAME)); - Ok(Redirect::to(admin_url(referer))) + Redirect::to(admin_url(referer)) } #[get("/users")] -fn get_users_json(_token: AdminToken, conn: DbConn) -> JsonResult { +fn get_users_json(_token: AdminToken, conn: DbConn) -> Json { let users = User::get_all(&conn); let users_json: Vec = users.iter().map(|u| u.to_json(&conn)).collect(); - Ok(Json(Value::Array(users_json))) + Json(Value::Array(users_json)) } #[get("/users/overview")] @@ -548,9 +548,9 @@ fn diagnostics(_token: AdminToken, _conn: DbConn) -> ApiResult> { } #[get("/diagnostics/config")] -fn get_diagnostics_config(_token: AdminToken) -> JsonResult { +fn get_diagnostics_config(_token: AdminToken) -> Json { let support_json = CONFIG.get_support_json(); - Ok(Json(support_json)) + Json(support_json) } #[post("/config", data = "")] diff --git a/src/api/core/accounts.rs b/src/api/core/accounts.rs index 2d747fee..6e45a947 100644 --- a/src/api/core/accounts.rs +++ b/src/api/core/accounts.rs @@ -1,5 +1,6 @@ use chrono::Utc; use rocket_contrib::json::Json; +use serde_json::Value; use crate::{ api::{EmptyResult, JsonResult, JsonUpcase, Notify, NumberOrString, PasswordData, UpdateType}, @@ -148,8 +149,8 @@ fn register(data: JsonUpcase, conn: DbConn) -> EmptyResult { } #[get("/accounts/profile")] -fn profile(headers: Headers, conn: DbConn) -> JsonResult { - Ok(Json(headers.user.to_json(&conn))) +fn profile(headers: Headers, conn: DbConn) -> Json { + Json(headers.user.to_json(&conn)) } #[derive(Deserialize, Debug)] @@ -610,7 +611,7 @@ struct PreloginData { } #[post("/accounts/prelogin", data = "")] -fn prelogin(data: JsonUpcase, conn: DbConn) -> JsonResult { +fn prelogin(data: JsonUpcase, conn: DbConn) -> Json { let data: PreloginData = data.into_inner().data; let (kdf_type, kdf_iter) = match User::find_by_mail(&data.Email, &conn) { @@ -618,10 +619,10 @@ fn prelogin(data: JsonUpcase, conn: DbConn) -> JsonResult { None => (User::CLIENT_KDF_TYPE_DEFAULT, User::CLIENT_KDF_ITER_DEFAULT), }; - Ok(Json(json!({ + Json(json!({ "Kdf": kdf_type, "KdfIterations": kdf_iter - }))) + })) } #[derive(Deserialize)] #[allow(non_snake_case)] diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index c9abb3d4..7b0de205 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -84,7 +84,7 @@ struct SyncData { } #[get("/sync?")] -fn sync(data: Form, headers: Headers, conn: DbConn) -> JsonResult { +fn sync(data: Form, headers: Headers, conn: DbConn) -> Json { let user_json = headers.user.to_json(&conn); let folders = Folder::find_by_user(&headers.user.uuid, &conn); @@ -113,10 +113,10 @@ fn sync(data: Form, headers: Headers, conn: DbConn) -> JsonResult { let domains_json = if data.exclude_domains { Value::Null } else { - api::core::_get_eq_domains(headers, true).unwrap().into_inner() + api::core::_get_eq_domains(headers, true).into_inner() }; - Ok(Json(json!({ + Json(json!({ "Profile": user_json, "Folders": folders_json, "Collections": collections_json, @@ -125,11 +125,11 @@ fn sync(data: Form, headers: Headers, conn: DbConn) -> JsonResult { "Domains": domains_json, "Sends": sends_json, "Object": "sync" - }))) + })) } #[get("/ciphers")] -fn get_ciphers(headers: Headers, conn: DbConn) -> JsonResult { +fn get_ciphers(headers: Headers, conn: DbConn) -> Json { let ciphers = Cipher::find_by_user_visible(&headers.user.uuid, &conn); let ciphers_json: Vec = ciphers @@ -137,11 +137,11 @@ fn get_ciphers(headers: Headers, conn: DbConn) -> JsonResult { .map(|c| c.to_json(&headers.host, &headers.user.uuid, &conn)) .collect(); - Ok(Json(json!({ + Json(json!({ "Data": ciphers_json, "Object": "list", "ContinuationToken": null - }))) + })) } #[get("/ciphers/")] diff --git a/src/api/core/folders.rs b/src/api/core/folders.rs index 63bf40e2..2779fe61 100644 --- a/src/api/core/folders.rs +++ b/src/api/core/folders.rs @@ -20,16 +20,16 @@ pub fn routes() -> Vec { } #[get("/folders")] -fn get_folders(headers: Headers, conn: DbConn) -> JsonResult { +fn get_folders(headers: Headers, conn: DbConn) -> Json { let folders = Folder::find_by_user(&headers.user.uuid, &conn); let folders_json: Vec = folders.iter().map(Folder::to_json).collect(); - Ok(Json(json!({ + Json(json!({ "Data": folders_json, "Object": "list", "ContinuationToken": null, - }))) + })) } #[get("/folders/")] diff --git a/src/api/core/mod.rs b/src/api/core/mod.rs index f0b449c4..36e83f0e 100644 --- a/src/api/core/mod.rs +++ b/src/api/core/mod.rs @@ -34,17 +34,18 @@ pub fn routes() -> Vec { // use rocket::Route; use rocket_contrib::json::Json; +use rocket::response::Response; use serde_json::Value; use crate::{ - api::{EmptyResult, JsonResult, JsonUpcase}, + api::{JsonResult, JsonUpcase}, auth::Headers, db::DbConn, error::Error, }; #[put("/devices/identifier//clear-token")] -fn clear_device_token(uuid: String) -> EmptyResult { +fn clear_device_token<'a>(uuid: String) -> Response<'a> { // This endpoint doesn't have auth header let _ = uuid; @@ -53,11 +54,11 @@ fn clear_device_token(uuid: String) -> EmptyResult { // This only clears push token // https://github.com/bitwarden/core/blob/master/src/Api/Controllers/DevicesController.cs#L109 // https://github.com/bitwarden/core/blob/master/src/Core/Services/Implementations/DeviceService.cs#L37 - Ok(()) + Response::new() } #[put("/devices/identifier//token", data = "")] -fn put_device_token(uuid: String, data: JsonUpcase, headers: Headers) -> JsonResult { +fn put_device_token(uuid: String, data: JsonUpcase, headers: Headers) -> Json { let _data: Value = data.into_inner().data; // Data has a single string value "PushToken" let _ = uuid; @@ -65,13 +66,13 @@ fn put_device_token(uuid: String, data: JsonUpcase, headers: Headers) -> // TODO: This should save the push token, but we don't have push functionality - Ok(Json(json!({ + Json(json!({ "Id": headers.device.uuid, "Name": headers.device.name, "Type": headers.device.atype, "Identifier": headers.device.uuid, "CreationDate": crate::util::format_date(&headers.device.created_at), - }))) + })) } #[derive(Serialize, Deserialize, Debug)] @@ -85,11 +86,11 @@ struct GlobalDomain { const GLOBAL_DOMAINS: &str = include_str!("../../static/global_domains.json"); #[get("/settings/domains")] -fn get_eq_domains(headers: Headers) -> JsonResult { +fn get_eq_domains(headers: Headers) -> Json { _get_eq_domains(headers, false) } -fn _get_eq_domains(headers: Headers, no_excluded: bool) -> JsonResult { +fn _get_eq_domains(headers: Headers, no_excluded: bool) -> Json { let user = headers.user; use serde_json::from_str; @@ -106,11 +107,11 @@ fn _get_eq_domains(headers: Headers, no_excluded: bool) -> JsonResult { globals.retain(|g| !g.Excluded); } - Ok(Json(json!({ + Json(json!({ "EquivalentDomains": equivalent_domains, "GlobalEquivalentDomains": globals, "Object": "domains", - }))) + })) } #[derive(Deserialize, Debug)] diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 34a0acdb..5698c187 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -192,8 +192,8 @@ fn post_organization( // GET /api/collections?writeOnly=false #[get("/collections")] -fn get_user_collections(headers: Headers, conn: DbConn) -> JsonResult { - Ok(Json(json!({ +fn get_user_collections(headers: Headers, conn: DbConn) -> Json { + Json(json!({ "Data": Collection::find_by_user_uuid(&headers.user.uuid, &conn) .iter() @@ -201,12 +201,12 @@ fn get_user_collections(headers: Headers, conn: DbConn) -> JsonResult { .collect::(), "Object": "list", "ContinuationToken": null, - }))) + })) } #[get("/organizations//collections")] -fn get_org_collections(org_id: String, _headers: AdminHeaders, conn: DbConn) -> JsonResult { - Ok(Json(json!({ +fn get_org_collections(org_id: String, _headers: AdminHeaders, conn: DbConn) -> Json { + Json(json!({ "Data": Collection::find_by_organization(&org_id, &conn) .iter() @@ -214,7 +214,7 @@ fn get_org_collections(org_id: String, _headers: AdminHeaders, conn: DbConn) -> .collect::(), "Object": "list", "ContinuationToken": null, - }))) + })) } #[post("/organizations//collections", data = "")] @@ -441,30 +441,30 @@ struct OrgIdData { } #[get("/ciphers/organization-details?")] -fn get_org_details(data: Form, headers: Headers, conn: DbConn) -> JsonResult { +fn get_org_details(data: Form, headers: Headers, conn: DbConn) -> Json { let ciphers = Cipher::find_by_org(&data.organization_id, &conn); let ciphers_json: Vec = ciphers .iter() .map(|c| c.to_json(&headers.host, &headers.user.uuid, &conn)) .collect(); - Ok(Json(json!({ + Json(json!({ "Data": ciphers_json, "Object": "list", "ContinuationToken": null, - }))) + })) } #[get("/organizations//users")] -fn get_org_users(org_id: String, _headers: ManagerHeadersLoose, conn: DbConn) -> JsonResult { +fn get_org_users(org_id: String, _headers: ManagerHeadersLoose, conn: DbConn) -> Json { let users = UserOrganization::find_by_org(&org_id, &conn); let users_json: Vec = users.iter().map(|c| c.to_json_user_details(&conn)).collect(); - Ok(Json(json!({ + Json(json!({ "Data": users_json, "Object": "list", "ContinuationToken": null, - }))) + })) } #[derive(Deserialize)] @@ -930,15 +930,15 @@ fn post_org_import( } #[get("/organizations//policies")] -fn list_policies(org_id: String, _headers: AdminHeaders, conn: DbConn) -> JsonResult { +fn list_policies(org_id: String, _headers: AdminHeaders, conn: DbConn) -> Json { let policies = OrgPolicy::find_by_org(&org_id, &conn); let policies_json: Vec = policies.iter().map(OrgPolicy::to_json).collect(); - Ok(Json(json!({ + Json(json!({ "Data": policies_json, "Object": "list", "ContinuationToken": null - }))) + })) } #[get("/organizations//policies/token?")] @@ -1017,8 +1017,8 @@ fn get_organization_tax(org_id: String, _headers: Headers, _conn: DbConn) -> Emp } #[get("/plans")] -fn get_plans(_headers: Headers, _conn: DbConn) -> JsonResult { - Ok(Json(json!({ +fn get_plans(_headers: Headers, _conn: DbConn) -> Json { + Json(json!({ "Object": "list", "Data": [ { @@ -1065,17 +1065,17 @@ fn get_plans(_headers: Headers, _conn: DbConn) -> JsonResult { } ], "ContinuationToken": null - }))) + })) } #[get("/plans/sales-tax-rates")] -fn get_plans_tax_rates(_headers: Headers, _conn: DbConn) -> JsonResult { +fn get_plans_tax_rates(_headers: Headers, _conn: DbConn) -> Json { // Prevent a 404 error, which also causes Javascript errors. - Ok(Json(json!({ + Json(json!({ "Object": "list", "Data": [], "ContinuationToken": null - }))) + })) } #[derive(Deserialize, Debug)] @@ -1128,7 +1128,7 @@ fn import(org_id: String, data: JsonUpcase, headers: Headers, con // If user is not part of the organization, but it exists } else if UserOrganization::find_by_email_and_org(&user_data.Email, &org_id, &conn).is_none() { if let Some (user) = User::find_by_mail(&user_data.Email, &conn) { - + let user_org_status = if CONFIG.mail_enabled() { UserOrgStatus::Invited as i32 } else { @@ -1157,18 +1157,18 @@ fn import(org_id: String, data: JsonUpcase, headers: Headers, con Some(headers.user.email.clone()), )?; } - } + } } } // If this flag is enabled, any user that isn't provided in the Users list will be removed (by default they will be kept unless they have Deleted == true) if data.OverwriteExisting { - for user_org in UserOrganization::find_by_org_and_type(&org_id, UserOrgType::User as i32, &conn) { + for user_org in UserOrganization::find_by_org_and_type(&org_id, UserOrgType::User as i32, &conn) { if let Some (user_email) = User::find_by_uuid(&user_org.user_uuid, &conn).map(|u| u.email) { if !data.Users.iter().any(|u| u.Email == user_email) { user_org.delete(&conn)?; } - } + } } } diff --git a/src/api/core/two_factor/mod.rs b/src/api/core/two_factor/mod.rs index 77e2cf2e..a3dfd319 100644 --- a/src/api/core/two_factor/mod.rs +++ b/src/api/core/two_factor/mod.rs @@ -38,15 +38,15 @@ pub fn routes() -> Vec { } #[get("/two-factor")] -fn get_twofactor(headers: Headers, conn: DbConn) -> JsonResult { +fn get_twofactor(headers: Headers, conn: DbConn) -> Json { let twofactors = TwoFactor::find_by_user(&headers.user.uuid, &conn); let twofactors_json: Vec = twofactors.iter().map(TwoFactor::to_json_provider).collect(); - Ok(Json(json!({ + Json(json!({ "Data": twofactors_json, "Object": "list", "ContinuationToken": null, - }))) + })) } #[post("/two-factor/get-recover", data = "")] diff --git a/src/api/notifications.rs b/src/api/notifications.rs index ff3c0054..8876937d 100644 --- a/src/api/notifications.rs +++ b/src/api/notifications.rs @@ -5,7 +5,7 @@ use rocket_contrib::json::Json; use serde_json::Value as JsonValue; use crate::{ - api::{EmptyResult, JsonResult}, + api::EmptyResult, auth::Headers, db::DbConn, Error, CONFIG, @@ -31,7 +31,7 @@ fn websockets_err() -> EmptyResult { } #[post("/hub/negotiate")] -fn negotiate(_headers: Headers, _conn: DbConn) -> JsonResult { +fn negotiate(_headers: Headers, _conn: DbConn) -> Json { use crate::crypto; use data_encoding::BASE64URL; @@ -47,10 +47,10 @@ fn negotiate(_headers: Headers, _conn: DbConn) -> JsonResult { // Rocket SSE support: https://github.com/SergioBenitez/Rocket/issues/33 // {"transport":"ServerSentEvents", "transferFormats":["Text"]}, // {"transport":"LongPolling", "transferFormats":["Text","Binary"]} - Ok(Json(json!({ + Json(json!({ "connectionId": conn_id, "availableTransports": available_transports - }))) + })) } //