Skip to content

Commit

Permalink
#225 fix YaD auth
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Apr 4, 2022
1 parent e2e50b1 commit 1aaacf9
Show file tree
Hide file tree
Showing 18 changed files with 186 additions and 40 deletions.
@@ -1,4 +1,5 @@
using System;
using System.Collections.Specialized;
using YaR.Clouds.Base.Repos.MailRuCloud.Mobile.Requests.Types;
using YaR.Clouds.Base.Requests;

Expand Down Expand Up @@ -27,7 +28,7 @@ protected override byte[] CreateHttpContent()
}
}

protected override RequestResponse<Result> DeserializeMessage(ResponseBodyStream data)
protected override RequestResponse<Result> DeserializeMessage(NameValueCollection responseHeaders, ResponseBodyStream data)
{
var opres = (OpResult)(int)data.OperationResult;

Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Collections.Specialized;
using System.Net;
using YaR.Clouds.Base.Repos.MailRuCloud.Mobile.Requests.Types;
using YaR.Clouds.Base.Requests;
Expand Down Expand Up @@ -67,7 +68,7 @@ protected override byte[] CreateHttpContent()
}
}

protected override RequestResponse<Result> DeserializeMessage(ResponseBodyStream data)
protected override RequestResponse<Result> DeserializeMessage(NameValueCollection responseHeaders, ResponseBodyStream data)
{
switch (data.OperationResult)
{
Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Collections.Specialized;
using System.Linq;
using YaR.Clouds.Base.Repos.MailRuCloud.Mobile.Requests.Types;
using YaR.Clouds.Base.Requests;
Expand Down Expand Up @@ -61,7 +62,7 @@ protected override byte[] CreateHttpContent()

private static readonly OpResult[] SuccessCodes = { OpResult.Ok, OpResult.NotModified, OpResult.Dunno04, OpResult.Dunno09};

protected override RequestResponse<Result> DeserializeMessage(ResponseBodyStream data)
protected override RequestResponse<Result> DeserializeMessage(NameValueCollection responseHeaders, ResponseBodyStream data)
{
var opres = (OpResult)(int)data.OperationResult;

Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Collections.Specialized;
using YaR.Clouds.Base.Repos.MailRuCloud.Mobile.Requests.Types;
using YaR.Clouds.Base.Requests;

Expand Down Expand Up @@ -35,7 +36,7 @@ protected override byte[] CreateHttpContent()
}
}

protected override RequestResponse<Result> DeserializeMessage(ResponseBodyStream data)
protected override RequestResponse<Result> DeserializeMessage(NameValueCollection responseHeaders, ResponseBodyStream data)
{
var opres = (OpResult) (int) data.OperationResult;

Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Collections.Specialized;
using YaR.Clouds.Base.Requests;
using YaR.Clouds.Base.Requests.Types;

Expand All @@ -10,7 +11,7 @@ protected ServerRequest(HttpCommonSettings settings) : base(settings, null)
{
}

protected override RequestResponse<ServerRequestResult> DeserializeMessage(string data)
protected override RequestResponse<ServerRequestResult> DeserializeMessage(NameValueCollection responseHeaders, string data)
{
var datas = data.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var msg = new RequestResponse<ServerRequestResult>
Expand Down
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using YaR.Clouds.Base.Repos.MailRuCloud.Mobile.Requests.Types;
using YaR.Clouds.Base.Requests;

Expand All @@ -22,7 +23,7 @@ protected override byte[] CreateHttpContent()
}
}

protected override RequestResponse<Result> DeserializeMessage(ResponseBodyStream data)
protected override RequestResponse<Result> DeserializeMessage(NameValueCollection responseHeaders, ResponseBodyStream data)
{
switch (data.OperationResult)
{
Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -30,7 +31,7 @@ protected override byte[] CreateHttpContent()
return Encoding.UTF8.GetBytes(data);
}

protected override RequestResponse<LoginResult> DeserializeMessage(string responseText)
protected override RequestResponse<LoginResult> DeserializeMessage(NameValueCollection responseHeaders, string responseText)
{
var csrf = responseText.Contains("csrf")
? new string(responseText.Split(new[] {"csrf"}, StringSplitOptions.None)[1].Split(',')[0].Where(char.IsLetterOrDigit).ToArray())
Expand Down
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -52,7 +53,7 @@ protected override byte[] CreateHttpContent()
.Select(m => m.Name)
.Aggregate((current, next) => current + "," + next);

protected override RequestResponse<YadResponceResult> DeserializeMessage(System.IO.Stream stream)
protected override RequestResponse<YadResponceResult> DeserializeMessage(NameValueCollection responseHeaders, System.IO.Stream stream)
{
using var sr = new StreamReader(stream);

Expand Down
@@ -1,4 +1,8 @@
using System.Net;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
using YaR.Clouds.Base.Requests;
Expand All @@ -25,16 +29,35 @@ protected override HttpWebRequest CreateRequest(string baseDomain = null)

protected override byte[] CreateHttpContent()
{
var data = Encoding.UTF8.GetBytes($"csrf_token={_csrf}");
return data;
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("csrf_token", _csrf),
new KeyValuePair<string, string>("origin", "disk_landing2_web_signin_ru")
};
var content = new FormUrlEncodedContent(keyValues);
var d = content.ReadAsByteArrayAsync().Result;
return d;
}
}

class YadAuthAccountsRequestResult
{
public bool HasError => Status == "error";
public bool HasError => Status == "error" ||
Accounts == null;

[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("csrf")]
public string Csrf { get; set; }

[JsonProperty("accounts")]
public YadAccounts Accounts { get; set; }
}

class YadAccounts
{
[JsonProperty("authorizedAccountsDefaultUid")]
public string AuthorizedAccountsDefaultUid { get; set; }
}
}
@@ -0,0 +1,60 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Net.Http;
using YaR.Clouds.Base.Requests;

namespace YaR.Clouds.Base.Repos.YandexDisk.YadWeb.Requests
{
class YadAuthAskV2Request : BaseRequestJson<YadAuthAskV2RequestResult>
{
private readonly IAuth _auth;
private readonly string _csrf;
private readonly string _uid;

public YadAuthAskV2Request(HttpCommonSettings settings, IAuth auth, string csrf, string uid)
: base(settings, auth)
{
_auth = auth;
_csrf = csrf;
_uid = uid;
}

protected override string RelationalUri => "/registration-validations/auth/additional_data/ask_v2";

protected override HttpWebRequest CreateRequest(string baseDomain = null)
{
var request = base.CreateRequest("https://passport.yandex.ru");
request.Referer = "https://passport.yandex.ru/";
request.Headers["Sec-Fetch-Mode"] = "cors";
request.Headers["Sec-Fetch-Site"] = "same-origin";

return request;
}

protected override byte[] CreateHttpContent()
{
var keyValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("csrf_token", _csrf),
new KeyValuePair<string, string>("uid", _uid),
};
FormUrlEncodedContent z = new FormUrlEncodedContent(keyValues);
var d = z.ReadAsByteArrayAsync().Result;
return d;
}
}

class YadAuthAskV2RequestResult
{
public bool HasError => Status == "error";

[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("errors")]
public List<string> Errors { get; set; }
}
}
@@ -1,4 +1,5 @@
using System.Net;
using System.Collections.Specialized;
using System.Net;
using System.Text.RegularExpressions;
using YaR.Clouds.Base.Requests;

Expand All @@ -15,37 +16,49 @@ public YadAuthDiskSkRequest(HttpCommonSettings settings, YadWebAuth auth) : base
protected override HttpWebRequest CreateRequest(string baseDomain = null)
{
var request = base.CreateRequest("https://disk.yandex.ru");
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
//request.Headers["Accept-Encoding"] = "gzip, deflate, br";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
request.Headers["Accept-Encoding"] = "gzip, deflate, br";
request.Headers["Accept-Language"] = "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,es;q=0.6";
request.Headers["sec-ch-ua"] = "\" Not A; Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"";
request.Headers["sec-ch-ua-mobile"] = "?0";
request.Headers["sec-ch-ua-platform"] = "\"Windows\"";
request.Headers["Sec-Fetch-Dest"] = "document";
request.Headers["Sec-Fetch-Mode"] = "navigate";
request.Headers["Sec-Fetch-Site"] = "same-site";
request.Headers["Sec-Fetch-User"] = "?1";
request.Referer = "https://passport.yandex.ru/auth/list?from=cloud&origin=disk_landing_web_signin_ru&retpath=https%3A%2F%2Fdisk.yandex.ru%2Fclient%2Fdisk&backpath=https%3A%2F%2Fdisk.yandex.ru&mode=edit";
request.Headers["Upgrade-Insecure-Requests"] = "1";

return request;
}

protected override RequestResponse<YadAuthDiskSkRequestResult> DeserializeMessage(string responseText)
protected override RequestResponse<YadAuthDiskSkRequestResult> DeserializeMessage(NameValueCollection responseHeaders, string responseText)
{
var matchSk = Regex.Match(responseText, @"""sk"":""(?<sk>.+?)""");
var matchSk = Regex.Match(responseText, _regex1);

var msg = new RequestResponse<YadAuthDiskSkRequestResult>
{
Ok = true,
Result = new YadAuthDiskSkRequestResult
{
DiskSk = matchSk.Success ? matchSk.Groups["sk"].Value : string.Empty
DiskSk = matchSk.Success ? matchSk.Groups["sk"].Value : string.Empty,
HtmlResponse = responseText
}
};

return msg;
}

private const string _regex1 = @"""sk"":""(?<sk>.+?)""";
//private const string _regex2 = @"sk=(?<sk>.*?)&"; // не надо, значит, уже все плохо
}

class YadAuthDiskSkRequestResult
{
public bool HasError => string.IsNullOrEmpty(DiskSk);

public string DiskSk { get; set; }

public string HtmlResponse { get; set; }
}

}
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
Expand All @@ -25,7 +27,7 @@ public YadAuthLoginRequest(HttpCommonSettings settings, IAuth auth, string csrf,
protected override HttpWebRequest CreateRequest(string baseDomain = null)
{
var request = base.CreateRequest("https://passport.yandex.ru");
request.Referer = "https://passport.yandex.ru/auth/list?from=cloud&origin=disk_landing_web_signin_ru&retpath=https%3A%2F%2Fdisk.yandex.ru%2Fclient%2Fdisk&backpath=https%3A%2F%2Fdisk.yandex.ru&mode=edit";
request.Referer = "https://passport.yandex.ru/auth/list?from=cloud&origin=disk_landing2_web_signin_ru&retpath=https%3A%2F%2Fdisk.yandex.ru%2Fclient%2Fdisk&backpath=https%3A%2F%2Fdisk.yandex.ru&mode=edit";
request.Headers["Sec-Fetch-Mode"] = "cors";
request.Headers["Sec-Fetch-Site"] = "same-origin";

Expand All @@ -34,22 +36,23 @@ protected override HttpWebRequest CreateRequest(string baseDomain = null)

protected override byte[] CreateHttpContent()
{

var keyValues = new List<KeyValuePair<string, string>>
{
new("csrf_token", _csrf),
new("process_uuid", _uuid),
new("login", _auth.Login),
new("service", "cloud"),
new("retpath", "https://disk.yandex.ru?source=landing2_signin_ru"),
new("origin", "disk_landing2_signin_ru")
new KeyValuePair<string, string>("csrf_token", _csrf),
new KeyValuePair<string, string>("login", _auth.Login),
new KeyValuePair<string, string>("process_uuid", _uuid),
new KeyValuePair<string, string>("retpath", "https://disk.yandex.ru/client/disk"),
new KeyValuePair<string, string>("origin", "disk_landing2_web_signin_ru"),
new KeyValuePair<string, string>("service", "cloud")
};
FormUrlEncodedContent z = new FormUrlEncodedContent(keyValues);
var d = z.ReadAsByteArrayAsync().Result;
return d;
}
}



class YadAuthLoginRequestResult
{
public bool HasError => Status == "error";
Expand All @@ -60,7 +63,8 @@ class YadAuthLoginRequestResult
[JsonProperty("track_id")]
public string TrackId { get; set; }


[JsonProperty("csrf_token")]
public string Csrf { get; set; }

[JsonProperty("errors")]
public List<string> Errors { get; set; }
Expand Down

0 comments on commit 1aaacf9

Please sign in to comment.