-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ basic login screen for username/pass + authentication cookie to keep the session alive until logout or expiration + some refactors
- Loading branch information
1 parent
e898915
commit a5f38f3
Showing
42 changed files
with
646 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Auth.Exceptions; | ||
|
||
public class LoginException : Exception { | ||
public LoginException(string message) : base(message) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
namespace Auth.Models; | ||
|
||
public class Credentials : ICredentials { | ||
public Credentials(string login, string password) { | ||
Login = login; | ||
Password = password; | ||
} | ||
public string Login { get; } | ||
public string Password { get; } | ||
public string Login { get; set; } | ||
public string Password { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace Auth.Models; | ||
|
||
public interface ICredentials { | ||
public string Login { get; } | ||
public string Password { get;} | ||
public string Login { get; set; } | ||
public string Password { get; set; } | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
<Router AppAssembly="@typeof(App).Assembly"> | ||
<Found Context="routeData"> | ||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/> | ||
<FocusOnNavigate RouteData="@routeData" Selector="h1"/> | ||
</Found> | ||
<NotFound> | ||
<PageTitle>Not found</PageTitle> | ||
<LayoutView Layout="@typeof(MainLayout)"> | ||
<p role="alert">Oops, it looks like there's nothing here.</p> | ||
</LayoutView> | ||
</NotFound> | ||
</Router> | ||
@using Linguard.Web.Shared.Layouts | ||
<CascadingAuthenticationState> | ||
<Router AppAssembly="@typeof(App).Assembly"> | ||
<Found Context="routeData"> | ||
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/> | ||
<FocusOnNavigate RouteData="@routeData" Selector="h1"/> | ||
</Found> | ||
<NotFound> | ||
<PageTitle>Not found</PageTitle> | ||
<LayoutView Layout="@typeof(MainLayout)"> | ||
<p role="alert">Oops, it looks like there's nothing here.</p> | ||
</LayoutView> | ||
</NotFound> | ||
</Router> | ||
</CascadingAuthenticationState> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Linguard.Web.Auth; | ||
|
||
public class ApplicationDbContext : IdentityDbContext { | ||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) | ||
: base(options) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
|
||
namespace Linguard.Web.Auth; | ||
|
||
public class AuthenticationCookieFormatBase : IAuthenticationCookieFormat { | ||
public AuthenticationCookieFormatBase(string scheme, string name) { | ||
Scheme = scheme; | ||
Name = name; | ||
} | ||
public string Scheme { get; } | ||
public string Name { get; } | ||
} | ||
|
||
public static class AuthenticationCookieFormat { | ||
public static readonly IAuthenticationCookieFormat Default = | ||
new AuthenticationCookieFormatBase(CookieAuthenticationDefaults.AuthenticationScheme, "Auth"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Linguard.Web.Auth; | ||
|
||
public interface IAuthenticationCookieFormat { | ||
public string Scheme { get; } | ||
public string Name { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
using Microsoft.AspNetCore.Components.Server; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Linguard.Web.Auth; | ||
|
||
public class IdentityAuthenticationStateProvider<TUser> | ||
: RevalidatingServerAuthenticationStateProvider where TUser : class { | ||
private readonly IServiceScopeFactory _scopeFactory; | ||
private readonly IdentityOptions _options; | ||
|
||
public IdentityAuthenticationStateProvider( | ||
ILoggerFactory loggerFactory, | ||
IServiceScopeFactory scopeFactory, | ||
IOptions<IdentityOptions> optionsAccessor) | ||
: base(loggerFactory) { | ||
_scopeFactory = scopeFactory; | ||
_options = optionsAccessor.Value; | ||
} | ||
|
||
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30); | ||
|
||
protected override async Task<bool> ValidateAuthenticationStateAsync( | ||
AuthenticationState authenticationState, CancellationToken cancellationToken) { | ||
// Get the user manager from a new scope to ensure it fetches fresh data | ||
var scope = _scopeFactory.CreateScope(); | ||
try { | ||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<TUser>>(); | ||
return await ValidateSecurityStampAsync(userManager, authenticationState.User); | ||
} | ||
finally { | ||
if (scope is IAsyncDisposable asyncDisposable) { | ||
await asyncDisposable.DisposeAsync(); | ||
} | ||
else { | ||
scope.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
private async Task<bool> ValidateSecurityStampAsync(UserManager<TUser> userManager, ClaimsPrincipal principal) { | ||
var user = await userManager.GetUserAsync(principal); | ||
if (user == null) { | ||
return false; | ||
} | ||
if (!userManager.SupportsUserSecurityStamp) { | ||
return true; | ||
} | ||
var principalStamp = principal.FindFirstValue(_options.ClaimsIdentity.SecurityStampClaimType); | ||
var userStamp = await userManager.GetSecurityStampAsync(user); | ||
return principalStamp == userStamp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Linguard.Core.Models.Wireguard; | ||
|
||
namespace Linguard.Web.Helpers; | ||
|
||
public interface IWebHelper { | ||
Task Download(string data, string filename); | ||
Task DownloadConfiguration(); | ||
Task DownloadWireguardModel(IWireguardPeer peer); | ||
void RemoveWireguardModel(IWireguardPeer peer); | ||
byte[] GetQrCode(IWireguardPeer peer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.Text; | ||
using Linguard.Core.Configuration; | ||
using Linguard.Core.Managers; | ||
using Linguard.Core.Models.Wireguard; | ||
using Linguard.Core.Services; | ||
using Linguard.Core.Utils; | ||
using Linguard.Core.Utils.Wireguard; | ||
using Microsoft.JSInterop; | ||
using QRCoder; | ||
|
||
namespace Linguard.Web.Helpers; | ||
|
||
public class WebHelper : IWebHelper { | ||
|
||
public WebHelper(IJSRuntime jsRuntime, IWireguardService wireguardService, | ||
IConfigurationManager configurationManager, QRCodeGenerator qrCodeGenerator) { | ||
JsRuntime = jsRuntime; | ||
WireguardService = wireguardService; | ||
ConfigurationManager = configurationManager; | ||
QrCodeGenerator = qrCodeGenerator; | ||
} | ||
|
||
private IJSRuntime JsRuntime { get; } | ||
private IWireguardService WireguardService { get; } | ||
private IConfigurationManager ConfigurationManager { get; } | ||
private IWireguardConfiguration Configuration => ConfigurationManager.Configuration.Wireguard; | ||
private QRCodeGenerator QrCodeGenerator {get; } | ||
|
||
public async Task Download(string data, string filename) { | ||
var bytes = Encoding.UTF8.GetBytes(data); | ||
var fileStream = new MemoryStream(bytes); | ||
using var streamRef = new DotNetStreamReference(fileStream); | ||
await JsRuntime.InvokeVoidAsync("downloadFileFromStream", filename, streamRef); | ||
} | ||
|
||
public Task DownloadConfiguration() { | ||
return Download(ConfigurationManager.Export(), $"{AssemblyInfo.Product.ToLower()}.config"); | ||
} | ||
|
||
public Task DownloadWireguardModel(IWireguardPeer peer) { | ||
return Download(WireguardUtils.GenerateWireguardConfiguration(peer), $"{peer.Name}.conf"); | ||
} | ||
|
||
public void RemoveWireguardModel(IWireguardPeer peer) { | ||
switch (peer) { | ||
case Client client: | ||
RemoveClient(client); | ||
break; | ||
case Interface iface: | ||
RemoveInterface(iface); | ||
break; | ||
} | ||
ConfigurationManager.Save(); | ||
} | ||
|
||
public byte[] GetQrCode(IWireguardPeer peer) { | ||
var qrCodeData = QrCodeGenerator.CreateQrCode( | ||
WireguardUtils.GenerateWireguardConfiguration(peer), QRCodeGenerator.ECCLevel.Q); | ||
return new PngByteQRCode(qrCodeData).GetGraphic(20); | ||
} | ||
|
||
private void RemoveClient(Client client) { | ||
WireguardService.RemoveClient(client); | ||
Configuration.GetInterface(client)?.Clients.Remove(client); | ||
} | ||
|
||
private void RemoveInterface(Interface iface) { | ||
Configuration.Interfaces.Remove(iface); | ||
WireguardService.RemoveInterface(iface); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.