Skip to content

Commit

Permalink
Актуализированы модели авторизации (#19)
Browse files Browse the repository at this point in the history
* Actualize authorization models
Return of authorization methods changed from (bool) to (YAuthBase and its childs)

* Fix AuthorizeByCaptcha return

* Reaction to #19
  • Loading branch information
shuryak committed Jan 23, 2023
1 parent 97e4e8a commit e529e9f
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 28 deletions.
40 changes: 24 additions & 16 deletions src/Yandex.Music.Api/API/YUserAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,28 @@ public string GetAuthQRLink(AuthStorage storage)
/// </summary>
/// <param name="storage">Хранилище</param>
/// <returns></returns>
public Task<bool> AuthorizeByQRAsync(AuthStorage storage)
public async Task<YAuthQRStatus> AuthorizeByQRAsync(AuthStorage storage)
{
if (storage.AuthToken == null)
throw new Exception("Не выполнен запрос на авторизацию по QR.");

try
{
return new YAuthLoginQRBuilder(api, storage)
return await new YAuthLoginQRBuilder(api, storage)
.Build(null)
.GetResponseAsync()
.ContinueWith(task => {
YAuthQRStatus qrStatus = task.Result;
if (qrStatus.Status != YAuthStatus.Ok)
return qrStatus;
bool ok = LoginByCookiesAsync(storage).GetAwaiter().GetResult();
if (!ok)
{
throw new Exception("Ошибка авторизации по QR.");
}
return LoginByCookiesAsync(storage)
.GetAwaiter()
.GetResult();
return qrStatus;
});
}
catch (Exception ex)
Expand All @@ -285,7 +289,7 @@ public Task<bool> AuthorizeByQRAsync(AuthStorage storage)
/// </summary>
/// <param name="storage">Хранилище</param>
/// <returns></returns>
public bool AuthorizeByQR(AuthStorage storage)
public YAuthQRStatus AuthorizeByQR(AuthStorage storage)
{
return AuthorizeByQRAsync(storage).GetAwaiter().GetResult();
}
Expand Down Expand Up @@ -399,21 +403,25 @@ public bool AuthorizeByLetter(AuthStorage storage)
/// <param name="storage">Хранилище</param>
/// <param name="password">Пароль</param>
/// <returns></returns>
public Task<bool> AuthorizeByAppPasswordAsync(AuthStorage storage, string password)
public async Task<YAuthBase> AuthorizeByAppPasswordAsync(AuthStorage storage, string password)
{
if (storage.AuthToken == null || string.IsNullOrWhiteSpace(storage.AuthToken.CsfrToken))
throw new AuthenticationException($"Не найдена сессия входа. Выполните {nameof(CreateAuthSessionAsync)} перед использованием.");

YAuthBase response = new YAuthAppPasswordBuilder(api, storage)
YAuthBase response = await new YAuthAppPasswordBuilder(api, storage)
.Build(password)
.GetResponseAsync()
.GetAwaiter()
.GetResult();
.GetResponseAsync();

if (response.Status != YAuthStatus.Ok || !string.IsNullOrWhiteSpace(response.RedirectUrl))
throw new AuthenticationException("Ошибка авторизации.");
if (response.Status == YAuthStatus.Ok)
{
bool ok = await LoginByCookiesAsync(storage);
if (!ok)
{
throw new AuthenticationException("Ошибка авторизации.");
}
}

return LoginByCookiesAsync(storage);
return response;
}

/// <summary>
Expand All @@ -422,7 +430,7 @@ public Task<bool> AuthorizeByAppPasswordAsync(AuthStorage storage, string passwo
/// <param name="storage">Хранилище</param>
/// <param name="password">Пароль</param>
/// <returns></returns>
public bool AuthorizeByAppPassword(AuthStorage storage, string password)
public YAuthBase AuthorizeByAppPassword(AuthStorage storage, string password)
{
return AuthorizeByAppPasswordAsync(storage, password).GetAwaiter().GetResult();
}
Expand Down Expand Up @@ -454,4 +462,4 @@ public YAccessToken GetAccessToken(AuthStorage storage)

#endregion Основные функции
}
}
}
7 changes: 5 additions & 2 deletions src/Yandex.Music.Api/Models/Account/YAuthBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Yandex.Music.Api.Models.Account
{
Expand All @@ -8,5 +9,7 @@ public class YAuthBase

[JsonProperty("redirect_url")]
public string RedirectUrl { get; set; }

public List<YAuthError> Errors { get; set; }
}
}
}
21 changes: 18 additions & 3 deletions src/Yandex.Music.Api/Models/Account/YAuthCaptcha.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Yandex.Music.Api.Models.Account
{
public class YAuthCaptcha : YAuthBase
{
public string Id { get; set; }

public string Name { get; set; }

public string Label { get; set; }

public string Mode { get; set; }

public List<YAuthCaptchaError> Error { get; set; }

public bool CountryFromAudioWhiteList { get; set; }

public YAuthCaptchaOptions Options { get; set; }

public YAuthCaptchaVoice Voice { get; set; }

[JsonProperty("image_url")]
public string ImageUrl { get; set; }

public string Key { get; set; }

public YAuthCaptchaVoice Voice { get; set; }
public string Static { get; set; }
}
}
}
8 changes: 8 additions & 0 deletions src/Yandex.Music.Api/Models/Account/YAuthCaptchaError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Yandex.Music.Api.Models.Account
{
public class YAuthCaptchaError
{
public string Message { get; set; }
public YAuthCaptchaErrorCode Code { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/Yandex.Music.Api/Models/Account/YAuthCaptchaErrorCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Yandex.Music.Api.Models.Account
{
public enum YAuthCaptchaErrorCode
{
MissingValue,
CaptchaLocate,
Incorrect
}
}
7 changes: 7 additions & 0 deletions src/Yandex.Music.Api/Models/Account/YAuthCaptchaOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Yandex.Music.Api.Models.Account
{
public class YAuthCaptchaOptions
{
public bool AsyncCheck { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/Yandex.Music.Api/Models/Account/YAuthError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Runtime.Serialization;

namespace Yandex.Music.Api.Models.Account
{
public enum YAuthError
{
[EnumMember(Value = "authorization.invalid")]
AuthorizationInvalid,
[EnumMember(Value = "sessionid.invalid")]
SessionIdInvalid,
[EnumMember(Value = "password.not_matched")]
PasswordNotMatched,
[EnumMember(Value = "password.empty")]
PasswordEmpty,
[EnumMember(Value = "captcha.required")]
CaptchaRequired,
[EnumMember(Value = "captcha.not_matched")]
CaptchaNotMatched
}
}
4 changes: 3 additions & 1 deletion src/Yandex.Music.Api/Models/Account/YAuthQrStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public class YAuthQRStatus : YAuthBase
public string Id { get; set; }

public string State { get; set; }

public YAuthCaptcha Captcha { get; set; }
}
}
}
5 changes: 3 additions & 2 deletions src/Yandex.Music.Api/Models/Account/YAuthStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public enum YAuthStatus
{
Ok
Ok,
Error
}
}
}
8 changes: 4 additions & 4 deletions src/Yandex.Music.Client/YandexMusicClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public string GetAuthQRLink()
return api.User.GetAuthQRLink(storage);
}

public bool AuthorizeByQR()
public YAuthQRStatus AuthorizeByQR()
{
return api.User.AuthorizeByQR(storage);
}
Expand All @@ -85,9 +85,9 @@ public YAuthCaptcha GetCaptcha()
return api.User.GetCaptcha(storage);
}

public void AuthorizeByCaptcha(string captcha)
public YAuthBase AuthorizeByCaptcha(string captcha)
{
api.User.AuthorizeByCaptcha(storage, captcha);
return api.User.AuthorizeByCaptcha(storage, captcha);
}

public YAuthLetter GetAuthLetter()
Expand All @@ -100,7 +100,7 @@ public bool AuthorizeByLetter()
return api.User.AuthorizeByLetter(storage);
}

public bool AuthorizeByAppPassword(string password)
public YAuthBase AuthorizeByAppPassword(string password)
{
return api.User.AuthorizeByAppPassword(storage, password);
}
Expand Down

0 comments on commit e529e9f

Please sign in to comment.