changeset 40:99f70bfe5606

Make secret handling consistent
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 03 Dec 2020 16:42:55 +0100
parents 0723411ca8a6
children f03f642c65a9
files TODO src/db.rs src/main.rs src/notifier.rs
diffstat 4 files changed, 8 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/TODO	Thu Dec 03 09:42:25 2020 +0100
+++ b/TODO	Thu Dec 03 16:42:55 2020 +0100
@@ -1,7 +1,5 @@
 GENERAL
 
-* Proper HTTP status for invalid secret/client
-
 FEATURES
 
 * GPX/json export (with UI + API)
--- a/src/db.rs	Thu Dec 03 09:42:25 2020 +0100
+++ b/src/db.rs	Thu Dec 03 16:42:55 2020 +0100
@@ -15,7 +15,7 @@
         name: &str,
         from_ts: chrono::DateTime<chrono::Utc>,
         to_ts: chrono::DateTime<chrono::Utc>,
-        secret: &str,
+        secret: &Option<String>,
         limit: i64,
         last: Option<i32>,
     ) -> Result<types::GeoJSON, postgres::Error> {
@@ -38,11 +38,11 @@
     pub fn log_geopoint(
         &self,
         name: &str,
-        secret: &str,
+        secret: &Option<String>,
         point: &types::GeoPoint,
     ) -> Result<(), postgres::Error> {
         let stmt = self.0.prepare_cached("INSERT INTO geohub.geodata (client, lat, long, spd, t, ele, secret) VALUES ($1, $2, $3, $4, $5, $6, public.digest($7, 'sha256'))").unwrap();
-        let channel = format!("NOTIFY {}, '{}'", ids::channel_name(name, secret), name);
+        let channel = format!("NOTIFY {}, '{}'", ids::channel_name(name, secret.as_ref().unwrap_or(&"".into()).as_str()), name);
         let notify = self.0.prepare_cached(channel.as_str()).unwrap();
         stmt.execute(&[
             &name,
@@ -62,7 +62,7 @@
     pub fn check_for_new_rows(
         &self,
         name: &str,
-        secret: Option<&str>,
+        secret: &Option<String>,
         last: &Option<i32>,
         limit: &Option<i64>,
     ) -> Option<(types::GeoJSON, i32)> {
--- a/src/main.rs	Thu Dec 03 09:42:25 2020 +0100
+++ b/src/main.rs	Thu Dec 03 16:42:55 2020 +0100
@@ -24,9 +24,7 @@
     limit: Option<i64>,
 ) -> rocket_contrib::json::Json<types::LiveUpdate> {
     let db = db::DBQuery(&db.0);
-    if let Some((geojson, newlast)) =
-        db.check_for_new_rows(&name, secret.as_ref().map(|s| s.as_str()), &last, &limit)
-    {
+    if let Some((geojson, newlast)) = db.check_for_new_rows(&name, &secret, &last, &limit) {
         rocket_contrib::json::Json(types::LiveUpdate::new(Some(newlast), Some(geojson), None))
     } else {
         rocket_contrib::json::Json(types::LiveUpdate::new(
@@ -84,9 +82,8 @@
         .and_then(util::flexible_timestamp_parse)
         .unwrap_or(chrono::Utc::now());
     let limit = limit.unwrap_or(16384);
-    let secret = secret.as_ref().map(|s| s.as_str()).unwrap_or("");
 
-    let result = db.retrieve_json(name.as_str(), from_ts, to_ts, secret, limit, last);
+    let result = db.retrieve_json(name.as_str(), from_ts, to_ts, &secret, limit, last);
     match result {
         Ok(json) => http::return_json(&json),
         Err(e) => http::server_error(e.to_string()),
@@ -127,11 +124,7 @@
         spd: s,
         ele: ele,
     };
-    if let Err(e) = db.log_geopoint(
-        name.as_str(),
-        secret.as_ref().map(|s| s.as_str()).unwrap_or(""),
-        &point,
-    ) {
+    if let Err(e) = db.log_geopoint(name.as_str(), &secret, &point) {
         return http::server_error(e.to_string());
     }
     http::GeoHubResponse::Ok("".into())
--- a/src/notifier.rs	Thu Dec 03 09:42:25 2020 +0100
+++ b/src/notifier.rs	Thu Dec 03 16:42:55 2020 +0100
@@ -115,7 +115,7 @@
 
             // These queries use the primary key index returning one row only and will be quite fast.
             // Still: One query per client.
-            let rows = db.check_for_new_rows(client, Some(secret), &None, &Some(1));
+            let rows = db.check_for_new_rows(client, &Some(secret.into()), &None, &Some(1));
             if let Some((geo, last)) = rows {
                 for request in clients.remove(&chan).unwrap_or(vec![]) {
                     request