protected override async Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            currentNumberOfAttempts += 1;
            var version   = request.GetAuthHeaderValue().GetVersionFromAuthenticationHeader();
            var mAuthCore = MAuthCoreFactory.Instantiate(version);

            if (currentNumberOfAttempts < SucceedAfterThisManyAttempts)
            {
                return(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable));
            }
            var authInfo = MAuthAuthenticator.GetAuthenticationInfo(request, mAuthCore);

            if (!mAuthCore.Verify(authInfo.Payload,
                                  await mAuthCore.GetSignature(request, authInfo),
                                  TestExtensions.ServerPublicKey
                                  ))
            {
                return new HttpResponseMessage(HttpStatusCode.Unauthorized)
                       {
                           RequestMessage = request
                       }
            }
            ;

            if (!request.RequestUri.AbsolutePath.Equals(
                    $"{Constants.MAuthTokenRequestPath}{clientUuid.ToHyphenString()}.json",
                    StringComparison.OrdinalIgnoreCase))
            {
                return new HttpResponseMessage(HttpStatusCode.NotFound)
                       {
                           RequestMessage = request
                       }
            }
            ;

            return(new HttpResponseMessage(HttpStatusCode.OK)
            {
                RequestMessage = request,
                Content = new StringContent(
                    JsonConvert.SerializeObject(new
                {
                    security_token = new ApplicationInfo()
                    {
                        Uuid = clientUuid,
                        Name = "Medidata.MAuth.Tests",
                        CreationDate = new DateTimeOffset(2016, 8, 1, 0, 0, 0, TimeSpan.Zero),
                        PublicKey = TestExtensions.ClientPublicKey
                    }
                })
                    )
            });
        }
    }
}
        public static async Task GetAuthenticationInfo_WithSignedRequest_ForMWSVersion_WillReturnCorrectAuthInfo(string method)
        {
            // Arrange
            var testData = await method.FromResource();

            var version     = MAuthVersion.MWS;
            var testOptions = TestExtensions.ServerOptions;
            var mAuthCore   = MAuthCoreFactory.Instantiate(version);

            // Act
            var actual = MAuthAuthenticator.GetAuthenticationInfo(testData.ToHttpRequestMessage(version), mAuthCore);

            // Assert
            Assert.Equal(testData.ApplicationUuid, actual.ApplicationUuid);
            Assert.Equal(Convert.FromBase64String(testData.Payload), actual.Payload);
            Assert.Equal(testData.SignedTime, actual.SignedTime);
        }
        public static HttpRequestMessage ToHttpRequestMessage(this RequestData data, MAuthVersion version = MAuthVersion.MWS)
        {
            var result = new HttpRequestMessage(new HttpMethod(data.Method), data.Url)
            {
                Content = !string.IsNullOrEmpty(data.Base64Content) ?
                          new ByteArrayContent(Convert.FromBase64String(data.Base64Content)) :
                          null,
            };
            var mAuthCore   = MAuthCoreFactory.Instantiate(version);
            var headerKeys  = mAuthCore.GetHeaderKeys();
            var mauthHeader = version == MAuthVersion.MWS
                ? $"{version} {data.ApplicationUuidString}:{data.Payload}"
                : $"{version} {data.ApplicationUuidString}:{data.Payload};";

            result.Headers.Add(headerKeys.mAuthHeaderKey, mauthHeader);
            result.Headers.Add(headerKeys.mAuthTimeHeaderKey, data.SignedTimeUnixSeconds.ToString());

            return(result);
        }