-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
77 additions
and
35 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 |
---|---|---|
@@ -1,47 +1,73 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Logging; | ||
using Remotely.Server.Services; | ||
using Remotely.Shared.Entities; | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Remotely.Server.Components; | ||
|
||
[Authorize] | ||
public class AuthComponentBase : ComponentBase | ||
{ | ||
private readonly ManualResetEventSlim _initSignal = new(); | ||
private RemotelyUser? _user; | ||
private string? _userName; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
IsAuthenticated = await AuthService.IsAuthenticated(); | ||
var userResult = await AuthService.GetUser(); | ||
if (userResult.IsSuccess) | ||
{ | ||
_user = userResult.Value; | ||
_userName = userResult.Value.UserName ?? string.Empty; | ||
} | ||
await base.OnInitializedAsync(); | ||
} | ||
|
||
public bool IsAuthenticated { get; private set; } | ||
|
||
public bool IsUserSet => _user is not null; | ||
|
||
public RemotelyUser User | ||
{ | ||
get => _user ?? throw new InvalidOperationException("User has not been resolved yet."); | ||
get | ||
{ | ||
if (_initSignal.Wait(TimeSpan.FromSeconds(5)) && _user is not null) | ||
{ | ||
return _user; | ||
} | ||
// This should never happen, since AuthBasedComponent is only | ||
// used on components that require authentication. This was easier | ||
// than making this explicitly nullable and refactoring everywhere. | ||
Logger.LogError("Failed to resolve user."); | ||
throw new InvalidOperationException("Failed to resolve user."); | ||
} | ||
private set => _user = value; | ||
} | ||
|
||
public string UserName | ||
{ | ||
get => _userName ?? throw new InvalidOperationException("User has not been resolved yet."); | ||
get | ||
{ | ||
if (_initSignal.Wait(TimeSpan.FromSeconds(5)) && _userName is not null) | ||
{ | ||
return _userName; | ||
} | ||
Logger.LogError("Failed to resolve user."); | ||
throw new InvalidOperationException("Failed to resolve user."); | ||
} | ||
private set => _userName = value; | ||
} | ||
|
||
[Inject] | ||
protected IAuthService AuthService { get; set; } = null!; | ||
|
||
[Inject] | ||
private ILogger<AuthComponentBase> Logger { get; init; } = null!; | ||
|
||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
IsAuthenticated = await AuthService.IsAuthenticated(); | ||
var userResult = await AuthService.GetUser(); | ||
if (userResult.IsSuccess) | ||
{ | ||
_user = userResult.Value; | ||
_userName = userResult.Value.UserName ?? string.Empty; | ||
} | ||
_initSignal.Set(); | ||
await base.OnInitializedAsync(); | ||
} | ||
} |
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
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