Files
server/src/Notifications/AzureQueueHostedService.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
3.4 KiB
C#
Raw Normal View History

using Azure.Storage.Queues;
using Bit.Core.Settings;
using Bit.Core.Utilities;
2018-08-02 12:14:33 -04:00
using Microsoft.AspNetCore.SignalR;
2018-08-16 13:45:31 -04:00
namespace Bit.Notifications;
2022-08-29 16:06:55 -04:00
2018-08-16 13:45:31 -04:00
public class AzureQueueHostedService : IHostedService, IDisposable
2018-08-02 12:14:33 -04:00
{
private readonly ILogger _logger;
2018-08-16 13:45:31 -04:00
private readonly IHubContext<NotificationsHub> _hubContext;
[SG-167] Implement Passwordless Authentication via Notifications (#2276) * [SG-549] Commit Initial AuthRequest Repository (#2174) * Model Passwordless * Scaffold database for Passwordless * Implement SQL Repository * [SG-167] Base Passwordless API (#2185) * Implement Passwordless notifications * Implement Controller * Add documentation to BaseRequestValidator * Register AuthRequestRepo * Remove ExpirationDate from the AuthRequest table * [SG-407] Create job to delete expired requests (#2187) * chore: init * remove exp date * fix: log name * [SG-167] Added fingerprint phrase to response model. (#2233) * Remove FailedLoginAttempt logic * Block unknown devices * Add EF Support for passwordless * Got SignalR working for responses * Added delete job method to EF repo * Implement a GetMany API endpoint for AuthRequests * Ran dotnet format * Fix a merge issues * Redated migration scripts * tried sorting sqlproj * Remove FailedLoginAttempts from SQL * Groom Postgres script * Remove extra commas from migration script * Correct isSpent() * [SG-167] Adde identity validation for passwordless requests. Registered IAuthRepository. * [SG-167] Added origin of the request to response model * Use display name for device identifier in response * Add datetime conversions back to postgres migration script * [SG-655] Add anonymous endpoint for checking if a device & user combo match * [review] Consolidate error conditions Co-authored-by: Brandon Maharaj <107377945+BrandonM-Bitwarden@users.noreply.github.com> Co-authored-by: André Filipe da Silva Bispo <andrefsbispo@hotmail.com> Co-authored-by: André Bispo <abispo@bitwarden.com>
2022-09-26 13:21:13 -04:00
private readonly IHubContext<AnonymousNotificationsHub> _anonymousHubContext;
2018-08-02 17:23:37 -04:00
private readonly GlobalSettings _globalSettings;
2018-08-02 12:14:33 -04:00
2018-08-02 17:23:37 -04:00
private Task _executingTask;
private CancellationTokenSource _cts;
2020-01-10 08:33:13 -05:00
private QueueClient _queueClient;
2018-08-02 17:23:37 -04:00
2018-08-16 13:45:31 -04:00
public AzureQueueHostedService(
2018-08-21 09:37:09 -04:00
ILogger<AzureQueueHostedService> logger,
2018-08-16 13:45:31 -04:00
IHubContext<NotificationsHub> hubContext,
[SG-167] Implement Passwordless Authentication via Notifications (#2276) * [SG-549] Commit Initial AuthRequest Repository (#2174) * Model Passwordless * Scaffold database for Passwordless * Implement SQL Repository * [SG-167] Base Passwordless API (#2185) * Implement Passwordless notifications * Implement Controller * Add documentation to BaseRequestValidator * Register AuthRequestRepo * Remove ExpirationDate from the AuthRequest table * [SG-407] Create job to delete expired requests (#2187) * chore: init * remove exp date * fix: log name * [SG-167] Added fingerprint phrase to response model. (#2233) * Remove FailedLoginAttempt logic * Block unknown devices * Add EF Support for passwordless * Got SignalR working for responses * Added delete job method to EF repo * Implement a GetMany API endpoint for AuthRequests * Ran dotnet format * Fix a merge issues * Redated migration scripts * tried sorting sqlproj * Remove FailedLoginAttempts from SQL * Groom Postgres script * Remove extra commas from migration script * Correct isSpent() * [SG-167] Adde identity validation for passwordless requests. Registered IAuthRepository. * [SG-167] Added origin of the request to response model * Use display name for device identifier in response * Add datetime conversions back to postgres migration script * [SG-655] Add anonymous endpoint for checking if a device & user combo match * [review] Consolidate error conditions Co-authored-by: Brandon Maharaj <107377945+BrandonM-Bitwarden@users.noreply.github.com> Co-authored-by: André Filipe da Silva Bispo <andrefsbispo@hotmail.com> Co-authored-by: André Bispo <abispo@bitwarden.com>
2022-09-26 13:21:13 -04:00
IHubContext<AnonymousNotificationsHub> anonymousHubContext,
2018-08-02 17:23:37 -04:00
GlobalSettings globalSettings)
2018-08-02 12:14:33 -04:00
{
_logger = logger;
_hubContext = hubContext;
2018-08-02 17:23:37 -04:00
_globalSettings = globalSettings;
[SG-167] Implement Passwordless Authentication via Notifications (#2276) * [SG-549] Commit Initial AuthRequest Repository (#2174) * Model Passwordless * Scaffold database for Passwordless * Implement SQL Repository * [SG-167] Base Passwordless API (#2185) * Implement Passwordless notifications * Implement Controller * Add documentation to BaseRequestValidator * Register AuthRequestRepo * Remove ExpirationDate from the AuthRequest table * [SG-407] Create job to delete expired requests (#2187) * chore: init * remove exp date * fix: log name * [SG-167] Added fingerprint phrase to response model. (#2233) * Remove FailedLoginAttempt logic * Block unknown devices * Add EF Support for passwordless * Got SignalR working for responses * Added delete job method to EF repo * Implement a GetMany API endpoint for AuthRequests * Ran dotnet format * Fix a merge issues * Redated migration scripts * tried sorting sqlproj * Remove FailedLoginAttempts from SQL * Groom Postgres script * Remove extra commas from migration script * Correct isSpent() * [SG-167] Adde identity validation for passwordless requests. Registered IAuthRepository. * [SG-167] Added origin of the request to response model * Use display name for device identifier in response * Add datetime conversions back to postgres migration script * [SG-655] Add anonymous endpoint for checking if a device & user combo match * [review] Consolidate error conditions Co-authored-by: Brandon Maharaj <107377945+BrandonM-Bitwarden@users.noreply.github.com> Co-authored-by: André Filipe da Silva Bispo <andrefsbispo@hotmail.com> Co-authored-by: André Bispo <abispo@bitwarden.com>
2022-09-26 13:21:13 -04:00
_anonymousHubContext = anonymousHubContext;
2018-08-02 12:14:33 -04:00
}
public Task StartAsync(CancellationToken cancellationToken)
{
2018-08-02 17:23:37 -04:00
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
_executingTask = ExecuteAsync(_cts.Token);
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
2018-08-02 12:14:33 -04:00
}
2018-08-02 17:23:37 -04:00
public async Task StopAsync(CancellationToken cancellationToken)
2022-08-29 16:06:55 -04:00
{
if (_executingTask == null)
2018-08-02 12:14:33 -04:00
{
2018-08-02 17:23:37 -04:00
return;
2018-08-02 12:14:33 -04:00
}
2019-07-11 21:48:44 -04:00
_logger.LogWarning("Stopping service.");
2018-08-02 17:23:37 -04:00
_cts.Cancel();
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken));
cancellationToken.ThrowIfCancellationRequested();
2022-08-29 16:06:55 -04:00
}
2018-08-02 12:14:33 -04:00
2018-08-02 17:23:37 -04:00
public void Dispose()
2018-08-21 09:37:09 -04:00
{ }
2018-08-02 12:14:33 -04:00
2018-08-02 17:23:37 -04:00
private async Task ExecuteAsync(CancellationToken cancellationToken)
2022-08-29 16:06:55 -04:00
{
2020-01-10 08:33:13 -05:00
_queueClient = new QueueClient(_globalSettings.Notifications.ConnectionString, "notifications");
while (!cancellationToken.IsCancellationRequested)
2018-08-02 12:14:33 -04:00
{
2020-01-10 08:33:13 -05:00
try
2018-08-02 17:23:37 -04:00
{
2020-01-10 08:33:13 -05:00
var messages = await _queueClient.ReceiveMessagesAsync(32);
if (messages.Value?.Any() ?? false)
2018-08-02 17:23:37 -04:00
{
2020-01-10 08:33:13 -05:00
foreach (var message in messages.Value)
2018-08-02 17:23:37 -04:00
{
2022-08-29 16:06:55 -04:00
try
{
await HubHelpers.SendNotificationToHubAsync(
[SG-167] Implement Passwordless Authentication via Notifications (#2276) * [SG-549] Commit Initial AuthRequest Repository (#2174) * Model Passwordless * Scaffold database for Passwordless * Implement SQL Repository * [SG-167] Base Passwordless API (#2185) * Implement Passwordless notifications * Implement Controller * Add documentation to BaseRequestValidator * Register AuthRequestRepo * Remove ExpirationDate from the AuthRequest table * [SG-407] Create job to delete expired requests (#2187) * chore: init * remove exp date * fix: log name * [SG-167] Added fingerprint phrase to response model. (#2233) * Remove FailedLoginAttempt logic * Block unknown devices * Add EF Support for passwordless * Got SignalR working for responses * Added delete job method to EF repo * Implement a GetMany API endpoint for AuthRequests * Ran dotnet format * Fix a merge issues * Redated migration scripts * tried sorting sqlproj * Remove FailedLoginAttempts from SQL * Groom Postgres script * Remove extra commas from migration script * Correct isSpent() * [SG-167] Adde identity validation for passwordless requests. Registered IAuthRepository. * [SG-167] Added origin of the request to response model * Use display name for device identifier in response * Add datetime conversions back to postgres migration script * [SG-655] Add anonymous endpoint for checking if a device & user combo match * [review] Consolidate error conditions Co-authored-by: Brandon Maharaj <107377945+BrandonM-Bitwarden@users.noreply.github.com> Co-authored-by: André Filipe da Silva Bispo <andrefsbispo@hotmail.com> Co-authored-by: André Bispo <abispo@bitwarden.com>
2022-09-26 13:21:13 -04:00
message.DecodeMessageText(), _hubContext, _anonymousHubContext, cancellationToken);
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
2022-08-29 16:06:55 -04:00
}
catch (Exception e)
{
_logger.LogError(e, "Error processing dequeued message: {MessageId} x{DequeueCount}.",
message.MessageId, message.DequeueCount);
if (message.DequeueCount > 2)
{
2020-01-10 08:33:13 -05:00
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
}
}
}
2018-08-02 17:23:37 -04:00
}
else
{
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}
2018-08-02 17:23:37 -04:00
}
2019-07-11 21:48:44 -04:00
catch (Exception e)
2022-08-29 16:06:55 -04:00
{
_logger.LogError(e, "Error processing messages.");
2022-08-29 16:06:55 -04:00
}
}
2022-08-29 16:06:55 -04:00
2019-07-11 21:48:44 -04:00
_logger.LogWarning("Done processing.");
2018-08-02 12:14:33 -04:00
}
}