QuickConnectController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.ComponentModel.DataAnnotations;
  2. using System.Threading.Tasks;
  3. using Jellyfin.Api.Constants;
  4. using Jellyfin.Api.Helpers;
  5. using MediaBrowser.Common.Extensions;
  6. using MediaBrowser.Controller.Authentication;
  7. using MediaBrowser.Controller.QuickConnect;
  8. using MediaBrowser.Model.QuickConnect;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. namespace Jellyfin.Api.Controllers
  13. {
  14. /// <summary>
  15. /// Quick connect controller.
  16. /// </summary>
  17. public class QuickConnectController : BaseJellyfinApiController
  18. {
  19. private readonly IQuickConnect _quickConnect;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="QuickConnectController"/> class.
  22. /// </summary>
  23. /// <param name="quickConnect">Instance of the <see cref="IQuickConnect"/> interface.</param>
  24. public QuickConnectController(IQuickConnect quickConnect)
  25. {
  26. _quickConnect = quickConnect;
  27. }
  28. /// <summary>
  29. /// Gets the current quick connect state.
  30. /// </summary>
  31. /// <response code="200">Quick connect state returned.</response>
  32. /// <returns>Whether Quick Connect is enabled on the server or not.</returns>
  33. [HttpGet("Enabled")]
  34. [ProducesResponseType(StatusCodes.Status200OK)]
  35. public ActionResult<bool> GetEnabled()
  36. {
  37. return _quickConnect.IsEnabled;
  38. }
  39. /// <summary>
  40. /// Initiate a new quick connect request.
  41. /// </summary>
  42. /// <response code="200">Quick connect request successfully created.</response>
  43. /// <response code="401">Quick connect is not active on this server.</response>
  44. /// <returns>A <see cref="QuickConnectResult"/> with a secret and code for future use or an error message.</returns>
  45. [HttpGet("Initiate")]
  46. [ProducesResponseType(StatusCodes.Status200OK)]
  47. public ActionResult<QuickConnectResult> Initiate()
  48. {
  49. try
  50. {
  51. return _quickConnect.TryConnect();
  52. }
  53. catch (AuthenticationException)
  54. {
  55. return Unauthorized("Quick connect is disabled");
  56. }
  57. }
  58. /// <summary>
  59. /// Attempts to retrieve authentication information.
  60. /// </summary>
  61. /// <param name="secret">Secret previously returned from the Initiate endpoint.</param>
  62. /// <response code="200">Quick connect result returned.</response>
  63. /// <response code="404">Unknown quick connect secret.</response>
  64. /// <returns>An updated <see cref="QuickConnectResult"/>.</returns>
  65. [HttpGet("Connect")]
  66. [ProducesResponseType(StatusCodes.Status200OK)]
  67. [ProducesResponseType(StatusCodes.Status404NotFound)]
  68. public ActionResult<QuickConnectResult> Connect([FromQuery, Required] string secret)
  69. {
  70. try
  71. {
  72. return _quickConnect.CheckRequestStatus(secret);
  73. }
  74. catch (ResourceNotFoundException)
  75. {
  76. return NotFound("Unknown secret");
  77. }
  78. catch (AuthenticationException)
  79. {
  80. return Unauthorized("Quick connect is disabled");
  81. }
  82. }
  83. /// <summary>
  84. /// Authorizes a pending quick connect request.
  85. /// </summary>
  86. /// <param name="code">Quick connect code to authorize.</param>
  87. /// <response code="200">Quick connect result authorized successfully.</response>
  88. /// <response code="403">Unknown user id.</response>
  89. /// <returns>Boolean indicating if the authorization was successful.</returns>
  90. [HttpPost("Authorize")]
  91. [Authorize(Policy = Policies.DefaultAuthorization)]
  92. [ProducesResponseType(StatusCodes.Status200OK)]
  93. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  94. public async Task<ActionResult<bool>> Authorize([FromQuery, Required] string code)
  95. {
  96. var userId = ClaimHelpers.GetUserId(Request.HttpContext.User);
  97. if (!userId.HasValue)
  98. {
  99. return StatusCode(StatusCodes.Status403Forbidden, "Unknown user id");
  100. }
  101. try
  102. {
  103. return await _quickConnect.AuthorizeRequest(userId.Value, code).ConfigureAwait(false);
  104. }
  105. catch (AuthenticationException)
  106. {
  107. return Unauthorized("Quick connect is disabled");
  108. }
  109. }
  110. }
  111. }