Ejemplo n.º 1
0
        /// <summary>
        /// This method supports the framework directly and should not be used from your code
        /// </summary>
        /// <param name="handler">The handler.</param>
        /// <param name="context">The context.</param>
        /// <returns><c>true</c> if the user is authenticated; otherwise, <c>false</c>.</returns>
        public static bool Impl(IRequireAuthentication handler, IContext context)
        {
            var authenticationProvider = SimpleWeb.Configuration.AuthenticationProvider ?? new DefaultAuthenticationProvider();
            var user = authenticationProvider.GetLoggedInUser(context);

            if (user == null || !user.IsAuthenticated)
            {
                if (SimpleWeb.Configuration.LoginPage != null)
                {
                    var uriTemplateAttribute = UriTemplateAttribute.Get(SimpleWeb.Configuration.LoginPage).FirstOrDefault();
                    if (uriTemplateAttribute != null)
                    {
                        var redirect = uriTemplateAttribute.Template + "?returnUrl=" + Uri.EscapeDataString(context.Request.Url.ToString());
                        context.Response.SetHeader("Location", redirect);
                        context.Response.Status = Status.TemporaryRedirect(redirect);
                        return(false);
                    }
                }
                context.Response.Status = "401 Unauthorized";
                return(false);
            }

            handler.CurrentUser = user;
            return(true);
        }
Ejemplo n.º 2
0
        private void PopulateRoutingTableWithHandlers(RoutingTable routingTable, IEnumerable <Type> handlerTypes)
        {
            foreach (var exportedType in handlerTypes)
            {
                var respondsWithTypes = RespondsWithAttribute.Get(exportedType).SelectMany(rta => rta.ContentTypes).ToList();
                var respondsToTypes   = RespondsToAttribute.Get(exportedType).SelectMany(rta => rta.ContentTypes).ToList();
                foreach (var uriTemplate in UriTemplateAttribute.GetAllTemplates(exportedType))
                {
                    if (exportedType.IsGenericTypeDefinition)
                    {
                        BuildRoutesForGenericHandlerType(routingTable, exportedType, uriTemplate, respondsToTypes, respondsWithTypes);
                    }
                    else
                    {
                        routingTable.Add(uriTemplate, new HandlerTypeInfo(exportedType, respondsToTypes, respondsWithTypes));
                    }
                }

                // If it's the LoginPage, set it to the configuration
                if (SimpleWeb.Configuration.LoginPage == null && typeof(ILoginPage).IsAssignableFrom(exportedType))
                {
                    SimpleWeb.Configuration.LoginPage = exportedType;
                }
            }
        }
        public void BuildsCompundUriTemplate()
        {
            var actual = UriTemplateAttribute.GetAllTemplates(typeof(CompoundTest)).ToList();

            Assert.Equal(4, actual.Count);
            Assert.Contains("/foo/quux/", actual);
            Assert.Contains("/foo/wibble/", actual);
            Assert.Contains("/bar/quux/", actual);
            Assert.Contains("/bar/wibble/", actual);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the URI for a handler with template parameters.
        /// </summary>
        /// <typeparam name="THandler">The type of the handler.</typeparam>
        /// <param name="expression">An expression specifying values for properties on the Handler.</param>
        /// <returns>A URI with the necessary values incorporated.</returns>
        public static Uri Get <THandler>(Expression <Func <THandler> > expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            var memberInitExpression = expression.Body as MemberInitExpression;

            if (memberInitExpression == null)
            {
                throw new ArgumentException("Expression must be a member initializer.");
            }

            var values = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            foreach (var memberAssignment in memberInitExpression.Bindings.OfType <MemberAssignment>())
            {
                object value;
                if (ExpressionHelper.TryGetValue(memberAssignment.Expression, out value))
                {
                    values.Add(memberAssignment.Member.Name, value);
                }
                else
                {
                    throw new ArgumentException("Cannot resolve values from member initializer. Make sure you are not calling a function within the expression.");
                }
            }

            var uriTemplateAttributes = UriTemplateAttribute.Get(typeof(THandler)).ToArray();

            AssertAtLeastOne(uriTemplateAttributes);

            foreach (var uriTemplateAttribute in uriTemplateAttributes)
            {
                var template  = uriTemplateAttribute.Template;
                var variables = new HashSet <string>(UriTemplateHelper.ExtractVariableNames(template), StringComparer.OrdinalIgnoreCase);
                if (variables.All(values.ContainsKey))
                {
                    var uri = uriTemplateAttribute.Template;
                    foreach (var variable in variables)
                    {
                        uri = uri.Replace("{" + variable + "}", (values[variable] ?? string.Empty).ToString());
                    }
                    return(new Uri(uri, UriKind.Relative));
                }
            }

            throw new InvalidOperationException("Cannot find matching Uri template.");
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the URI for a handler without template parameters.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>The URI.</returns>
        public static Uri Get(Type type)
        {
            var uriTemplateAttributes = UriTemplateAttribute.Get(type).ToArray();

            AssertAtLeastOne(uriTemplateAttributes);
            AssertSingle(uriTemplateAttributes);

            var template = uriTemplateAttributes[0].Template;

            if (template.Contains("{"))
            {
                throw new InvalidOperationException("UriTemplateAttribute includes variables. Use the Get<THandler>(Expression<Func<T>>) override.");
            }

            return(new Uri(template, UriKind.Relative));
        }
Ejemplo n.º 6
0
        private static Link CreateLink(Type type, LinkAttributeBase linkAttribute)
        {
            string uriTemplate;

            if (string.IsNullOrWhiteSpace(linkAttribute.UriTemplate))
            {
                try
                {
                    uriTemplate = UriTemplateAttribute.GetAllTemplates(type).Single();
                }
                catch (InvalidOperationException)
                {
                    throw new InvalidOperationException("Must specify a UriTemplate for LinkAttribute where more than one UriTemplateAttribute is used.");
                }
            }
            else
            {
                uriTemplate = linkAttribute.UriTemplate;
            }

            return(new Link(type, uriTemplate, linkAttribute.GetRel(), linkAttribute.Type, linkAttribute.Title));
        }
Ejemplo n.º 7
0
        internal static void Redirect(IContext context)
        {
            var accept = context.Request.GetAccept();

            if (accept.Contains(MediaType.Html) || accept.Contains(MediaType.XHtml))
            {
                if (SimpleWeb.Configuration.LoginPage != null)
                {
                    var uriTemplateAttribute =
                        UriTemplateAttribute.Get(SimpleWeb.Configuration.LoginPage).FirstOrDefault();
                    if (uriTemplateAttribute != null)
                    {
                        var redirect = uriTemplateAttribute.Template + "?returnUrl=" +
                                       Uri.EscapeDataString(context.Request.Url.ToString());
                        context.Response.SetHeader("Location", redirect);
                        context.Response.Status = Status.TemporaryRedirect(redirect);
                        return;
                    }
                }
            }
            context.Response.Status = "401 Unauthorized";
        }
        public void OverrideExcludesBase()
        {
            var actual = UriTemplateAttribute.GetAllTemplates(typeof(OverrideBaseTest)).Single();

            Assert.Equal("/override", actual);
        }
        public void StillFindsSingleTemplate()
        {
            var actual = UriTemplateAttribute.GetAllTemplates(typeof(NoBaseTest)).Single();

            Assert.Equal("/rock", actual);
        }
Ejemplo n.º 10
0
        public void SkipsMiddleBase()
        {
            var actual = UriTemplateAttribute.GetAllTemplates(typeof(Bottom)).Single();

            Assert.Equal("/top/bottom", actual);
        }