Starting updating mooncore dependency usage
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
using System.Text;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace Moonlight.Client.Services;
|
||||
|
||||
public class DownloadService
|
||||
{
|
||||
private readonly IJSRuntime JsRuntime;
|
||||
|
||||
public DownloadService(IJSRuntime jsRuntime)
|
||||
{
|
||||
JsRuntime = jsRuntime;
|
||||
}
|
||||
|
||||
public async Task DownloadStream(string fileName, Stream stream)
|
||||
{
|
||||
using var streamRef = new DotNetStreamReference(stream);
|
||||
|
||||
await JsRuntime.InvokeVoidAsync("moonlight.utils.download", fileName, streamRef);
|
||||
}
|
||||
|
||||
public async Task DownloadBytes(string fileName, byte[] bytes)
|
||||
{
|
||||
var ms = new MemoryStream(bytes);
|
||||
|
||||
await DownloadStream(fileName, ms);
|
||||
|
||||
ms.Close();
|
||||
await ms.DisposeAsync();
|
||||
}
|
||||
|
||||
public async Task DownloadString(string fileName, string content) =>
|
||||
await DownloadBytes(fileName, Encoding.UTF8.GetBytes(content));
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
using MoonCore.Attributes;
|
||||
using MoonCore.Blazor.Services;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.Shared.Http.Responses.Auth;
|
||||
|
||||
namespace Moonlight.Client.Services;
|
||||
|
||||
[Scoped]
|
||||
public class IdentityService
|
||||
{
|
||||
public string Username { get; private set; } = "";
|
||||
public string Email { get; private set; } = "";
|
||||
public string[] Permissions { get; private set; } = [];
|
||||
public bool IsLoggedIn { get; private set; } = false;
|
||||
|
||||
private readonly HttpApiClient HttpApiClient;
|
||||
private readonly LocalStorageService LocalStorageService;
|
||||
|
||||
public IdentityService(HttpApiClient httpApiClient, LocalStorageService localStorageService)
|
||||
{
|
||||
HttpApiClient = httpApiClient;
|
||||
LocalStorageService = localStorageService;
|
||||
}
|
||||
|
||||
public async Task Check()
|
||||
{
|
||||
IsLoggedIn = false;
|
||||
|
||||
var response = await HttpApiClient.GetJson<CheckResponse>("api/auth/check");
|
||||
|
||||
Username = response.Username;
|
||||
Email = response.Email;
|
||||
Permissions = response.Permissions;
|
||||
|
||||
IsLoggedIn = true;
|
||||
|
||||
//await OnStateChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task Logout()
|
||||
{
|
||||
await LocalStorageService.SetString("AccessToken", "unset");
|
||||
await LocalStorageService.SetString("RefreshToken", "unset");
|
||||
await LocalStorageService.Set("ExpiresAt", DateTime.MinValue);
|
||||
}
|
||||
|
||||
public bool HasPermission(string requiredPermission)
|
||||
{
|
||||
// Check for wildcard permission
|
||||
if (Permissions.Contains("*"))
|
||||
return true;
|
||||
|
||||
var requiredSegments = requiredPermission.Split('.');
|
||||
|
||||
// Check if the user has the exact permission or a wildcard match
|
||||
foreach (var permission in Permissions)
|
||||
{
|
||||
var permissionSegments = permission.Split('.');
|
||||
|
||||
// Iterate over the segments of the required permission
|
||||
for (var i = 0; i < requiredSegments.Length; i++)
|
||||
{
|
||||
// If the current segment matches or is a wildcard, continue to the next segment
|
||||
if (i < permissionSegments.Length && requiredSegments[i] == permissionSegments[i] ||
|
||||
permissionSegments[i] == "*")
|
||||
{
|
||||
// If we've reached the end of the permissionSegments array, it means we've found a match
|
||||
if (i == permissionSegments.Length - 1)
|
||||
return true; // Found an exact match or a wildcard match
|
||||
}
|
||||
else
|
||||
{
|
||||
// If we reach here, it means the segments don't match and we break out of the loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No matching permission found
|
||||
return false;
|
||||
}
|
||||
}
|
||||
124
Moonlight.Client/Services/RemoteAuthStateManager.cs
Normal file
124
Moonlight.Client/Services/RemoteAuthStateManager.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using System.Security.Claims;
|
||||
using System.Web;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using MoonCore.Blazor.Services;
|
||||
using MoonCore.Blazor.Tailwind.Auth;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.Shared.Http.Requests.Auth;
|
||||
using Moonlight.Shared.Http.Responses.Auth;
|
||||
|
||||
namespace Moonlight.Client.Services;
|
||||
|
||||
public class RemoteAuthStateManager : AuthenticationStateManager
|
||||
{
|
||||
private readonly NavigationManager NavigationManager;
|
||||
private readonly HttpApiClient HttpApiClient;
|
||||
private readonly LocalStorageService LocalStorageService;
|
||||
private readonly ILogger<RemoteAuthStateManager> Logger;
|
||||
|
||||
public RemoteAuthStateManager(
|
||||
HttpApiClient httpApiClient,
|
||||
LocalStorageService localStorageService,
|
||||
NavigationManager navigationManager,
|
||||
ILogger<RemoteAuthStateManager> logger
|
||||
)
|
||||
{
|
||||
HttpApiClient = httpApiClient;
|
||||
LocalStorageService = localStorageService;
|
||||
NavigationManager = navigationManager;
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
=> await LoadAuthState();
|
||||
|
||||
public override async Task HandleLogin()
|
||||
{
|
||||
var uri = new Uri(NavigationManager.Uri);
|
||||
var codeParam = HttpUtility.ParseQueryString(uri.Query).Get("code");
|
||||
|
||||
if (string.IsNullOrEmpty(codeParam)) // If this is true, we need to log in the user
|
||||
{
|
||||
await StartLogin();
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
var loginCompleteData = await HttpApiClient.PostJson<LoginCompleteResponse>(
|
||||
"api/auth/complete",
|
||||
new LoginCompleteRequest()
|
||||
{
|
||||
Code = codeParam
|
||||
}
|
||||
);
|
||||
|
||||
await LocalStorageService.SetString("AccessToken", loginCompleteData.AccessToken);
|
||||
|
||||
NavigationManager.NavigateTo("/");
|
||||
NotifyAuthenticationStateChanged(LoadAuthState());
|
||||
}
|
||||
catch (HttpApiException e)
|
||||
{
|
||||
Logger.LogError("Unable to complete login: {e}", e);
|
||||
|
||||
await StartLogin();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task Logout()
|
||||
{
|
||||
if (await LocalStorageService.ContainsKey("AccessToken"))
|
||||
await LocalStorageService.SetString("AccessToken", "");
|
||||
|
||||
NotifyAuthenticationStateChanged(LoadAuthState());
|
||||
}
|
||||
|
||||
#region Utilities
|
||||
|
||||
private async Task StartLogin()
|
||||
{
|
||||
var loginStartData = await HttpApiClient.GetJson<LoginStartResponse>("api/auth/start");
|
||||
|
||||
var url = $"{loginStartData.Endpoint}" +
|
||||
$"?client_id={loginStartData.ClientId}" +
|
||||
$"&redirect_uri={loginStartData.RedirectUri}" +
|
||||
$"&response_type=code";
|
||||
|
||||
NavigationManager.NavigateTo(url, true);
|
||||
}
|
||||
|
||||
private async Task<AuthenticationState> LoadAuthState()
|
||||
{
|
||||
AuthenticationState newState;
|
||||
|
||||
try
|
||||
{
|
||||
var checkData = await HttpApiClient.GetJson<CheckResponse>("api/auth/check");
|
||||
|
||||
newState = new(new ClaimsPrincipal(
|
||||
new ClaimsIdentity(
|
||||
[
|
||||
new Claim("username", checkData.Username),
|
||||
new Claim("email", checkData.Email),
|
||||
new Claim("permissions", checkData.Permissions)
|
||||
],
|
||||
"RemoteAuthStateManager"
|
||||
)
|
||||
));
|
||||
}
|
||||
catch (HttpApiException)
|
||||
{
|
||||
newState = new(new ClaimsPrincipal(
|
||||
new ClaimsIdentity()
|
||||
));
|
||||
}
|
||||
|
||||
return newState;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user