<commit_before>﻿using abremir.AllMyBricks.Core.Configuration;
using abremir.AllMyBricks.Core.Extensions;
using abremir.AllMyBricks.Core.Models;
using fastJSON;
using Microsoft.AspNetCore.Http.Extensions;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace abremir.AllMyBricks.Core.Security
{
    public class HmacDelegatingHandler : DelegatingHandler
    {
        public HmacDelegatingHandler(HttpMessageHandler next) : base(next) { }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var content = request.Content as StringContent;

            if(content == null)
            {
                return null;
            }

            var apiKeyRequest = JSON.ToObject<ApiKeyRequest>(await content.ReadAsStringAsync());
            var appId = apiKeyRequest.DeviceIdentification.DeviceHash;
            var apiKey = apiKeyRequest.RegistrationHash;

            var requestUri = UriHelper.Encode(request.RequestUri);
            var requestHttpMethod = request.Method.Method;
            var nonce = Guid.NewGuid().ToString("N");
            var requestContentBase64String = string.Empty;
            var requestTimestamp = DateTime.UtcNow.TotalSecondsFromEpochStart().ToString();

            if (request.Content != null)
            {
                var requestContentHash = SHA256Hash.ComputeHash(await request.Content.ReadAsStreamAsync().ConfigureAwait(false));
                requestContentBase64String = Convert.ToBase64String(requestContentHash);
            }

            var signatureRawData = appId + requestHttpMethod + requestUri + requestTimestamp + nonce + requestContentBase64String;

            var secretKeyByteArray = Encoding.UTF8.GetBytes(apiKey);

            var signature = Encoding.UTF8.GetBytes(signatureRawData);

            using (var hmac = new HMACSHA256(secretKeyByteArray))
            {
                var signatureBytes = hmac.ComputeHash(signature);
                var requestSignatureBase64String = Convert.ToBase64String(signatureBytes);

                request.Headers.Authorization = new AuthenticationHeaderValue(Constants.HmacAuthenticationScheme, $"{appId}:{requestSignatureBase64String}:{nonce}:{requestTimestamp}");
            }

            return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
        }
    }
}<commit_msg>hmac handler: remove ConfigureAwait and adjust to match abremir.allmybricks.onboarding<commit_after>﻿using abremir.AllMyBricks.Core.Configuration;
using abremir.AllMyBricks.Core.Extensions;
using abremir.AllMyBricks.Core.Models;
using fastJSON;
using Microsoft.AspNetCore.Http.Extensions;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace abremir.AllMyBricks.Core.Security
{
    public class HmacDelegatingHandler : DelegatingHandler
    {
        public HmacDelegatingHandler(HttpMessageHandler next) : base(next) { }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.Content == null)
            {
                return null;
            }

            var apiKeyRequest = JSON.ToObject<ApiKeyRequest>(await request.Content.ReadAsStringAsync());
            var appId = apiKeyRequest.DeviceIdentification.DeviceHash;
            var apiKey = apiKeyRequest.RegistrationHash;

            var requestUri = UriHelper.Encode(request.RequestUri);
            var requestHttpMethod = request.Method.Method;
            var nonce = Guid.NewGuid().ToString("N");
            var requestTimestamp = DateTime.UtcNow.TotalSecondsFromEpochStart().ToString();

            var requestContentHash = SHA256Hash.ComputeHash(await request.Content.ReadAsStreamAsync());
            var requestContentBase64String = Convert.ToBase64String(requestContentHash);

            var signatureRawData = appId + requestHttpMethod + requestUri + requestTimestamp + nonce + requestContentBase64String;

            var secretKeyByteArray = Encoding.UTF8.GetBytes(apiKey);

            var signature = Encoding.UTF8.GetBytes(signatureRawData);

            using (var hmac = new HMACSHA256(secretKeyByteArray))
            {
                var signatureBytes = hmac.ComputeHash(signature);
                var requestSignatureBase64String = Convert.ToBase64String(signatureBytes);

                request.Headers.Authorization = new AuthenticationHeaderValue(Constants.HmacAuthenticationScheme, $"{appId}:{requestSignatureBase64String}:{nonce}:{requestTimestamp}");
            }

            return await base.SendAsync(request, cancellationToken);
        }
    }
}
{'fraction_non_alphanumeric': 0.08210313447927199, 'fraction_numerical': 0.008493427704752275, 'mean_word_length': 3.152812762384551, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '134742', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1252, 'n_words': 235}