Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache AppSettings in static class #4

Merged
merged 2 commits into from Oct 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -130,7 +130,7 @@ publish/

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/
packages/

# Windows Azure Build Output
csx
Expand Down Expand Up @@ -213,3 +213,4 @@ pip-log.txt

#Mr Developer
.mr.developer.cfg

13 changes: 3 additions & 10 deletions Daishi.Armor.WebFramework/ArmorAuthorize.cs
@@ -1,8 +1,6 @@
#region Includes

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Claims;

Expand Down Expand Up @@ -50,14 +48,9 @@ private readonly HttpRequestArmorHeaderParserFactory

#region Validate ArmorToken

var encryptionKey =
Convert.FromBase64String(
ConfigurationManager.AppSettings["ArmorEncryptionKey"]);
var hashingKey =
Convert.FromBase64String(
ConfigurationManager.AppSettings["ArmorHashKey"]);
var armorTimeOut =
Convert.ToInt64(ConfigurationManager.AppSettings["ArmorTimeout"]);
var encryptionKey = ArmorSettings.EncryptionKey;
var hashingKey = ArmorSettings.HashingKey;
var armorTimeOut = ArmorSettings.Timeout;

var secureArmorTokenValidator =
new SecureArmorTokenValidator(armorTokenHeader.ArmorToken,
Expand Down
10 changes: 2 additions & 8 deletions Daishi.Armor.WebFramework/ArmorFortify.cs
@@ -1,8 +1,6 @@
#region Includes

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Claims;
using System.Web;
Expand Down Expand Up @@ -32,12 +30,8 @@ public class ArmorFortify {
var userId = claims.Single(c => c.Type.Equals("UserId")).Value;
var platform = claims.Single(c => c.Type.Equals("Platform")).Value;

var encryptionKey =
Convert.FromBase64String(
ConfigurationManager.AppSettings["ArmorEncryptionKey"]);
var hashingKey =
Convert.FromBase64String(
ConfigurationManager.AppSettings["ArmorHashKey"]);
var encryptionKey = ArmorSettings.EncryptionKey;
var hashingKey = ArmorSettings.HashingKey;

var nonceGenerator = new NonceGenerator();
nonceGenerator.Execute();
Expand Down
56 changes: 56 additions & 0 deletions Daishi.Armor.WebFramework/ArmorSettings.cs
@@ -0,0 +1,56 @@
using System;
using System.Configuration;

namespace Daishi.Armor.WebFramework {
// Since changes to the AppSettings will bounce the AppDomain, we can statically
// cache the answer to all this configuration settings
public static class ArmorSettings {
private static byte[] s_EncryptionKey;
public static byte[] EncryptionKey {
get {
if (s_EncryptionKey == null) {
s_EncryptionKey = Convert.FromBase64String(
ConfigurationManager.AppSettings["ArmorEncryptionKey"]);
}

return s_EncryptionKey;
}
}

private static byte[] s_HashingKey;
public static byte[] HashingKey {
get {
if (s_HashingKey == null) {
s_HashingKey = Convert.FromBase64String(
ConfigurationManager.AppSettings["ArmorHashKey"]);
}

return s_HashingKey;
}
}

private static long? s_Timeout;
public static long Timeout {
get {
if (!s_Timeout.HasValue) {
s_Timeout = Convert.ToInt64(
ConfigurationManager.AppSettings["ArmorTimeout"]);
}

return s_Timeout.Value;
}
}

private static bool? s_IsArmed;
public static bool IsArmed {
get {
if (!s_IsArmed.HasValue) {
s_IsArmed = Convert.ToBoolean(
ConfigurationManager.AppSettings["IsArmed"]);
}

return s_IsArmed.Value;
}
}
}
}
1 change: 1 addition & 0 deletions Daishi.Armor.WebFramework/Daishi.Armor.WebFramework.csproj
Expand Up @@ -87,6 +87,7 @@
<ItemGroup>
<Compile Include="ArmorAuthorize.cs" />
<Compile Include="ArmorFortify.cs" />
<Compile Include="ArmorSettings.cs" />
<Compile Include="ArmorTokenHeader.cs" />
<Compile Include="GenerateSecureArmorToken.cs" />
<Compile Include="HttpRequestArmorHeaderParser.cs" />
Expand Down
6 changes: 1 addition & 5 deletions Daishi.Armor.WebFramework/MvcArmorAuthorizeAttribute.cs
@@ -1,7 +1,5 @@
#region Includes

using System;
using System.Configuration;
using System.Threading;
using System.Web;
using System.Web.Mvc;
Expand All @@ -11,9 +9,7 @@
namespace Daishi.Armor.WebFramework {
public class MvcArmorAuthorizeAttribute : AuthorizeAttribute {
protected override bool AuthorizeCore(HttpContextBase httpContext) {
var isArmed =
Convert.ToBoolean(ConfigurationManager.AppSettings["IsArmed"]);
if (!isArmed) return true;
if (!ArmorSettings.IsArmed) return true;

var armorAuthorize =
new ArmorAuthorize(
Expand Down
6 changes: 1 addition & 5 deletions Daishi.Armor.WebFramework/MvcArmorFortifyFilter.cs
@@ -1,7 +1,5 @@
#region Includes

using System;
using System.Configuration;
using System.Web.Mvc;

#endregion
Expand All @@ -10,9 +8,7 @@ namespace Daishi.Armor.WebFramework {
public class MvcArmorFortifyFilter : ActionFilterAttribute {
public override void OnActionExecuted(
ActionExecutedContext filterContext) {
var isArmed =
Convert.ToBoolean(ConfigurationManager.AppSettings["IsArmed"]);
if (!isArmed) return;
if (!ArmorSettings.IsArmed) return;

var armorFortify =
new ArmorFortify(
Expand Down
6 changes: 1 addition & 5 deletions Daishi.Armor.WebFramework/WebApiArmorAuthorizeAttribute.cs
@@ -1,7 +1,5 @@
#region Includes

using System;
using System.Configuration;
using System.Threading;
using System.Web.Http;
using System.Web.Http.Controllers;
Expand All @@ -11,9 +9,7 @@
namespace Daishi.Armor.WebFramework {
public class WebApiArmorAuthorizeAttribute : AuthorizeAttribute {
protected override bool IsAuthorized(HttpActionContext actionContext) {
var isArmed =
Convert.ToBoolean(ConfigurationManager.AppSettings["IsArmed"]);
if (!isArmed) return true;
if (!ArmorSettings.IsArmed) return true;

var armorAuthorize =
new ArmorAuthorize(
Expand Down
6 changes: 1 addition & 5 deletions Daishi.Armor.WebFramework/WebApiArmorFortifyFilter.cs
@@ -1,7 +1,5 @@
#region Includes

using System;
using System.Configuration;
using System.Net;
using System.Threading;
using System.Web;
Expand All @@ -14,9 +12,7 @@ namespace Daishi.Armor.WebFramework {
public class WebApiArmorFortifyFilter : ActionFilterAttribute {
public override void OnActionExecuted(
HttpActionExecutedContext actionExecutedContext) {
var isArmed =
Convert.ToBoolean(ConfigurationManager.AppSettings["IsArmed"]);
if (!isArmed) return;
if (!ArmorSettings.IsArmed) return;

var armorFortify =
new ArmorFortify(
Expand Down