Merge pull request #16 from mateuszskoczek/features/small_fixes

Authentication fix + other small fixes
This commit is contained in:
2024-09-17 20:35:41 +02:00
committed by GitHub
Unverified
20 changed files with 303 additions and 220 deletions

View File

@@ -1,3 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using WatchIt.Database; using WatchIt.Database;
@@ -5,13 +6,36 @@ using WatchIt.Database.Model.Account;
namespace WatchIt.WebAPI.WorkerServices; namespace WatchIt.WebAPI.WorkerServices;
public class DeleteExpiredRefreshTokensService(ILogger<DeleteExpiredRefreshTokensService> logger, DatabaseContext database) : BackgroundService public class DeleteExpiredRefreshTokensService : BackgroundService
{ {
#region SERVICES
private readonly ILogger<DeleteExpiredRefreshTokensService> _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;
#endregion
#region CONSTRUCTORS
public DeleteExpiredRefreshTokensService(ILogger<DeleteExpiredRefreshTokensService> logger, IServiceScopeFactory serviceScopeFactory)
{
_logger = logger;
_serviceScopeFactory = serviceScopeFactory;
}
#endregion
#region PUBLIC METHODS
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
Task delayTask = Task.Delay(300000, stoppingToken); Task delayTask = Task.Delay(300000, stoppingToken);
Task actionTask = Action(); Task actionTask = Action();
@@ -22,9 +46,16 @@ public class DeleteExpiredRefreshTokensService(ILogger<DeleteExpiredRefreshToken
protected async Task Action() protected async Task Action()
{ {
using (IServiceScope scope = _serviceScopeFactory.CreateScope())
{
DatabaseContext database = scope.ServiceProvider.GetService<DatabaseContext>();
IEnumerable<AccountRefreshToken> tokens = database.AccountRefreshTokens.Where(x => x.ExpirationDate < DateTime.UtcNow); IEnumerable<AccountRefreshToken> tokens = database.AccountRefreshTokens.Where(x => x.ExpirationDate < DateTime.UtcNow);
database.AccountRefreshTokens.AttachRange(tokens); database.AccountRefreshTokens.AttachRange(tokens);
database.AccountRefreshTokens.RemoveRange(tokens); database.AccountRefreshTokens.RemoveRange(tokens);
await database.SaveChangesAsync(); await database.SaveChangesAsync();
} }
}
#endregion
} }

View File

@@ -121,7 +121,7 @@ public static class Program
private static WebApplicationBuilder SetupDatabase(this WebApplicationBuilder builder) private static WebApplicationBuilder SetupDatabase(this WebApplicationBuilder builder)
{ {
builder.Services.AddDbContext<DatabaseContext>(x => x.UseLazyLoadingProxies().UseNpgsql(builder.Configuration.GetConnectionString("Default")), ServiceLifetime.Singleton); builder.Services.AddDbContext<DatabaseContext>(x => x.UseLazyLoadingProxies().UseNpgsql(builder.Configuration.GetConnectionString("Default")), ServiceLifetime.Transient);
return builder; return builder;
} }
@@ -134,15 +134,15 @@ public static class Program
private static WebApplicationBuilder SetupServices(this WebApplicationBuilder builder) private static WebApplicationBuilder SetupServices(this WebApplicationBuilder builder)
{ {
// Utility // Utility
builder.Services.AddSingleton<IConfigurationService, ConfigurationService>(); builder.Services.AddTransient<IConfigurationService, ConfigurationService>();
builder.Services.AddSingleton<ITokensService, TokensService>(); builder.Services.AddTransient<ITokensService, TokensService>();
builder.Services.AddSingleton<IUserService, UserService>(); builder.Services.AddTransient<IUserService, UserService>();
// Controller // Controller
builder.Services.AddSingleton<IAccountsControllerService, AccountsControllerService>(); builder.Services.AddTransient<IAccountsControllerService, AccountsControllerService>();
builder.Services.AddSingleton<IGenresControllerService, GenresControllerService>(); builder.Services.AddTransient<IGenresControllerService, GenresControllerService>();
builder.Services.AddSingleton<IMoviesControllerService, MoviesControllerService>(); builder.Services.AddTransient<IMoviesControllerService, MoviesControllerService>();
builder.Services.AddSingleton<IMediaControllerService, MediaControllerService>(); builder.Services.AddTransient<IMediaControllerService, MediaControllerService>();
return builder; return builder;
} }

View File

@@ -114,7 +114,12 @@ public class JWTAuthenticationStateProvider : AuthenticationStateProvider
{ {
AuthenticateResponse? response = null; AuthenticateResponse? response = null;
await _accountsService.AuthenticateRefresh((data) => response = data); void SetResponse(AuthenticateResponse data)
{
response = data;
}
await _accountsService.AuthenticateRefresh(SetResponse);
if (response is not null) if (response is not null)
{ {
@@ -151,8 +156,9 @@ public class JWTAuthenticationStateProvider : AuthenticationStateProvider
public static DateTime ConvertFromUnixTimestamp(int timestamp) public static DateTime ConvertFromUnixTimestamp(int timestamp)
{ {
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp); date = date.AddSeconds(timestamp);
return date;
} }
#endregion #endregion

View File

@@ -9,8 +9,8 @@
<link rel="icon" type="image/png" href="favicon.png"/> <link rel="icon" type="image/png" href="favicon.png"/>
<!-- CSS --> <!-- CSS -->
<link rel="stylesheet" href="app.css?version=0.7"/> <link rel="stylesheet" href="app.css?version=0.13"/>
<link rel="stylesheet" href="WatchIt.Website.styles.css?version=0.7"/> <link rel="stylesheet" href="WatchIt.Website.styles.css?version=0.12"/>
<!-- BOOTSTRAP --> <!-- BOOTSTRAP -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
@@ -25,8 +25,8 @@
</head> </head>
<body> <body>
<Routes @rendermode="InteractiveServer"/> <Routes @rendermode="InteractiveServer"/>
<script src="_framework/blazor.web.js"></script> <script src="_framework/blazor.web.js"></script>
</body> </body>
</html> </html>

View File

@@ -1,3 +0,0 @@
#posterInput {
width: 300px;
}

View File

@@ -4,21 +4,35 @@
<br/> <br/>
<InputFile id="posterInput" class="form-control my-1" OnChange="LoadPoster" disabled=@(!Id.HasValue) autocomplete="off"/> <InputFile id="posterInput" class="form-control my-1" OnChange="LoadPoster" disabled=@(!Id.HasValue) autocomplete="off"/>
@if (_posterChanged || !string.IsNullOrWhiteSpace(_actualPosterBase64))
{
<div id="posterButtons" class="container-fluid mt-2 p-0">
<div class="row gx-1">
@if (_posterChanged) @if (_posterChanged)
{ {
<div id="posterButtons" class="container-fluid mt-2"> <div class="col">
<div class="row"> <button type="button" class="btn btn-secondary btn-block btn-stretch-x" @onclick="SavePoster" disabled=@(!Id.HasValue || _posterLoading) autocomplete="off">Save poster</button>
<button type="button" class="col btn btn-secondary me-1" @onclick="SavePoster" disabled=@(!Id.HasValue) autocomplete="off">Save poster</button> </div>
<button type="button" class="col btn btn-danger ms-1" @onclick="CancelPoster" disabled=@(!Id.HasValue) autocomplete="off">Drop changes</button> <div class="col">
<button type="button" class="btn btn-danger btn-block btn-stretch-x" @onclick="CancelPoster" disabled=@(!Id.HasValue || _posterLoading) autocomplete="off">Drop changes</button>
</div>
}
else if (!string.IsNullOrWhiteSpace(_actualPosterBase64))
{
<div class="col">
<button type="button" class="btn btn-danger btn-block btn-stretch-x" @onclick="DeletePoster" disabled=@(!Id.HasValue || _posterLoading) autocomplete="off">Delete poster</button>
</div>
}
@if (_posterLoading)
{
<div class="col-auto">
<div class="d-flex align-items-center justify-content-center">
<div class="spinner-border" role="status"></div>
</div> </div>
</div> </div>
} }
else </div>
{ </div>
if (!string.IsNullOrWhiteSpace(_actualPosterBase64))
{
<button id="posterButtons" type="button" class="btn btn-danger form-control mt-1" @onclick="DeletePoster" disabled=@(!Id.HasValue) autocomplete="off">Delete poster</button>
}
} }
</div> </div>
<div class="col rounded-3 panel panel-regular m-1 p-3"> <div class="col rounded-3 panel panel-regular m-1 p-3">
@@ -73,6 +87,8 @@
</div> </div>
</div> </div>
<style> <style>
#posterInput, #posterButtons { #posterInput, #posterButtons {
width: 300px; width: 300px;

View File

@@ -7,7 +7,7 @@ using WatchIt.Website.Services.WebAPI.Media;
namespace WatchIt.Website.Components; namespace WatchIt.Website.Components;
public partial class MediaForm : ComponentBase public partial class MediaFormComponent : ComponentBase
{ {
#region SERVICES #region SERVICES
@@ -37,6 +37,7 @@ public partial class MediaForm : ComponentBase
private bool _posterChanged = false; private bool _posterChanged = false;
private string? _posterBase64 = null; private string? _posterBase64 = null;
private string? _posterMediaType = null; private string? _posterMediaType = null;
private bool _posterLoading = false;
#endregion #endregion
@@ -84,6 +85,7 @@ public partial class MediaForm : ComponentBase
_actualPosterBase64 = _posterBase64; _actualPosterBase64 = _posterBase64;
_actualPosterMediaType = _posterMediaType; _actualPosterMediaType = _posterMediaType;
_posterChanged = false; _posterChanged = false;
_posterLoading = false;
} }
MediaPosterRequest data = new MediaPosterRequest MediaPosterRequest data = new MediaPosterRequest
@@ -92,6 +94,7 @@ public partial class MediaForm : ComponentBase
MimeType = _posterMediaType MimeType = _posterMediaType
}; };
_posterLoading = true;
await MediaWebAPIService.PutPoster(Id.Value, data, SuccessAction); await MediaWebAPIService.PutPoster(Id.Value, data, SuccessAction);
} }
@@ -104,8 +107,10 @@ public partial class MediaForm : ComponentBase
_posterChanged = false; _posterChanged = false;
_posterBase64 = null; _posterBase64 = null;
_posterMediaType = null; _posterMediaType = null;
_posterLoading = false;
} }
_posterLoading = true;
await MediaWebAPIService.DeletePoster(Id.Value, SuccessAction); await MediaWebAPIService.DeletePoster(Id.Value, SuccessAction);
} }

View File

@@ -0,0 +1,5 @@
/* IDS */
#posterInput, #posterButtons {
width: 300px;
}

View File

@@ -31,26 +31,3 @@
} }
</div> </div>
} }
@code {
[Inject] public IAuthenticationService AuthenticationService { get; set; } = default!;
private bool _loaded = false;
private bool _authenticated = false;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
User? user = await AuthenticationService.GetUserAsync();
if (user is not null && user.IsAdmin)
{
_authenticated = true;
}
_loaded = true;
StateHasChanged();
}
}
}

View File

@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Components;
using WatchIt.Website.Services.Utility.Authentication;
namespace WatchIt.Website.Pages;
public partial class AdminPage
{
#region SERVICE
[Inject] public IAuthenticationService AuthenticationService { get; set; } = default!;
#endregion
#region FIELDS
private bool _loaded = false;
private bool _authenticated = false;
#endregion
#region PRIVATE METHODS
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
User? user = await AuthenticationService.GetUserAsync();
if (user is not null && user.IsAdmin)
{
_authenticated = true;
}
_loaded = true;
StateHasChanged();
}
}
#endregion
}

View File

@@ -1,8 +0,0 @@
body {
background-size: cover;
}
.logo {
font-size: 60px;
margin: 10px;
}

View File

@@ -1,11 +1,4 @@
@page "/auth" @page "/auth"
@using System.Text
@using WatchIt.Common.Model.Accounts
@using WatchIt.Common.Model.Media
@using WatchIt.Website.Services.Utility.Authentication
@using WatchIt.Website.Services.Utility.Tokens
@using WatchIt.Website.Services.WebAPI.Accounts
@using WatchIt.Website.Services.WebAPI.Media
@layout EmptyLayout @layout EmptyLayout
<PageTitle>WatchIt - @(_authType == AuthType.SignIn ? "Sign in" : "Sign up")</PageTitle> <PageTitle>WatchIt - @(_authType == AuthType.SignIn ? "Sign in" : "Sign up")</PageTitle>
@@ -108,15 +101,10 @@
<style> <style>
html {
height: 100%;
}
body { body {
background-image: url('@_background');
height: 100%; height: 100%;
background-image: url('@_background');
background-position: center; background-position: center;
background-repeat: no-repeat; background-repeat: no-repeat;
background-size: cover; background-size: cover;
@@ -127,134 +115,3 @@
} }
</style> </style>
} }
@code
{
#region SERVICES
[Inject] public ILogger<Auth> Logger { get; set; } = default!;
[Inject] public IAuthenticationService AuthenticationService { get; set; } = default!;
[Inject] public ITokensService TokensService { get; set; } = default!;
[Inject] public IMediaWebAPIService MediaWebAPIService { get; set; } = default!;
[Inject] public IAccountsWebAPIService AccountsWebAPIService { get; set; } = default!;
[Inject] public NavigationManager NavigationManager { get; set; } = default!;
#endregion
#region ENUMS
private enum AuthType
{
SignIn,
SignUp
}
#endregion
#region FIELDS
private bool _loaded = false;
private AuthType _authType = AuthType.SignIn;
private string _background = "assets/background_temp.jpg";
private string _firstGradientColor = "#c6721c";
private string _secondGradientColor = "#85200c";
private AuthenticateRequest _loginModel = new AuthenticateRequest
{
UsernameOrEmail = null,
Password = null
};
private RegisterRequest _registerModel = new RegisterRequest
{
Username = null,
Email = null,
Password = null
};
private string _passwordConfirmation;
private IEnumerable<string> _errors;
#endregion
#region METHODS
protected override async Task OnInitializedAsync()
{
if (await AuthenticationService.GetAuthenticationStatusAsync())
{
NavigationManager.NavigateTo("/");
}
Action<MediaPhotoResponse> backgroundSuccess = (data) =>
{
string imageBase64 = Convert.ToBase64String(data.Image);
string firstColor = BitConverter.ToString(data.Background.FirstGradientColor)
.Replace("-", string.Empty);
string secondColor = BitConverter.ToString(data.Background.SecondGradientColor)
.Replace("-", string.Empty);
_background = $"data:{data.MimeType};base64,{imageBase64}";
_firstGradientColor = $"#{firstColor}";
_secondGradientColor = $"#{secondColor}";
};
await MediaWebAPIService.GetPhotoRandomBackground(backgroundSuccess);
_loaded = true;
}
private async Task Login()
{
await AccountsWebAPIService.Authenticate(_loginModel, LoginSuccess, LoginBadRequest, LoginUnauthorized);
async void LoginSuccess(AuthenticateResponse data)
{
await TokensService.SaveAuthenticationData(data);
NavigationManager.NavigateTo("/");
}
void LoginBadRequest(IDictionary<string, string[]> data)
{
_errors = data.SelectMany(x => x.Value).Select(x => $"• {x}");
}
void LoginUnauthorized()
{
_errors = [ "Incorrect account data" ];
}
}
private async Task Register()
{
if (_registerModel.Password != _passwordConfirmation)
{
_errors = [ "Password fields don't match" ];
return;
}
await AccountsWebAPIService.Register(_registerModel, RegisterSuccess, RegisterBadRequest);
void RegisterSuccess(RegisterResponse data)
{
_authType = AuthType.SignIn;
}
void RegisterBadRequest(IDictionary<string, string[]> data)
{
_errors = data.SelectMany(x => x.Value).Select(x => $"• {x}");
}
}
#endregion
}

View File

@@ -0,0 +1,136 @@
using Microsoft.AspNetCore.Components;
using WatchIt.Common.Model.Accounts;
using WatchIt.Common.Model.Media;
using WatchIt.Website.Services.Utility.Authentication;
using WatchIt.Website.Services.Utility.Tokens;
using WatchIt.Website.Services.WebAPI.Accounts;
using WatchIt.Website.Services.WebAPI.Media;
namespace WatchIt.Website.Pages;
public partial class AuthPage
{
#region SERVICES
[Inject] public ILogger<AuthPage> Logger { get; set; } = default!;
[Inject] public IAuthenticationService AuthenticationService { get; set; } = default!;
[Inject] public ITokensService TokensService { get; set; } = default!;
[Inject] public IMediaWebAPIService MediaWebAPIService { get; set; } = default!;
[Inject] public IAccountsWebAPIService AccountsWebAPIService { get; set; } = default!;
[Inject] public NavigationManager NavigationManager { get; set; } = default!;
#endregion
#region ENUMS
private enum AuthType
{
SignIn,
SignUp
}
#endregion
#region FIELDS
private bool _loaded = false;
private AuthType _authType = AuthType.SignIn;
private string _background = "assets/background_temp.jpg";
private string _firstGradientColor = "#c6721c";
private string _secondGradientColor = "#85200c";
private AuthenticateRequest _loginModel = new AuthenticateRequest
{
UsernameOrEmail = null,
Password = null
};
private RegisterRequest _registerModel = new RegisterRequest
{
Username = null,
Email = null,
Password = null
};
private string _passwordConfirmation;
private IEnumerable<string> _errors;
#endregion
#region METHODS
protected override async Task OnInitializedAsync()
{
if (await AuthenticationService.GetAuthenticationStatusAsync())
{
NavigationManager.NavigateTo("/");
}
Action<MediaPhotoResponse> backgroundSuccess = (data) =>
{
string imageBase64 = Convert.ToBase64String(data.Image);
string firstColor = BitConverter.ToString(data.Background.FirstGradientColor)
.Replace("-", string.Empty);
string secondColor = BitConverter.ToString(data.Background.SecondGradientColor)
.Replace("-", string.Empty);
_background = $"data:{data.MimeType};base64,{imageBase64}";
_firstGradientColor = $"#{firstColor}";
_secondGradientColor = $"#{secondColor}";
};
await MediaWebAPIService.GetPhotoRandomBackground(backgroundSuccess);
_loaded = true;
}
private async Task Login()
{
await AccountsWebAPIService.Authenticate(_loginModel, LoginSuccess, LoginBadRequest, LoginUnauthorized);
async void LoginSuccess(AuthenticateResponse data)
{
await TokensService.SaveAuthenticationData(data);
NavigationManager.NavigateTo("/");
}
void LoginBadRequest(IDictionary<string, string[]> data)
{
_errors = data.SelectMany(x => x.Value).Select(x => $"• {x}");
}
void LoginUnauthorized()
{
_errors = [ "Incorrect account data" ];
}
}
private async Task Register()
{
if (_registerModel.Password != _passwordConfirmation)
{
_errors = [ "Password fields don't match" ];
return;
}
await AccountsWebAPIService.Register(_registerModel, RegisterSuccess, RegisterBadRequest);
void RegisterSuccess(RegisterResponse data)
{
_authType = AuthType.SignIn;
}
void RegisterBadRequest(IDictionary<string, string[]> data)
{
_errors = data.SelectMany(x => x.Value).Select(x => $"• {x}");
}
}
#endregion
}

View File

@@ -0,0 +1,14 @@
/* TAGS */
html {
height: 100%;
}
/* CLASSES */
.logo {
font-size: 60px;
margin: 10px;
}

View File

@@ -0,0 +1,6 @@
namespace WatchIt.Website.Pages;
public partial class HomePage
{
}

View File

@@ -1,6 +1 @@
@page "/movies/{id:long}" @page "/movies/{id:long}"
@code {
}

View File

@@ -13,7 +13,7 @@
<h2>@(Id is null ? "Create new movie" : $"Edit movie \"{_movieInfo.Title}\"")</h2> <h2>@(Id is null ? "Create new movie" : $"Edit movie \"{_movieInfo.Title}\"")</h2>
</div> </div>
</div> </div>
<MediaForm Data=@(_movieData) SaveDataAction=SaveData Id=@(Id) SaveDataErrors=@(_movieDataErrors) SaveDataInfo=@(_movieDataInfo)/> <MediaFormComponent Data=@(_movieData) SaveDataAction=SaveData Id=@(Id) SaveDataErrors=@(_movieDataErrors) SaveDataInfo=@(_movieDataInfo)/>
} }
else else
{ {

View File

@@ -81,3 +81,7 @@ body, html {
right: auto; right: auto;
left: 0; left: 0;
} }
.btn-stretch-x {
width: 100%;
}