aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Controllers/ApiKeyController.cs
blob: 96efde5fbde1809686dcb7336f6f15b03c977968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using MediaBrowser.Controller.Security;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Jellyfin.Api.Controllers
{
    /// <summary>
    /// Authentication controller.
    /// </summary>
    [Route("Auth")]
    public class ApiKeyController : BaseJellyfinApiController
    {
        private readonly IAuthenticationManager _authenticationManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="ApiKeyController"/> class.
        /// </summary>
        /// <param name="authenticationManager">Instance of <see cref="IAuthenticationManager"/> interface.</param>
        public ApiKeyController(IAuthenticationManager authenticationManager)
        {
            _authenticationManager = authenticationManager;
        }

        /// <summary>
        /// Get all keys.
        /// </summary>
        /// <response code="200">Api keys retrieved.</response>
        /// <returns>A <see cref="QueryResult{AuthenticationInfo}"/> with all keys.</returns>
        [HttpGet("Keys")]
        [Authorize(Policy = Policies.RequiresElevation)]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task<ActionResult<QueryResult<AuthenticationInfo>>> GetKeys()
        {
            var keys = await _authenticationManager.GetApiKeys();

            return new QueryResult<AuthenticationInfo>
            {
                Items = keys,
                TotalRecordCount = keys.Count
            };
        }

        /// <summary>
        /// Create a new api key.
        /// </summary>
        /// <param name="app">Name of the app using the authentication key.</param>
        /// <response code="204">Api key created.</response>
        /// <returns>A <see cref="NoContentResult"/>.</returns>
        [HttpPost("Keys")]
        [Authorize(Policy = Policies.RequiresElevation)]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        public async Task<ActionResult> CreateKey([FromQuery, Required] string app)
        {
            await _authenticationManager.CreateApiKey(app).ConfigureAwait(false);

            return NoContent();
        }

        /// <summary>
        /// Remove an api key.
        /// </summary>
        /// <param name="key">The access token to delete.</param>
        /// <response code="204">Api key deleted.</response>
        /// <returns>A <see cref="NoContentResult"/>.</returns>
        [HttpDelete("Keys/{key}")]
        [Authorize(Policy = Policies.RequiresElevation)]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        public async Task<ActionResult> RevokeKey([FromRoute, Required] Guid key)
        {
            await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);

            return NoContent();
        }
    }
}