Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Moved remaining diagnostics properties
Browse files Browse the repository at this point in the history
You can now configure cookie name and timeout using the
DiagnosticsConfiguration Timeout and CookieName properties
  • Loading branch information
thecodejunkie committed Nov 27, 2012
1 parent 5e27c65 commit 023e2b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
37 changes: 32 additions & 5 deletions src/Nancy/Diagnostics/DiagnosticsConfiguration.cs
Expand Up @@ -8,20 +8,40 @@
public class DiagnosticsConfiguration
{

public DiagnosticsConfiguration() : this(CryptographyConfiguration.Default)
/// <summary>
/// Initializes a new instance of the <see cref="DiagnosticsConfiguration"/> class,
/// using the <see cref="CryptographyConfiguration.Default"/> cryptographic
/// configuration.
/// </summary>
public DiagnosticsConfiguration()
: this(CryptographyConfiguration.Default)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DiagnosticsConfiguration"/> class,
/// using the <paramref name="cryptographyConfiguration"/> cryptographic
/// configuration.
/// </summary>
/// <param name="cryptographyConfiguration">The <see cref="CryptographyConfiguration"/> to use with diagnostics.</param>
public DiagnosticsConfiguration(CryptographyConfiguration cryptographyConfiguration)
{
this.CookieName = "__ncd";
this.CryptographyConfiguration = cryptographyConfiguration;
this.Path = "/_Nancy";
this.Timeout = 15;
}

/// <summary>
/// Gets or sets the path that the diagnostics dashboard will be accessible on.
/// Gets or sets the name of the cookie used by the diagnostics dashboard.
/// </summary>
public string Path { get; set; }
/// <remarks>The default is __ncd</remarks>
public string CookieName { get; set; }

/// <summary>
/// Gets or sets the cryptography config to use for securing the diagnostics dashboard
/// </summary>
public CryptographyConfiguration CryptographyConfiguration { get; set; }

/// <summary>
/// Gets or sets password for accessing the diagnostics screen.
Expand All @@ -30,9 +50,16 @@ public DiagnosticsConfiguration(CryptographyConfiguration cryptographyConfigurat
public string Password { get; set; }

/// <summary>
/// Gets or sets the cryptography config to use for securing the diagnostics dashboard
/// Gets or sets the path that the diagnostics dashboard will be accessible on.
/// </summary>
public CryptographyConfiguration CryptographyConfiguration { get; set; }
/// <remarks>The default is /_Nancy</remarks>
public string Path { get; set; }

/// <summary>
/// The number of minutes that you stay logged into the diagnostics dashboard.
/// </summary>
/// <remarks>The default is 15 minutes.</remarks>
public int Timeout { get; set; }

/// <summary>
/// Gets a value indicating whether the configuration is valid
Expand Down
17 changes: 7 additions & 10 deletions src/Nancy/Diagnostics/DiagnosticsHook.cs
Expand Up @@ -17,9 +17,6 @@ public static class DiagnosticsHook
{
private const string PipelineKey = "__Diagnostics";

private const string DiagsCookieName = "__ncd";
private const int DiagnosticsSessionTimeoutMinutes = 15;

public static void Enable(DiagnosticsConfiguration diagnosticsConfiguration, IPipelines pipelines, IEnumerable<IDiagnosticsProvider> providers, IRootPathProvider rootPathProvider, IEnumerable<ISerializer> serializers, IRequestTracing requestTracing, NancyInternalConfiguration configuration, IModelBinderLocator modelBinderLocator, IEnumerable<IResponseProcessor> responseProcessors)
{
var keyGenerator = new DefaultModuleKeyGenerator();
Expand Down Expand Up @@ -115,7 +112,7 @@ private static Response ExecuteDiagnostics(NancyContext ctx, IRouteResolver rout
var view = GetDiagnosticsLoginView(ctx);

view.AddCookie(
new NancyCookie(DiagsCookieName, String.Empty, true) { Expires = DateTime.Now.AddDays(-1) });
new NancyCookie(diagnosticsConfiguration.CookieName, String.Empty, true) { Expires = DateTime.Now.AddDays(-1) });

return view;
}
Expand Down Expand Up @@ -154,14 +151,14 @@ private static void AddUpdateSessionCookie(DiagnosticsSession session, NancyCont
return;
}

session.Expiry = DateTime.Now.AddMinutes(DiagnosticsSessionTimeoutMinutes);
session.Expiry = DateTime.Now.AddMinutes(diagnosticsConfiguration.Timeout);
var serializedSession = serializer.Serialize(session);

var encryptedSession = diagnosticsConfiguration.CryptographyConfiguration.EncryptionProvider.Encrypt(serializedSession);
var hmacBytes = diagnosticsConfiguration.CryptographyConfiguration.HmacProvider.GenerateHmac(encryptedSession);
var hmacString = Convert.ToBase64String(hmacBytes);

var cookie = new NancyCookie(DiagsCookieName, String.Format("{1}{0}", encryptedSession, hmacString), true);
var cookie = new NancyCookie(diagnosticsConfiguration.CookieName, String.Format("{1}{0}", encryptedSession, hmacString), true);

context.Response.AddCookie(cookie);
}
Expand All @@ -178,12 +175,12 @@ private static DiagnosticsSession GetSession(NancyContext context, DiagnosticsCo
return ProcessLogin(context, diagnosticsConfiguration, serializer);
}

if (!context.Request.Cookies.ContainsKey(DiagsCookieName))
if (!context.Request.Cookies.ContainsKey(diagnosticsConfiguration.CookieName))
{
return null;
}

var encryptedValue = HttpUtility.UrlDecode(context.Request.Cookies[DiagsCookieName]);
var encryptedValue = HttpUtility.UrlDecode(context.Request.Cookies[diagnosticsConfiguration.CookieName]);
var hmacStringLength = Base64Helpers.GetBase64Length(diagnosticsConfiguration.CryptographyConfiguration.HmacProvider.HmacLength);
var encryptedSession = encryptedValue.Substring(hmacStringLength);
var hmacString = encryptedValue.Substring(0, hmacStringLength);
Expand Down Expand Up @@ -230,7 +227,7 @@ private static DiagnosticsSession ProcessLogin(NancyContext context, Diagnostics
{
Hash = hash,
Salt = salt,
Expiry = DateTime.Now.AddMinutes(DiagnosticsSessionTimeoutMinutes),
Expiry = DateTime.Now.AddMinutes(diagnosticsConfiguration.Timeout)
};

return session;
Expand All @@ -239,7 +236,7 @@ private static DiagnosticsSession ProcessLogin(NancyContext context, Diagnostics
private static bool IsLoginRequest(NancyContext context, DiagnosticsConfiguration diagnosticsConfiguration)
{
return context.Request.Method == "POST" &&
context.Request.Path == string.Concat(diagnosticsConfiguration.Path);
context.Request.Path == diagnosticsConfiguration.Path;
}

private static void ExecuteRoutePreReq(NancyContext context, Func<NancyContext, Response> resolveResultPreReq)
Expand Down

0 comments on commit 023e2b7

Please sign in to comment.