Skip to content

ADD: logo_path and is_stealer_log attribute #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@

use std::io::{BufRead, Cursor};
use derive_builder::Builder;
use reqwest;
use reqwest::{Response, StatusCode};
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
use sha1::{Sha1};
use serde_json::{from_str};
use derive_builder::Builder;
use reqwest::{Response, StatusCode};
use serde_json::from_str;
use sha1::Sha1;
use std::io::{BufRead, Cursor};

use crate::{errors::Result, model::{Breach, Password}};
use crate::{
errors::Result,
model::{Breach, Password},
};

static MAIN_API_URL : &'static str = "https://haveibeenpwned.com/api/v3/";
static RANGE_API_URL : &'static str = "https://api.pwnedpasswords.com/range/";
static DEFAULT_USER_AGENT : &'static str = "wisespace-io";
static MAIN_API_URL: &'static str = "https://haveibeenpwned.com/api/v3/";
static RANGE_API_URL: &'static str = "https://api.pwnedpasswords.com/range/";
static DEFAULT_USER_AGENT: &'static str = "wisespace-io";
static API_KEY: &'static str = "hibp-api-key";

#[derive(Builder, Debug, PartialEq)]
Expand All @@ -28,7 +30,7 @@ pub struct Pwned {
pub pad_password_responses: bool,

#[builder(setter(into), default = "None")]
pub api_key: Option<String>
pub api_key: Option<String>,
}

impl PwnedBuilder {
Expand All @@ -43,7 +45,8 @@ impl PwnedBuilder {

impl Pwned {
pub async fn check_password<P>(&self, password: P) -> Result<Password>
where P: Into<String>
where
P: Into<String>,
{
let mut sha1 = Sha1::new();

Expand All @@ -62,24 +65,32 @@ impl Pwned {
let v: Vec<&str> = value.split(":").collect();
let count = v[1].parse::<u64>().unwrap();
let found = count > 0;
return Ok(Password {found, count});
return Ok(Password { found, count });
}
}
Ok(Password {found: false, count: 0})
},
Ok(Password {
found: false,
count: 0,
})
}
Err(e) => Err(e),
}
}

pub async fn check_email<E>(&self, email: E) -> Result<Vec<Breach>>
where E: Into<String>
where
E: Into<String>,
{
let url = format!("{}breachedaccount/{}?truncateResponse=false", MAIN_API_URL, email.into());
let url = format!(
"{}breachedaccount/{}?truncateResponse=false",
MAIN_API_URL,
email.into()
);
match self.get(url).await {
Ok(answer) => {
let breach: Vec<Breach> = from_str(answer.as_str()).unwrap();
Ok(breach)
},
}
Err(e) => Err(e),
}
}
Expand All @@ -99,26 +110,23 @@ impl Pwned {
let response = client
.get(url.as_str())
.headers(custom_headers)
.send().await?;
.send()
.await?;

self.handler(response).await
}

async fn handler(&self, response: Response) -> Result<String> {
match response.status() {
StatusCode::OK => {
Ok(response.text().await?)
},
StatusCode::NOT_FOUND => {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"The account could not be found and has therefore not been pwned").into())
}
StatusCode::OK => Ok(response.text().await?),
StatusCode::NOT_FOUND => Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"The account could not be found and has therefore not been pwned",
)
.into()),
status => {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("{:?}", status)).into())
Err(std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", status)).into())
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std;
use reqwest;
use std;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod model;

pub mod api;
pub mod errors;
pub mod api;
4 changes: 3 additions & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde_derive::{Serialize, Deserialize};
use serde_derive::{Deserialize, Serialize};

#[derive(Debug)]
pub struct Password {
Expand All @@ -21,4 +21,6 @@ pub struct Breach {
pub is_sensitive: bool,
pub is_retired: bool,
pub is_spam_list: bool,
pub logo_path: String,
pub is_stealer_log: bool,
}