|
1 | 1 | @page "/" |
| 2 | +@using Microsoft.AspNetCore.Components.WebAssembly.Authentication |
| 3 | +@using Microsoft.JSInterop |
| 4 | +@using System.Text.Json |
2 | 5 | @inject IAuthenticationService AuthService |
3 | 6 | @inject IAzureResourceService ResourceService |
| 7 | +@inject IAccessTokenProvider TokenProvider |
4 | 8 | @inject NavigationManager Navigation |
5 | 9 | @inject NavigationStateService NavState |
| 10 | +@inject IConfiguration Configuration |
| 11 | +@inject IJSRuntime JSRuntime |
6 | 12 |
|
7 | 13 | <PageTitle>bussin</PageTitle> |
8 | 14 |
|
|
12 | 18 | <h1>bussin</h1> |
13 | 19 | <p class="lead mb-3">A zero-backend PWA for managing Azure Service Bus—peek messages, purge queues, monitor metrics, and more. All processing happens in your browser, with no data sent to external servers.</p> |
14 | 20 |
|
15 | | - @if (isRefreshing) |
| 21 | + @if (needsServiceBusConsent && !isGrantingConsent) |
16 | 22 | { |
17 | | - <div class="d-flex align-items-center gap-2 mb-3"> |
18 | | - <div class="spinner-border spinner-border-sm" role="status"> |
19 | | - <span class="visually-hidden">Refreshing...</span> |
| 23 | + <div class="alert alert-warning"> |
| 24 | + <h5><i class="bi bi-shield-lock"></i> One More Permission Needed</h5> |
| 25 | + <p class="mb-3"> |
| 26 | + You can see your Service Bus namespaces below, but to actually <strong>send and receive messages</strong>, |
| 27 | + you need to grant one more permission. |
| 28 | + </p> |
| 29 | + <div class="card mb-3"> |
| 30 | + <div class="card-body"> |
| 31 | + <h6 class="card-title">What you're granting now:</h6> |
| 32 | + <ul class="mb-0"> |
| 33 | + <li><strong>Azure Service Bus API</strong> - Send and receive messages from queues, topics, and subscriptions</li> |
| 34 | + </ul> |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + <div class="card mb-3 bg-light"> |
| 38 | + <div class="card-body"> |
| 39 | + <h6 class="card-title">What you already granted:</h6> |
| 40 | + <ul class="mb-0"> |
| 41 | + <li><strong>Azure Management API</strong> - Browse and list your Service Bus resources (this is why you can see namespaces)</li> |
| 42 | + </ul> |
| 43 | + </div> |
20 | 44 | </div> |
21 | | - <small class="text-muted">Refreshing...</small> |
| 45 | + <p class="text-muted small mb-3"> |
| 46 | + <strong>Why two separate permissions?</strong> Azure AD requires separate consent for different services. |
| 47 | + Management API lets you browse resources, while Service Bus API lets you send/receive messages. |
| 48 | + </p> |
| 49 | + @if (!string.IsNullOrEmpty(consentErrorMessage)) |
| 50 | + { |
| 51 | + <div class="alert alert-danger mb-3"> |
| 52 | + @consentErrorMessage |
| 53 | + </div> |
| 54 | + } |
| 55 | + <button class="btn btn-warning btn-lg" @onclick="GrantConsent" disabled="@isGrantingConsent"> |
| 56 | + <i class="bi bi-check-circle"></i> Grant Service Bus Permission |
| 57 | + </button> |
22 | 58 | </div> |
23 | 59 | } |
24 | | - |
25 | | - @if (namespaces.Count == 0) |
| 60 | + else if (isGrantingConsent) |
26 | 61 | { |
27 | | - @if (!isRefreshing) |
28 | | - { |
29 | | - <div class="alert alert-info"> |
30 | | - <h5>No Service Bus namespaces found</h5> |
31 | | - <p>You don't have access to any Service Bus namespaces, or none exist in your subscriptions.</p> |
32 | | - <p>Make sure you have the appropriate permissions to view Service Bus resources.</p> |
| 62 | + <div class="alert alert-info"> |
| 63 | + <div class="d-flex align-items-center gap-2"> |
| 64 | + <div class="spinner-border spinner-border-sm" role="status"></div> |
| 65 | + <span>Opening consent popup...</span> |
33 | 66 | </div> |
34 | | - } |
| 67 | + </div> |
35 | 68 | } |
36 | 69 | else |
37 | 70 | { |
38 | | - <div class="mb-3"> |
39 | | - <input type="text" class="form-control" placeholder="Search namespaces..." |
40 | | - @bind="searchTerm" @bind:event="oninput" /> |
41 | | - </div> |
| 71 | + @if (isRefreshing) |
| 72 | + { |
| 73 | + <div class="d-flex align-items-center gap-2 mb-3"> |
| 74 | + <div class="spinner-border spinner-border-sm" role="status"> |
| 75 | + <span class="visually-hidden">Refreshing...</span> |
| 76 | + </div> |
| 77 | + <small class="text-muted">Refreshing...</small> |
| 78 | + </div> |
| 79 | + } |
42 | 80 |
|
43 | | - <div class="row"> |
44 | | - @foreach (var ns in FilteredNamespaces) |
| 81 | + @if (namespaces.Count == 0) |
| 82 | + { |
| 83 | + @if (!isRefreshing) |
45 | 84 | { |
46 | | - <div class="col-md-4 mb-3"> |
47 | | - <div class="card h-100 namespace-card" @onclick="() => NavigateToExplorer(ns)" style="cursor: pointer;"> |
48 | | - <div class="card-body"> |
49 | | - <h5 class="card-title">@ns.Name</h5> |
50 | | - <p class="card-text"> |
51 | | - <small class="text-muted"> |
52 | | - <strong>Subscription:</strong> @ns.SubscriptionName<br /> |
53 | | - <strong>Resource Group:</strong> @ns.ResourceGroup<br /> |
54 | | - <strong>Location:</strong> @ns.Location<br /> |
55 | | - <strong>Tenant:</strong> @ns.TenantId |
56 | | - </small> |
57 | | - </p> |
58 | | - </div> |
59 | | - </div> |
| 85 | + <div class="alert alert-info"> |
| 86 | + <h5>No Service Bus namespaces found</h5> |
| 87 | + <p>You don't have access to any Service Bus namespaces, or none exist in your subscriptions.</p> |
| 88 | + <p>Make sure you have the appropriate permissions to view Service Bus resources.</p> |
60 | 89 | </div> |
61 | 90 | } |
62 | | - </div> |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + <div class="mb-3"> |
| 95 | + <input type="text" class="form-control" placeholder="Search namespaces..." |
| 96 | + @bind="searchTerm" @bind:event="oninput" /> |
| 97 | + </div> |
| 98 | + |
| 99 | + <div class="row"> |
| 100 | + @foreach (var ns in FilteredNamespaces) |
| 101 | + { |
| 102 | + <div class="col-md-4 mb-3"> |
| 103 | + <div class="card h-100 namespace-card" @onclick="() => NavigateToExplorer(ns)" style="cursor: pointer;"> |
| 104 | + <div class="card-body"> |
| 105 | + <h5 class="card-title">@ns.Name</h5> |
| 106 | + <p class="card-text"> |
| 107 | + <small class="text-muted"> |
| 108 | + <strong>Subscription:</strong> @ns.SubscriptionName<br /> |
| 109 | + <strong>Resource Group:</strong> @ns.ResourceGroup<br /> |
| 110 | + <strong>Location:</strong> @ns.Location<br /> |
| 111 | + <strong>Tenant:</strong> @ns.TenantId |
| 112 | + </small> |
| 113 | + </p> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + } |
| 118 | + </div> |
| 119 | + } |
63 | 120 | } |
64 | 121 |
|
65 | 122 | @if (!string.IsNullOrEmpty(errorMessage)) |
|
92 | 149 | { |
93 | 150 | <div class="card-body"> |
94 | 151 | <h5>What permissions are requested?</h5> |
95 | | - <p>When you sign in, this application requests consent for two Azure permissions:</p> |
96 | | - <ul> |
97 | | - <li><strong>Azure Management API</strong> - To list your Service Bus namespaces, queues, topics, and subscriptions</li> |
98 | | - <li><strong>Azure Service Bus</strong> - To peek, send, and manage messages in your Service Bus entities</li> |
99 | | - </ul> |
| 152 | + <p>This application requires two separate Azure permissions:</p> |
| 153 | + <div class="mb-3"> |
| 154 | + <strong>1. Azure Management API</strong> (requested at initial sign-in) |
| 155 | + <ul> |
| 156 | + <li>Browse and list your Service Bus namespaces, queues, topics, and subscriptions</li> |
| 157 | + </ul> |
| 158 | + </div> |
| 159 | + <div class="mb-3"> |
| 160 | + <strong>2. Azure Service Bus API</strong> (requested via popup after sign-in) |
| 161 | + <ul> |
| 162 | + <li>Send and receive messages from queues, topics, and subscriptions</li> |
| 163 | + <li>Perform message operations (peek, delete, purge, etc.)</li> |
| 164 | + </ul> |
| 165 | + </div> |
| 166 | + <p class="text-muted small"> |
| 167 | + <strong>Why two permissions?</strong> Azure AD requires separate consent for different services. |
| 168 | + You'll be asked to consent to each one separately. |
| 169 | + </p> |
100 | 170 |
|
101 | 171 | <h5 class="mt-3">Is this safe?</h5> |
102 | 172 | <p>Yes! This application is completely safe:</p> |
|
131 | 201 | private bool showSecurityInfo = false; |
132 | 202 | private string searchTerm = ""; |
133 | 203 | private CancellationTokenSource? _loadCts; |
| 204 | + private bool needsServiceBusConsent = false; |
| 205 | + private bool isGrantingConsent = false; |
| 206 | + private string? consentErrorMessage = null; |
134 | 207 |
|
135 | 208 | private IEnumerable<ServiceBusNamespaceInfo> FilteredNamespaces => |
136 | 209 | string.IsNullOrWhiteSpace(searchTerm) |
|
150 | 223 | var isAuthenticated = await AuthService.IsAuthenticatedAsync(); |
151 | 224 | if (isAuthenticated) |
152 | 225 | { |
| 226 | + await CheckServiceBusConsent(); |
153 | 227 | _ = LoadNamespacesAsync(); |
154 | 228 | } |
155 | 229 | } |
156 | 230 |
|
| 231 | + private async Task CheckServiceBusConsent() |
| 232 | + { |
| 233 | + try |
| 234 | + { |
| 235 | + var result = await TokenProvider.RequestAccessToken(new AccessTokenRequestOptions |
| 236 | + { |
| 237 | + Scopes = ["https://servicebus.azure.net/user_impersonation"] |
| 238 | + }); |
| 239 | + |
| 240 | + needsServiceBusConsent = result.Status == AccessTokenResultStatus.RequiresRedirect; |
| 241 | + } |
| 242 | + catch (Exception ex) |
| 243 | + { |
| 244 | + Console.WriteLine($"Error checking Service Bus consent: {ex.Message}"); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + private async Task GrantConsent() |
| 249 | + { |
| 250 | + isGrantingConsent = true; |
| 251 | + consentErrorMessage = null; |
| 252 | + StateHasChanged(); |
| 253 | + |
| 254 | + try |
| 255 | + { |
| 256 | + var clientId = Configuration["AzureAd:ClientId"]; |
| 257 | + var authority = Configuration["AzureAd:Authority"]; |
| 258 | + |
| 259 | + await JSRuntime.InvokeVoidAsync("eval", $@" |
| 260 | + window.msalConfig = {{ |
| 261 | + clientId: '{clientId}', |
| 262 | + authority: '{authority}' |
| 263 | + }}; |
| 264 | + "); |
| 265 | + |
| 266 | + var result = await JSRuntime.InvokeAsync<JsonElement>("msalHelper.acquireTokenPopup", |
| 267 | + "https://servicebus.azure.net/user_impersonation"); |
| 268 | + |
| 269 | + if (result.TryGetProperty("success", out var success) && success.GetBoolean()) |
| 270 | + { |
| 271 | + Console.WriteLine("✓ Service Bus consent granted successfully - reloading page to refresh tokens"); |
| 272 | + Navigation.NavigateTo(Navigation.Uri, forceLoad: true); |
| 273 | + return; |
| 274 | + } |
| 275 | + else if (result.TryGetProperty("error", out var error)) |
| 276 | + { |
| 277 | + var errorMsg = error.GetString() ?? "Unknown error"; |
| 278 | + if (!errorMsg.Contains("user_cancelled")) |
| 279 | + { |
| 280 | + consentErrorMessage = errorMsg; |
| 281 | + Console.WriteLine($"✗ Consent failed: {consentErrorMessage}"); |
| 282 | + } |
| 283 | + else |
| 284 | + { |
| 285 | + Console.WriteLine("Consent cancelled by user"); |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | + catch (Exception ex) |
| 290 | + { |
| 291 | + consentErrorMessage = ex.Message; |
| 292 | + Console.WriteLine($"✗ Error during consent: {ex}"); |
| 293 | + } |
| 294 | + finally |
| 295 | + { |
| 296 | + isGrantingConsent = false; |
| 297 | + StateHasChanged(); |
| 298 | + } |
| 299 | + } |
| 300 | + |
157 | 301 | private async Task LoadNamespacesAsync() |
158 | 302 | { |
159 | 303 | _loadCts?.Cancel(); |
|
0 commit comments