Ejemplo n.º 1
0
        /// <summary>
        /// This method will properly decrypt the connection request that is coming from Microsoft Graph.
        /// </summary>
        /// <param name="secretKeyBytes">The symmetric key established, and having the necessary storage done.</param>
        /// <param name="request">The HTTP request that is coming in from Microsoft Graph.</param>
        /// <returns>A unit of execution that contains a type of <see cref="ConnectRequest"/>.</returns>
        public static async Task <ConnectRequest> DecryptConnectionRequest(
            byte[] secretKeyBytes,
            HttpRequest request)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            string decryptedRequestBody = null;

            // Step 1 - using a memory stream for the processing of the request.
            using (MemoryStream ms = new MemoryStream())
            {
                await request.Body.CopyToAsync(ms).ConfigureAwait(false);

                byte[] encryptedRequestBytes           = ms.ToArray();
                Aes256CbcHmacSha256Encryptor decryptor = new Aes256CbcHmacSha256Encryptor(secretKeyBytes);
                byte[] decryptedRequestBodyBytes       = decryptor.Decrypt(encryptedRequestBytes);
                decryptedRequestBody = Encoding.UTF8.GetString(decryptedRequestBodyBytes);
            }

            // Step 2 - Parse the decrypted request into the correct model.
            return(JsonConvert.DeserializeObject <ConnectRequest>(decryptedRequestBody));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method will properly decrypt the encrypted payload that is being received from Shifts.
        /// </summary>
        /// <param name="secretKeyBytes">The sharedSecret from Shifts casted into a byte array.</param>
        /// <param name="request">The incoming request from Shifts UI that contains an encrypted payload.</param>
        /// <returns>A unit of execution which contains the RequestModel.</returns>
        private static async Task <RequestModel> DecryptEncryptedRequestFromShiftsAsync(byte[] secretKeyBytes, HttpRequest request)
        {
            string decryptedRequestBody = null;

            // Step 1 - using a memory stream for the processing of the request.
            using (MemoryStream ms = new MemoryStream())
            {
                await request.Body.CopyToAsync(ms).ConfigureAwait(false);

                byte[] encryptedRequestBytes           = ms.ToArray();
                Aes256CbcHmacSha256Encryptor decryptor = new Aes256CbcHmacSha256Encryptor(secretKeyBytes);
                byte[] decryptedRequestBodyBytes       = decryptor.Decrypt(encryptedRequestBytes);
                decryptedRequestBody = Encoding.UTF8.GetString(decryptedRequestBodyBytes);
            }

            // Step 2 - Parse the decrypted request into the correct model.
            return(JsonConvert.DeserializeObject <RequestModel>(decryptedRequestBody));
        }
        public static async Task <T> ReadAsObjectAsync <T>(this HttpRequest request, string secret)
        {
            if (string.IsNullOrEmpty(secret))
            {
                return(JsonConvert.DeserializeObject <T>(await request.ReadAsStringAsync()));
            }

            var secretBytes = Encoding.UTF8.GetBytes(secret);
            var decryptor   = new Aes256CbcHmacSha256Encryptor(secretBytes);

            using (var ms = new MemoryStream())
            {
                await request.Body.CopyToAsync(ms);

                var decryptedPayload = decryptor.Decrypt(ms.ToArray());

                return(JsonConvert.DeserializeObject <T>(Encoding.UTF8.GetString(decryptedPayload)));
            }
        }