aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Session/ApiKeysService.cs
diff options
context:
space:
mode:
authordkanada <dkanada@users.noreply.github.com>2020-02-06 00:26:21 +0900
committerdkanada <dkanada@users.noreply.github.com>2020-02-06 00:26:21 +0900
commitc2733ac0dc50e0447a6906a634d27ec9f111d23a (patch)
tree257d5f94ce4ed38f96d7c016a084d7abf9e64d88 /MediaBrowser.Api/Session/ApiKeysService.cs
parentb3811a9498fe06b68693f4a269de902cdd7eb2a2 (diff)
split api keys into their own service
Diffstat (limited to 'MediaBrowser.Api/Session/ApiKeysService.cs')
-rw-r--r--MediaBrowser.Api/Session/ApiKeysService.cs85
1 files changed, 85 insertions, 0 deletions
diff --git a/MediaBrowser.Api/Session/ApiKeysService.cs b/MediaBrowser.Api/Session/ApiKeysService.cs
new file mode 100644
index 000000000..45d7ff421
--- /dev/null
+++ b/MediaBrowser.Api/Session/ApiKeysService.cs
@@ -0,0 +1,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.Session
+{
+ [Route("/Auth/Keys", "GET")]
+ [Authenticated(Roles = "Admin")]
+ public class GetApiKeys
+ {
+ }
+
+ [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 ApiKeysService : BaseApiService
+ {
+ private readonly ISessionManager _sessionManager;
+
+ private readonly IAuthenticationRepository _authRepo;
+
+ private readonly IServerApplicationHost _appHost;
+
+ public ApiKeysService(
+ ILogger<ApiKeysService> 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(GetApiKeys request)
+ {
+ var result = _authRepo.Get(new AuthenticationInfoQuery
+ {
+ HasUser = false
+ });
+
+ return result;
+ }
+ }
+}