public AuthorizationBehavior(IAuthorizationNode authorization, IFubuRequestContext context, IAuthorizationFailureHandler failureHandler, SecuritySettings settings)
 {
     _authorization = authorization;
     _context = context;
     _failureHandler = failureHandler;
     _settings = settings;
 }
Exemple #2
0
        public IMedia <T> SelectMedia(CurrentMimeType mimeTypes, IFubuRequestContext context)
        {
            foreach (var acceptType in mimeTypes.AcceptTypes)
            {
                var candidates = Media.Where(x => x.Mimetypes.Contains(acceptType));
                if (candidates.Any())
                {
                    var writer = candidates.FirstOrDefault(x => x.MatchesRequest(context));
                    if (writer != null)
                    {
                        context.Logger.DebugMessage(() => new WriterChoice(acceptType, writer, writer.Condition));
                        return(writer);
                    }

                    context.Logger.DebugMessage(() => NoWritersMatch.For(acceptType, candidates));
                }
            }

            if (mimeTypes.AcceptsAny())
            {
                var media = Media.FirstOrDefault(x => x.MatchesRequest(context));
                context.Logger.DebugMessage(() => new WriterChoice(MimeType.Any.Value, media, media.Condition));

                return(media);
            }

            return(null);
        }
Exemple #3
0
        public T Read(string mimeType, IFubuRequestContext context)
        {
            var json   = context.Services.GetInstance <NewtonSoftJsonReader>().GetInputText();
            var values = new JObjectValues(json);

            return((T)context.Services.GetInstance <IObjectResolver>().BindModel(typeof(T), values).Value);
        }
 public AuthorizationBehavior(IAuthorizationNode authorization, IFubuRequestContext context,
     IAuthorizationFailureHandler failureHandler)
 {
     _authorization = authorization;
     _context = context;
     _failureHandler = failureHandler;
 }
Exemple #5
0
        public T Read <T>(IFubuRequestContext context)
        {
            var serializer = new XmlSerializer(typeof(T));
            var reader     = new StreamReader(context.Request.Input, true);

            return((T)serializer.Deserialize(reader));
        }
Exemple #6
0
 public AuthorizationBehavior(IAuthorizationNode authorization, IFubuRequestContext context, IAuthorizationFailureHandler failureHandler, SecuritySettings settings)
 {
     _authorization  = authorization;
     _context        = context;
     _failureHandler = failureHandler;
     _settings       = settings;
 }
        public void Write(string mimeType, IFubuRequestContext context, T resource)
        {
            var serializer = context.Services.GetInstance <IJsonSerializer>();
            var json       = serializer.Serialize(resource.ToDictionary());

            context.Writer.Write(mimeType, json);
        }
        protected static string serializeData <T>(IFubuRequestContext context, T target)
        {
            var serializer = context.Services.GetInstance <IJsonSerializer>();
            var text       = serializer.Serialize(target);

            return(text);
        }
Exemple #9
0
        public T Read <T>(IFubuRequestContext context)
        {
            var serializer = new JavaScriptSerializer {
                MaxJsonLength = int.MaxValue
            };

            return(serializer.Deserialize <T>(context.Request.InputText()));
        }
        public virtual AuthorizationRight IsAuthorized(IFubuRequestContext context,
            IEnumerable<IAuthorizationPolicy> policies)
        {
            var result = DetermineRights(context, policies);
            _logger.DebugMessage(() => new AuthorizationResult(result));

            return result;
        }
Exemple #11
0
        public Task <InputMessage> Read(string mimeType, IFubuRequestContext context)
        {
            // read the body of the http request from IHttpRequest
            // read header information and route information from
            // IHttpRequest

            return(new InputMessage().ToCompletionTask());
        }
Exemple #12
0
        public void Write(string mimeType, IFubuRequestContext context, T resource)
        {
            var view = BuildView(context);

            view.Page.ServiceLocator = context.Services;
            view.Page.As <IFubuPage <T> >().Model = resource;
            view.Render(context);
        }
Exemple #13
0
        public T Read(string mimeType, IFubuRequestContext context)
        {
            var model = _formatter.Read <T>(context);

            context.Services.GetInstance <IBindingContext>().BindProperties(model);

            return(model);
        }
Exemple #14
0
        public async Task <T> Read(string mimeType, IFubuRequestContext context)
        {
            var model = await _formatter.Read <T>(context).ConfigureAwait(false);

            context.Services.GetInstance <IBindingContext>().BindProperties(model);

            return(model);
        }
Exemple #15
0
        public virtual AuthorizationRight IsAuthorized(IFubuRequestContext context,
                                                       IEnumerable <IAuthorizationPolicy> policies)
        {
            var result = DetermineRights(context, policies);

            _logger.DebugMessage(() => new AuthorizationResult(result));

            return(result);
        }
Exemple #16
0
        public virtual void Write <T>(IFubuRequestContext context, T resource, string mimeType)
        {
            var serializer = new JavaScriptSerializer {
                MaxJsonLength = int.MaxValue
            };
            var text = serializer.Serialize(resource);

            context.Writer.Write(mimeType, text);
        }
Exemple #17
0
        public AuthorizationRight IsAuthorized(IFubuRequestContext context)
        {
            if (!_policies.Any())
            {
                return(AuthorizationRight.Allow);
            }

            return(AuthorizationRight.Combine(_policies.Select(x => x.RightsFor(context))));
        }
Exemple #18
0
        public async Task <T> Read <T>(IFubuRequestContext context)
        {
            var serializer = new XmlSerializer(typeof(T));
            var reader     = new StreamReader(context.Request.Input, true);

            var xml = await reader.ReadToEndAsync().ConfigureAwait(false);

            return((T)serializer.Deserialize(new XmlTextReader(new StringReader(xml))));
        }
Exemple #19
0
        public Task Write(string mimeType, IFubuRequestContext context, T resource)
        {
            var stringWriter = new StringWriter();
            var writer       = new JsonTextWriter(stringWriter);

            _serializer.Serialize(writer, resource);

            return(context.Writer.Write(mimeType, stringWriter.ToString()));
        }
Exemple #20
0
        public IRenderableView BuildView(IFubuRequestContext context)
        {
            if (context.Request.IsAjaxRequest() || context.Services.GetInstance <ICurrentChain>().IsInPartial())
            {
                return(_view.GetPartialView());
            }

            return(_view.GetView());
        }
        public ConditionalService(IServiceLocator services, IFubuRequestContext context)
        {
            _conditions = new Cache <Type, bool>(type =>
            {
                var condition = services.GetInstance(type).As <IConditional>();

                return(condition.ShouldExecute(context));
            });
        }
 public void SetUp()
 {
     ClassUnderTest = new JsonSerializer();
     theFubuRequestContext = MockRepository.GenerateMock<IFubuRequestContext>();
     theHttpRequest = MockRepository.GenerateMock<IHttpRequest>();
     theOutputWriter = new InMemoryOutputWriter();
     theFubuRequestContext.Stub(x => x.Request).Return(theHttpRequest);
     theFubuRequestContext.Stub(x => x.Writer).Return(theOutputWriter);
 }
Exemple #23
0
 public void SetUp()
 {
     ClassUnderTest        = new JsonSerializer();
     theFubuRequestContext = MockRepository.GenerateMock <IFubuRequestContext>();
     theHttpRequest        = MockRepository.GenerateMock <IHttpRequest>();
     theOutputWriter       = new InMemoryOutputWriter();
     theFubuRequestContext.Stub(x => x.Request).Return(theHttpRequest);
     theFubuRequestContext.Stub(x => x.Writer).Return(theOutputWriter);
 }
Exemple #24
0
        void IMediaWriter <T> .Write(string mimeType, IFubuRequestContext request, T resource)
        {
            var node    = new DictionaryMediaNode();
            var context = new ProjectionContext <T>(request.Services, new SimpleValues <T>(resource));

            write(context, node);

            request.Writer.Write(mimeType, JsonUtil.ToJson(node.Values));
        }
        public async Task <T> Read <T>(IFubuRequestContext context)
        {
            var serializer = context.Services.GetInstance <IJsonSerializer>();

            var reader = new StreamReader(context.Request.Input);
            var json   = await reader.ReadToEndAsync().ConfigureAwait(false);

            return(serializer.Deserialize <T>(json));
        }
        public ConditionalService(IServiceLocator services, IFubuRequestContext context)
        {
            _conditions = new Cache<Type, bool>(type =>
            {
                var condition = services.GetInstance(type).As<IConditional>();

                return condition.ShouldExecute(context);
            });
        }
        public AggregatedQuery Read(string mimeType, IFubuRequestContext context)
        {
            var messageTypes = context.Service <IClientMessageCache>();
            var serializer   = context.Service <NewtonSoftJsonSerializer>().InnerSerializer();

            var json = context.Request.Input.ReadAllText();


            return(Read(serializer, messageTypes, json));
        }
Exemple #28
0
        public async Task <AggregatedQuery> Read(string mimeType, IFubuRequestContext context)
        {
            var messageTypes = context.Service <IClientMessageCache>();
            var serializer   = context.Service <NewtonSoftJsonSerializer>().InnerSerializer();

            var reader = new StreamReader(context.Request.Input);
            var json   = await reader.ReadToEndAsync().ConfigureAwait(false);

            return(Read(serializer, messageTypes, json));
        }
Exemple #29
0
        public AuthorizationRight RightsFor(IFubuRequestContext request)
        {
            var customerId = request.Models.Get<Customer>().Id;
            var productId = request.Models.Get<Product>().Id;

            var hasPurchasedProduct = request.Service<IRepository>().Get<IPurchaseHistory>(customerId)
                .Any(x => x.ContainsProduct(productId));

            return !hasPurchasedProduct ? AuthorizationRight.Deny : AuthorizationRight.Allow;
        }
        public AuthorizationRight RightsFor(IFubuRequestContext request)
        {
            var customerId = request.Models.Get <Customer>().Id;
            var productId  = request.Models.Get <Product>().Id;

            var hasPurchasedProduct = request.Service <IRepository>().Get <IPurchaseHistory>(customerId)
                                      .Any(x => x.ContainsProduct(productId));

            return(!hasPurchasedProduct ? AuthorizationRight.Deny : AuthorizationRight.Allow);
        }
Exemple #31
0
        public async Task <T> Read(string mimeType, IFubuRequestContext context)
        {
            var json = await context.Services.GetInstance <NewtonSoftJsonReader>().GetInputText().ConfigureAwait(false);

            var values = new JObjectValues(json);

            var value = context.Services.GetInstance <IObjectResolver>().BindModel(typeof(T), values).Value.As <T>();

            return(value);
        }
Exemple #32
0
 // This signature is necessary because we are assuming
 // that some Writer's will be able to produce representations
 // for multiple mimetype's
 public void Write(string mimeType, IFubuRequestContext context, SomeResource resource)
 {
     if (mimeType == "special/format")
     {
         writeSpecial(resource);
     }
     else
     {
         writeJson(resource);
     }
 }
Exemple #33
0
 // This signature is necessary because we are assuming
 // that some Writer's will be able to produce representations
 // for multiple mimetype's
 public Task Write(string mimeType, IFubuRequestContext context, SomeResource resource)
 {
     if (mimeType == "special/format")
     {
         return(writeSpecial(resource));
     }
     else
     {
         return(writeJson(resource));
     }
 }
Exemple #34
0
        Task IMediaWriter <T> .Write(string mimeType, IFubuRequestContext request, T resource)
        {
            var node    = new DictionaryMediaNode();
            var context = new ProjectionContext <T>(request.Services, new SimpleValues <T>(resource));

            write(context, node);

            var serializer = request.Services.GetInstance <IJsonSerializer>();

            return(request.Writer.Write(mimeType, serializer.Serialize(node.Values, false)));
        }
Exemple #35
0
        public void Write <T>(IFubuRequestContext context, T target, string mimeType)
        {
            var serializer = new XmlSerializer(typeof(T));

            context.Writer.Write(mimeType, stream => {
                var xmlWriter = new XmlTextWriter(stream, Encoding.Unicode)
                {
                    Formatting = Formatting.None
                };

                serializer.Serialize(xmlWriter, target);
            });
        }
        public AuthorizationRight DetermineRights(IFubuRequestContext context, IEnumerable<IAuthorizationPolicy> policies)
        {
            // Check every authorization policy for this endpoint
            var rights = policies.Select(policy => {
                var policyRights = policy.RightsFor(context);

                _logger.DebugMessage(() => new AuthorizationPolicyResult(policy, policyRights));

                return policyRights;
            });

            // Combine the results
            var result = AuthorizationRight.Combine(rights);
            return result;
        }
Exemple #37
0
        public AuthorizationRight DetermineRights(IFubuRequestContext context, IEnumerable <IAuthorizationPolicy> policies)
        {
            // Check every authorization policy for this endpoint
            var rights = policies.Select(policy => {
                var policyRights = policy.RightsFor(context);

                _logger.DebugMessage(() => new AuthorizationPolicyResult(policy, policyRights));

                return(policyRights);
            });

            // Combine the results
            var result = AuthorizationRight.Combine(rights);

            return(result);
        }
        public AuthorizationRight RightsFor(IFubuRequestContext request)
        {
            var currentSdkUser = request.Service<ICurrentSDKUser>();
            var tokenRepository = request.Service<IAuthenticationTokenRepository>();
            var logger = request.Service<ILogger>();
            var authToken = request.Models.Get<AuthenticationTokenRequest>();

            //Workaround: RightsFor is getting called multiple times because of a Fubu bug
            if (request.Models.Has<IAuthenticationToken>()) return AuthorizationRight.Allow;

            var token = authToken.authToken;

            if (token.IsEmpty())
            {
                if (currentSdkUser.IsAuthenticated)
                {
                    logger.LogDebug("No AuthToken was found in this request but a user is already authenticated. Using the current user's credentials.");
                    return AuthorizationRight.Allow;
                }

                return AuthorizationRight.Deny;
            }

            logger.LogDebug("Authentication token {0} found.", token);

            var authenticationToken = tokenRepository.RetrieveByToken(token);
            if (authenticationToken == null)
            {
                return AuthorizationRight.Deny;

            }

            logger.LogDebug("Authentication token {0} found and validated for user {1}.", authenticationToken, authenticationToken);
            request.Models.Set(authenticationToken);

            currentSdkUser.SetUser(authenticationToken.Username);

            return AuthorizationRight.Allow;
        }
 public bool ShouldExecute(IFubuRequestContext context)
 {
     throw new NotImplementedException();
 }
Exemple #40
0
 public bool ShouldExecute(IFubuRequestContext context)
 {
     return true;
 }
 public AuthorizationRight RightsFor(IFubuRequestContext request)
 {
     var principal = Thread.CurrentPrincipal;
     return DetermineRights(principal);
 }
Exemple #42
0
 public AuthorizationRight RightsFor(IFubuRequestContext request)
 {
     return PrincipalRoles.IsInRole(_role) ? AuthorizationRight.Allow : AuthorizationRight.None;
 }
 public bool ShouldExecute(IFubuRequestContext context)
 {
     return _condition();
 }
Exemple #44
0
 public ChainAuthorizor(IFubuRequestContext context, ITypeResolver types)
 {
     _context = context;
     _types = types;
 }
Exemple #45
0
 public AuthorizationRight RightsFor(IFubuRequestContext request)
 {
     return AuthorizationRight.Deny;
 }
 public AuthorizationRight RightsFor(IFubuRequestContext request)
 {
     throw new NotImplementedException();
 }
 public AuthorizationRight RightsFor(IFubuRequestContext request)
 {
     return AuthorizationCheck.IsAuthorized ? AuthorizationRight.Allow : AuthorizationRight.Deny;
 }