Переделан способ авторизации по qr
All checks were successful
Release / pack-and-publish (release) Successful in 1m5s
All checks were successful
Release / pack-and-publish (release) Successful in 1m5s
This commit is contained in:
@@ -26,8 +26,8 @@ public class YTrackAPI : YCommonAPI
|
||||
return $"https://{host}/get-{codec}/{sign}/{ts}{path}";
|
||||
}
|
||||
|
||||
public Task<YTrack?> GetAsync(string trackId)
|
||||
=> GetAsync(trackId);
|
||||
public async Task<YTrack?> GetAsync(string trackId)
|
||||
=> (await GetAsync([trackId]))?.FirstOrDefault();
|
||||
|
||||
public Task<List<YTrack>?> GetAsync(IEnumerable<string> trackIds)
|
||||
=> new YGetTracksBuilder(Api).ExecuteAsync(trackIds);
|
||||
|
||||
@@ -23,7 +23,7 @@ public class YUserAPI : YCommonAPI
|
||||
if (!csrfMatch.Success || !processMatch.Success)
|
||||
return false;
|
||||
|
||||
Api.Storage.AuthToken = new YAuthToken
|
||||
Api.Storage.HeaderToken = new YAuthToken
|
||||
{
|
||||
CsfrToken = csrfMatch.Groups[1].Value,
|
||||
ProcessUuid = processMatch.Groups[1].Value
|
||||
@@ -45,9 +45,7 @@ public class YUserAPI : YCommonAPI
|
||||
Api.Storage.AccessToken = accessToken;
|
||||
Api.Storage.Token = accessToken.AccessToken;
|
||||
|
||||
var shortInfo = await new YGetShortAccountInfoBuilder(Api).ExecuteAsync(null!);
|
||||
if (shortInfo?.Status != YAuthStatus.Ok || string.IsNullOrWhiteSpace(shortInfo.Uid))
|
||||
throw new Exception("Не удалось подтвердить авторизацию");
|
||||
await AuthorizeAsync(accessToken.AccessToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -96,13 +94,31 @@ public class YUserAPI : YCommonAPI
|
||||
return $"https://passport.yandex.ru/auth/magic/code/?track_id={qr.TrackId}";
|
||||
}
|
||||
|
||||
public async Task<YAuthQRStatus?> AuthorizeByQRAsync()
|
||||
public async Task<YAuthQRStatus?> CheckQRStatusAsync()
|
||||
{
|
||||
if (Api.Storage.AuthToken == null)
|
||||
throw new Exception("Сессия не инициализирована");
|
||||
|
||||
var status = await new YPostQrStatus(Api).ExecuteAsync(null!);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(status?.TrackId))
|
||||
{
|
||||
Api.Storage.AuthToken.SessionTrackId = status.TrackId;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
public async Task<YAuthQRSession?> AuthorizeByQRAsync()
|
||||
{
|
||||
if (Api.Storage.AuthToken == null)
|
||||
throw new Exception("Сессия не инициализирована");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Api.Storage.AuthToken.SessionTrackId))
|
||||
throw new Exception("Токен сессии не инициализирован");
|
||||
|
||||
var status = await new YGetAuthLoginQRBuilder(Api).ExecuteAsync(null!);
|
||||
if (status?.Status == YAuthStatus.Ok && await LoginByCookiesAsync())
|
||||
if (status != null && status.DefaultUid != 0 && await LoginByCookiesAsync())
|
||||
return status;
|
||||
throw new AuthenticationException("Ошибка авторизации по QR");
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Net;
|
||||
using YandexMusic.API.Models.Account;
|
||||
|
||||
namespace YandexMusic.API.Common;
|
||||
@@ -7,6 +8,15 @@ namespace YandexMusic.API.Common;
|
||||
/// </summary>
|
||||
public class AuthStorage
|
||||
{
|
||||
private CookieContainer _cookieContainer;
|
||||
|
||||
public AuthStorage(CookieContainer cookieContainer)
|
||||
{
|
||||
_cookieContainer = cookieContainer;
|
||||
}
|
||||
|
||||
public CookieContainer CookieContainer => _cookieContainer;
|
||||
|
||||
/// <summary>
|
||||
/// Флаг, указывающий, авторизован ли пользователь.
|
||||
/// </summary>
|
||||
@@ -32,6 +42,11 @@ public class AuthStorage
|
||||
/// </summary>
|
||||
public YAccessToken AccessToken { get; internal set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Внутренние данные авторизации (CSRF, track_id и т.д.).
|
||||
/// </summary>
|
||||
public YAuthToken HeaderToken { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Внутренние данные авторизации (CSRF, track_id и т.д.).
|
||||
/// </summary>
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
|
||||
public class YAuthEmpty
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
21
YandexMusic.API/Models/Account/YAuthQRSession.cs
Normal file
21
YandexMusic.API/Models/Account/YAuthQRSession.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace YandexMusic.API.Models.Account;
|
||||
|
||||
public class YAuthQRSession
|
||||
{
|
||||
[JsonPropertyName("default_uid")]
|
||||
public int DefaultUid { get; set; }
|
||||
|
||||
[JsonPropertyName("retpath")]
|
||||
public string RetPath { get; set; }
|
||||
|
||||
[JsonPropertyName("track_id")]
|
||||
public string TrackId { get; set; }
|
||||
|
||||
public string Id { get; set; }
|
||||
|
||||
public string State { get; set; }
|
||||
|
||||
public YAuthCaptcha Captcha { get; set; }
|
||||
}
|
||||
9
YandexMusic.API/Models/Account/YAuthQrState.cs
Normal file
9
YandexMusic.API/Models/Account/YAuthQrState.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace YandexMusic.API.Models.Account;
|
||||
|
||||
public enum YAuthQrState
|
||||
{
|
||||
[EnumMember(Value = "otp_auth_finished")]
|
||||
OtpAuthFinished,
|
||||
}
|
||||
@@ -2,19 +2,11 @@
|
||||
|
||||
namespace YandexMusic.API.Models.Account;
|
||||
|
||||
public class YAuthQRStatus : YAuthBase
|
||||
public class YAuthQRStatus
|
||||
{
|
||||
[JsonPropertyName("default_uid")]
|
||||
public int DefaultUid { get; set; }
|
||||
[JsonPropertyName("state")]
|
||||
public string? State { get; set; } = null;
|
||||
|
||||
public string RetPath { get; set; }
|
||||
|
||||
[JsonPropertyName("track_id")]
|
||||
public string TrackId { get; set; }
|
||||
|
||||
public string Id { get; set; }
|
||||
|
||||
public string State { get; set; }
|
||||
|
||||
public YAuthCaptcha Captcha { get; set; }
|
||||
[JsonPropertyName("trackId")]
|
||||
public string TrackId { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace YandexMusic.API.Models.Account;
|
||||
namespace YandexMusic.API.Models.Account;
|
||||
|
||||
public class YAuthToken
|
||||
{
|
||||
[JsonPropertyName("csfr_token")]
|
||||
public string CsfrToken { get; set; }
|
||||
|
||||
[JsonPropertyName("track_id")]
|
||||
public string TrackId { get; set; }
|
||||
|
||||
[JsonPropertyName("process_uuid")]
|
||||
public string SessionTrackId { get; set; }
|
||||
|
||||
public string ProcessUuid { get; set; }
|
||||
|
||||
public Dictionary<string, string> Cookie { get; set; } = new();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using YandexMusic.API.Models.Account;
|
||||
using YandexMusic.API.Requests.Common;
|
||||
|
||||
@@ -8,7 +9,47 @@ internal class YGetAuthCookiesBuilder : YAuthRequestBuilder<YAccessToken?, objec
|
||||
{
|
||||
public YGetAuthCookiesBuilder(YandexMusicApi api) : base(api) { }
|
||||
protected override string Method => WebRequestMethods.Http.Post;
|
||||
protected override string BaseUrl => YConstants.Endpoints.MobilePassportUrl;
|
||||
protected override string PathTemplate => "1/bundle/oauth/token_by_sessionid";
|
||||
protected override HttpContent? GetContent(object _)
|
||||
=> new FormUrlEncodedContent(new Dictionary<string, string> { { "client_id", YConstants.XClientId }, { "client_secret", YConstants.XClientSecret } });
|
||||
protected override void SetCustomHeaders(HttpRequestHeaders headers)
|
||||
{
|
||||
base.SetCustomHeaders(headers);
|
||||
headers.Add("ya-client-host", "passport.yandex.ru");
|
||||
|
||||
var cookieString = GetCookieString();
|
||||
if (!string.IsNullOrEmpty(cookieString))
|
||||
headers.Add("Ya-Client-Cookie", cookieString);
|
||||
}
|
||||
private string GetCookieString()
|
||||
{
|
||||
var container = Storage.CookieContainer;
|
||||
if (container == null) return string.Empty;
|
||||
|
||||
var uris = new[]
|
||||
{
|
||||
new Uri("https://yandex.ru"),
|
||||
new Uri("https://passport.yandex.ru"),
|
||||
new Uri("https://mobileproxy.passport.yandex.net")
|
||||
};
|
||||
|
||||
var cookies = new List<string>();
|
||||
foreach (var uri in uris)
|
||||
{
|
||||
var cookieCollection = container.GetCookies(uri);
|
||||
foreach (Cookie cookie in cookieCollection)
|
||||
{
|
||||
cookies.Add($"{cookie.Name}={cookie.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
var distinct = cookies
|
||||
.Select(c => c.Split('=')[0])
|
||||
.Distinct()
|
||||
.Select(name => cookies.First(c => c.StartsWith(name + "=")))
|
||||
.ToList();
|
||||
|
||||
return string.Join("; ", distinct);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using YandexMusic.API.Models.Account;
|
||||
|
||||
namespace YandexMusic.API.Requests.Account;
|
||||
|
||||
internal class YGetAuthLoginQRBuilder : YAuthRequestBuilder<YAuthQRStatus, string>
|
||||
internal class YGetAuthLoginQRBuilder : YAuthRequestBuilder<YAuthQRSession, string>
|
||||
{
|
||||
public YGetAuthLoginQRBuilder(YandexMusicApi yandex) : base(yandex)
|
||||
{
|
||||
@@ -11,13 +12,17 @@ internal class YGetAuthLoginQRBuilder : YAuthRequestBuilder<YAuthQRStatus, strin
|
||||
|
||||
protected override string Method => WebRequestMethods.Http.Post;
|
||||
|
||||
protected override string PathTemplate => "auth/new/magic/status/";
|
||||
protected override string PathTemplate => "pwl-yandex/api/passport/sessions/get_session";
|
||||
|
||||
protected override HttpContent GetContent(string tuple)
|
||||
{
|
||||
return new FormUrlEncodedContent(new Dictionary<string, string> {
|
||||
{ "csrf_token", Api.Storage.AuthToken.CsfrToken },
|
||||
{ "track_id", Api.Storage.AuthToken.TrackId }
|
||||
{ "track_id", Api.Storage.AuthToken.SessionTrackId }
|
||||
});
|
||||
}
|
||||
protected override void SetCustomHeaders(HttpRequestHeaders headers)
|
||||
{
|
||||
headers.Add("X-Csrf-Token", Api.Storage.HeaderToken.CsfrToken);
|
||||
headers.Add("Process-Uuid", Api.Storage.HeaderToken.ProcessUuid);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ internal class YGetAuthQRBuilder : YAuthRequestBuilder<YAuthQR?, object>
|
||||
=> new FormUrlEncodedContent(new Dictionary<string, string> { { "retpath", "" } });
|
||||
protected override void SetCustomHeaders(HttpRequestHeaders headers)
|
||||
{
|
||||
headers.Add("X-Csrf-Token", Api.Storage.AuthToken.CsfrToken);
|
||||
headers.Add("Process-Uuid", Api.Storage.AuthToken.ProcessUuid);
|
||||
headers.Add("X-Csrf-Token", Api.Storage.HeaderToken.CsfrToken);
|
||||
headers.Add("Process-Uuid", Api.Storage.HeaderToken.ProcessUuid);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ internal class YPostAuthStats : YAuthRequestBuilder<YAuthEmpty?, object>
|
||||
=> new FormUrlEncodedContent(new Dictionary<string, string> { { "messageType", "CLIENT_READY" } });
|
||||
protected override void SetCustomHeaders(HttpRequestHeaders headers)
|
||||
{
|
||||
headers.Add("X-Csrf-Token", Api.Storage.AuthToken.CsfrToken);
|
||||
headers.Add("Process-Uuid", Api.Storage.AuthToken.ProcessUuid);
|
||||
headers.Add("X-Csrf-Token", Api.Storage.HeaderToken.CsfrToken);
|
||||
headers.Add("Process-Uuid", Api.Storage.HeaderToken.ProcessUuid);
|
||||
}
|
||||
}
|
||||
23
YandexMusic.API/Requests/Account/YPostQrStatus.cs
Normal file
23
YandexMusic.API/Requests/Account/YPostQrStatus.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using YandexMusic.API.Models.Account;
|
||||
|
||||
namespace YandexMusic.API.Requests.Account;
|
||||
|
||||
internal class YPostQrStatus : YAuthRequestBuilder<YAuthQRStatus?, object>
|
||||
{
|
||||
public YPostQrStatus(YandexMusicApi api) : base(api) { }
|
||||
protected override string Method => WebRequestMethods.Http.Post;
|
||||
protected override string PathTemplate => "pwl-yandex/api/passport/auth/magic/code/status";
|
||||
protected override HttpContent? GetContent(object _)
|
||||
=> new FormUrlEncodedContent(new Dictionary<string, string>
|
||||
{
|
||||
["csrf_token"] = Api.Storage.AuthToken.CsfrToken,
|
||||
["track_id"] = Api.Storage.AuthToken.TrackId,
|
||||
});
|
||||
protected override void SetCustomHeaders(HttpRequestHeaders headers)
|
||||
{
|
||||
headers.Add("X-Csrf-Token", Api.Storage.HeaderToken.CsfrToken);
|
||||
headers.Add("Process-Uuid", Api.Storage.HeaderToken.ProcessUuid);
|
||||
}
|
||||
}
|
||||
@@ -12,5 +12,6 @@ internal class YConstants
|
||||
{
|
||||
public const string MusicUrl = "https://api.music.yandex.net";
|
||||
public const string PassportUrl = "https://passport.yandex.ru/";
|
||||
public const string MobilePassportUrl = "https://mobileproxy.passport.yandex.net";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user