Remove the weird configuration options, add ApiKeys configuration

This commit is contained in:
Zoe Roux 2023-03-20 11:55:04 +09:00
parent 4d0dd674ad
commit bf281820b9
11 changed files with 47 additions and 79 deletions

View File

@ -1,9 +1,20 @@
# Useful config options
LIBRARY_ROOT=/video
# The following two values should be set to a random sequence of characters.
# You MUST change thoses when installing kyoo (for security)
AUTHENTICATION_SECRET=4c@mraGB!KRfF@kpS8739y9FcHemKxBsqqxLbdR?
# You can input multiple api keys separated by a ,
KYOO_APIKEYS=t7H5!@4iMNsAaSJQ49pat4jprJgTcF656if#J3
DEFAULT_PERMISSIONS=overall.read
UNLOGGED_PERMISSIONS=overall.read
TVDB__APIKEY=
THEMOVIEDB_APIKEY=
PUBLIC_BACK_URL=http://localhost:5000
AUTHENTICATION_SECRET=
# Following options are optional and only useful for debugging.

View File

@ -39,15 +39,6 @@ namespace Kyoo.Abstractions.Controllers
/// </summary>
string Name { get; }
/// <summary>
/// A list of types that will be available via the IOptions interfaces and will be listed inside
/// an IConfiguration.
///
/// If a field should be loosely typed, <see cref="Dictionary{TKey,TValue}"/> or <c>null</c>
/// can be specified.
/// </summary>
Dictionary<string, Type> Configuration { get; }
/// <summary>
/// An optional configuration step to allow a plugin to change asp net configurations.
/// </summary>

View File

@ -16,7 +16,6 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Text;
using Autofac;
@ -38,13 +37,6 @@ namespace Kyoo.Authentication
/// <inheritdoc />
public string Name => "Authentication";
/// <inheritdoc />
public Dictionary<string, Type> Configuration => new()
{
{ AuthenticationOption.Path, typeof(AuthenticationOption) },
{ PermissionOption.Path, typeof(PermissionOption) },
};
/// <summary>
/// The configuration to use.
/// </summary>
@ -69,9 +61,17 @@ namespace Kyoo.Authentication
/// <inheritdoc />
public void Configure(IServiceCollection services)
{
AuthenticationOption jwt = ConfigurationBinder.Get<AuthenticationOption>(
_configuration.GetSection(AuthenticationOption.Path)
);
string secret = _configuration.GetValue("AUTHENTICATION_SECRET", AuthenticationOption.DefaultSecret);
services.Configure<AuthenticationOption>(x =>
{
x.Secret = secret;
x.Permissions = new PermissionOption
{
Default = _configuration.GetValue<string>("UNLOGGED_PERMISSIONS", "overall.read").Split(','),
NewUser = _configuration.GetValue<string>("DEFAULT_PERMISSIONS", "overall.read").Split(','),
ApiKeys = _configuration.GetValue("KYOO_APIKEYS", string.Empty).Split(','),
};
});
// TODO handle direct-videos with bearers (probably add a cookie and a app.Use to translate that for videos)
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
@ -83,7 +83,7 @@ namespace Kyoo.Authentication
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwt.Secret))
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret))
};
});
}

View File

@ -42,10 +42,5 @@ namespace Kyoo.Authentication.Models
/// Options for permissions
/// </summary>
public PermissionOption Permissions { get; set; } = new();
/// <summary>
/// Root path of user's profile pictures.
/// </summary>
public string ProfilePicturePath { get; set; } = "users/";
}
}

View File

@ -49,11 +49,16 @@ namespace Kyoo.Authentication.Models
/// <summary>
/// The default permissions that will be given to a non-connected user.
/// </summary>
public string[] Default { get; set; } = new[] { "overall.read" };
public string[] Default { get; set; } = { "overall.read" };
/// <summary>
/// Permissions applied to a new user.
/// </summary>
public string[] NewUser { get; set; } = new[] { "overall.read" };
public string[] NewUser { get; set; } = { "overall.read" };
/// <summary>
/// The list of available ApiKeys.
/// </summary>
public string[] ApiKeys { get; set; } = Array.Empty<string>();
}
}

View File

@ -42,12 +42,6 @@ namespace Kyoo.Core
/// <inheritdoc />
public string Name => "Core";
/// <inheritdoc />
public Dictionary<string, Type> Configuration => new()
{
{ "database", null },
};
/// <inheritdoc />
public void Configure(ContainerBuilder builder)
{

View File

@ -16,7 +16,6 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using Autofac;
using Autofac.Extras.AttributeMetadata;
@ -24,6 +23,8 @@ using Kyoo.Abstractions.Controllers;
using Kyoo.Core.Models.Options;
using Kyoo.Host.Controllers;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
namespace Kyoo.Host
@ -36,24 +37,25 @@ namespace Kyoo.Host
/// <inheritdoc />
public string Name => "Host";
/// <inheritdoc />
public Dictionary<string, Type> Configuration => new()
{
{ BasicOptions.Path, typeof(BasicOptions) },
};
/// <summary>
/// The plugin manager that loaded all plugins.
/// </summary>
private readonly IPluginManager _plugins;
/// <summary>
/// The configuration used to register options.
/// </summary>
private readonly IConfiguration _configuration;
/// <summary>
/// Create a new <see cref="HostModule"/>.
/// </summary>
/// <param name="plugins">The plugin manager that loaded all plugins.</param>
public HostModule(IPluginManager plugins)
/// <param name="configuration"> The configuration used to register options.</param>
public HostModule(IPluginManager plugins, IConfiguration configuration)
{
_plugins = plugins;
_configuration = configuration;
}
/// <inheritdoc />
@ -64,6 +66,12 @@ namespace Kyoo.Host
builder.RegisterComposite<FileSystemComposite, IFileSystem>().InstancePerLifetimeScope();
}
/// <inheritdoc />
public void Configure(IServiceCollection services)
{
services.Configure<BasicOptions>(_configuration.GetSection(BasicOptions.Path));
}
/// <inheritdoc />
public IEnumerable<IStartupAction> ConfigureSteps => new[]
{

View File

@ -112,20 +112,6 @@ namespace Kyoo.Host
_hostModule.Configure(services);
foreach (IPlugin plugin in _plugins.GetAllPlugins())
plugin.Configure(services);
IEnumerable<KeyValuePair<string, Type>> configTypes = _plugins.GetAllPlugins()
.Append(_hostModule)
.SelectMany(x => x.Configuration)
.Where(x => x.Value != null);
foreach ((string path, Type type) in configTypes)
{
Utility.RunGenericMethod<object>(
typeof(OptionsConfigurationServiceCollectionExtensions),
nameof(OptionsConfigurationServiceCollectionExtensions.Configure),
type,
services, _configuration.GetSection(path)
);
}
}
/// <summary>

View File

@ -1,16 +0,0 @@
{
"basics": {
"transmuxPath": "cached/transmux",
"transcodePath": "cached/transcode",
"metadataPath": "metadata/"
},
"authentication": {
"permissions": {
"default": ["overall.read", "overall.write"],
"newUser": ["overall.read", "overall.write"]
},
"profilePicturePath": "users/",
"secret": "4c@mraGB!KRfF@kpS8740y9FcHemKxBsqqxLbdR?"
}
}

View File

@ -37,9 +37,6 @@ namespace Kyoo.Postgresql
/// <inheritdoc />
public string Name => "Postgresql";
/// <inheritdoc />
public Dictionary<string, Type> Configuration => new();
/// <summary>
/// The configuration to use. The database connection string is pulled from it.
/// </summary>

View File

@ -40,9 +40,6 @@ namespace Kyoo.Swagger
/// <inheritdoc />
public string Name => "Swagger";
/// <inheritdoc />
public Dictionary<string, Type> Configuration => new();
/// <inheritdoc />
public void Configure(IServiceCollection services)
{