web.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. use std::path::{Path, PathBuf};
  2. use rocket::http::ContentType;
  3. use rocket::response::content::Content;
  4. use rocket::response::NamedFile;
  5. use rocket::Route;
  6. use rocket_contrib::json::Json;
  7. use serde_json::Value;
  8. use crate::error::Error;
  9. use crate::util::Cached;
  10. use crate::CONFIG;
  11. pub fn routes() -> Vec<Route> {
  12. // If addding more routes here, consider also adding them to
  13. // crate::utils::LOGGED_ROUTES to make sure they appear in the log
  14. if CONFIG.web_vault_enabled() {
  15. routes![web_index, app_id, web_files, attachments, alive, static_files]
  16. } else {
  17. routes![attachments, alive, static_files]
  18. }
  19. }
  20. #[get("/")]
  21. fn web_index() -> Cached<Option<NamedFile>> {
  22. Cached::short(NamedFile::open(Path::new(&CONFIG.web_vault_folder()).join("index.html")).ok())
  23. }
  24. #[get("/app-id.json")]
  25. fn app_id() -> Cached<Content<Json<Value>>> {
  26. let content_type = ContentType::new("application", "fido.trusted-apps+json");
  27. Cached::long(Content(
  28. content_type,
  29. Json(json!({
  30. "trustedFacets": [
  31. {
  32. "version": { "major": 1, "minor": 0 },
  33. "ids": [
  34. // Per <https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-appid-and-facets-v2.0-id-20180227.html#determining-the-facetid-of-a-calling-application>:
  35. //
  36. // "In the Web case, the FacetID MUST be the Web Origin [RFC6454]
  37. // of the web page triggering the FIDO operation, written as
  38. // a URI with an empty path. Default ports are omitted and any
  39. // path component is ignored."
  40. //
  41. // This leaves it unclear as to whether the path must be empty,
  42. // or whether it can be non-empty and will be ignored. To be on
  43. // the safe side, use a proper web origin (with empty path).
  44. &CONFIG.domain_origin(),
  45. "ios:bundle-id:com.8bit.bitwarden",
  46. "android:apk-key-hash:dUGFzUzf3lmHSLBDBIv+WaFyZMI" ]
  47. }]
  48. })),
  49. ))
  50. }
  51. #[get("/<p..>", rank = 10)] // Only match this if the other routes don't match
  52. fn web_files(p: PathBuf) -> Cached<Option<NamedFile>> {
  53. Cached::long(NamedFile::open(Path::new(&CONFIG.web_vault_folder()).join(p)).ok())
  54. }
  55. #[get("/attachments/<uuid>/<file..>")]
  56. fn attachments(uuid: String, file: PathBuf) -> Option<NamedFile> {
  57. NamedFile::open(Path::new(&CONFIG.attachments_folder()).join(uuid).join(file)).ok()
  58. }
  59. #[get("/alive")]
  60. fn alive() -> Json<String> {
  61. use crate::util::format_date;
  62. use chrono::Utc;
  63. Json(format_date(&Utc::now().naive_utc()))
  64. }
  65. #[get("/bwrs_static/<filename>")]
  66. fn static_files(filename: String) -> Result<Content<&'static [u8]>, Error> {
  67. match filename.as_ref() {
  68. "mail-github.png" => Ok(Content(ContentType::PNG, include_bytes!("../static/images/mail-github.png"))),
  69. "logo-gray.png" => Ok(Content(ContentType::PNG, include_bytes!("../static/images/logo-gray.png"))),
  70. "error-x.svg" => Ok(Content(ContentType::SVG, include_bytes!("../static/images/error-x.svg"))),
  71. "hibp.png" => Ok(Content(ContentType::PNG, include_bytes!("../static/images/hibp.png"))),
  72. "bootstrap.css" => Ok(Content(ContentType::CSS, include_bytes!("../static/scripts/bootstrap.css"))),
  73. "bootstrap-native-v4.js" => Ok(Content(ContentType::JavaScript, include_bytes!("../static/scripts/bootstrap-native-v4.js"))),
  74. "md5.js" => Ok(Content(ContentType::JavaScript, include_bytes!("../static/scripts/md5.js"))),
  75. "identicon.js" => Ok(Content(ContentType::JavaScript, include_bytes!("../static/scripts/identicon.js"))),
  76. _ => err!(format!("Static file not found: {}", filename)),
  77. }
  78. }