From 7ac3e6f1da8adf4eec3d4af8c8331a14ae01b4fb Mon Sep 17 00:00:00 2001 From: Greg Edwards Date: Thu, 26 Feb 2015 02:24:11 -0500 Subject: [PATCH] Added RequireSession flag which, if false, removes session validation. --- GamestatsBase/GamestatsHandler.cs | 19 +++++++++++-------- Sample/dummy.ashx.cs | 2 +- Sample/tetrisds/store.ashx.cs | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/GamestatsBase/GamestatsHandler.cs b/GamestatsBase/GamestatsHandler.cs index 9957e12..119bb13 100644 --- a/GamestatsBase/GamestatsHandler.cs +++ b/GamestatsBase/GamestatsHandler.cs @@ -19,7 +19,8 @@ namespace GamestatsBase public class GamestatsHandler : IHttpHandler, IRequiresSessionState { public GamestatsHandler(String initString, GamestatsRequestVersions reqVersion, - GamestatsResponseVersions respVersion, bool encryptedRequest = true) + GamestatsResponseVersions respVersion, bool encryptedRequest = true, + bool requireSession = true) { if (initString.Length < 44) throw new FormatException(); @@ -30,21 +31,21 @@ namespace GamestatsBase uint hashMask = UInt32.Parse(initString.Substring(44, 8), NumberStyles.AllowHexSpecifier); String gameId = initString.Substring(52); - Initialize(salt, rngMul, rngAdd, rngMask, hashMask, gameId, reqVersion, respVersion, encryptedRequest); + Initialize(salt, rngMul, rngAdd, rngMask, hashMask, gameId, reqVersion, respVersion, encryptedRequest, requireSession); } public GamestatsHandler(String salt, uint rngMul, uint rngAdd, uint rngMask, uint hashMask, String gameId, GamestatsRequestVersions reqVersion, GamestatsResponseVersions respVersion, - bool encryptedRequest = true) + bool encryptedRequest = true, bool requireSession = true) { - Initialize(salt, rngMul, rngAdd, rngMask, hashMask, gameId, reqVersion, respVersion, encryptedRequest); + Initialize(salt, rngMul, rngAdd, rngMask, hashMask, gameId, reqVersion, respVersion, encryptedRequest, requireSession); } private void Initialize(String salt, uint rngMul, uint rngAdd, uint rngMask, uint hashMask, String gameId, GamestatsRequestVersions reqVersion, GamestatsResponseVersions respVersion, - bool encryptedRequest) + bool encryptedRequest, bool requireSession) { if (salt.Length != 20) throw new FormatException(); Salt = salt; @@ -56,6 +57,7 @@ namespace GamestatsBase RequestVersion = reqVersion; ResponseVersion = respVersion; EncryptedRequest = encryptedRequest; + RequireSession = requireSession; } public String Salt { get; protected set; } @@ -67,6 +69,7 @@ namespace GamestatsBase public GamestatsRequestVersions RequestVersion { get; protected set; } public GamestatsResponseVersions ResponseVersion { get; protected set; } public bool EncryptedRequest { get; protected set; } + public bool RequireSession { get; protected set; } public GamestatsSessionManager SessionManager { @@ -115,7 +118,7 @@ namespace GamestatsBase else if (form.Count >= 3) { // this is a main request - if (form["hash"] == null || + if ((form["hash"] == null && RequireSession) || form["data"] == null || form["data"].Length < ((RequestVersion != GamestatsRequestVersions.Version3) ? 12 : 16)) @@ -138,7 +141,7 @@ namespace GamestatsBase GamestatsSession session = null; - if (SessionManager.Sessions.ContainsKey(form["hash"])) + if (form["hash"] != null && SessionManager.Sessions.ContainsKey(form["hash"])) { session = SessionManager.Sessions[form["hash"]]; if (session.GameId != GameId) @@ -148,7 +151,7 @@ namespace GamestatsBase return; } } - else if (context.Request.HttpMethod == "GET") + else if (RequireSession) { // session hash not matched ShowError(context, 400); diff --git a/Sample/dummy.ashx.cs b/Sample/dummy.ashx.cs index 3ded853..4156a89 100644 --- a/Sample/dummy.ashx.cs +++ b/Sample/dummy.ashx.cs @@ -13,7 +13,7 @@ namespace Sample { public dummy() : base("00000000000000000000", 0, 0, 0, 0, "dummy", - GamestatsRequestVersions.Version1, GamestatsResponseVersions.Version1, false) + GamestatsRequestVersions.Version1, GamestatsResponseVersions.Version1, false, false) { } diff --git a/Sample/tetrisds/store.ashx.cs b/Sample/tetrisds/store.ashx.cs index 78caec2..f8ccf01 100644 --- a/Sample/tetrisds/store.ashx.cs +++ b/Sample/tetrisds/store.ashx.cs @@ -16,7 +16,7 @@ namespace Sample.tetrisds public class store : GamestatsHandler { public store() : base("Wo3vqrDoL56sAdveYeC1", 0, 0, 0, 0, "tetrisds", - GamestatsRequestVersions.Version1, GamestatsResponseVersions.Version1, false) + GamestatsRequestVersions.Version1, GamestatsResponseVersions.Version1, false, true) { }