118 lines
4.1 KiB
C#
118 lines
4.1 KiB
C#
using System.Collections.Concurrent;
|
|
using Jellyfin.Plugin.YandexMusic.Yandex;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Jellyfin.Plugin.YandexMusic.Api;
|
|
|
|
/// <summary>
|
|
/// Plugin API used by the configuration page for Yandex OAuth device flow.
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("YandexMusic")]
|
|
[Authorize(Policy = "RequiresElevation")]
|
|
public class YandexMusicController : ControllerBase
|
|
{
|
|
private static readonly ConcurrentDictionary<string, DateTimeOffset> Sessions = new();
|
|
|
|
private readonly YandexMusicClient _client;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="YandexMusicController"/> class.
|
|
/// </summary>
|
|
public YandexMusicController(YandexMusicClient client)
|
|
{
|
|
_client = client;
|
|
}
|
|
|
|
/// <summary>Starts the device flow.</summary>
|
|
[HttpPost("DeviceCode")]
|
|
public async Task<ActionResult<DeviceCodeResult>> StartDeviceCode(CancellationToken cancellationToken)
|
|
{
|
|
var code = await _client.RequestDeviceCodeAsync(cancellationToken).ConfigureAwait(false);
|
|
if (code is null)
|
|
{
|
|
return StatusCode(502, new { error = "Yandex OAuth did not return a device code." });
|
|
}
|
|
|
|
Sessions[code.DeviceCode] = DateTimeOffset.UtcNow.AddSeconds(code.ExpiresIn);
|
|
return new DeviceCodeResult(
|
|
code.DeviceCode,
|
|
code.UserCode,
|
|
code.VerificationUrl,
|
|
code.ExpiresIn,
|
|
Math.Max(2, code.Interval));
|
|
}
|
|
|
|
/// <summary>Polls the device flow once.</summary>
|
|
[HttpPost("DeviceCode/Poll")]
|
|
public async Task<ActionResult<DevicePollResult>> PollDeviceCode(
|
|
[FromBody] DevicePollRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(request.DeviceCode)
|
|
|| !Sessions.TryGetValue(request.DeviceCode, out var expires)
|
|
|| expires < DateTimeOffset.UtcNow)
|
|
{
|
|
return new DevicePollResult("expired", null, "Код истёк, запросите новый.");
|
|
}
|
|
|
|
var token = await _client.PollDeviceTokenAsync(request.DeviceCode, cancellationToken).ConfigureAwait(false);
|
|
if (token is null)
|
|
{
|
|
return new DevicePollResult("pending", null, null);
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(token.AccessToken))
|
|
{
|
|
return new DevicePollResult("error", null, token.ErrorDescription ?? token.Error ?? "Не удалось получить токен.");
|
|
}
|
|
|
|
var config = Plugin.Instance!.Configuration;
|
|
config.Token = token.AccessToken!;
|
|
Plugin.Instance.UpdateConfiguration(config);
|
|
Sessions.TryRemove(request.DeviceCode, out _);
|
|
|
|
return new DevicePollResult("ok", token.AccessToken, null);
|
|
}
|
|
|
|
/// <summary>Returns connection status.</summary>
|
|
[HttpGet("Status")]
|
|
public async Task<ActionResult<StatusResult>> Status(CancellationToken cancellationToken)
|
|
{
|
|
var connected = await _client.TestConnectionAsync(cancellationToken).ConfigureAwait(false);
|
|
var config = Plugin.Instance?.Configuration;
|
|
return new StatusResult(
|
|
connected,
|
|
!string.IsNullOrWhiteSpace(config?.Token),
|
|
connected ? null : "api.music.yandex.net недоступен");
|
|
}
|
|
|
|
/// <summary>Clears the saved token.</summary>
|
|
[HttpPost("Logout")]
|
|
public ActionResult Logout()
|
|
{
|
|
var config = Plugin.Instance!.Configuration;
|
|
config.Token = string.Empty;
|
|
Plugin.Instance.UpdateConfiguration(config);
|
|
return NoContent();
|
|
}
|
|
}
|
|
|
|
/// <summary>Device code returned to the config page.</summary>
|
|
public record DeviceCodeResult(
|
|
string DeviceCode,
|
|
string UserCode,
|
|
string VerificationUrl,
|
|
int ExpiresIn,
|
|
int Interval);
|
|
|
|
/// <summary>Device poll request.</summary>
|
|
public record DevicePollRequest(string DeviceCode);
|
|
|
|
/// <summary>Device poll result.</summary>
|
|
public record DevicePollResult(string Status, string? Token, string? Error);
|
|
|
|
/// <summary>Plugin status.</summary>
|
|
public record StatusResult(bool Connected, bool HasToken, string? Error);
|