Example #1
0
        public async Task ShouldReceive200AndValueContentWithAuthorization()
        {
            var client = new HttpClient();

            var url    = string.Concat(_address, "/api/values/5");
            var result = await client.GetAsync(url);

            var wwwAuthenticate = result.Headers.WwwAuthenticate.First();

            Assert.AreEqual(HttpStatusCode.Unauthorized, result.StatusCode);
            StringAssert.Equals(wwwAuthenticate.Scheme, "AuthAPI");

            var responsePayload = new ResponsePayload(wwwAuthenticate.Parameter);

            var requestPayload = new RequestPayload
            {
                ClientId     = "TestAPI",
                Identifier   = responsePayload.Identifier,
                RequestCount = string.Format("{0:D8}", int.Parse(responsePayload.RequestCount) + 1),
                UserName     = "******"
            };

            var dataPayload = new DataPayload
            {
                ClientId          = "TestAPI",
                Method            = "GET",
                Password          = "******",
                RequestBodyBase64 = string.Empty,
                RequestURI        = "/api/values/5",
                UserName          = "******"
            };

            var authHeader = new AuthHeader
            {
                Data    = dataPayload,
                Request = requestPayload
            };


            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("AuthAPI", authHeader.ToAuthorizationHeader("QiU6bSt3anE2OURfX3IsKlVZen05K1tBLW5AQ1x1d0xIXVZwaGE7Zj83QTc0ZXthVy9aWV9UZ0tUcnRUVEQ6d2JxTEhGOi9fMitBfiNZOS5NXHlyJzNnNSl1VzxNQExkQXtHJEQ+fWElMkMhWUJhLT8kbUFeQERWa310J2N+NkQ="));

            result = await client.GetAsync(url);

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.AreEqual(await result.Content.ReadAsAsync <string>(), "value");
        }
Example #2
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            try
            {
                if (_authHeader != null)
                {
                    _authHeader.Request.RequestCount = string.Format("{0:D8}", int.Parse(_authHeader.Request.RequestCount) + 1);

                    _authHeader.Data = new DataPayload
                    {
                        ClientId          = AuthAPIConfiguration.Instance.ClientId,
                        Method            = request.Method.Method,
                        Password          = _password,
                        RequestBodyBase64 = request.Content != null?Convert.ToBase64String(await request.Content.ReadAsByteArrayAsync()) : string.Empty,
                                                RequestURI = request.RequestUri.PathAndQuery,
                                                UserName   = _userName
                    };

                    request.Headers
                    .Authorization = new AuthenticationHeaderValue(AuthAPIConfiguration.Instance.ClientId,
                                                                   _authHeader.ToAuthorizationHeader(AuthAPIConfiguration.Instance.ClientSecret));
                }

                return(await base.SendAsync(request, cancellationToken)
                       .ContinueWith(async task =>
                {
                    var response = task.Result;
                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        var wwwAuthenticate = response.Headers.WwwAuthenticate.FirstOrDefault();

                        if (wwwAuthenticate != null && wwwAuthenticate.Scheme.Equals("AuthAPI"))
                        {
                            var responsePayload = new ResponsePayload(wwwAuthenticate.Parameter);

                            var requestPayload = new RequestPayload
                            {
                                ClientId = AuthAPIConfiguration.Instance.ClientId,
                                Identifier = responsePayload.Identifier,
                                RequestCount = string.Format("{0:D8}", int.Parse(responsePayload.RequestCount) + 1),
                                UserName = _userName
                            };

                            var dataPayload = new DataPayload
                            {
                                ClientId = AuthAPIConfiguration.Instance.ClientId,
                                Method = request.Method.Method,
                                Password = _password,
                                RequestBodyBase64 = request.Content != null ? Convert.ToBase64String(await request.Content.ReadAsByteArrayAsync()) : string.Empty,
                                RequestURI = request.RequestUri.PathAndQuery,
                                UserName = _userName
                            };

                            var authHeader = new AuthHeader
                            {
                                Data = dataPayload,
                                Request = requestPayload
                            };

                            _tempAuthHeader = authHeader;

                            request.Headers.Authorization = new AuthenticationHeaderValue("AuthAPI", _tempAuthHeader.ToAuthorizationHeader(AuthAPIConfiguration.Instance.ClientSecret));

                            response = await ReplayRequest(request, cancellationToken);

                            if (response.IsSuccessStatusCode)
                            {
                                _authHeader = _tempAuthHeader;
                            }
                        }
                    }
                    else if (response.IsSuccessStatusCode)
                    {
                        if (_tempAuthHeader != null)
                        {
                            _authHeader = _tempAuthHeader;
                        }
                    }

                    return response;
                }).Unwrap());
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #3
0
        public async Task ShouldReceive200OnPost()
        {
            var client = new HttpClient();

            var valuesModel = new ValuesModel
            {
                Id   = 1,
                Name = "oi"
            };
            var url    = string.Concat(_address, "/api/values");
            var result = await client.PostAsJsonAsync <ValuesModel>(url, valuesModel);

            var wwwAuthenticate = result.Headers.WwwAuthenticate.First();

            Assert.AreEqual(HttpStatusCode.Unauthorized, result.StatusCode);
            StringAssert.Equals(wwwAuthenticate.Scheme, "AuthAPI");

            var responsePayload = new ResponsePayload(wwwAuthenticate.Parameter);

            var requestPayload = new RequestPayload
            {
                ClientId     = "TestAPI",
                Identifier   = responsePayload.Identifier,
                RequestCount = string.Format("{0:D8}", int.Parse(responsePayload.RequestCount) + 1),
                UserName     = "******"
            };

            var json = JsonConvert.SerializeObject(valuesModel);

            var dataPayload = new DataPayload
            {
                ClientId          = "TestAPI",
                Method            = "POST",
                Password          = "******",
                RequestBodyBase64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(valuesModel))),
                RequestURI        = "/api/values",
                UserName          = "******"
            };

            var authHeader = new AuthHeader
            {
                Data    = dataPayload,
                Request = requestPayload
            };

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("AuthAPI", authHeader.ToAuthorizationHeader("QiU6bSt3anE2OURfX3IsKlVZen05K1tBLW5AQ1x1d0xIXVZwaGE7Zj83QTc0ZXthVy9aWV9UZ0tUcnRUVEQ6d2JxTEhGOi9fMitBfiNZOS5NXHlyJzNnNSl1VzxNQExkQXtHJEQ+fWElMkMhWUJhLT8kbUFeQERWa310J2N+NkQ="));

            result = result = await client.PostAsJsonAsync <ValuesModel>(url, valuesModel);

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        }