Repository: driftluo/InfluxDBClient-rs (language: rust)

Issue:
Add Token-based authentication
I see that you can set_authentication using a username and password.  But, in many cases an authentication token is preferred.


Discussion / hints:
https://docs.influxdata.com/influxdb/v1.8/administration/authentication_and_authorization/#3-include-the-token-in-http-requests

Are you talking about this kind of jwt token? This is very simple, you only need to add an interface.

This seems to be a newly added feature, there was no such authentication method in the previous version

Fix patch:
diff --git a/src/client.rs b/src/client.rs
index 77dd5f5..8d6f4b5 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -12,6 +12,7 @@ pub struct Client {
     host: Url,
     db: String,
     authentication: Option<(String, String)>,
+    jwt_token: Option<String>,
     client: HttpClient,
 }
 
@@ -25,6 +26,7 @@ impl Client {
             host,
             db: db.into(),
             authentication: None,
+            jwt_token: None,
             client: HttpClient::default(),
         }
     }
@@ -38,6 +40,7 @@ impl Client {
             host,
             db: db.into(),
             authentication: None,
+            jwt_token: None,
             client,
         }
     }
@@ -59,6 +62,15 @@ impl Client {
         self
     }
 
+    /// Set the client's jwt token
+    pub fn set_jwt_token<T>(mut self, token: T) -> Self
+    where
+        T: Into<String>,
+    {
+        self.jwt_token = Some(token.into());
+        self
+    }
+
     /// View the current db name
     pub fn get_db(&self) -> &str {
         self.db.as_str()
@@ -69,10 +81,7 @@ impl Client {
         let url = self.build_url("ping", None);
         self.client.get(url).send().map(move |res| {
             if let Ok(res) = res {
-                match res.status().as_u16() {
-                    204 => true,
-                    _ => false,
-                }
+                matches!(res.status().as_u16(), 204)
             } else {
                 false
             }
@@ -397,14 +406,20 @@ impl Client {
         let url = self.build_url("query", Some(param));
 
         let q_lower = q.to_lowercase();
-        let resp_future = if q_lower.starts_with("select") && !q_lower.contains("into")
+        let mut builder = if q_lower.starts_with("select") && !q_lower.contains("into")
             || q_lower.starts_with("show")
         {
-            self.client.get(url).send().boxed()
+            self.client.get(url)
         } else {
-            self.client.post(url).send().boxed()
+            self.client.post(url)
         };
 
+        if let Some(ref token) = self.jwt_token {
+            builder = builder.bearer_auth(token);
+        }
+
+        let resp_future = builder.send().boxed();
+
         async move {
             let res = resp_future.await?;
             match res.status().as_u16() {
diff --git a/src/keys.rs b/src/keys.rs
index b55a76c..c7b263c 100644
--- a/src/keys.rs
+++ b/src/keys.rs
@@ -1,5 +1,4 @@
 use serde::{Deserialize, Serialize};
-use serde_json;
 use std::collections::HashMap;
 use std::iter::FromIterator;
 use std::iter::Iterator;


Test patch: