protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {

            base.ApplicationStartup(container, pipelines);

            var configuration = new BasicAuthenticationConfiguration(container.Resolve<IUserValidator>(), "test-realm");
            BasicAuthentication.Enable(pipelines,configuration);

            var stateless = new StatelessAuthenticationConfiguration(c =>
            {
                const string key = "X-Auth-Token";
                string token = null;

                if (c.Request.Headers.Authorization == null || !c.Request.Headers.Authorization.Any())
                {
                    _log.ErrorFormat("No request headers are present in the request {0}", c);
                    return null;
                }

                if (c.Request.Headers.FirstOrDefault(f => f.Key == key).Value == null ||
                    string.IsNullOrEmpty(c.Request.Headers.FirstOrDefault(f => f.Key == key).Value.First()))
                {
                    _log.ErrorFormat("No Key present in the request headers");
                    return null;
                }

                token = c.Request.Headers.FirstOrDefault(f => f.Key == key).Value.First();
                _log.InfoFormat("Token used {0}", token);

                var user = container.Resolve<IUserApiMapper>();
                return user.GetUserFromToken(token);

            });
            StatelessAuthentication.Enable(pipelines, stateless);
        }
        public void Configure(XmlElement configurationElement)
        {
            var validatorEl = configurationElement.GetElementsByTagName("validator")
                                                  .OfType<XmlElement>().FirstOrDefault(x => x.HasAttribute("type"));
            
            if (validatorEl == null)
            {
                throw new ConfigurationErrorsException("Missing required validator element for Basic Authentication authentication provider.");
            }
            
            var validatorType = Type.GetType(validatorEl.GetAttribute("type"));
            if (validatorType == null)
            {
                throw new ConfigurationErrorsException(String.Format("Cannot resolve validator type '{0}'", validatorEl.GetAttribute("type")));
            }

            var userValidator = Activator.CreateInstance(validatorType) as IUserValidator;
            if (userValidator == null)
            {
                throw new ConfigurationErrorsException(String.Format("Type {0} does not implement the IUserValidator interface.", validatorType.FullName));
            }

            if (userValidator is IConfigurableUserValidator)
            {
                (userValidator as IConfigurableUserValidator).Configure(validatorEl);
            }

            var realmEl = configurationElement.GetElementsByTagName("realm").OfType<XmlElement>().FirstOrDefault();
            var realm = realmEl == null ? "BrightstarDB" : realmEl.InnerText;
            _configuration= new BasicAuthenticationConfiguration(userValidator, realm);
        }
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);

            var config = new BasicAuthenticationConfiguration(container.Resolve<IUserValidator>(), "basic");

            pipelines.EnableBasicAuthentication(config);
        }
示例#4
0
 private static Action<NancyContext> GetAuthenticationPromptHook(BasicAuthenticationConfiguration configuration)
 {
     return context =>
         {
             if (context.Response.StatusCode == HttpStatusCode.Unauthorized && SendAuthenticateResponseHeader(context, configuration))
             {
                 context.Response.Headers["WWW-Authenticate"] = String.Format("{0} realm=\"{1}\"", SCHEME, configuration.Realm);
             }
         };
 }
示例#5
0
 private static Action <NancyContext> GetAuthenticationPromptHook(BasicAuthenticationConfiguration configuration)
 {
     return(context =>
     {
         if (context.Response.StatusCode == HttpStatusCode.Unauthorized && SendAuthenticateResponseHeader(context, configuration))
         {
             context.Response.Headers["WWW-Authenticate"] = String.Format("{0} realm=\"{1}\"", SCHEME, configuration.Realm);
         }
     });
 }
示例#6
0
        /// <summary>
        /// Gets the pre request hook for loading the authenticated user's details
        /// from the auth header.
        /// </summary>
        /// <param name="configuration">Basic authentication configuration to use</param>
        /// <returns>Pre request hook delegate</returns>
        private static Func<NancyContext, Response> GetCredentialRetrievalHook(BasicAuthenticationConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            return context =>
                {
                    RetrieveCredentials(context, configuration);
                    return null;
                };
        }
示例#7
0
        /// <summary>
        /// Gets the pre request hook for loading the authenticated user's details
        /// from the auth header.
        /// </summary>
        /// <param name="configuration">Basic authentication configuration to use</param>
        /// <returns>Pre request hook delegate</returns>
        private static Func <NancyContext, Response> GetCredentialRetrievalHook(BasicAuthenticationConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            return(context =>
            {
                RetrieveCredentials(context, configuration);
                return null;
            });
        }
示例#8
0
        private static void RetrieveCredentials(NancyContext context, BasicAuthenticationConfiguration configuration)
        {
            var credentials = ExtractCredentialsFromHeaders(context.Request);

            if (credentials != null && credentials.Length == 2)
            {
                var user = configuration.UserValidator.Validate(credentials[0], credentials[1]);

                if (user != null)
                {
                    context.CurrentUser = user;
                }
            }
        }
示例#9
0
        /// <summary>
        /// Enables basic authentication for the application
        /// </summary>
        /// <param name="pipelines">Pipelines to add handlers to (usually "this")</param>
        /// <param name="configuration">Forms authentication configuration</param>
        public static void Enable(IPipelines pipelines, BasicAuthenticationConfiguration configuration)
        {
            if (pipelines == null)
            {
                throw new ArgumentNullException("pipelines");
            }

            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            pipelines.BeforeRequest.AddItemToStartOfPipeline(GetCredentialRetrievalHook(configuration));
            pipelines.AfterRequest.AddItemToEndOfPipeline(GetAuthenticationPromptHook(configuration));
        }
示例#10
0
        /// <summary>
        /// Enables basic authentication for the application
        /// </summary>
        /// <param name="pipelines">Pipelines to add handlers to (usually "this")</param>
        /// <param name="configuration">Forms authentication configuration</param>
        public static void Enable(IPipelines pipelines, BasicAuthenticationConfiguration configuration)
        {
            if (pipelines == null)
            {
                throw new ArgumentNullException("pipelines");
            }

            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            pipelines.BeforeRequest.AddItemToStartOfPipeline(GetCredentialRetrievalHook(configuration));
            pipelines.AfterRequest.AddItemToEndOfPipeline(GetAuthenticationPromptHook(configuration));
        }
示例#11
0
        /// <summary>
        /// Enables basic authentication for a module
        /// </summary>
        /// <param name="module">Module to add handlers to (usually "this")</param>
        /// <param name="configuration">Forms authentication configuration</param>
        public static void Enable(INancyModule module, BasicAuthenticationConfiguration configuration)
        {
            if (module == null)
            {
                throw new ArgumentNullException("module");
            }

            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            module.RequiresAuthentication();
            module.Before.AddItemToStartOfPipeline(GetCredentialRetrievalHook(configuration));
            module.After.AddItemToEndOfPipeline(GetAuthenticationPromptHook(configuration));
        }
示例#12
0
        /// <summary>
        /// Enables basic authentication for a module
        /// </summary>
        /// <param name="module">Module to add handlers to (usually "this")</param>
        /// <param name="configuration">Forms authentication configuration</param>
        public static void Enable(INancyModule module, BasicAuthenticationConfiguration configuration)
        {
            if (module == null)
            {
                throw new ArgumentNullException("module");
            }

            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            module.RequiresAuthentication();
            module.Before.AddItemToStartOfPipeline(GetCredentialRetrievalHook(configuration));
            module.After.AddItemToEndOfPipeline(GetAuthenticationPromptHook(configuration));
        }
示例#13
0
 private static bool SendAuthenticateResponseHeader(NancyContext context, BasicAuthenticationConfiguration configuration)
 {
     return configuration.UserPromptBehaviour == UserPromptBehaviour.Always || (configuration.UserPromptBehaviour == UserPromptBehaviour.NonAjax && !context.Request.IsAjaxRequest());
 }
示例#14
0
        private static void RetrieveCredentials(NancyContext context, BasicAuthenticationConfiguration configuration)
        {
            var credentials =
                ExtractCredentialsFromHeaders(context.Request);

            if (credentials != null && credentials.Length == 2)
            {
                var user = configuration.UserValidator.Validate(credentials[0], credentials[1]);

                if (user != null)
                {
                    context.CurrentUser = user;
                }
            }
        }
示例#15
0
        private void EnableBasicAuth(IUnityContainer container, IPipelines pipelines)
        {
            var config =
                new BasicAuthenticationConfiguration(
                    container.Resolve<IUserValidator>()
                    , "you need login"
                    , UserPromptBehaviour.NonAjax);

            BasicAuthentication.Enable(pipelines, config);
        }
示例#16
0
 /// <summary>
 /// Module requires basic authentication
 /// </summary>
 /// <param name="module">Module to enable</param>
 /// <param name="configuration">Basic authentication configuration</param>
 public static void EnableBasicAuthentication(this NancyModule module, BasicAuthenticationConfiguration configuration)
 {
     BasicAuthentication.Enable(module, configuration);
 }
 public BasicAuthAuthenticationProvider(BasicAuthenticationConfiguration configuration)
 {
     _configuration = configuration;
 }
示例#18
0
 private static bool SendAuthenticateResponseHeader(NancyContext context, BasicAuthenticationConfiguration configuration)
 {
     return(configuration.UserPromptBehaviour == UserPromptBehaviour.Always || (configuration.UserPromptBehaviour == UserPromptBehaviour.NonAjax && !context.Request.IsAjaxRequest()));
 }
示例#19
0
 /// <summary>
 /// Module requires basic authentication
 /// </summary>
 /// <param name="module">Module to enable</param>
 /// <param name="configuration">Basic authentication configuration</param>
 public static void EnableBasicAuthentication(this INancyModule module, BasicAuthenticationConfiguration configuration)
 {
     BasicAuthentication.Enable(module, configuration);
 }
示例#20
0
 /// <summary>
 /// Module requires basic authentication
 /// </summary>
 /// <param name="pipeline">Bootstrapper to enable</param>
 /// <param name="configuration">Basic authentication configuration</param>
 public static void EnableBasicAuthentication(this IPipelines pipeline, BasicAuthenticationConfiguration configuration)
 {
     BasicAuthentication.Enable(pipeline, configuration);
 }
示例#21
0
 /// <summary>
 /// Module requires basic authentication
 /// </summary>
 /// <param name="pipeline">Bootstrapper to enable</param>
 /// <param name="configuration">Basic authentication configuration</param>
 public static void EnableBasicAuthentication(this IPipelines pipeline, BasicAuthenticationConfiguration configuration)
 {
     BasicAuthentication.Enable(pipeline, configuration);
 }
示例#22
0
        private static void RetrieveCredentials(NancyContext context, BasicAuthenticationConfiguration configuration)
        {
            var credentials = ExtractCredentialsFromHeaders(context.Request);

            if (credentials != null && credentials.Length == 2)
            {
                if (configuration.UserValidator.Validate(credentials[0], credentials[1]))
                {
                    context.Items[SecurityConventions.AuthenticatedUsernameKey] = credentials[0];
                }
            }
        }