From 213634f9d5f3b305fbea0746ee1c613160f4f123 Mon Sep 17 00:00:00 2001
From: gomi
Date: Sun, 23 Mar 2014 23:05:02 +0100
Subject: [PATCH] add ICookieValidator to have external source to validate
cookie (user not disabled, password not changed)
---
.../AppHarbor.Web.Security.csproj | 113 +++++++++---------
.../CookieAuthenticationModule.cs | 24 +++-
AppHarbor.Web.Security/ICookieValidator.cs | 12 ++
3 files changed, 88 insertions(+), 61 deletions(-)
create mode 100644 AppHarbor.Web.Security/ICookieValidator.cs
diff --git a/AppHarbor.Web.Security/AppHarbor.Web.Security.csproj b/AppHarbor.Web.Security/AppHarbor.Web.Security.csproj
index 840b218..06f208e 100644
--- a/AppHarbor.Web.Security/AppHarbor.Web.Security.csproj
+++ b/AppHarbor.Web.Security/AppHarbor.Web.Security.csproj
@@ -1,63 +1,64 @@
-
-
-
- Debug
- AnyCPU
- 8.0.30703
- 2.0
- {BA59E36D-677F-4F70-94DF-C75C8860F67B}
- Library
- Properties
- AppHarbor.Web.Security
- AppHarbor.Web.Security
- v4.0
- 512
-
-
- true
- full
- false
- bin\Debug\
- DEBUG;TRACE
- prompt
- 4
-
-
- pdbonly
- true
- bin\Release\
- TRACE
- prompt
- 4
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+ Debug
+ AnyCPU
+ 8.0.30703
+ 2.0
+ {BA59E36D-677F-4F70-94DF-C75C8860F67B}
+ Library
+ Properties
+ AppHarbor.Web.Security
+ AppHarbor.Web.Security
+ v4.0
+ 512
+
+
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+
+
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ -->
\ No newline at end of file
diff --git a/AppHarbor.Web.Security/CookieAuthenticationModule.cs b/AppHarbor.Web.Security/CookieAuthenticationModule.cs
index 30a0062..2f51dd2 100644
--- a/AppHarbor.Web.Security/CookieAuthenticationModule.cs
+++ b/AppHarbor.Web.Security/CookieAuthenticationModule.cs
@@ -6,7 +6,8 @@ namespace AppHarbor.Web.Security
{
public sealed class CookieAuthenticationModule : IHttpModule
{
- private readonly ICookieAuthenticationConfiguration _configuration;
+ private readonly ICookieAuthenticationConfiguration _configuration;
+ private readonly ICookieValidator _validator;
public CookieAuthenticationModule()
: this(new ConfigFileAuthenticationConfiguration())
@@ -16,8 +17,15 @@ public CookieAuthenticationModule()
public CookieAuthenticationModule(ICookieAuthenticationConfiguration configuration)
{
_configuration = configuration;
- }
-
+ }
+
+ public CookieAuthenticationModule(ICookieAuthenticationConfiguration configuration,
+ ICookieValidator validator)
+ {
+ _configuration = configuration;
+ _validator = validator;
+
+ }
private void OnAuthenticateRequest(object sender, EventArgs e)
{
var context = ((HttpApplication)sender).Context;
@@ -29,8 +37,14 @@ private void OnAuthenticateRequest(object sender, EventArgs e)
{
byte[] data;
var cookieData = protector.Validate(cookie.Value, out data);
- var authenticationCookie = AuthenticationCookie.Deserialize(data);
- if (!authenticationCookie.IsExpired(_configuration.Timeout))
+ var authenticationCookie = AuthenticationCookie.Deserialize(data);
+
+ bool isCookieValid = true;
+ if (_validator != null)
+ isCookieValid = _validator.IsCookieValid(authenticationCookie);
+
+ if (isCookieValid && !authenticationCookie.IsExpired(_configuration.Timeout))
+
{
context.User = authenticationCookie.GetPrincipal();
RenewCookieIfExpiring(context, protector, authenticationCookie);
diff --git a/AppHarbor.Web.Security/ICookieValidator.cs b/AppHarbor.Web.Security/ICookieValidator.cs
new file mode 100644
index 0000000..f57bf4d
--- /dev/null
+++ b/AppHarbor.Web.Security/ICookieValidator.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace AppHarbor.Web.Security
+{
+ public interface ICookieValidator
+ {
+ bool IsCookieValid(AuthenticationCookie cookie);
+ }
+}