-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dhiogo Acioli
committed
Apr 7, 2024
1 parent
5ad2cc5
commit 71760d8
Showing
278 changed files
with
10,219 additions
and
4,400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.Azure.Cosmos; | ||
|
||
namespace MM.API.Core | ||
{ | ||
public static class ApiStartup | ||
{ | ||
private static readonly HttpClient _httpClient = new(); | ||
|
||
private static CosmosClient _cosmosClient = default!; | ||
|
||
public static HttpClient HttpClient => _httpClient; | ||
public static CosmosClient CosmosClient => _cosmosClient; | ||
|
||
public static void Startup(string conn) | ||
{ | ||
_cosmosClient = new(conn, new CosmosClientOptions() | ||
{ | ||
SerializerOptions = new CosmosSerializationOptions() | ||
{ | ||
PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Middleware; | ||
using Microsoft.Extensions.Logging; | ||
using System.Net; | ||
|
||
namespace MM.API.Core.Middleware | ||
{ | ||
internal sealed class ExceptionHandlingMiddleware(ILogger<ExceptionHandlingMiddleware> logger) : IFunctionsWorkerMiddleware | ||
{ | ||
private readonly ILogger<ExceptionHandlingMiddleware> _logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
|
||
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next) | ||
{ | ||
try | ||
{ | ||
await next(context); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error processing invocation"); | ||
|
||
await context.SetHttpResponseStatusCode(HttpStatusCode.InternalServerError, "Invocation failed!"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using System.Net; | ||
|
||
namespace MM.API.Core.Middleware | ||
{ | ||
public static class FunctionContextExtensions | ||
{ | ||
public static async Task SetHttpResponseStatusCode(this FunctionContext context, HttpStatusCode statusCode, string? status) | ||
{ | ||
var req = await context.GetHttpRequestDataAsync(); | ||
|
||
var newHttpResponse = req.CreateResponse(statusCode); | ||
|
||
// You need to explicitly pass the status code in WriteAsJsonAsync method. | ||
// https://github.com/Azure/azure-functions-dotnet-worker/issues/776 | ||
await newHttpResponse.WriteAsJsonAsync(new { Status = status }, newHttpResponse.StatusCode); | ||
|
||
var invocationResult = context.GetInvocationResult(); | ||
|
||
var httpOutputBindingFromMultipleOutputBindings = GetHttpOutputBindingFromMultipleOutputBinding(context); | ||
if (httpOutputBindingFromMultipleOutputBindings is not null) | ||
{ | ||
httpOutputBindingFromMultipleOutputBindings.Value = newHttpResponse; | ||
} | ||
else | ||
{ | ||
invocationResult.Value = newHttpResponse; | ||
} | ||
} | ||
|
||
private static OutputBindingData<HttpResponseData>? GetHttpOutputBindingFromMultipleOutputBinding(FunctionContext context) | ||
{ | ||
// The output binding entry name will be "$return" only when the function return type is HttpResponseData | ||
var httpOutputBinding = context.GetOutputBindings<HttpResponseData>() | ||
.FirstOrDefault(b => b.BindingType == "http" && b.Name != "$return"); | ||
|
||
return httpOutputBinding; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace MM.API.Core | ||
{ | ||
public static class PaddleHelper | ||
{ | ||
public static async Task<bool> ValidPaddleSignature(this HttpRequestData req, string? paddleSignature, CancellationToken cancellationToken) | ||
{ | ||
var paddleHeader = req.Headers.GetValues("Paddle-Signature").First(); | ||
var ts = paddleHeader.Split(";")[0]; | ||
var h1 = paddleHeader.Split(";")[1]; | ||
var tsValue = ts.Split("=")[1]; | ||
var h1Value = h1.Split("=")[1]; | ||
var rawbody = await new StreamReader(req.Body).ReadToEndAsync(cancellationToken); | ||
var payload = tsValue + ":" + rawbody; | ||
|
||
var encoding = new UTF8Encoding(); | ||
var keyByte = encoding.GetBytes(paddleSignature ?? throw new ArgumentNullException(nameof(paddleSignature))); | ||
var hmacsha256 = new HMACSHA256(keyByte); | ||
var messageBytes = encoding.GetBytes(payload); | ||
var hashmessage = hmacsha256.ComputeHash(messageBytes); | ||
var hash = ByteToString(hashmessage); | ||
|
||
return h1Value.Equals(hash, StringComparison.CurrentCultureIgnoreCase); | ||
} | ||
|
||
public static string ByteToString(byte[] buff) | ||
{ | ||
var sbinary = new StringBuilder(); | ||
for (int i = 0; i < buff.Length; i++) | ||
{ | ||
sbinary.Append(buff[i].ToString("X2")); // hex format | ||
} | ||
return (sbinary.ToString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.