Skip to content

Commit

Permalink
fix(core): yahoo api changes: yid is userId now, sessionIndex is requ…
Browse files Browse the repository at this point in the history
…ired and fo… (#1314)

* yahoo api changes: yid is userId now, sessionIndex is required and found inside signup page body, IDENTIFIER_EXISTS is IDENTIFIER_NOT_AVAILABLE now

* cargo fmt

---------

Co-authored-by: itsmariush <[email protected]>
Co-authored-by: Amaury <[email protected]>
  • Loading branch information
3 people authored Oct 6, 2023
1 parent ce19081 commit 0209111
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions core/src/smtp/yahoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,31 @@ const USER_AGENT: &str = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleW
#[derive(Serialize)]
struct FormRequest {
acrumb: String,
#[serde(rename(serialize = "sessionIndex"))]
session_index: String,
#[serde(rename(serialize = "specId"))]
spec_id: String,
yid: String,
#[serde(rename(serialize = "userId"))]
user_id: String,
}

impl Default for FormRequest {
fn default() -> Self {
FormRequest {
acrumb: "".into(),
session_index: "".into(),
spec_id: "yidReg".into(),
yid: "".into(),
user_id: "".into(),
}
}
}

impl FormRequest {
fn new(acrumb: String, yid: String) -> Self {
fn new(acrumb: String, session_index: String, user_id: String) -> Self {
FormRequest {
acrumb,
yid,
session_index,
user_id,
..Default::default()
}
}
Expand All @@ -79,6 +84,8 @@ struct FormResponse {
pub enum YahooError {
/// Cannot find "acrumb" field in cookie.
NoAcrumb,
/// Cannot find "sessionIndex" hidden input in body
NoSessionIndex,
/// Cannot find cookie in Yahoo response.
NoCookie,
/// Error when serializing or deserializing HTTP requests and responses.
Expand Down Expand Up @@ -122,7 +129,7 @@ pub async fn check_yahoo(

// Get the cookies from the response.
let cookies = match response.headers().get("Set-Cookie") {
Some(x) => x,
Some(x) => x.to_owned(),
_ => {
return Err(YahooError::NoCookie);
}
Expand All @@ -142,6 +149,8 @@ pub async fn check_yahoo(
cookies
);

let body = response.text().await?;

let username = to_email
.split('@')
.next()
Expand All @@ -154,14 +163,24 @@ pub async fn check_yahoo(
return Err(YahooError::NoAcrumb);
}
};
let re = Regex::new(r"s=(?P<acrumb>[^;]*)").expect("Correct regex. qed.");
let re = Regex::new(r"s=(?P<acrumb>[^;]*)&d").expect("Correct regex. qed.");
let acrumb = match re.captures(acrumb) {
Some(x) => x,
_ => {
return Err(YahooError::NoAcrumb);
}
};

let re =
Regex::new(r#"<input type="hidden" value="(?P<sessionIndex>.*)" name="sessionIndex">"#)
.expect("Correct regex. qed");
let session_index = match re.captures(&body) {
Some(y) => y,
_ => {
return Err(YahooError::NoSessionIndex);
}
};

// Mimic a real HTTP request.
let response = create_client(input, "yahoo")?
.post(SIGNUP_API)
Expand All @@ -176,9 +195,10 @@ pub async fn check_yahoo(
.header("Referer", SIGNUP_PAGE)
.header("Accept-Encoding", "gzip, deflate, br")
.header("Accept-Language", "en-US,en;q=0.8,ar;q=0.6")
.header("Cookie", cookies)
.header("Cookie", &cookies)
.json(&FormRequest::new(
acrumb["acrumb"].to_string(),
session_index["sessionIndex"].to_string(),
username.into(),
))
.send()
Expand All @@ -196,7 +216,7 @@ pub async fn check_yahoo(
let username_exists = response
.errors
.iter()
.any(|item| item.name == "yid" && item.error == "IDENTIFIER_EXISTS");
.any(|item| item.name == "userId" && item.error == "IDENTIFIER_NOT_AVAILABLE");

Ok(SmtpDetails {
can_connect_smtp: true,
Expand Down

0 comments on commit 0209111

Please sign in to comment.