Добавлен вывод QR яндекса

This commit is contained in:
FrigaT
2026-04-19 21:06:36 +03:00
parent 4324b86512
commit 12241639dc
21 changed files with 1349 additions and 46 deletions

View File

@@ -6,8 +6,8 @@ using PlaylistShared.Api.Extensions;
using PlaylistShared.Api.Services;
using PlaylistShared.Shared;
using PlaylistShared.Shared.Enums;
using PlaylistShared.Shared.Yandex;
using PlaylistShared.Shared.SharedPlaylist;
using PlaylistShared.Shared.Yandex;
using YandexMusic;
namespace PlaylistShared.Api.Controllers;
@@ -20,15 +20,18 @@ public class PlaylistsController : ControllerBase
private readonly UserManager<ApplicationUser> _userManager;
private readonly SharedPlaylistService _sharedService;
private readonly YandexMusicService _yandexService;
private readonly YandexApiService _yandexApiService;
public PlaylistsController(
UserManager<ApplicationUser> userManager,
SharedPlaylistService sharedService,
YandexMusicService yandexService)
YandexMusicService yandexService,
YandexApiService yandexApiService)
{
_userManager = userManager;
_sharedService = sharedService;
_yandexService = yandexService;
_yandexApiService = yandexApiService;
}
[HttpGet]
@@ -38,7 +41,7 @@ public class PlaylistsController : ControllerBase
var user = await _userManager.FindByIdAsync(userId.ToString());
if (user == null) return Unauthorized();
var decryptedToken = _yandexService.DecryptToken(user.YandexAccessToken);
var decryptedToken = _yandexApiService.DecryptToken(user.YandexAccessToken);
if (string.IsNullOrEmpty(decryptedToken))
return BadRequest(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 400, Message = "Токен Яндекс.Музыки не установлен или недействителен" }));
@@ -74,11 +77,9 @@ public class PlaylistsController : ControllerBase
if (user == null) return Unauthorized();
// Проверяем, что плейлист действительно принадлежит пользователю
var yandexClient = new YandexMusicClient();
await yandexClient.Authorize(_yandexService.DecryptToken(user.YandexAccessToken));
var playlist = await yandexClient.GetPlaylistAsync(request.OwnerUid, request.Kind);
if (playlist == null || playlist.Owner.Uid != yandexClient.Account.Uid)
return BadRequest(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 400, Message = "Плейлист не принадлежит вам" }));
var playlist = await _yandexService.GetPlaylistAsync(user, request.OwnerUid, request.Kind);
if (playlist == null)
return BadRequest(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 404, Message = "Плейлист не найден" }));
var dto = new SharePlaylistDto
{

View File

@@ -6,31 +6,32 @@ using PlaylistShared.Api.Extensions;
using PlaylistShared.Api.Services;
using PlaylistShared.Shared;
using PlaylistShared.Shared.Profile;
using PlaylistShared.Shared.Yandex;
namespace PlaylistShared.Api.Controllers;
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class YandexTokenController : ControllerBase
public class YandexAccountController : ControllerBase
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly YandexMusicService _yandexService;
private readonly YandexAuthService _yandexService;
public YandexTokenController(UserManager<ApplicationUser> userManager, YandexMusicService yandexService)
public YandexAccountController(UserManager<ApplicationUser> userManager, YandexAuthService yandexService)
{
_userManager = userManager;
_yandexService = yandexService;
}
[HttpPost("set")]
[HttpPost("token")]
public async Task<ActionResult<ApiResponse<object>>> SetToken([FromBody] SetYandexTokenRequest request)
{
var userId = User.GetUserId();
var user = await _userManager.FindByIdAsync(userId.ToString());
if (user == null) return Unauthorized();
user.YandexAccessToken = _yandexService.EncryptToken(request.Token);
user.YandexAccessToken = _yandexService.Service.EncryptToken(request.Token);
// Не храним refresh-токен, так как пользователь вводит только access-токен
user.YandexTokenExpiryUtc = DateTime.UtcNow.AddMonths(1); // условно, т.к. срок жизни токена неизвестен
await _userManager.UpdateAsync(user);
@@ -55,4 +56,35 @@ public class YandexTokenController : ControllerBase
ExpiryUtc = user.YandexTokenExpiryUtc
}));
}
[HttpGet("qr")]
public async Task<ActionResult<ApiResponse<YandexAuthQr>>> GetQr()
{
var userId = User.GetUserId();
var user = await _userManager.FindByIdAsync(userId.ToString());
if (user == null) return Unauthorized();
var qr = await _yandexService.GenerateQrAsync(user);
return Ok(ApiResponse<YandexAuthQr>.Ok(qr));
}
[HttpGet("qr/{sessionId}")]
public async Task<IActionResult> CheckQr(int sessionId)
{
var userId = User.GetUserId();
var user = await _userManager.FindByIdAsync(userId.ToString());
if (user == null) return Unauthorized();
var checkResult = await _yandexService.CheckQrAsync(sessionId);
if (checkResult == null) return NotFound();
if (checkResult.Status == Shared.Enums.YandexAuthQrStatus.Authorized)
{
await SetToken(new() { Token = _yandexService.Service.Client.AuthStorage.Token });
}
return Ok(ApiResponse<YandexAuthQrCheck>.Ok(checkResult));
}
}

View File

@@ -47,7 +47,7 @@ public class YandexSearchController : ControllerBase
user = await _userManager.FindByIdAsync(userId.Value.ToString());
// Если нет пользователя или у него нет токена, пробуем через shared_id
if (user == null || string.IsNullOrEmpty(_yandexService.DecryptToken(user.YandexAccessToken)))
if (user == null || string.IsNullOrEmpty(user.YandexAccessToken))
{
if (string.IsNullOrEmpty(shared_id))
return Unauthorized("Не установлен яндекс токен.");
@@ -63,8 +63,7 @@ public class YandexSearchController : ControllerBase
user = owner;
}
var decryptedToken = _yandexService.DecryptToken(user.YandexAccessToken);
if (string.IsNullOrEmpty(decryptedToken))
if (string.IsNullOrEmpty(user.YandexAccessToken))
return BadRequest(ApiResponse<YandexSearchResult>.Fail(new ErrorResponse
{
StatusCode = 400,