Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Implemented the ability to change the JWT key on runtime. (#217)

Joseph Milazzo03b49a52685f
diff --git a/API/Controllers/ReaderController.cs b/API/Controllers/ReaderController.cs
index 0436e7d65..14fc9aead 100644
--- a/API/Controllers/ReaderController.cs
+++ b/API/Controllers/ReaderController.cs
@@ -288,21 +288,6 @@ namespace API.Controllers
return Ok(-1);
}
- private int GetNextChapterId(Volume currentVolume, int currentChapterId)
- {
- var next = false;
- foreach (var chapter in currentVolume.Chapters)
- {
- if (next)
- {
- return chapter.Id;
- }
- if (currentChapterId == chapter.Id) next = true;
- }
-
- return -1;
- }
-
private int GetNextChapterId(IEnumerable<Chapter> chapters, int currentChapterId)
{
var next = false;
diff --git a/API/Parser/Parser.cs b/API/Parser/Parser.cs
index 9612fb7cd..3cffb87e6 100644
--- a/API/Parser/Parser.cs
+++ b/API/Parser/Parser.cs
@@ -9,7 +9,7 @@ namespace API.Parser
{
public static class Parser
{
- public static readonly string ArchiveFileExtensions = @"\.cbz|\.zip|\.rar|\.cbr|\.tar.gz|\.7zip";
+ public static readonly string ArchiveFileExtensions = @"\.cbz|\.zip|\.rar|\.cbr|\.tar.gz|\.7zip|\.7z";
public static readonly string BookFileExtensions = @"\.epub";
public static readonly string ImageFileExtensions = @"^(\.png|\.jpeg|\.jpg)";
public static readonly Regex FontSrcUrlRegex = new Regex("(src:url\\(\"?'?)([a-z0-9/\\._]+)(\"?'?\\))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
diff --git a/API/Program.cs b/API/Program.cs
index 2240e6ad6..db3ad9a1f 100644
--- a/API/Program.cs
+++ b/API/Program.cs
@@ -1,5 +1,6 @@
using System;
using System.IO;
+using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using API.Data;
@@ -14,7 +15,6 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Sentry;
-using Sentry.Extensions.Logging;
namespace API
{
@@ -26,12 +26,26 @@ namespace API
{
}
+ private static string GetAppSettingFilename()
+ {
+ var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
+ var isDevelopment = environment == Environments.Development;
+ return "appSettings" + (isDevelopment ? ".Development" : "") + ".json";
+ }
+
public static async Task Main(string[] args)
{
// Before anything, check if JWT has been generated properly or if user still has default
+ if (!Configuration.CheckIfJwtTokenSet(GetAppSettingFilename()))
+ {
+ Console.WriteLine("Generating JWT TokenKey for encrypting user sessions...");
+ var rBytes = new byte[24];
+ using (var crypto = new RNGCryptoServiceProvider()) crypto.GetBytes(rBytes);
+ var base64 = Convert.ToBase64String(rBytes).Replace("/", "");
+ Configuration.UpdateJwtToken(GetAppSettingFilename(), base64);
+ }
+
-
-
var host = CreateHostBuilder(args).Build();
using var scope = host.Services.CreateScope();
diff --git a/API/Startup.cs b/API/Startup.cs
index 90c796944..29cce3832 100644
--- a/API/Startup.cs
+++ b/API/Startup.cs
@@ -1,8 +1,6 @@
using System;
-using System.IO;
using System.IO.Compression;
using System.Linq;
-using System.Reflection;
using API.Extensions;
using API.Interfaces;
using API.Middleware;
diff --git a/Kavita.Common/Configuration.cs b/Kavita.Common/Configuration.cs
index e69de29bb..5503e5dc1 100644
--- a/Kavita.Common/Configuration.cs
+++ b/Kavita.Common/Configuration.cs
@@ -0,0 +1,47 @@
+using System;
+using System.IO;
+using System.Text.Json;
+
+namespace Kavita.Common
+{
+ public static class Configuration
+ {
+
+ public static bool CheckIfJwtTokenSet(string filePath)
+ {
+ try {
+ var json = File.ReadAllText(filePath);
+ var jsonObj = JsonSerializer.Deserialize<dynamic>(json);
+ const string key = "TokenKey";
+
+ JsonElement? tokenElement = null;
+ if (jsonObj?.TryGetProperty(key, out tokenElement))
+ {
+ return tokenElement?.GetString() != "super secret unguessable key";
+ }
+
+ return false;
+
+ }
+ catch (Exception ex) {
+ Console.WriteLine("Error writing app settings: " + ex.Message);
+ }
+
+ return false;
+ }
+
+ public static bool UpdateJwtToken(string filePath, string token)
+ {
+ try
+ {
+ var json = File.ReadAllText(filePath).Replace("super secret unguessable key", token);
+ File.WriteAllText(filePath, json);
+ return true;
+ }
+ catch (Exception)
+ {
+ return false;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/Kavita.Common/Kavita.Common.csproj b/Kavita.Common/Kavita.Common.csproj
index 736bcdb7a..1b93ba622 100644
--- a/Kavita.Common/Kavita.Common.csproj
+++ b/Kavita.Common/Kavita.Common.csproj
@@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
+ <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageReference Include="Sentry" Version="3.3.4" />
</ItemGroup>
@@ -16,9 +17,6 @@
<Reference Include="JetBrains.ReSharper.TestRunner.Merged, Version=1.3.1.55, Culture=neutral, PublicKeyToken=5c492ec4f3eccde3">
<HintPath>D:\Program Files\JetBrains\JetBrains Rider 2020.3.2\lib\ReSharperHost\TestRunner\netcoreapp2.0\JetBrains.ReSharper.TestRunner.Merged.dll</HintPath>
</Reference>
- <Reference Include="Microsoft.Win32.Registry, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
- <HintPath>..\..\..\..\..\..\..\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.5\Microsoft.Win32.Registry.dll</HintPath>
- </Reference>
</ItemGroup>
</Project>
diff --git a/README.md b/README.md
index 83229889c..286dbc50e 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,7 @@ your manga collection with your friends and family!
[![Discord](https://img.shields.io/badge/discord-chat-7289DA.svg?maxAge=60)](https://discord.gg/eczRp9eeem)
![Github Downloads](https://img.shields.io/github/downloads/Kareadita/Kavita/total.svg)
+[![Feature Requests](https://feathub.com/Kareadita/Kavita?format=svg)](https://feathub.com/Kareadita/Kavita)
## Goals:
diff --git a/favicon.ico b/favicon.ico
index 1ed03f4f7..cc5e73519 100644
Binary files a/favicon.ico and b/favicon.ico differ