Проведен аудит. Добавлено переключение треков
This commit is contained in:
@@ -80,7 +80,7 @@ public class YandexApiService : IDisposable
|
||||
/// </summary>
|
||||
/// <param name="encryptedToken"></param>
|
||||
/// <returns></returns>
|
||||
public string DecryptToken(string encryptedToken)
|
||||
public string? DecryptToken(string encryptedToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PlaylistShared.Api.Data;
|
||||
using PlaylistShared.Api.Entities;
|
||||
using PlaylistShared.Shared.Yandex;
|
||||
@@ -24,10 +24,10 @@ public class YandexAuthService
|
||||
|
||||
internal async Task<YandexAuthQr> GetQrOrGenerate(ApplicationUser user)
|
||||
{
|
||||
var existingSession = _dbContext.YandexAuthSessions
|
||||
var existingSession = await _dbContext.YandexAuthSessions
|
||||
.Where(s => s.UserId == user.Id && !s.IsConfirmed && s.CreatedAt > DateTime.UtcNow.AddMinutes(-5))
|
||||
.OrderByDescending(s => s.CreatedAt)
|
||||
.FirstOrDefault();
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (existingSession != null)
|
||||
{
|
||||
@@ -45,14 +45,14 @@ public class YandexAuthService
|
||||
{
|
||||
var qr = await Api.Passport.GetAuthQRLinkAsync();
|
||||
var trackId = Service.Client.AuthStorage.AuthToken.TrackId;
|
||||
var csfrToken = Service.Client.AuthStorage.AuthToken.CsfrToken;
|
||||
var csrfToken = Service.Client.AuthStorage.AuthToken.CsfrToken;
|
||||
var headerProcessUuid = Service.Client.AuthStorage.HeaderToken.ProcessUuid;
|
||||
var headerCsfrToken = Service.Client.AuthStorage.HeaderToken.CsfrToken;
|
||||
var headerCsrfToken = Service.Client.AuthStorage.HeaderToken.CsfrToken;
|
||||
|
||||
if (string.IsNullOrEmpty(qr))
|
||||
throw new Exception("Не удалось получить QR-ссылку");
|
||||
|
||||
var cookiesJson = SerializeCookies(_apiService.CookieContainer);
|
||||
var cookiesJson = _apiService.EncryptToken(SerializeCookies(_apiService.CookieContainer));
|
||||
|
||||
var session = new YandexAuthSession
|
||||
{
|
||||
@@ -62,10 +62,9 @@ public class YandexAuthService
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
IsConfirmed = false,
|
||||
TrackId = trackId,
|
||||
CsfrToken = csfrToken,
|
||||
HeaderCsfrToken = headerCsfrToken,
|
||||
CsrfToken = csrfToken,
|
||||
HeaderCsrfToken = headerCsrfToken,
|
||||
HeaderProcessId = headerProcessUuid,
|
||||
|
||||
};
|
||||
|
||||
_dbContext.YandexAuthSessions.Add(session);
|
||||
@@ -83,16 +82,19 @@ public class YandexAuthService
|
||||
var session = await _dbContext.YandexAuthSessions.FindAsync(sessionId);
|
||||
if (session == null) return null;
|
||||
|
||||
RestoreCookies(Service.CookieContainer, session.SerializedCookies);
|
||||
var decryptedCookies = _apiService.DecryptToken(session.SerializedCookies);
|
||||
if (decryptedCookies == null) return null;
|
||||
|
||||
RestoreCookies(Service.CookieContainer, decryptedCookies);
|
||||
if (Service.Client.AuthStorage.AuthToken is null)
|
||||
{
|
||||
Service.Client.AuthStorage.AuthToken = new();
|
||||
}
|
||||
|
||||
Service.Client.AuthStorage.AuthToken.CsfrToken = session?.CsfrToken ?? "";
|
||||
Service.Client.AuthStorage.AuthToken.TrackId = session?.TrackId ?? "";
|
||||
Service.Client.AuthStorage.HeaderToken.CsfrToken = session?.HeaderCsfrToken ?? "";
|
||||
Service.Client.AuthStorage.HeaderToken.ProcessUuid = session?.HeaderProcessId ?? "";
|
||||
Service.Client.AuthStorage.AuthToken.CsfrToken = session.CsrfToken ?? "";
|
||||
Service.Client.AuthStorage.AuthToken.TrackId = session.TrackId ?? "";
|
||||
Service.Client.AuthStorage.HeaderToken.CsfrToken = session.HeaderCsrfToken ?? "";
|
||||
Service.Client.AuthStorage.HeaderToken.ProcessUuid = session.HeaderProcessId ?? "";
|
||||
|
||||
var status = await Api.Passport.CheckQRStatusAsync();
|
||||
|
||||
@@ -100,36 +102,29 @@ public class YandexAuthService
|
||||
{
|
||||
try
|
||||
{
|
||||
var auth = await Api.Passport.AuthorizeByQRAsync();
|
||||
await Api.Passport.AuthorizeByQRAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
return new() { Status = Shared.Enums.YandexAuthQrStatus.Error, };
|
||||
return new() { Status = Shared.Enums.YandexAuthQrStatus.Error };
|
||||
}
|
||||
|
||||
_dbContext.YandexAuthSessions.Where(t => t.UserId == session.UserId).ExecuteDelete();
|
||||
_dbContext.SaveChanges();
|
||||
await _dbContext.YandexAuthSessions
|
||||
.Where(t => t.UserId == session.UserId)
|
||||
.ExecuteDeleteAsync();
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return new() { Status = Shared.Enums.YandexAuthQrStatus.Authorized, };
|
||||
return new() { Status = Shared.Enums.YandexAuthQrStatus.Authorized };
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Status = Shared.Enums.YandexAuthQrStatus.Pending,
|
||||
};
|
||||
return new() { Status = Shared.Enums.YandexAuthQrStatus.Pending };
|
||||
}
|
||||
|
||||
|
||||
private string SerializeCookies(CookieContainer container)
|
||||
{
|
||||
var allCookies = new List<object>();
|
||||
|
||||
var cookies = container.GetAllCookies();
|
||||
foreach (Cookie cookie in cookies)
|
||||
{
|
||||
foreach (Cookie cookie in container.GetAllCookies())
|
||||
allCookies.Add(new { cookie.Name, cookie.Value, cookie.Domain, cookie.Path });
|
||||
}
|
||||
|
||||
return JsonSerializer.Serialize(allCookies);
|
||||
}
|
||||
|
||||
@@ -137,9 +132,7 @@ public class YandexAuthService
|
||||
{
|
||||
var cookies = JsonSerializer.Deserialize<List<CookieData>>(serializedCookies);
|
||||
foreach (var c in cookies)
|
||||
{
|
||||
container.Add(new Cookie(c.Name, c.Value, c.Path, c.Domain));
|
||||
}
|
||||
}
|
||||
|
||||
private class CookieData
|
||||
@@ -149,4 +142,4 @@ public class YandexAuthService
|
||||
public string Domain { get; set; }
|
||||
public string Path { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,21 @@ public class YandexMusicService
|
||||
return await Api.Playlist.GetAsync(ownerUid, kind);
|
||||
}
|
||||
|
||||
public async Task<YandexPlaylistData?> GetPlaylistDataAsync(ApplicationUser user, string ownerUid, string kind)
|
||||
{
|
||||
var playlist = await GetPlaylistAsync(user, ownerUid, kind);
|
||||
return playlist == null ? null : MapToPlaylistData(playlist);
|
||||
}
|
||||
|
||||
public async Task<(List<YPlaylist>? OwnPlaylists, string? AccountUid)> GetOwnFavoritesAsync(ApplicationUser user)
|
||||
{
|
||||
await AuthorizeIfNot(user);
|
||||
var favorites = await Api.Playlist.FavoritesAsync();
|
||||
var accountUid = _yandexApiService.Client.Account?.Uid;
|
||||
var ownPlaylists = favorites?.Where(p => p.Owner?.Uid == accountUid).ToList();
|
||||
return (ownPlaylists, accountUid);
|
||||
}
|
||||
|
||||
public async Task<YPlaylist?> CreatePlaylistAsync(ApplicationUser user, string title)
|
||||
{
|
||||
await AuthorizeIfNot(user);
|
||||
@@ -339,4 +354,24 @@ public class YandexMusicService
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static YandexPlaylistData MapToPlaylistData(YPlaylist playlist) => new()
|
||||
{
|
||||
Title = playlist.Title,
|
||||
Description = playlist.Description,
|
||||
Tracks = playlist.Tracks.Select(t => new YandexTrack
|
||||
{
|
||||
TrackId = t.Track.Id,
|
||||
Title = t.Track.Title,
|
||||
Artists = t.Track.Artists.Select(a => new YandexArtist
|
||||
{
|
||||
Id = a.Id,
|
||||
Name = a.Name,
|
||||
CoverUrl = a.Cover.GetUrl(),
|
||||
Description = a.Description?.Text ?? string.Empty,
|
||||
}).ToList(),
|
||||
DurationMs = (int)(t.Track?.DurationMs ?? 0),
|
||||
CoverUri = t.Track?.CoverUri ?? ""
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user