示例#1
0
        /// <summary>
        /// Bind the incoming request to a model and validate
        /// </summary>
        /// <typeparam name="TModel">Model type</typeparam>
        /// <param name="module">Current module</param>
        /// <returns>Bound model instance</returns>
        /// <remarks><see cref="ModelValidationResult"/> is stored in NancyModule.ModelValidationResult and NancyContext.ModelValidationResult.</remarks>
        public static TModel BindAndValidate <TModel>(this INancyModule module)
        {
            var model = module.Bind <TModel>(NoBlacklistedProperties);

            module.Validate(model);
            return(model);
        }
示例#2
0
        /// <summary>
        /// Bind the incoming request to a model and validate
        /// </summary>
        /// <typeparam name="TModel">Model type</typeparam>
        /// <param name="module">Current module</param>
        /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
        /// <param name="blacklistedProperties">Expressions that tell which property should be ignored</param>
        /// <example>this.Bind&lt;Person&gt;(p =&gt; p.Name, p =&gt; p.Age)</example>
        /// <returns>Bound model instance</returns>
        /// <remarks><see cref="ModelValidationResult"/> is stored in NancyModule.ModelValidationResult and NancyContext.ModelValidationResult.</remarks>
        public static TModel BindAndValidate <TModel>(this INancyModule module, BindingConfig configuration, params Expression <Func <TModel, object> >[] blacklistedProperties)
        {
            var model = module.Bind <TModel>(configuration, blacklistedProperties.ParseBlacklistedPropertiesExpressionTree());

            module.Validate(model);
            return(model);
        }
示例#3
0
        public dynamic Execute(dynamic parameters, INancyModule module)
        {
            SignInRequest signInRequest;

            try
            {
                signInRequest = module.Bind <SignInRequest>();
            }
            catch (Exception ex)
            {
                return(null);
            }


            var userIdentity = UserDatabase.ValidateUser(signInRequest);

            if (userIdentity == null)
            {
                return(HttpStatusCode.Unauthorized);
            }

            var token = _tokenizer.Tokenize(userIdentity, module.Context);

            return(new
            {
                Token = token
            });
        }
示例#4
0
        /// <summary>
        /// Bind the incoming request to a model and validate
        /// </summary>
        /// <typeparam name="TModel">Model type</typeparam>
        /// <param name="module">Current module</param>
        /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
        /// <returns>Bound model instance</returns>
        /// <remarks><see cref="ModelValidationResult"/> is stored in NancyModule.ModelValidationResult and NancyContext.ModelValidationResult.</remarks>
        public static TModel BindAndValidate <TModel>(this INancyModule module, params string[] blacklistedProperties)
        {
            var model = module.Bind <TModel>(blacklistedProperties);

            module.Validate(model);
            return(model);
        }
示例#5
0
        /// <summary>
        /// Bind the incoming request to a model and validate
        /// </summary>
        /// <typeparam name="TModel">Model type</typeparam>
        /// <param name="module">Current module</param>
        /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
        /// <returns>Bound model instance</returns>
        /// <remarks><see cref="ModelValidationResult"/> is stored in NancyModule.ModelValidationResult and NancyContext.ModelValidationResult.</remarks>
        public static TModel BindAndValidate <TModel>(this INancyModule module, BindingConfig configuration)
        {
            var model = module.Bind <TModel>(configuration, NoBlacklistedProperties);

            module.Validate(model);
            return(model);
        }
示例#6
0
        public static PatchResult Patch <T>(this INancyModule module, T target)
        {
            var config = new BindingConfig
            {
                Overwrite    = false,
                IgnoreErrors = true
            };
            var boundModel        = module.Bind <T>(config);
            var propertiesToMerge = ExtractPropertiesToMerge(module.Request);

            return(new PatchExecutor().Patch(boundModel, target, propertiesToMerge));
        }
示例#7
0
        public dynamic Execute(dynamic parameters, INancyModule module)
        {
            module.RequiresAuthentication();

            IFeed feed;

            try
            {
                int feedId = int.Parse(parameters.id);

                feed = module.Bind <Feed>();

                if (feedId != feed.Id)
                {
                    return(HttpStatusCode.BadRequest);
                }

                ITransaction transaction = _store.BeginTransaction();

                var existingFeedExists =
                    transaction.Query <IFeed>().Where("Id = @feedId").Parameter("feedId", feedId).First();

                if (existingFeedExists == null)
                {
                    return(HttpStatusCode.NotFound);
                }

                if (!string.IsNullOrWhiteSpace(feed.ApiKey))
                {
                    ICryptoService cryptoService = new PBKDF2();

                    feed.ApiKeySalt   = cryptoService.GenerateSalt();
                    feed.ApiKeyHashed = cryptoService.Compute(feed.ApiKey);
                }
                else if (feed.HasApiKey)
                {
                    feed.ApiKeyHashed = existingFeedExists.ApiKeyHashed; //Temporary until API Key table is used
                    feed.ApiKeySalt   = existingFeedExists.ApiKeySalt;   //Temporary until API Key table is used
                }

                transaction.Update(feed);
                transaction.Commit();
                transaction.Dispose();
            }
            catch (Exception ex)
            {
                return(HttpStatusCode.InternalServerError);
            }


            return(feed);
        }
示例#8
0
        public static T TryBind <T>(this INancyModule t)
        {
            try
            {
                return(t.Bind <T>());
            }
            catch (Exception ex)
            {
#if DEBUG
                throw ex;
#else
                return(default(T));
#endif
            }
        }
示例#9
0
        public dynamic Execute(dynamic parameters, INancyModule module)
        {
            module.RequiresAuthentication();

            IFeedConfiguration feedConfig;

            try
            {
                int feedId = int.Parse(parameters.id);

                feedConfig = module.Bind <FeedConfiguration>();

                if (feedId != feedConfig.FeedId)
                {
                    return(HttpStatusCode.BadRequest);
                }

                ITransaction transaction = _store.BeginTransaction();

                var existingFeedExists = transaction.Query <IFeedConfiguration>().Where("FeedId = @feedId").Parameter("feedId", feedId).Count() > 0;

                if (!existingFeedExists)
                {
                    return(HttpStatusCode.NotFound);
                }

                transaction.Update(feedConfig);
                transaction.Commit();
                transaction.Dispose();
            }
            catch (Exception ex)
            {
                return(HttpStatusCode.InternalServerError);
            }


            return(feedConfig);
        }
示例#10
0
 public virtual T CreateCommand(INancyModule module)
 {
     return(module.Bind <T>());
 }
示例#11
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <typeparam name="TModel">Model type</typeparam>
 /// <param name="module">Current module</param>
 /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
 /// <returns>Bound model instance</returns>
 public static TModel Bind <TModel>(this INancyModule module, params string[] blacklistedProperties)
 {
     return(module.Bind(blacklistedProperties));
 }
示例#12
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <typeparam name="TModel">Model type</typeparam>
 /// <param name="module">Current module</param>
 /// <param name="blacklistedProperties">Expressions that tell which property should be ignored</param>
 /// <example>this.Bind&lt;Person&gt;(p =&gt; p.Name, p =&gt; p.Age)</example>
 /// <returns>Bound model instance</returns>
 public static TModel Bind <TModel>(this INancyModule module, params Expression <Func <TModel, object> >[] blacklistedProperties)
 {
     return(module.Bind <TModel>(blacklistedProperties.ParseBlacklistedPropertiesExpressionTree()));
 }
示例#13
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <param name="module">Current module</param>
 /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
 /// <returns>Model adapter - cast to a model type to bind it</returns>
 public static dynamic Bind(this INancyModule module, params string[] blacklistedProperties)
 {
     return(module.Bind(BindingConfig.Default, blacklistedProperties));
 }
示例#14
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <typeparam name="TModel">Model type</typeparam>
 /// <param name="module">Current module</param>
 /// <returns>Bound model instance</returns>
 public static TModel Bind <TModel>(this INancyModule module)
 {
     return(module.Bind());
 }
        private static async Task <Response> GetResponse(INancyModule module, NancyContext ctx)
        {
            MicrosoftAppId = MicrosoftAppId ??
                             ConfigurationManager.AppSettings[MicrosoftAppIdSettingName ?? "MicrosoftAppId"];

            if (Debugger.IsAttached && string.IsNullOrEmpty(MicrosoftAppId))
            {
                // then auth is disabled
                return(null);
            }

            var tokenExtractor =
                new JwtTokenExtractor(JwtConfig.GetToBotFromChannelTokenValidationParameters(MicrosoftAppId),
                                      OpenIdConfigurationUrl);

            var identity = await tokenExtractor.GetIdentityAsync(ctx.Request.Headers.Authorization);

            // No identity? If we're allowed to, fall back to MSA
            // This code path is used by the emulator
            if (identity == null && !DisableSelfIssuedTokens)
            {
                tokenExtractor = new JwtTokenExtractor(JwtConfig.ToBotFromMSATokenValidationParameters,
                                                       JwtConfig.ToBotFromMSAOpenIdMetadataUrl);

                identity = await tokenExtractor.GetIdentityAsync(ctx.Request.Headers.Authorization);

                // Check to make sure the app ID in the token is ours
                if (identity != null)
                {
                    // If it doesn't match, throw away the identity
                    if (tokenExtractor.GetBotIdFromClaimsIdentity(identity) != MicrosoftAppId)
                    {
                        identity = null;
                    }
                }
            }

            // Still no identity? Fail out.
            if (identity == null)
            {
                //https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Library/Microsoft.Bot.Connector/JwtTokenExtractor.cs#L87
                //tokenExtractor.GenerateUnauthorizedResponse(actionContext);
                return(new Response
                {
                    StatusCode = HttpStatusCode.Unauthorized,
                    Headers =
                        new Dictionary <string, string>()
                    {
                        { "WWW-Authenticate", $"Bearer realm=\"{ctx.Request.Url.HostName}\"" }
                    }
                });
            }

            var activity = module.Bind <Activity>();

            if (!string.IsNullOrWhiteSpace(activity.ServiceUrl))
            {
                MicrosoftAppCredentials.TrustServiceUrl(activity.ServiceUrl);
            }
            else
            {
                Trace.TraceWarning("No activity in the Bot Authentication Action Arguments");
            }

            ctx.CurrentUser = new ClaimsPrincipal(identity);

            return(null);
        }
示例#16
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <typeparam name="TModel">Model type</typeparam>
 /// <param name="module">Current module</param>
 /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
 /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
 /// <returns>Bound model instance</returns>
 public static TModel Bind <TModel>(this INancyModule module, BindingConfig configuration, params string[] blacklistedProperties)
 {
     return(module.Bind(configuration, blacklistedProperties));
 }
示例#17
0
        public dynamic Execute(dynamic parameters, INancyModule module)
        {
            module.RequiresAuthentication();

            IFeed feed;

            try
            {
                feed = module.Bind <Feed>();

                ITransaction transaction = _store.BeginTransaction();

                var existingFeedExists =
                    transaction.Query <IFeed>().Where("Name = @feedName").Parameter("feedName", feed.Name).Count() >
                    0;

                if (existingFeedExists)
                {
                    return(HttpStatusCode.Conflict);
                }

                if (!string.IsNullOrWhiteSpace(feed.ApiKey))
                {
                    ICryptoService cryptoService = new PBKDF2();

                    feed.ApiKeySalt   = cryptoService.GenerateSalt();
                    feed.ApiKeyHashed = cryptoService.Compute(feed.ApiKey);
                }

                transaction.Insert(feed);
                transaction.Commit();
                transaction.Dispose();

                transaction = _store.BeginTransaction();

                feed =
                    transaction.Query <IFeed>()
                    .Where("Name = @feedName")
                    .Parameter("feedName", feed.Name)
                    .First();

                var appFolder  = _home.InstallDirectory;
                var feedFolder = Path.Combine(appFolder, "Feeds", feed.Id.ToString());

                IFeedConfiguration config = new FeedConfiguration
                {
                    FeedId            = feed.Id,
                    PackagesDirectory = feedFolder
                };

                transaction.Insert(config);
                transaction.Commit();
                transaction.Dispose();
            }
            catch (Exception ex)
            {
                return(HttpStatusCode.InternalServerError);
            }

            feed.ApiKeyHashed = null; //Temporary until API Key table is used
            feed.ApiKeySalt   = null; //Temporary until API Key table is used

            return(feed);
        }
示例#18
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <typeparam name="TModel">Model type</typeparam>
 /// <param name="module">Current module</param>
 /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
 /// <returns>Bound model instance</returns>
 public static TModel Bind <TModel>(this INancyModule module, BindingConfig configuration)
 {
     return(module.Bind(configuration));
 }
示例#19
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <typeparam name="TModel">Model type</typeparam>
 /// <param name="module">Current module</param>
 /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
 /// <param name="blacklistedProperty">Expressions that tell which property should be ignored</param>
 /// <example>this.Bind&lt;Person&gt;(p =&gt; p.Name, p =&gt; p.Age)</example>
 /// <returns>Bound model instance</returns>
 public static TModel Bind <TModel>(this INancyModule module, BindingConfig configuration, Expression <Func <TModel, object> > blacklistedProperty)
 {
     return(module.Bind(configuration, new [] { blacklistedProperty }.ParseBlacklistedPropertiesExpressionTree()));
 }