jellyfin/Jellyfin.Api/Controllers/QuickConnectController.cs

130 lines
4.9 KiB
C#
Raw Normal View History

using System;
2020-08-13 20:35:04 +00:00
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
2023-02-17 22:16:08 +00:00
using Jellyfin.Api.Helpers;
2020-08-13 20:35:04 +00:00
using MediaBrowser.Common.Extensions;
2021-06-18 17:31:47 +00:00
using MediaBrowser.Controller.Authentication;
using MediaBrowser.Controller.Net;
2020-08-13 20:35:04 +00:00
using MediaBrowser.Controller.QuickConnect;
using MediaBrowser.Model.QuickConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Controllers;
/// <summary>
/// Quick connect controller.
/// </summary>
public class QuickConnectController : BaseJellyfinApiController
2020-08-13 20:35:04 +00:00
{
2023-01-31 11:18:10 +00:00
private readonly IQuickConnect _quickConnect;
private readonly IAuthorizationContext _authContext;
/// <summary>
/// Initializes a new instance of the <see cref="QuickConnectController"/> class.
/// </summary>
/// <param name="quickConnect">Instance of the <see cref="IQuickConnect"/> interface.</param>
/// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
public QuickConnectController(IQuickConnect quickConnect, IAuthorizationContext authContext)
{
_quickConnect = quickConnect;
_authContext = authContext;
}
2020-08-13 20:35:04 +00:00
/// <summary>
2023-01-31 11:18:10 +00:00
/// Gets the current quick connect state.
2020-08-13 20:35:04 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <response code="200">Quick connect state returned.</response>
/// <returns>Whether Quick Connect is enabled on the server or not.</returns>
[HttpGet("Enabled")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<bool> GetQuickConnectEnabled()
2020-08-13 20:35:04 +00:00
{
2023-01-31 11:18:10 +00:00
return _quickConnect.IsEnabled;
}
2020-08-13 20:35:04 +00:00
2023-01-31 11:18:10 +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>
[HttpPost("Initiate")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<QuickConnectResult>> InitiateQuickConnect()
{
try
2020-08-13 20:35:04 +00:00
{
2023-01-31 11:18:10 +00:00
var auth = await _authContext.GetAuthorizationInfo(Request).ConfigureAwait(false);
return _quickConnect.TryConnect(auth);
2020-08-13 20:35:04 +00:00
}
2023-01-31 11:18:10 +00:00
catch (AuthenticationException)
2020-08-13 20:35:04 +00:00
{
2023-01-31 11:18:10 +00:00
return Unauthorized("Quick connect is disabled");
2020-08-13 20:35:04 +00:00
}
2023-01-31 11:18:10 +00:00
}
/// <summary>
/// Old version of <see cref="InitiateQuickConnect" /> using a GET method.
/// Still available to avoid breaking compatibility.
/// </summary>
/// <returns>The result of <see cref="InitiateQuickConnect" />.</returns>
[Obsolete("Use POST request instead")]
[HttpGet("Initiate")]
[ApiExplorerSettings(IgnoreApi = true)]
public Task<ActionResult<QuickConnectResult>> InitiateQuickConnectLegacy() => InitiateQuickConnect();
2020-08-13 20:35:04 +00:00
2023-01-31 11:18:10 +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)]
public ActionResult<QuickConnectResult> GetQuickConnectState([FromQuery, Required] string secret)
{
try
{
return _quickConnect.CheckRequestStatus(secret);
}
catch (ResourceNotFoundException)
{
return NotFound("Unknown secret");
}
catch (AuthenticationException)
2020-08-13 20:35:04 +00:00
{
2023-01-31 11:18:10 +00:00
return Unauthorized("Quick connect is disabled");
2020-08-13 20:35:04 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-08-13 20:35:04 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Authorizes a pending quick connect request.
/// </summary>
/// <param name="code">Quick connect code to authorize.</param>
/// <param name="userId">The user the authorize. Access to the requested user is required.</param>
/// <response code="200">Quick connect result authorized successfully.</response>
/// <response code="403">Unknown user id.</response>
/// <returns>Boolean indicating if the authorization was successful.</returns>
[HttpPost("Authorize")]
2023-02-08 22:55:26 +00:00
[Authorize]
2023-01-31 11:18:10 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult<bool>> AuthorizeQuickConnect([FromQuery, Required] string code, [FromQuery] Guid? userId = null)
{
2023-02-17 22:16:08 +00:00
userId = RequestHelpers.GetUserId(User, userId);
2020-08-13 20:35:04 +00:00
2023-01-31 11:18:10 +00:00
try
2020-08-13 20:35:04 +00:00
{
2023-02-17 22:16:08 +00:00
return await _quickConnect.AuthorizeRequest(userId.Value, code).ConfigureAwait(false);
2023-01-31 11:18:10 +00:00
}
catch (AuthenticationException)
{
return Unauthorized("Quick connect is disabled");
2020-08-13 20:35:04 +00:00
}
}
}