Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@using CodeBeam.UltimateAuth.Core.Domain
@using CodeBeam.UltimateAuth.Core.Runtime
@using CodeBeam.UltimateAuth.Server.Abstractions
@using CodeBeam.UltimateAuth.Server.Cookies
@using CodeBeam.UltimateAuth.Server.Infrastructure
@using CodeBeam.UltimateAuth.Server.Services
@using CodeBeam.UltimateAuth.Server.Stores
Expand Down Expand Up @@ -53,7 +52,6 @@

<MudStack Spacing="0">
<MudText><b>@ProductInfo.Get().ProductName</b> v @ProductInfo.Get().Version</MudText>
<MudText>Client Profile: @ProductInfo.Get().ClientProfile.ToString()</MudText>
</MudStack>

<MudStack Spacing="0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>0.0.1-preview</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
<PackageReference Include="MudBlazor" Version="9.0.0-preview.1" />
<PackageReference Include="CodeBeam.MudBlazor.Extensions" Version="9.0.0-preview.2" />
<PackageReference Include="Scalar.AspNetCore" Version="2.12.18" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
@using CodeBeam.UltimateAuth.Client.Authentication
@using CodeBeam.UltimateAuth.Client.Device
@using CodeBeam.UltimateAuth.Client.Diagnostics
@using CodeBeam.UltimateAuth.Client.Runtime
@using CodeBeam.UltimateAuth.Core.Abstractions
@using CodeBeam.UltimateAuth.Core.Runtime
@using CodeBeam.UltimateAuth.Server.Abstractions
@using CodeBeam.UltimateAuth.Server.Cookies
@using CodeBeam.UltimateAuth.Server.Infrastructure
@using CodeBeam.UltimateAuth.Server.Services
@inject IUAuthStateManager StateManager
Expand All @@ -20,7 +20,7 @@
@inject IHttpContextAccessor HttpContextAccessor
@inject IUAuthClient UAuth
@inject NavigationManager Nav
@inject IUAuthProductInfoProvider ProductInfo
@inject IUAuthClientProductInfoProvider ClientProductInfo
@inject AuthenticationStateProvider AuthStateProvider
@inject UAuthClientDiagnostics Diagnostics
@inject IDeviceIdProvider DeviceIdProvider
Expand Down Expand Up @@ -50,8 +50,8 @@
</MudStack>

<MudStack Spacing="0">
<MudText><b>@ProductInfo.Get().ProductName</b> v @ProductInfo.Get().Version</MudText>
<MudText>Client Profile: @ProductInfo.Get().ClientProfile.ToString()</MudText>
<MudText><b>@ClientProductInfo.Get().ProductName</b> v @ClientProductInfo.Get().Version</MudText>
<MudText>Client Profile: @ClientProductInfo.Get().ClientProfile.ToString()</MudText>
</MudStack>

<MudStack Spacing="0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,146 +1,144 @@
using CodeBeam.UltimateAuth.Client;
using CodeBeam.UltimateAuth.Client.Device;
using CodeBeam.UltimateAuth.Core.Contracts;
using CodeBeam.UltimateAuth.Core.Domain;
using CodeBeam.UltimateAuth.Users.Contracts;
using Microsoft.AspNetCore.Components.Authorization;
using MudBlazor;

namespace CodeBeam.UltimateAuth.Sample.BlazorServer.Components.Pages
namespace CodeBeam.UltimateAuth.Sample.BlazorServer.Components.Pages;

public partial class Home
{
public partial class Home
{
private string? _username;
private string? _password;
private string? _username;
private string? _password;

private UALoginForm _form = null!;
private UALoginForm _form = null!;

private AuthenticationState _authState = null!;
private AuthenticationState _authState = null!;

protected override async Task OnInitializedAsync()
{
Diagnostics.Changed += OnDiagnosticsChanged;
}
protected override async Task OnInitializedAsync()
{
Diagnostics.Changed += OnDiagnosticsChanged;
}

protected override async Task OnAfterRenderAsync(bool firstRender)
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
if (firstRender)
{
await StateManager.EnsureAsync();
_authState = await AuthStateProvider.GetAuthenticationStateAsync();
StateHasChanged();
}
await StateManager.EnsureAsync();
_authState = await AuthStateProvider.GetAuthenticationStateAsync();
StateHasChanged();
}
}

private void OnDiagnosticsChanged()
{
InvokeAsync(StateHasChanged);
}
private void OnDiagnosticsChanged()
{
InvokeAsync(StateHasChanged);
}

private async Task ProgrammaticLogin()
private async Task ProgrammaticLogin()
{
var deviceId = await DeviceIdProvider.GetOrCreateAsync();
var request = new LoginRequest
{
var deviceId = await DeviceIdProvider.GetOrCreateAsync();
var request = new LoginRequest
{
Identifier = "admin",
Secret = "admin",
Device = DeviceContext.FromDeviceId(deviceId),
};
await UAuth.Flows.LoginAsync(request);
_authState = await AuthStateProvider.GetAuthenticationStateAsync();
}
Identifier = "admin",
Secret = "admin",
Device = DeviceContext.FromDeviceId(deviceId),
};
await UAuth.Flows.LoginAsync(request);
_authState = await AuthStateProvider.GetAuthenticationStateAsync();
}

private async Task ValidateAsync()
{
var result = await UAuth.Flows.ValidateAsync();
private async Task ValidateAsync()
{
var result = await UAuth.Flows.ValidateAsync();

Snackbar.Add(
result.IsValid ? "Session is valid ✅" : $"Session invalid ❌ ({result.State})",
result.IsValid ? Severity.Success : Severity.Error);
}
Snackbar.Add(
result.IsValid ? "Session is valid ✅" : $"Session invalid ❌ ({result.State})",
result.IsValid ? Severity.Success : Severity.Error);
}

private async Task LogoutAsync()
private async Task LogoutAsync()
{
await UAuth.Flows.LogoutAsync();
Snackbar.Add("Logged out", Severity.Success);
}

private async Task RefreshAsync()
{
await UAuth.Flows.RefreshAsync();
}

private async Task HandleGetMe()
{
var profileResult = await UAuth.Users.GetMeAsync();
if (profileResult.Ok)
{
await UAuth.Flows.LogoutAsync();
Snackbar.Add("Logged out", Severity.Success);
var profile = profileResult.Value;
Snackbar.Add($"User Profile: {profile?.UserName} ({profile?.DisplayName})", Severity.Info);
}

private async Task RefreshAsync()
else
{
await UAuth.Flows.RefreshAsync();
Snackbar.Add($"Failed to get profile: {profileResult.Error}", Severity.Error);
}
}

private async Task HandleGetMe()
private async Task ChangeUserInactive()
{
ChangeUserStatusAdminRequest request = new ChangeUserStatusAdminRequest
{
var profileResult = await UAuth.Users.GetMeAsync();
if (profileResult.Ok)
{
var profile = profileResult.Value;
Snackbar.Add($"User Profile: {profile?.UserName} ({profile?.DisplayName})", Severity.Info);
}
else
{
Snackbar.Add($"Failed to get profile: {profileResult.Error}", Severity.Error);
}
UserKey = UserKey.FromString("user"),
NewStatus = UserStatus.Disabled
};
var result = await UAuth.Users.ChangeStatusAdminAsync(request);
if (result.Ok)
{
Snackbar.Add($"User is disabled.", Severity.Info);
}

private async Task ChangeUserInactive()
else
{
ChangeUserStatusAdminRequest request = new ChangeUserStatusAdminRequest
{
UserKey = UserKey.FromString("user"),
NewStatus = UserStatus.Disabled
};
var result = await UAuth.Users.ChangeStatusAdminAsync(request);
if (result.Ok)
{
Snackbar.Add($"User is disabled.", Severity.Info);
}
else
{
Snackbar.Add($"Failed to change user status.", Severity.Error);
}
Snackbar.Add($"Failed to change user status.", Severity.Error);
}
}

protected override void OnAfterRender(bool firstRender)
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
if (firstRender)
var uri = Nav.ToAbsoluteUri(Nav.Uri);
var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);

if (query.TryGetValue("error", out var error))
{
var uri = Nav.ToAbsoluteUri(Nav.Uri);
var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);

if (query.TryGetValue("error", out var error))
{
ShowLoginError(error.ToString());
ClearQueryString();
}
ShowLoginError(error.ToString());
ClearQueryString();
}
}
}

private void ShowLoginError(string code)
private void ShowLoginError(string code)
{
var message = code switch
{
var message = code switch
{
"invalid" => "Invalid username or password.",
"locked" => "Your account is locked.",
"mfa" => "Multi-factor authentication required.",
_ => "Login failed."
};
"invalid" => "Invalid username or password.",
"locked" => "Your account is locked.",
"mfa" => "Multi-factor authentication required.",
_ => "Login failed."
};

Snackbar.Add(message, Severity.Error);
}

private void ClearQueryString()
{
var uri = new Uri(Nav.Uri);
var clean = uri.GetLeftPart(UriPartial.Path);
Nav.NavigateTo(clean, replace: true);
}
Snackbar.Add(message, Severity.Error);
}

public void Dispose()
{
Diagnostics.Changed -= OnDiagnosticsChanged;
}
private void ClearQueryString()
{
var uri = new Uri(Nav.Uri);
var clean = uri.GetLeftPart(UriPartial.Path);
Nav.NavigateTo(clean, replace: true);
}

public void Dispose()
{
Diagnostics.Changed -= OnDiagnosticsChanged;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using CodeBeam.UltimateAuth.Server.Authentication;
using CodeBeam.UltimateAuth.Server.Defaults;
using CodeBeam.UltimateAuth.Server.Extensions;
using CodeBeam.UltimateAuth.Server.Options;
using CodeBeam.UltimateAuth.Sessions.InMemory;
using CodeBeam.UltimateAuth.Tokens.InMemory;
using CodeBeam.UltimateAuth.Users.InMemory;
Expand Down Expand Up @@ -54,11 +55,13 @@

builder.Services.AddUltimateAuth();

builder.Services.AddUltimateAuthServer(o => {
builder.Services.AddUltimateAuthServer(o =>
{
o.Diagnostics.EnableRefreshHeaders = true;
//o.Session.MaxLifetime = TimeSpan.FromSeconds(32);
//o.Session.TouchInterval = TimeSpan.FromSeconds(9);
//o.Session.IdleTimeout = TimeSpan.FromSeconds(15);
o.AuthResponse.Login.AllowReturnUrlOverride = true;
})
.AddUltimateAuthUsersInMemory()
.AddUltimateAuthUsersReference()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@inject ISnackbar Snackbar
@inject IUAuthClient UAuthClient
@inject NavigationManager Nav
@inject IUAuthProductInfoProvider ProductInfo
@inject IUAuthClientProductInfoProvider ClientProductInfo
@inject AuthenticationStateProvider AuthStateProvider
@inject UAuthClientDiagnostics Diagnostics
@inject IUAuthClientBootstrapper Bootstrapper
Expand Down Expand Up @@ -41,8 +41,8 @@
</MudStack>

<MudStack Spacing="0">
<MudText><b>@ProductInfo.Get().ProductName</b> v @ProductInfo.Get().Version</MudText>
<MudText>Client Profile: @ProductInfo.Get().ClientProfile.ToString()</MudText>
<MudText><b>@ClientProductInfo.Get().ProductName</b> v @ClientProductInfo.Get().Version</MudText>
<MudText>Client Profile: @ClientProductInfo.Get().ClientProfile.ToString()</MudText>
</MudStack>

<MudStack Spacing="0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
builder.Services.AddUltimateAuth();
builder.Services.AddUltimateAuthClient(o =>
{
o.Endpoints.Authority = "https://localhost:6110";
o.Endpoints.BasePath = "https://localhost:6110";
});

//builder.Services.AddScoped<AuthenticationStateProvider, UAuthAuthenticationStateProvider>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
@using CodeBeam.UltimateAuth.Core.Options
@using Microsoft.Extensions.Options
@inject IJSRuntime JS
@inject IOptions<UAuthOptions> CoreOptions
@inject IOptions<UAuthClientOptions> Options
@inject NavigationManager Navigation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public async Task SubmitAsync()
await JS.InvokeVoidAsync("uauth.submitForm", _form);
}

private string ClientProfileValue => CoreOptions.Value.ClientProfile.ToString();
private string ClientProfileValue => Options.Value.ClientProfile.ToString();

private string EffectiveEndpoint => LoginType == UAuthLoginType.Pkce
? Options.Value.Endpoints.PkceComplete
Expand All @@ -130,7 +130,7 @@ private string ResolvedEndpoint
? EffectiveEndpoint
: Endpoint;

var baseUrl = UAuthUrlBuilder.Combine(Options.Value.Endpoints.Authority, loginPath);
var baseUrl = UAuthUrlBuilder.Build(Options.Value.Endpoints.BasePath, loginPath, Options.Value.MultiTenant);
var returnUrl = EffectiveReturnUrl;

if (string.IsNullOrWhiteSpace(returnUrl))
Expand Down
Loading