using System;
using Jellyfin.Plugin.SSO_Auth.State;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Plugins;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
namespace Jellyfin.Plugin.SSO_Auth;
///
/// Registers the plugin's services. The OAuth state store is backed by the shared cache the server
/// already connects to when one is configured, which is what lets a login start on one server instance
/// and be completed on another.
///
public class SsoAuthServiceRegistrator : IPluginServiceRegistrator
{
///
public void RegisterServices(IServiceCollection serviceCollection, IServerApplicationHost applicationHost)
{
ArgumentNullException.ThrowIfNull(serviceCollection);
serviceCollection.AddSingleton(sp =>
{
var logger = sp.GetRequiredService().CreateLogger();
var multiplexer = sp.GetService();
if (multiplexer is null)
{
logger.LogWarning(
"SSO OAuth state store: {Store}. Logins must start and finish on the same server instance.",
nameof(InMemorySsoStateStore));
return new InMemorySsoStateStore();
}
logger.LogInformation(
"SSO OAuth state store: {Store}. Logins are shared across server instances.",
nameof(RedisSsoStateStore));
return new RedisSsoStateStore(multiplexer);
});
}
}