Fixed cookie loading. Started implementing service expire handling

This commit is contained in:
Baumgartner Marcel 2023-11-15 15:24:27 +01:00
parent 0f989a38c3
commit 3092daaad4
8 changed files with 96 additions and 10 deletions

View file

@ -1,4 +1,5 @@
using Microsoft.JSInterop;
using Moonlight.App.Helpers;
namespace Moonlight.App.Services.Interop;
@ -30,7 +31,9 @@ public class CookieService
if(string.IsNullOrEmpty(cookiePart))
continue;
var cookieKeyValue = cookiePart.Split("=");
var cookieKeyValue = cookiePart.Split("=")
.Select(x => x.Trim()) // There may be spaces e.g. with the "AspNetCore.Culture" key
.ToArray();
if (cookieKeyValue.Length == 2)
{

View file

@ -45,4 +45,17 @@ public class ServiceManageService
// No match
return Task.FromResult(false);
}
public Task<bool> NeedsRenewal(Service s)
{
// We fetch the service in a new scope wo ensure that we are not caching
using var scope = ServiceScopeFactory.CreateScope();
var serviceRepo = scope.ServiceProvider.GetRequiredService<Repository<Service>>();
var service = serviceRepo
.Get()
.First(x => x.Id == s.Id);
return Task.FromResult(DateTime.UtcNow > service.RenewAt);
}
}

View file

@ -1,5 +1,6 @@
using JWT.Algorithms;
using JWT.Builder;
using Moonlight.App.Helpers;
using Newtonsoft.Json;
namespace Moonlight.App.Services.Utils;
@ -47,6 +48,7 @@ public class JwtService
}
catch (Exception e)
{
Logger.Warn(e.Message);
return Task.FromResult(false);
}
}

View file

@ -0,0 +1,16 @@
<div class="d-flex flex-column flex-center text-center p-10">
<div class="card card-flush w-lg-650px py-5">
<div class="card-body py-15 py-lg-20">
<div class="mb-5">
<img src="/svg/expired.svg" style="width: 10vh" alt="Expired illustration">
</div>
<h1 class="fw-bolder fs-2hx text-gray-900 mb-4">
This service has expired
</h1>
<div class="fw-semibold fs-6 text-gray-500 mb-7">
<span class="fs-5">This service has expired and has to be renewed in order to manage it</span>
</div>
<a href="/services" class="btn btn-primary">Go back to services</a>
</div>
</div>
</div>

View file

@ -128,7 +128,13 @@ else
<div class="card">
<div class="card-header">
<h3 class="card-title align-items-start flex-column">
<span class="card-label fw-bold text-dark fs-3">@(Service.Nickname ?? $"Service {Service.Id}")</span>
<span class="card-label fw-bold text-dark fs-3">
@(Service.Nickname ?? $"Service {Service.Id}")
@if (NeedsRenewal)
{
<span class="ms-2 text-danger">(Expired)</span>
}
</span>
<span class="text-gray-400 mt-1 fw-semibold fs-6">@(Service.Product.Name)</span>
</h3>
</div>
@ -177,14 +183,28 @@ else
[Parameter]
public Func<Task> OnChange { get; set; }
// Renew access state
private bool NeedsRenewal = false;
// States
private bool ShowDeletionScreen = false;
private bool ShowRenewScreen = false;
private ManageServiceShareModal ShareModal;
// Renewing
private int DurationMultiplier = 1;
private bool CanBeRenewed = false;
private bool IsValidating = false;
private string ErrorMessage = "";
private bool ShowRenewScreen = false;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
NeedsRenewal = await ServiceService.Manage.NeedsRenewal(Service);
await InvokeAsync(StateHasChanged);
}
}
private Task Revalidate()
{

View file

@ -19,15 +19,22 @@
}
else
{
<CascadingValue Name="Service" Value="Service">
<CascadingValue Name="Implementation" Value="Definition">
<CascadingValue Name="Route" Value="Route">
<CascadingValue Name="ViewContext" Value="ViewContext">
@ViewContext.Layout
if (NeedsRenewal)
{
<NeedsRenewalAlert />
}
else
{
<CascadingValue Name="Service" Value="Service">
<CascadingValue Name="Implementation" Value="Definition">
<CascadingValue Name="Route" Value="Route">
<CascadingValue Name="ViewContext" Value="ViewContext">
@ViewContext.Layout
</CascadingValue>
</CascadingValue>
</CascadingValue>
</CascadingValue>
</CascadingValue>
}
}
</LazyLoader>
@ -43,6 +50,8 @@
private ServiceDefinition Definition;
private ServiceViewContext ViewContext;
private bool NeedsRenewal = false;
private async Task Load(LazyLoader lazyLoader)
{
await lazyLoader.SetText("Requesting service");
@ -64,6 +73,11 @@
if (Service == null)
return;
NeedsRenewal = await ServiceService.Manage.NeedsRenewal(Service);
if(NeedsRenewal) // Stop loading more data
return;
// Load implementation
await lazyLoader.SetText("Loading implementation");
Definition = ServiceService.Definition.Get(Service.Product.Type);

View file

@ -19,11 +19,19 @@
@foreach (var service in SharedServices)
{
var needsRenewal = SharedRenewalStates[service];
<div class="col-md-3 col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title align-items-start flex-column">
<span class="card-label fw-bold text-dark fs-3">@(service.Nickname ?? $"Service {service.Id}")</span>
<span class="card-label fw-bold text-dark fs-3">
@(service.Nickname ?? $"Service {service.Id}")
@if (needsRenewal)
{
<span class="ms-2 text-danger">(Expired)</span>
}
</span>
<span class="text-gray-400 mt-1 fw-semibold fs-6">@(service.Product.Name)</span>
</h3>
</div>
@ -57,10 +65,19 @@
private Service[] MyServices;
private Service[] SharedServices;
private Dictionary<Service, bool> SharedRenewalStates = new();
private async Task Load(LazyLoader _)
{
// Load all services
MyServices = await ServiceService.Get(IdentityService.CurrentUser);
SharedServices = await ServiceService.GetShared(IdentityService.CurrentUser);
// Load all services renewal states
foreach (var service in SharedServices)
{
if(!SharedRenewalStates.ContainsKey(service))
SharedRenewalStates.Add(service, await ServiceService.Manage.NeedsRenewal(service));
}
}
}

1
Moonlight/wwwroot/svg/expired.svg vendored Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 12 KiB