1
0
mirror of https://github.com/bitwarden/server synced 2025-12-23 11:43:23 +00:00

Revert filescoped (#2227)

* Revert "Add git blame entry (#2226)"

This reverts commit 239286737d.

* Revert "Turn on file scoped namespaces (#2225)"

This reverts commit 34fb4cca2a.
This commit is contained in:
Justin Baur
2022-08-29 15:53:48 -04:00
committed by GitHub
parent 239286737d
commit bae03feffe
1208 changed files with 74317 additions and 73126 deletions

View File

@@ -4,69 +4,70 @@ using Azure.Messaging.EventGrid.SystemEvents;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Mvc;
namespace Bit.Api.Utilities;
public static class ApiHelpers
namespace Bit.Api.Utilities
{
public static string EventGridKey { get; set; }
public async static Task<T> ReadJsonFileFromBody<T>(HttpContext httpContext, IFormFile file, long maxSize = 51200)
public static class ApiHelpers
{
T obj = default(T);
if (file != null && httpContext.Request.ContentLength.HasValue && httpContext.Request.ContentLength.Value <= maxSize)
public static string EventGridKey { get; set; }
public async static Task<T> ReadJsonFileFromBody<T>(HttpContext httpContext, IFormFile file, long maxSize = 51200)
{
try
T obj = default(T);
if (file != null && httpContext.Request.ContentLength.HasValue && httpContext.Request.ContentLength.Value <= maxSize)
{
using var stream = file.OpenReadStream();
obj = await JsonSerializer.DeserializeAsync<T>(stream, JsonHelpers.IgnoreCase);
}
catch { }
}
return obj;
}
/// <summary>
/// Validates Azure event subscription and calls the appropriate event handler. Responds HttpOk.
/// </summary>
/// <param name="request">HttpRequest received from Azure</param>
/// <param name="eventTypeHandlers">Dictionary of eventType strings and their associated handlers.</param>
/// <returns>OkObjectResult</returns>
/// <remarks>Reference https://docs.microsoft.com/en-us/azure/event-grid/receive-events</remarks>
public async static Task<ObjectResult> HandleAzureEvents(HttpRequest request,
Dictionary<string, Func<EventGridEvent, Task>> eventTypeHandlers)
{
var queryKey = request.Query["key"];
if (!CoreHelpers.FixedTimeEquals(queryKey, EventGridKey))
{
return new UnauthorizedObjectResult("Authentication failed. Please use a valid key.");
}
var response = string.Empty;
var requestData = await BinaryData.FromStreamAsync(request.Body);
var eventGridEvents = EventGridEvent.ParseMany(requestData);
foreach (var eventGridEvent in eventGridEvents)
{
if (eventGridEvent.TryGetSystemEventData(out object systemEvent))
{
if (systemEvent is SubscriptionValidationEventData eventData)
try
{
// Might want to enable additional validation: subject, topic etc.
var responseData = new SubscriptionValidationResponse()
{
ValidationResponse = eventData.ValidationCode
};
using var stream = file.OpenReadStream();
obj = await JsonSerializer.DeserializeAsync<T>(stream, JsonHelpers.IgnoreCase);
}
catch { }
}
return new OkObjectResult(responseData);
return obj;
}
/// <summary>
/// Validates Azure event subscription and calls the appropriate event handler. Responds HttpOk.
/// </summary>
/// <param name="request">HttpRequest received from Azure</param>
/// <param name="eventTypeHandlers">Dictionary of eventType strings and their associated handlers.</param>
/// <returns>OkObjectResult</returns>
/// <remarks>Reference https://docs.microsoft.com/en-us/azure/event-grid/receive-events</remarks>
public async static Task<ObjectResult> HandleAzureEvents(HttpRequest request,
Dictionary<string, Func<EventGridEvent, Task>> eventTypeHandlers)
{
var queryKey = request.Query["key"];
if (!CoreHelpers.FixedTimeEquals(queryKey, EventGridKey))
{
return new UnauthorizedObjectResult("Authentication failed. Please use a valid key.");
}
var response = string.Empty;
var requestData = await BinaryData.FromStreamAsync(request.Body);
var eventGridEvents = EventGridEvent.ParseMany(requestData);
foreach (var eventGridEvent in eventGridEvents)
{
if (eventGridEvent.TryGetSystemEventData(out object systemEvent))
{
if (systemEvent is SubscriptionValidationEventData eventData)
{
// Might want to enable additional validation: subject, topic etc.
var responseData = new SubscriptionValidationResponse()
{
ValidationResponse = eventData.ValidationCode
};
return new OkObjectResult(responseData);
}
}
if (eventTypeHandlers.ContainsKey(eventGridEvent.EventType))
{
await eventTypeHandlers[eventGridEvent.EventType](eventGridEvent);
}
}
if (eventTypeHandlers.ContainsKey(eventGridEvent.EventType))
{
await eventTypeHandlers[eventGridEvent.EventType](eventGridEvent);
}
return new OkObjectResult(response);
}
return new OkObjectResult(response);
}
}