Skip to content

Commit

Permalink
Implemented the ability to change the JWT key on runtime. (Kareadita#217
Browse files Browse the repository at this point in the history
)

* Implemented the ability to change the JWT key on runtime.

* Added .7z file extension support

* Cleanup

* Added Feathub link

* Code cleanup

* Fixed up a build issue on CI
  • Loading branch information
majora2007 authored May 14, 2021
1 parent 98e8b72 commit 03b49a5
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 24 deletions.
15 changes: 0 additions & 15 deletions API/Controllers/ReaderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,21 +288,6 @@ public async Task<ActionResult<int>> GetNextChapter(int seriesId, int volumeId,
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;
Expand Down
2 changes: 1 addition & 1 deletion API/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 17 additions & 3 deletions API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using API.Data;
Expand All @@ -14,7 +15,6 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Sentry;
using Sentry.Extensions.Logging;

namespace API
{
Expand All @@ -26,12 +26,26 @@ protected Program()
{
}

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();
Expand Down
2 changes: 0 additions & 2 deletions API/Startup.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
47 changes: 47 additions & 0 deletions Kavita.Common/Configuration.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
4 changes: 1 addition & 3 deletions Kavita.Common/Kavita.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageReference Include="Sentry" Version="3.3.4" />
</ItemGroup>

<ItemGroup>
<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>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Binary file modified favicon.ico
Binary file not shown.

0 comments on commit 03b49a5

Please sign in to comment.