aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Sessions/ApiKeyService.cs
blob: 5102ce0a7c32b3720e65260387773ce467c08a64 (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
82
83
84
85
using System;
using System.Globalization;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Services;
using Microsoft.Extensions.Logging;

namespace MediaBrowser.Api.Sessions
{
    [Route("/Auth/Keys", "GET")]
    [Authenticated(Roles = "Admin")]
    public class GetKeys
    {
    }

    [Route("/Auth/Keys/{Key}", "DELETE")]
    [Authenticated(Roles = "Admin")]
    public class RevokeKey
    {
        [ApiMember(Name = "Key", Description = "Authentication key", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
        public string Key { get; set; }
    }

    [Route("/Auth/Keys", "POST")]
    [Authenticated(Roles = "Admin")]
    public class CreateKey
    {
        [ApiMember(Name = "App", Description = "Name of the app using the authentication key", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string App { get; set; }
    }

    public class ApiKeyService : BaseApiService
    {
        private readonly ISessionManager _sessionManager;

        private readonly IAuthenticationRepository _authRepo;

        private readonly IServerApplicationHost _appHost;

        public ApiKeyService(
            ILogger<ApiKeyService> logger,
            IServerConfigurationManager serverConfigurationManager,
            IHttpResultFactory httpResultFactory,
            ISessionManager sessionManager,
            IServerApplicationHost appHost,
            IAuthenticationRepository authRepo)
            : base(logger, serverConfigurationManager, httpResultFactory)
        {
            _sessionManager = sessionManager;
            _authRepo = authRepo;
            _appHost = appHost;
        }

        public void Delete(RevokeKey request)
        {
            _sessionManager.RevokeToken(request.Key);
        }

        public void Post(CreateKey request)
        {
            _authRepo.Create(new AuthenticationInfo
            {
                AppName = request.App,
                AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
                DateCreated = DateTime.UtcNow,
                DeviceId = _appHost.SystemId,
                DeviceName = _appHost.FriendlyName,
                AppVersion = _appHost.ApplicationVersionString
            });
        }

        public object Get(GetKeys request)
        {
            var result = _authRepo.Get(new AuthenticationInfoQuery
            {
                HasUser = false
            });

            return result;
        }
    }
}