Kavita+ Reset License & Discord Integration (#2516)

This commit is contained in:
Joe Milazzo 2024-01-04 14:53:15 -06:00 committed by GitHub
parent 138794ffed
commit c37596889a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 230 additions and 86 deletions

View file

@ -26,8 +26,9 @@ public interface ILicenseService
{
Task ValidateLicenseStatus();
Task RemoveLicense();
Task AddLicense(string license, string email);
Task AddLicense(string license, string email, string? discordId);
Task<bool> HasActiveLicense(bool forceCheck = false);
Task<bool> ResetLicense(string license, string email);
}
public class LicenseService : ILicenseService
@ -87,7 +88,7 @@ public class LicenseService : ILicenseService
/// <param name="license"></param>
/// <param name="email"></param>
/// <returns></returns>
private async Task<string> RegisterLicense(string license, string email)
private async Task<string> RegisterLicense(string license, string email, string? discordId)
{
if (string.IsNullOrWhiteSpace(license) || string.IsNullOrWhiteSpace(email)) return string.Empty;
try
@ -104,7 +105,8 @@ public class LicenseService : ILicenseService
{
License = license.Trim(),
InstallId = HashUtil.ServerToken(),
EmailId = email.Trim()
EmailId = email.Trim(),
DiscordId = discordId?.Trim()
})
.ReceiveJson<RegisterLicenseResponseDto>();
@ -164,10 +166,10 @@ public class LicenseService : ILicenseService
await provider.RemoveAsync(CacheKey);
}
public async Task AddLicense(string license, string email)
public async Task AddLicense(string license, string email, string? discordId)
{
var serverSetting = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
var lic = await RegisterLicense(license, email);
var lic = await RegisterLicense(license, email, discordId);
if (string.IsNullOrWhiteSpace(lic))
throw new KavitaException("unable-to-register-k+");
serverSetting.Value = lic;
@ -199,4 +201,43 @@ public class LicenseService : ILicenseService
return false;
}
public async Task<bool> ResetLicense(string license, string email)
{
try
{
var encryptedLicense = await _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.LicenseKey);
var response = await (Configuration.KavitaPlusApiUrl + "/api/license/reset")
.WithHeader("Accept", "application/json")
.WithHeader("User-Agent", "Kavita")
.WithHeader("x-license-key", encryptedLicense.Value)
.WithHeader("x-installId", HashUtil.ServerToken())
.WithHeader("x-kavita-version", BuildInfo.Version)
.WithHeader("Content-Type", "application/json")
.WithTimeout(TimeSpan.FromSeconds(Configuration.DefaultTimeOutSecs))
.PostJsonAsync(new ResetLicenseDto()
{
License = license.Trim(),
InstallId = HashUtil.ServerToken(),
EmailId = email
})
.ReceiveString();
if (string.IsNullOrEmpty(response))
{
var provider = _cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
await provider.RemoveAsync(CacheKey);
return true;
}
_logger.LogError("An error happened during the request to Kavita+ API: {ErrorMessage}", response);
throw new KavitaException(response);
}
catch (FlurlHttpException e)
{
_logger.LogError(e, "An error happened during the request to Kavita+ API");
}
return false;
}
}