jellyfin/Jellyfin.Api/Controllers/QuickConnectController.cs

155 lines
6.1 KiB
C#
Raw Normal View History

2020-08-13 20:35:04 +00:00
using System.ComponentModel.DataAnnotations;
using Jellyfin.Api.Constants;
2020-08-16 22:45:53 +00:00
using Jellyfin.Api.Helpers;
2020-08-13 20:35:04 +00:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.QuickConnect;
using MediaBrowser.Model.QuickConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Quick connect controller.
/// </summary>
public class QuickConnectController : BaseJellyfinApiController
{
private readonly IQuickConnect _quickConnect;
/// <summary>
/// Initializes a new instance of the <see cref="QuickConnectController"/> class.
/// </summary>
/// <param name="quickConnect">Instance of the <see cref="IQuickConnect"/> interface.</param>
2020-08-17 23:48:58 +00:00
public QuickConnectController(IQuickConnect quickConnect)
2020-08-13 20:35:04 +00:00
{
_quickConnect = quickConnect;
}
/// <summary>
/// Gets the current quick connect state.
/// </summary>
/// <response code="200">Quick connect state returned.</response>
/// <returns>The current <see cref="QuickConnectState"/>.</returns>
[HttpGet("Status")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<QuickConnectState> GetStatus()
{
_quickConnect.ExpireRequests();
return _quickConnect.State;
2020-08-13 20:35:04 +00:00
}
/// <summary>
/// Initiate a new quick connect request.
/// </summary>
/// <response code="200">Quick connect request successfully created.</response>
/// <response code="401">Quick connect is not active on this server.</response>
/// <returns>A <see cref="QuickConnectResult"/> with a secret and code for future use or an error message.</returns>
[HttpGet("Initiate")]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-08-17 21:36:45 +00:00
public ActionResult<QuickConnectResult> Initiate()
2020-08-13 20:35:04 +00:00
{
2020-08-17 21:36:45 +00:00
return _quickConnect.TryConnect();
2020-08-13 20:35:04 +00:00
}
/// <summary>
/// Attempts to retrieve authentication information.
/// </summary>
/// <param name="secret">Secret previously returned from the Initiate endpoint.</param>
/// <response code="200">Quick connect result returned.</response>
/// <response code="404">Unknown quick connect secret.</response>
/// <returns>An updated <see cref="QuickConnectResult"/>.</returns>
[HttpGet("Connect")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-08-17 21:36:45 +00:00
public ActionResult<QuickConnectResult> Connect([FromQuery, Required] string secret)
2020-08-13 20:35:04 +00:00
{
try
{
2020-08-17 21:36:45 +00:00
return _quickConnect.CheckRequestStatus(secret);
2020-08-13 20:35:04 +00:00
}
catch (ResourceNotFoundException)
{
return NotFound("Unknown secret");
}
}
/// <summary>
/// Temporarily activates quick connect for five minutes.
/// </summary>
/// <response code="204">Quick connect has been temporarily activated.</response>
/// <response code="403">Quick connect is unavailable on this server.</response>
/// <returns>An <see cref="NoContentResult"/> on success.</returns>
[HttpPost("Activate")]
[Authorize(Policy = Policies.DefaultAuthorization)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public ActionResult Activate()
{
if (_quickConnect.State == QuickConnectState.Unavailable)
{
2020-12-07 15:55:42 +00:00
return StatusCode(StatusCodes.Status403Forbidden, "Quick connect is unavailable");
2020-08-13 20:35:04 +00:00
}
_quickConnect.Activate();
return NoContent();
}
/// <summary>
/// Enables or disables quick connect.
/// </summary>
/// <param name="status">New <see cref="QuickConnectState"/>.</param>
/// <response code="204">Quick connect state set successfully.</response>
/// <returns>An <see cref="NoContentResult"/> on success.</returns>
[HttpPost("Available")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-08-17 21:36:45 +00:00
public ActionResult Available([FromQuery] QuickConnectState status = QuickConnectState.Available)
2020-08-13 20:35:04 +00:00
{
2020-08-17 21:36:45 +00:00
_quickConnect.SetState(status);
2020-08-13 20:35:04 +00:00
return NoContent();
}
/// <summary>
/// Authorizes a pending quick connect request.
/// </summary>
/// <param name="code">Quick connect code to authorize.</param>
/// <response code="200">Quick connect result authorized successfully.</response>
2020-08-17 23:48:58 +00:00
/// <response code="403">Unknown user id.</response>
2020-08-13 20:35:04 +00:00
/// <returns>Boolean indicating if the authorization was successful.</returns>
[HttpPost("Authorize")]
[Authorize(Policy = Policies.DefaultAuthorization)]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-08-17 21:36:45 +00:00
[ProducesResponseType(StatusCodes.Status403Forbidden)]
2020-08-17 23:48:58 +00:00
public ActionResult<bool> Authorize([FromQuery, Required] string code)
2020-08-13 20:35:04 +00:00
{
2020-08-17 23:48:58 +00:00
var userId = ClaimHelpers.GetUserId(Request.HttpContext.User);
if (!userId.HasValue)
2020-08-17 21:36:45 +00:00
{
2020-12-07 15:55:42 +00:00
return StatusCode(StatusCodes.Status403Forbidden, "Unknown user id");
2020-08-17 21:36:45 +00:00
}
2020-08-17 23:48:58 +00:00
return _quickConnect.AuthorizeRequest(userId.Value, code);
2020-08-13 20:35:04 +00:00
}
/// <summary>
/// Deauthorize all quick connect devices for the current user.
/// </summary>
/// <response code="200">All quick connect devices were deleted.</response>
/// <returns>The number of devices that were deleted.</returns>
[HttpPost("Deauthorize")]
[Authorize(Policy = Policies.DefaultAuthorization)]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<int> Deauthorize()
{
2020-08-16 22:45:53 +00:00
var userId = ClaimHelpers.GetUserId(Request.HttpContext.User);
if (!userId.HasValue)
{
return 0;
}
return _quickConnect.DeleteAllDevices(userId.Value);
2020-08-13 20:35:04 +00:00
}
}
}