|
| 1 | +using CodeBeam.UltimateAuth.Client.Contracts; |
| 2 | +using CodeBeam.UltimateAuth.Client.Utilities; |
| 3 | +using CodeBeam.UltimateAuth.Core.Contracts; |
| 4 | +using CodeBeam.UltimateAuth.Core.Domain; |
| 5 | +using CodeBeam.UltimateAuth.Server.Stores; |
| 6 | +using Microsoft.AspNetCore.Components; |
| 7 | +using Microsoft.AspNetCore.WebUtilities; |
| 8 | +using MudBlazor; |
| 9 | + |
| 10 | +namespace CodeBeam.UltimateAuth.Sample.UAuthHub.Components.Pages |
| 11 | +{ |
| 12 | + public partial class Home |
| 13 | + { |
| 14 | + [SupplyParameterFromQuery(Name = "hub")] |
| 15 | + public string? HubKey { get; set; } |
| 16 | + |
| 17 | + private string? _username; |
| 18 | + private string? _password; |
| 19 | + |
| 20 | + private HubFlowState? _state; |
| 21 | + |
| 22 | + protected override async Task OnParametersSetAsync() |
| 23 | + { |
| 24 | + if (string.IsNullOrWhiteSpace(HubKey)) |
| 25 | + { |
| 26 | + _state = null; |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + _state = await HubFlowReader.GetStateAsync(new HubSessionId(HubKey)); |
| 31 | + } |
| 32 | + |
| 33 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 34 | + { |
| 35 | + if (!firstRender) |
| 36 | + return; |
| 37 | + |
| 38 | + var currentError = await BrowserStorage.GetAsync(StorageScope.Session, "uauth:last_error"); |
| 39 | + |
| 40 | + if (!string.IsNullOrWhiteSpace(currentError)) |
| 41 | + { |
| 42 | + Snackbar.Add(ResolveErrorMessage(currentError), Severity.Error); |
| 43 | + await BrowserStorage.RemoveAsync(StorageScope.Session, "uauth:last_error"); |
| 44 | + } |
| 45 | + |
| 46 | + var uri = Nav.ToAbsoluteUri(Nav.Uri); |
| 47 | + var query = QueryHelpers.ParseQuery(uri.Query); |
| 48 | + |
| 49 | + if (query.TryGetValue("__uauth_error", out var error)) |
| 50 | + { |
| 51 | + await BrowserStorage.SetAsync(StorageScope.Session, "uauth:last_error", error.ToString()); |
| 52 | + } |
| 53 | + |
| 54 | + if (string.IsNullOrWhiteSpace(HubKey)) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (_state is null || !_state.Exists) |
| 60 | + return; |
| 61 | + |
| 62 | + if (_state?.IsActive != true) |
| 63 | + { |
| 64 | + await StartNewPkceAsync(); |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // For testing & debugging |
| 70 | + private async Task ProgrammaticPkceLogin() |
| 71 | + { |
| 72 | + var hub = _state; |
| 73 | + |
| 74 | + if (hub is null) |
| 75 | + return; |
| 76 | + |
| 77 | + var credentials = await HubCredentialResolver.ResolveAsync(new HubSessionId(HubKey)); |
| 78 | + |
| 79 | + var request = new PkceLoginRequest |
| 80 | + { |
| 81 | + Identifier = "Admin", |
| 82 | + Secret = "Password!", |
| 83 | + AuthorizationCode = credentials?.AuthorizationCode ?? string.Empty, |
| 84 | + CodeVerifier = credentials?.CodeVerifier ?? string.Empty, |
| 85 | + ReturnUrl = _state?.ReturnUrl ?? string.Empty |
| 86 | + }; |
| 87 | + await UAuthClient.CompletePkceLoginAsync(request); |
| 88 | + } |
| 89 | + |
| 90 | + private async Task StartNewPkceAsync() |
| 91 | + { |
| 92 | + var returnUrl = await ResolveReturnUrlAsync(); |
| 93 | + await UAuthClient.BeginPkceAsync(returnUrl); |
| 94 | + } |
| 95 | + |
| 96 | + private async Task<string> ResolveReturnUrlAsync() |
| 97 | + { |
| 98 | + var fromContext = _state?.ReturnUrl; |
| 99 | + if (!string.IsNullOrWhiteSpace(fromContext)) |
| 100 | + return fromContext; |
| 101 | + |
| 102 | + var uri = Nav.ToAbsoluteUri(Nav.Uri); |
| 103 | + var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query); |
| 104 | + |
| 105 | + if (query.TryGetValue("return_url", out var ru) && !string.IsNullOrWhiteSpace(ru)) |
| 106 | + return ru!; |
| 107 | + |
| 108 | + if (query.TryGetValue("hub", out var hubKey) && !string.IsNullOrWhiteSpace(hubKey)) |
| 109 | + { |
| 110 | + var artifact = await AuthStore.GetAsync(new AuthArtifactKey(hubKey!)); |
| 111 | + if (artifact is HubFlowArtifact flow && !string.IsNullOrWhiteSpace(flow.ReturnUrl)) |
| 112 | + return flow.ReturnUrl!; |
| 113 | + } |
| 114 | + |
| 115 | + // Config default (recommend adding to options) |
| 116 | + //if (!string.IsNullOrWhiteSpace(_options.Login.DefaultReturnUrl)) |
| 117 | + // return _options.Login.DefaultReturnUrl!; |
| 118 | + |
| 119 | + return Nav.Uri; |
| 120 | + } |
| 121 | + |
| 122 | + private string ResolveErrorMessage(string? errorKey) |
| 123 | + { |
| 124 | + if (errorKey == "invalid") |
| 125 | + { |
| 126 | + return "Login failed."; |
| 127 | + } |
| 128 | + |
| 129 | + return "Failed attempt."; |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | +} |
0 commit comments