From a994502e59d0b65c2374241833a9fe764ac00493 Mon Sep 17 00:00:00 2001 From: ta264 Date: Sun, 19 Jun 2022 10:29:29 +0100 Subject: [PATCH] Add api endpoint to generate the required login cookie (cherry picked from commit 4180e2787a1ad5284873de4847f345b2c47df72a) --- .../AuthenticationController.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Radarr.Api.V4/Authentication/AuthenticationController.cs b/src/Radarr.Api.V4/Authentication/AuthenticationController.cs index fce1f1ef6..8a0b82657 100644 --- a/src/Radarr.Api.V4/Authentication/AuthenticationController.cs +++ b/src/Radarr.Api.V4/Authentication/AuthenticationController.cs @@ -4,8 +4,10 @@ using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Mvc; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Notifications.Plex.PlexTv; using Radarr.Http; +using Radarr.Http.Authentication.Plex; namespace Radarr.Api.V4.Authentication { @@ -13,10 +15,14 @@ namespace Radarr.Api.V4.Authentication public class AuthenticationController : Controller { private readonly IPlexTvService _plex; + private readonly IConfigService _configService; + private readonly IConfigFileProvider _configFileProvider; - public AuthenticationController(IPlexTvService plex) + public AuthenticationController(IPlexTvService plex, IConfigService configService, IConfigFileProvider configFileProvider) { _plex = plex; + _configService = configService; + _configFileProvider = configFileProvider; } [HttpGet("plex/resources")] @@ -24,5 +30,32 @@ namespace Radarr.Api.V4.Authentication { return _plex.GetResources(accessToken); } + + [HttpGet("cookie")] + public async Task GetCookie() + { + var authType = _configFileProvider.AuthenticationMethod; + + var claims = new List + { + new Claim("user", "Anonymous"), + new Claim("AuthType", authType.ToString()) + }; + + if (authType == NzbDrone.Core.Authentication.AuthenticationType.Plex) + { + var claimType = _configService.PlexRequireOwner ? PlexConstants.ServerOwnedClaim : PlexConstants.ServerAccessClaim; + claims.Add(new Claim(claimType, _configService.PlexAuthServer)); + } + + var properties = new AuthenticationProperties + { + IsPersistent = true + }; + + await HttpContext.SignInAsync(authType.ToString(), new ClaimsPrincipal(new ClaimsIdentity(claims, "Cookies", "user", "identifier")), properties); + + return StatusCode((int)HttpStatusCode.OK); + } } }