Created
June 25, 2012 21:16
-
-
Save codeprogression/2991312 to your computer and use it in GitHub Desktop.
Playing with Nancy.Auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Auth | |
{ | |
public static AuthenticationProviderConfiguration Configuration; | |
public static void Enable(IPipelines pipelines, AuthenticationProviderConfiguration configuration) | |
{ | |
pipelines.AfterRequest.AddItemToEndOfPipeline(ctx=> | |
{ | |
if (ctx.Response.StatusCode==HttpStatusCode.Unauthorized) | |
{ | |
ctx.Response = ctx.GetRedirect(string.Format("{0}?{1}={2}", | |
configuration.LoginUrl, | |
"returnUrl", | |
ctx.ToFullPath("~" + ctx.Request.Path + HttpUtility.UrlEncode(ctx.Request.Url.Query)))); | |
} | |
}); | |
} | |
} | |
public class AuthenticationProviderConfiguration | |
{ | |
public AuthenticationProviderConfiguration() | |
{ | |
LoginUrl = "/auth/login"; | |
LoginView = "login.cshtml"; | |
Providers = new List<IAuthenticationProvider> | |
{ | |
new FormsAuthenticationProvider(), | |
}; | |
} | |
public string LoginUrl { get; set; } | |
public string LoginView { get; set; } | |
public AuthenticationProviderConfiguration(string loginUrl, string loginView, params IAuthenticationProvider[] providers) : this() | |
{ | |
LoginUrl = loginUrl; | |
LoginView = loginView; | |
if (providers!=null){Providers = new List<IAuthenticationProvider>(providers);} | |
} | |
public IList<IAuthenticationProvider> Providers { get; set; } | |
} | |
public class AuthModule: NancyModule | |
{ | |
public AuthModule(AuthenticationProviderConfiguration configuration) | |
{ | |
Get[configuration.LoginUrl] = _ => | |
{ | |
return View[configuration.LoginView, configuration]; | |
}; | |
} | |
} | |
public class FormsAuthenticationProvider : IAuthenticationProvider, IDiagnosticsProvider | |
{ | |
Func<string,string,IUserIdentity> _authenticate = (login,password) => null; | |
public dynamic Configuration { get; set; } | |
public FormsAuthenticationProvider(IDictionary<string,object> configuration) : this() | |
{ | |
foreach (var pair in configuration) | |
{ | |
if (pair.Key == "Authenticate") | |
{ | |
if (!(pair.Value is Func<string, string, IUserIdentity>)) | |
throw new ConfigurationException(); | |
_authenticate = (Func<string, string, IUserIdentity>) pair.Value; | |
continue; | |
} | |
Configuration[pair.Key] = pair.Value; | |
} | |
} | |
public FormsAuthenticationProvider() | |
{ | |
Configuration = DynamicDictionary.Empty; | |
Configuration.GetLogin = "/auth/login"; | |
Configuration.PostLogin = "/auth/login"; | |
Configuration.LoginView = "login.html"; | |
Configuration.LoginSuccessRedirect = "/"; | |
} | |
public IUserIdentity Authenticate(NancyContext context, params string[] credentials) | |
{ | |
return _authenticate.Invoke(credentials[0], credentials[1]); | |
} | |
/// <summary> | |
/// Gets the name of the provider. | |
/// </summary> | |
/// <value>A <see cref="string"/> containing the name of the provider.</value> | |
public string Name | |
{ | |
get { return "Forms Authentication Configuration"; } | |
} | |
/// <summary> | |
/// Gets the description of the provider. | |
/// </summary> | |
/// <value>A <see cref="string"/> containing the description of the provider.</value> | |
public string Description | |
{ | |
get { return "Configures authentication via forms authorization."; } | |
} | |
/// <summary> | |
/// Gets the object that contains the interactive diagnostics methods. | |
/// </summary> | |
/// <value>An instance of the interactive diagnostics object.</value> | |
public object DiagnosticObject | |
{ | |
get | |
{ | |
return new AuthenticationDiagnostics(new[] | |
{ | |
new AuthenticationConfigurationItem("Authenticate", "Returns user when passed login and password", | |
Configuration.Authenticate), | |
new AuthenticationConfigurationItem("GetLoginUrl", "Uri path to the login page", | |
Configuration.GetLogin), | |
new AuthenticationConfigurationItem("PostLoginUrl", "Uri path where login form POSTs to", | |
Configuration.PostLogin), | |
new AuthenticationConfigurationItem("LoginView", "Path to the login view", | |
Configuration.LoginView), | |
new AuthenticationConfigurationItem("LoginSuccessRedirect", "Uri path to redirect to after a successful login", | |
Configuration.LoginSuccessRedirect), | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment