Cleanup JWT error messages

This commit is contained in:
Zoe Roux 2023-03-20 12:11:17 +09:00
parent 86955cf0cb
commit 9e98bf3532
4 changed files with 15 additions and 9 deletions

View File

@ -173,7 +173,8 @@ namespace Kyoo.Authentication
string overallStr = $"{_group.ToString().ToLower()}.{kind.ToString()!.ToLower()}"; string overallStr = $"{_group.ToString().ToLower()}.{kind.ToString()!.ToLower()}";
AuthenticateResult res = _ApiKeyCheck(context); AuthenticateResult res = _ApiKeyCheck(context);
if (res.None) if (res.None)
res = await context.HttpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme); res = await _JwtCheck(context);
if (res.Succeeded) if (res.Succeeded)
{ {
ICollection<string> permissions = res.Principal.GetPermissions(); ICollection<string> permissions = res.Principal.GetPermissions();
@ -190,6 +191,8 @@ namespace Kyoo.Authentication
} }
else if (res.Failure != null) else if (res.Failure != null)
context.Result = _ErrorResult(res.Failure.Message, StatusCodes.Status403Forbidden); context.Result = _ErrorResult(res.Failure.Message, StatusCodes.Status403Forbidden);
else
context.Result = _ErrorResult("Authentication panic", StatusCodes.Status500InternalServerError);
} }
private AuthenticateResult _ApiKeyCheck(ActionContext context) private AuthenticateResult _ApiKeyCheck(ActionContext context)
@ -214,6 +217,15 @@ namespace Kyoo.Authentication
) )
); );
} }
private async Task<AuthenticateResult> _JwtCheck(ActionContext context)
{
AuthenticateResult ret = await context.HttpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
// Change the failure message to make the API nice to use.
if (ret.Failure != null)
return AuthenticateResult.Fail("Invalid JWT token. The token may have expired.");
return ret;
}
} }
/// <summary> /// <summary>

View File

@ -82,11 +82,7 @@ namespace Kyoo.Host
/// <returns>A task representing the whole process</returns> /// <returns>A task representing the whole process</returns>
public async Task Start(string[] args, Action<ContainerBuilder> configure) public async Task Start(string[] args, Action<ContainerBuilder> configure)
{ {
IConfiguration parsed = new ConfigurationBuilder() IConfiguration parsed = _SetupConfig(new ConfigurationBuilder(), args).Build();
.AddEnvironmentVariables()
.AddEnvironmentVariables("KYOO_")
.AddCommandLine(args)
.Build();
string path = Path.GetFullPath(parsed.GetValue("DATADIR", "/kyoo")); string path = Path.GetFullPath(parsed.GetValue("DATADIR", "/kyoo"));
if (!Directory.Exists(path)) if (!Directory.Exists(path))
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
@ -161,7 +157,6 @@ namespace Kyoo.Host
private IConfigurationBuilder _SetupConfig(IConfigurationBuilder builder, string[] args) private IConfigurationBuilder _SetupConfig(IConfigurationBuilder builder, string[] args)
{ {
return builder return builder
.AddJsonFile(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "./settings.json"), false, true)
.AddEnvironmentVariables() .AddEnvironmentVariables()
.AddEnvironmentVariables("KYOO_") .AddEnvironmentVariables("KYOO_")
.AddCommandLine(args); .AddCommandLine(args);

View File

@ -70,7 +70,7 @@ namespace Kyoo.Host
{ {
_plugins = plugins; _plugins = plugins;
_configuration = configuration; _configuration = configuration;
_hostModule = new HostModule(_plugins); _hostModule = new HostModule(_plugins, configuration);
_plugins.LoadPlugins( _plugins.LoadPlugins(
typeof(CoreModule), typeof(CoreModule),
typeof(AuthenticationModule), typeof(AuthenticationModule),

View File

@ -17,7 +17,6 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Controllers;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;