Skip to content

Commit

Permalink
Fix auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
syepes committed Jan 12, 2023
1 parent 148bd32 commit a4c751b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 119 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachang

## [Unreleased]

## 1.0.3 - 2023-01-12

- Fix the reutilization of the auth token

## 1.0.2 - 2023-01-07

- Fix lint warning
Expand Down
106 changes: 31 additions & 75 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sio2prom"
version = "1.0.2"
version = "1.0.3"
edition = "2021"
authors = ["Sebastian YEPES F <syepes@gmail.com>"]
keywords = ["prometheus", "metrics", "scaleio", "VxFlex", "PowerFlex"]
Expand Down
88 changes: 45 additions & 43 deletions src/sio/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,52 @@ impl<'a> ClientInfo<'a> {

async fn auth(&mut self) {
trace!("auth");
if let Ok(c) = reqwest::Client::builder().user_agent(env!("CARGO_PKG_NAME")).danger_accept_invalid_certs(true).timeout(Duration::from_secs(10)).connection_verbose(true).build() {
if !self.auth_usr.unwrap().is_empty() && !self.auth_pwd.unwrap().is_empty() {
let req_url = format!("https://{ip}/api/login", ip = self.ip.unwrap());
trace!("Auth on {:?} with {:?}/{:?}", req_url.clone(), self.auth_usr.unwrap().to_string(), self.auth_pwd.unwrap().to_string());

let req = c.get(req_url).basic_auth(self.auth_usr.unwrap(), Some(self.auth_pwd.unwrap()));
match req.send().await {
Ok(r) => {
trace!("resp:{:#?}", r);
match r.status() {
StatusCode::OK => {
match r.json::<serde_json::Value>().await {
Ok(t) => {
*self.token.borrow_mut() = Some(t.to_string().replace('"', ""));
},
_ => *self.token.borrow_mut() = None,
}
},
StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => {
*self.token.borrow_mut() = None;
let msg: String = match r.json::<serde_json::Value>().await {
Ok(Value::Object(m)) => m.get("message").map(|m| m.to_string().replace('"', "")).unwrap_or_else(|| "unknown".to_string()),
_ => "unknown".to_string(),
};
error!("Auth failed: {:?}", msg);
},
_ => {
*self.token.borrow_mut() = None;
let msg: String = match r.json::<serde_json::Value>().await {
Ok(Value::Object(m)) => m.get("message").map(|m| m.to_string().replace('"', "")).unwrap_or_else(|| "unknown".to_string()),
_ => "unknown".to_string(),
};
error!("Unknown auth request error: {:?}", msg);
},
};
},
Err(e) => {
*self.token.borrow_mut() = None;
error!("Auth request error: {:?}", e.to_string());
},
if self.token.borrow().is_none() {
if let Ok(c) = reqwest::Client::builder().user_agent(env!("CARGO_PKG_NAME")).danger_accept_invalid_certs(true).timeout(Duration::from_secs(10)).connection_verbose(true).build() {
if !self.auth_usr.unwrap().is_empty() && !self.auth_pwd.unwrap().is_empty() && self.token.borrow().is_none() {
let req_url = format!("https://{ip}/api/login", ip = self.ip.unwrap());
trace!("Auth on {:?} with {:?}/{:?}", req_url.clone(), self.auth_usr.unwrap().to_string(), self.auth_pwd.unwrap().to_string());

let req = c.get(req_url).basic_auth(self.auth_usr.unwrap(), Some(self.auth_pwd.unwrap()));
match req.send().await {
Ok(r) => {
trace!("resp:{:#?}", r);
match r.status() {
StatusCode::OK => {
match r.json::<serde_json::Value>().await {
Ok(t) => {
*self.token.borrow_mut() = Some(t.to_string().replace('"', ""));
},
_ => *self.token.borrow_mut() = None,
}
},
StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => {
*self.token.borrow_mut() = None;
let msg: String = match r.json::<serde_json::Value>().await {
Ok(Value::Object(m)) => m.get("message").map(|m| m.to_string().replace('"', "")).unwrap_or_else(|| "unknown".to_string()),
_ => "unknown".to_string(),
};
error!("Auth failed: {:?}", msg);
},
_ => {
*self.token.borrow_mut() = None;
let msg: String = match r.json::<serde_json::Value>().await {
Ok(Value::Object(m)) => m.get("message").map(|m| m.to_string().replace('"', "")).unwrap_or_else(|| "unknown".to_string()),
_ => "unknown".to_string(),
};
error!("Unknown auth request error: {:?}", msg);
},
};
},
Err(e) => {
*self.token.borrow_mut() = None;
error!("Auth request error: {:?}", e.to_string());
},
}
} else {
*self.token.borrow_mut() = None;
error!("Auth missing credentials");
}
} else {
*self.token.borrow_mut() = None;
error!("Auth missing credentials");
}
}

Expand Down

0 comments on commit a4c751b

Please sign in to comment.