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); }
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); } }; }
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); } }); }
/// <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; }; }
/// <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; }); }
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; } } }
/// <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)); }
/// <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)); }
private static bool SendAuthenticateResponseHeader(NancyContext context, BasicAuthenticationConfiguration configuration) { return configuration.UserPromptBehaviour == UserPromptBehaviour.Always || (configuration.UserPromptBehaviour == UserPromptBehaviour.NonAjax && !context.Request.IsAjaxRequest()); }
private void EnableBasicAuth(IUnityContainer container, IPipelines pipelines) { var config = new BasicAuthenticationConfiguration( container.Resolve<IUserValidator>() , "you need login" , UserPromptBehaviour.NonAjax); BasicAuthentication.Enable(pipelines, config); }
/// <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; }
private static bool SendAuthenticateResponseHeader(NancyContext context, BasicAuthenticationConfiguration configuration) { return(configuration.UserPromptBehaviour == UserPromptBehaviour.Always || (configuration.UserPromptBehaviour == UserPromptBehaviour.NonAjax && !context.Request.IsAjaxRequest())); }
/// <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); }
/// <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); }
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]; } } }