From cc05b12a230ab63ea2c496d99385aa80c776c10d Mon Sep 17 00:00:00 2001 From: Matthew Strasiotto <39424834+matthewstrasiotto@users.noreply.github.com> Date: Sat, 18 Jun 2022 14:51:10 +1000 Subject: [PATCH] Add controller for serving views --- SSO-Auth/Api/SSOViewsController.cs | 124 +++++++++++++++++++++++++++++ SSO-Auth/SSOPlugin.cs | 28 ++++++- 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 SSO-Auth/Api/SSOViewsController.cs diff --git a/SSO-Auth/Api/SSOViewsController.cs b/SSO-Auth/Api/SSOViewsController.cs new file mode 100644 index 0000000..70770b5 --- /dev/null +++ b/SSO-Auth/Api/SSOViewsController.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Mime; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using IdentityModel.OidcClient; +using Jellyfin.Data.Entities; +using Jellyfin.Data.Enums; +using Jellyfin.Plugin.SSO_Auth; +using Jellyfin.Plugin.SSO_Auth.Config; +using Jellyfin.Plugin.SSO_Auth.Helpers; +using MediaBrowser.Controller.Authentication; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Net; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model; +using MediaBrowser.Model.Plugins; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ViewEngines; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace Jellyfin.Plugin.SSO_Auth.Views; + +/// +/// The sso views controller. +/// +[ApiController] +[Route("[controller]")] +public class SSOViewsController : ControllerBase +{ + private readonly IUserManager _userManager; + private readonly ISessionManager _sessionManager; + private readonly IAuthorizationContext _authContext; + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + public SSOViewsController(ILogger logger, ISessionManager sessionManager, IUserManager userManager, IAuthorizationContext authContext) + { + _sessionManager = sessionManager; + _userManager = userManager; + _authContext = authContext; + _logger = logger; + _logger.LogInformation("SSO Views Controller initialized"); + } + + private ActionResult ServeView(string viewName) + { + IEnumerable pages = null; + if (SSOPlugin.Instance == null) + { + return BadRequest("No plugin instance found"); + } + + pages = SSOPlugin.Instance.GetViews(); + + if (pages == null) + { + return NotFound("Pages is null or empty"); + } + + var view = pages.FirstOrDefault(pageInfo => pageInfo.Name == viewName, null); + + if (view == null) + { + return NotFound("No matching view found"); + } +#nullable enable + Stream? stream = SSOPlugin.Instance.GetType().Assembly.GetManifestResourceStream(view.EmbeddedResourcePath); + + if (stream == null) + { + _logger.LogError("Failed to get resource {Resource}", view.EmbeddedResourcePath); + return NotFound(); + } +#nullable disable + return File(stream, MimeTypes.GetMimeType(view.EmbeddedResourcePath)); + } + + /// + /// Gets the html view for the linking interface. + /// + /// The html view for the linking interface. + // [Authorize(Policy = "DefaultAuthorization")] + [HttpGet("linking")] + public ActionResult GetLinkingView() + { + return ServeView("SSO-Auth-linking"); + } + + /// + /// Returns the client code for the linking view. + /// + /// The html view for the linking interface. + // [Authorize(Policy = "DefaultAuthorization")] + [HttpGet("linking.js")] + public ActionResult GetLinkingJS() + { + return ServeView("SSO-Auth-linking.js"); + } + + /// + /// Returns the shared js module that initializes the ApiClient. + /// + /// The html view for the linking interface. + // [Authorize(Policy = "DefaultAuthorization")] + [HttpGet("ApiClient.js")] + public ActionResult GetApiClientView() + { + return ServeView("SSO-Auth-ApiClient.js"); + } +} \ No newline at end of file diff --git a/SSO-Auth/SSOPlugin.cs b/SSO-Auth/SSOPlugin.cs index aff3dfc..fd612a5 100644 --- a/SSO-Auth/SSOPlugin.cs +++ b/SSO-Auth/SSOPlugin.cs @@ -11,7 +11,7 @@ namespace Jellyfin.Plugin.SSO_Auth; /// /// The SSO plugin class. /// -public class SSOPlugin : BasePlugin, IHasWebPages +public class SSOPlugin : BasePlugin, IPlugin, IHasWebPages { /// /// Initializes a new instance of the class. @@ -74,4 +74,30 @@ public class SSOPlugin : BasePlugin, IHasWebPages }, }; } + + /// + /// Returns the available user views for this plugin. + /// + /// A list of user views for this plugin. + public IEnumerable GetViews() + { + return new[] + { + new PluginPageInfo + { + Name = Name + ".css", + EmbeddedResourcePath = $"{GetType().Namespace}.Config.style.css" + }, + new PluginPageInfo + { + Name = Name + "-linking", + EmbeddedResourcePath = $"{GetType().Namespace}.Config.linking.html" + }, + new PluginPageInfo + { + Name = Name + "-linking.js", + EmbeddedResourcePath = $"{GetType().Namespace}.Config.linking.js" + }, + }; + } }