Beispiel #1
0
        internal static IDictionary <string, string[]> GetQuery(OwinRequest request)
        {
            var query = request.Get <IDictionary <string, string[]> >("Microsoft.Owin.Query#dictionary");

            if (query == null)
            {
                query = new Dictionary <string, string[]>(StringComparer.OrdinalIgnoreCase);
                request.Set("Microsoft.Owin.Query#dictionary", query);
            }

            string text = request.QueryString.Value;

            if (request.Get <string>("Microsoft.Owin.Query#text") != text)
            {
                query.Clear();
                var accumulator = new Dictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase);
                ParseDelimited(text, AmpersandAndSemicolon, AppendItemCallback, accumulator);
                foreach (var kv in accumulator)
                {
                    query.Add(kv.Key, kv.Value.ToArray());
                }
                request.Set("Microsoft.Owin.Query#text", text);
            }
            return(query);
        }
Beispiel #2
0
        /// <summary>
        /// Reads and parses the request body as a form.
        /// </summary>
        /// <returns>The parsed form data.</returns>
        internal static IFormCollection ReadForm(this OwinRequest request)
        {
            var form = request.Get <IFormCollection>("Microsoft.Owin.Form#collection");

            if (form == null)
            {
                request.Body.Seek(0, SeekOrigin.Begin);

                string text;

                // Don't close, it prevents re-winding.
                using (var reader = new StreamReader(request.Body, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 4 * 1024, leaveOpen: true))
                {
                    text = reader.ReadToEnd();
                }

                // re-wind for the next caller
                request.Body.Seek(0, SeekOrigin.Begin);

                form = OwinHelpers.GetForm(text);
                request.Set("Microsoft.Owin.Form#collection", form);
            }

            return(form);
        }
Beispiel #3
0
        public void ShouldSkipAuthOnWrongAuthScheme()
        {
            var builder = new AppBuilderFactory().Create();

            var         context = new OwinContext();
            OwinRequest request = (OwinRequest)context.Request;

            request.Set <Action <Action <object>, object> >("server.OnSendingHeaders", RegisterForOnSendingHeaders);
            request.Method = "get";
            request.SetUri(new Uri("http://example.com:8080/resource/4?filter=a"));
            request.SetHeader("Authorization", new string[] { "Basic " });

            var response = context.Response;

            var middleware = new HawkAuthenticationMiddleware(
                new AppFuncTransition((env) =>
            {
                response.StatusCode = 200;
                return(Task.FromResult <object>(null));
            }),
                builder,
                new HawkAuthenticationOptions
            {
                Credentials = GetCredential
            }
                );

            middleware.Invoke(context);

            Assert.IsNotNull(response);
            Assert.AreEqual(200, response.StatusCode);
        }
Beispiel #4
0
        public static object RegisterAuthenticationHandler(this OwinRequest request, AuthenticationHandler handler)
        {
            var chained = request.Get <AuthenticateDelegate>(Constants.SecurityAuthenticate);
            var hook    = new Hook(handler, chained);

            request.Set <AuthenticateDelegate>(Constants.SecurityAuthenticate, hook.Authenticate);
            return(hook);
        }
    public static IDictionary<string, string> GetCookies(OwinRequest request)
    {
        var cookies = request.Get<IDictionary<string, string>>("Owin.Types.Cookies#dictionary");
            if (cookies == null)
            {
                cookies = new Dictionary<string, string>(StringComparer.Ordinal);
                request.Set("Owin.Types.Cookies#dictionary", cookies);
            }

            var text = request.GetHeader("Cookie");
            if (request.Get<string>("Owin.Types.Cookies#text") != text)
            {
                cookies.Clear();
                ParseDelimited(text, SemicolonAndComma, AddCookieCallback, cookies);
                request.Set("Owin.Types.Cookies#text", text);
            }
            return cookies;
    }
Beispiel #6
0
        public static void UnregisterAuthenticationHandler(this OwinRequest request, object registration)
        {
            var hook = registration as Hook;

            if (hook == null)
            {
                throw new InvalidOperationException(Resources.Exception_UnhookAuthenticationStateType);
            }
            request.Set(Constants.SecurityAuthenticate, hook.Chained);
        }
        public static IDictionary <string, string[]> GetQuery(OwinRequest request)
        {
            var query = request.Get <IDictionary <string, string[]> >("Owin.Types.Query#dictionary");

            if (query == null)
            {
                query = new Dictionary <string, string[]>(StringComparer.Ordinal);
                request.Set("Owin.Types.Query#dictionary", query);
            }

            var text = request.QueryString;

            if (request.Get <string>("Owin.Types.Query#text") != text)
            {
                query.Clear();
                ParseDelimited(text, AmpersandAndSemicolon, AddQueryCallback, query);
                request.Set("Owin.Types.Query#text", text);
            }
            return(query);
        }
Beispiel #8
0
        public static IDictionary <string, string> GetCookies(OwinRequest request)
        {
            var cookies = request.Get <IDictionary <string, string> >("Owin.Types.Cookies#dictionary");

            if (cookies == null)
            {
                cookies = new Dictionary <string, string>(StringComparer.Ordinal);
                request.Set("Owin.Types.Cookies#dictionary", cookies);
            }

            var text = request.GetHeader("Cookie");

            if (request.Get <string>("Owin.Types.Cookies#text") != text)
            {
                cookies.Clear();
                ParseDelimited(text, SemicolonAndComma, AddCookieCallback, cookies);
                request.Set("Owin.Types.Cookies#text", text);
            }
            return(cookies);
        }
Beispiel #9
0
        public static void SetHeader(this OwinRequest request, string name, string[] values)
        {
            var headers = request.Get <IDictionary <string, string[]> >("owin.RequestHeaders");

            if (headers == null)
            {
                headers = new Dictionary <string, string[]>();
                request.Set <IDictionary <string, string[]> >("owin.RequestHeaders", headers);
            }

            headers.Add(name, values);
        }
Beispiel #10
0
        /// <summary>
        /// Creates request OWIN respresentation.
        /// </summary>
        /// <param name="method">The request method.</param>
        /// <param name="scheme">The request scheme.</param>
        /// <param name="pathBase">The request base path.</param>
        /// <param name="path">The request path.</param>
        /// <param name="headers">The request headers.</param>
        /// <param name="queryString">The request query string</param>
        /// <param name="requestBody">The body of request.</param>
        /// <returns>OWIN representation for provided request parameters.</returns>
        private static Dictionary <string, object> CreateOwinEnvironment(string method, string scheme, string pathBase,
                                                                         string path, IDictionary <string, string[]> headers, string queryString = "", byte[] requestBody = null)
        {
            var environment = new Dictionary <string, object>(StringComparer.Ordinal);

            environment[CommonOwinKeys.OwinCallCancelled] = new CancellationToken();

            #region OWIN request params

            var request = new OwinRequest(environment)
            {
                Method      = method,
                Scheme      = scheme,
                Path        = new PathString(path),
                PathBase    = new PathString(pathBase),
                QueryString = new QueryString(queryString),
                Body        = new MemoryStream(requestBody ?? new byte[0]),
                Protocol    = Protocols.Http1
            };

            // request.Headers is readonly
            request.Set(CommonOwinKeys.RequestHeaders, headers);

            #endregion


            #region set default OWIN response params

            var response = new OwinResponse(environment)
            {
                Body = new MemoryStream(), StatusCode = StatusCode.Code200Ok
            };
            //response.Headers is readonly
            response.Set(CommonOwinKeys.ResponseHeaders, new Dictionary <string, string[]>(StringComparer.OrdinalIgnoreCase));

            #endregion


            return(environment);
        }