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