diff --git a/Net.Pokeshot.JiveSdk/App.config b/Net.Pokeshot.JiveSdk/App.config index ca49f51..9d49ea2 100644 --- a/Net.Pokeshot.JiveSdk/App.config +++ b/Net.Pokeshot.JiveSdk/App.config @@ -1,20 +1,14 @@  - - -
- - - - - - - - + + + + + diff --git a/Net.Pokeshot.JiveSdk/Auth/SignedRequest.cs b/Net.Pokeshot.JiveSdk/Auth/SignedRequest.cs index b528603..b0da544 100644 --- a/Net.Pokeshot.JiveSdk/Auth/SignedRequest.cs +++ b/Net.Pokeshot.JiveSdk/Auth/SignedRequest.cs @@ -1,10 +1,5 @@ -using Net.Pokeshot.JiveSdk.Models; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; +using System; using System.Net; -using System.Security.Cryptography; using System.Security.Principal; using System.Threading; using System.Web; @@ -13,8 +8,7 @@ namespace Net.Pokeshot.JiveSdk.Auth { - - public class SignedRequest : ActionFilterAttribute + public class SignedRequest : ActionFilterAttribute { private static readonly string PARAM_ALGORITHM = "algorithm"; private static readonly string PARAM_CLIENT_ID = "client_id"; @@ -24,220 +18,50 @@ public class SignedRequest : ActionFilterAttribute private static readonly string PARAM_SIGNATURE = "signature"; private static readonly string JIVE_EXTN = "JiveEXTN "; - private JiveSdkContext db = new JiveSdkContext(); - - private Dictionary GetParametersFromAuthHeader(string authHeader) - { - authHeader = authHeader.Substring(JIVE_EXTN.Length); - string[] parameters = authHeader.Split('&', '?'); - Dictionary parameterDictionary = new Dictionary(); - foreach (string keyValue in parameters) - { - string[] tokens = keyValue.Split('='); - if (tokens.Length != 2) - { - //Windows Azure tracing. Replace with a logging mechanism of your choice - //Trace.WriteLine("Authorization header not formatted correctly"); - throw new HttpRequestValidationException(); - } - parameterDictionary.Add(HttpUtility.UrlDecode(tokens[0]), HttpUtility.UrlDecode(tokens[1])); - - } - - return parameterDictionary; - } - - private string validateSignature(string parameterStrWithoutSignature, string clientSecret) - { - System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); - //byte[] secret = encoding.GetBytes(clientSecret); - byte[] secret = Convert.FromBase64String(clientSecret); - - - byte[] headerToValidate = encoding.GetBytes(parameterStrWithoutSignature); - HMACSHA256 hmacsha256 = new HMACSHA256(secret); - - byte[] calculatedSignature = hmacsha256.ComputeHash(headerToValidate); - - return Convert.ToBase64String(calculatedSignature); - } - private string ToUrlBase64String(byte[] Input) - { - return Convert.ToBase64String(Input).Replace("=", String.Empty) - .Replace('+', '-') - .Replace('/', '_'); - } - - private byte[] SignWithHmac(byte[] dataToSign, byte[] keyBody) - { - using (var hmacAlgorithm = new HMACSHA256(keyBody)) - { - hmacAlgorithm.ComputeHash(dataToSign); - return hmacAlgorithm.Hash; - } - } - public override void OnActionExecuting(HttpActionContext actionContext) { try { - //Example for retrieving config settings from the web config - //bool oauthValidationEnabled = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsOauthValidationEnabled"]); - bool oauthValidationEnabled = false; - string uri = actionContext.Request.RequestUri.AbsolutePath; - string host = actionContext.Request.RequestUri.Host; - - - - - if (oauthValidationEnabled) + if (actionContext.Request.Headers.Authorization != null) { + var authTemp = actionContext.Request.Headers.Authorization; + string authString = authTemp.Parameter; - if (actionContext.Request.Headers.Authorization != null) + //string authString = HttpContext.Current.Request.Headers["authorization"]; + string userId = HttpContext.Current.Request.Headers["x-jive-user-id"]; + + string[] authStringArray = authString.Split('&'); + string tenantId = null; + string jiveUrl = null; + foreach (string authElement in authStringArray) { - try + string[] keyValue = authElement.Split('='); + if (keyValue[0].Equals("tenant_id")) { - var authTemp = actionContext.Request.Headers.Authorization; - - string authHeader = authTemp.ToString(); - string userId="0"; - - if (HttpContext.Current.Request.Headers["x-jive-user-id"]!=null){ - userId = HttpContext.Current.Request.Headers["x-jive-user-id"]; - } - - - - - if (authHeader.StartsWith(JIVE_EXTN) == false || authHeader.Contains(PARAM_SIGNATURE) == false) - { - Trace.WriteLine("Authorization header not formatted correctly"); - throw new HttpRequestValidationException("Authorization header not formatted correctly"); - } - - Dictionary parameterDict = GetParametersFromAuthHeader(authHeader); - string signature = parameterDict[PARAM_SIGNATURE]; - parameterDict.Remove(PARAM_SIGNATURE); - string algorithm = parameterDict[PARAM_ALGORITHM]; - string clientId = parameterDict[PARAM_CLIENT_ID]; - string jiveUrl = parameterDict[PARAM_JIVE_URL]; - string tenantId = parameterDict[PARAM_TENANT_ID]; - string timeStampStr = parameterDict[PARAM_TIMESTAMP]; - - long timestampMilliSeconds = Convert.ToInt64(timeStampStr); - DateTime timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(timestampMilliSeconds); - if (timestamp > DateTime.Now.AddMinutes(5) || DateTime.Now.AddMinutes(-5) > timestamp) - { - Trace.WriteLine("Timestamp older than 5 minutes"); - int timestampDiff = timestamp.CompareTo(DateTime.Now); - throw new HttpRequestValidationException("Timestamp difference more than 5 minutes. Difference: " + timestampDiff.ToString()); - } - - var _myAddon = db.JiveAddons - .Include("JiveInstance") - .Where(a => a.JiveInstance.JiveInstanceId.Equals(tenantId)); - if (_myAddon.Count() == 0) - { - Trace.WriteLine("Jive Instance not found"); - throw new HttpRequestValidationException(); - } - JiveAddon myAddon = _myAddon.Single(); - if (myAddon.ClientId.Equals(clientId) == false) - { - Trace.WriteLine("Not the expected client id for this tenant"); - throw new HttpRequestValidationException("Not the expected client id for this tenant"); - } - - string parameterStrWithoutSignature = authHeader.Substring(JIVE_EXTN.Length, authHeader.IndexOf(PARAM_SIGNATURE) - (PARAM_SIGNATURE.Length + 1)); - - - string expectedSignature = validateSignature(parameterStrWithoutSignature, myAddon.ClientSecret); - - if (expectedSignature.Equals(signature)) - { - string ownerId = userId + "@" + tenantId; - - GenericIdentity MyIdentity = new GenericIdentity(ownerId); - - String[] MyStringArray = { "User" }; - GenericPrincipal MyPrincipal = - new GenericPrincipal(MyIdentity, MyStringArray); - Thread.CurrentPrincipal = MyPrincipal; - } - else - { - Trace.WriteLine("Signature not correctly validated"); - throw new HttpRequestValidationException("Signature not correctly validated"); - } - - + tenantId = keyValue[1]; } - catch (HttpRequestValidationException authEx) - { - Trace.WriteLine(authEx.Message, "Error"); - //NewRelic.Api.Agent.NewRelic.NoticeError(authEx); - actionContext.Response = new System.Net.Http.HttpResponseMessage(); - actionContext.Response.Content = null; - actionContext.Response.StatusCode = HttpStatusCode.Unauthorized; + if (keyValue[0].Equals("jive_url")) + { + jiveUrl = HttpUtility.UrlDecode(keyValue[1]); } } + string ownerId = userId + "@" + tenantId; + GenericIdentity MyIdentity = new GenericIdentity(ownerId); + String[] MyStringArray = { "User" }; + GenericPrincipal MyPrincipal = + new GenericPrincipal(MyIdentity, MyStringArray); + Thread.CurrentPrincipal = MyPrincipal; } else { - if (actionContext.Request.Headers.Authorization != null) - { - - var authTemp = actionContext.Request.Headers.Authorization; - - string authString = authTemp.Parameter; - - //string authString = HttpContext.Current.Request.Headers["authorization"]; - string userId = HttpContext.Current.Request.Headers["x-jive-user-id"]; - - string[] authStringArray = authString.Split('&'); - string tenantId = null; - string jiveUrl = null; - foreach (string authElement in authStringArray) - { - string[] keyValue = authElement.Split('='); - if (keyValue[0].Equals("tenant_id")) - { - tenantId = keyValue[1]; - } - - if (keyValue[0].Equals("jive_url")) - { - jiveUrl = HttpUtility.UrlDecode(keyValue[1]); - } - } - string ownerId = userId + "@" + tenantId; - - GenericIdentity MyIdentity = new GenericIdentity(ownerId); - - String[] MyStringArray = { "User" }; - GenericPrincipal MyPrincipal = - new GenericPrincipal(MyIdentity, MyStringArray); - Thread.CurrentPrincipal = MyPrincipal; - - } - - else - { - - throw new HttpRequestValidationException("Authorization header not formatted correctly"); - - - } + throw new HttpRequestValidationException("Authorization header not formatted correctly"); } - - - } catch (Exception ex) { @@ -245,10 +69,7 @@ public override void OnActionExecuting(HttpActionContext actionContext) actionContext.Response = new System.Net.Http.HttpResponseMessage(); actionContext.Response.Content = null; actionContext.Response.StatusCode = HttpStatusCode.InternalServerError; - } } - - } } diff --git a/Net.Pokeshot.JiveSdk/Models/JiveSdkContext.cs b/Net.Pokeshot.JiveSdk/Models/JiveSdkContext.cs deleted file mode 100644 index 74f942e..0000000 --- a/Net.Pokeshot.JiveSdk/Models/JiveSdkContext.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Data.Entity; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Net.Pokeshot.JiveSdk.Models -{ - public class JiveSdkContext : DbContext - { - public JiveSdkContext() - : base("name=JiveSdkContext") - { - this.Configuration.LazyLoadingEnabled = false; - } - public DbSet JiveInstances { get; set; } - public DbSet Users { get; set; } - public DbSet JiveRegistrations { get; set; } - public DbSet JiveTileRegistrations { get; set; } - public DbSet JiveAddons { get; set; } - } -} diff --git a/Net.Pokeshot.JiveSdk/Net.Pokeshot.JiveSdk.csproj b/Net.Pokeshot.JiveSdk/Net.Pokeshot.JiveSdk.csproj index b40aafc..6cb7ca6 100644 --- a/Net.Pokeshot.JiveSdk/Net.Pokeshot.JiveSdk.csproj +++ b/Net.Pokeshot.JiveSdk/Net.Pokeshot.JiveSdk.csproj @@ -44,24 +44,12 @@ MinimumRecommendedRules.ruleset - - ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll - True - - - ..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll - True - - - ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll - True - ..\packages\NewRelic.Agent.Api.5.20.61.0\lib\NewRelic.Api.Agent.dll True - - ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.8.0.1\lib\net45\Newtonsoft.Json.dll True @@ -74,43 +62,7 @@ - - ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll - True - - - ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll - True - - - ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll - True - - - ..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll - True - - - ..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll - True - - - ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll - True - - - ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll - True - - - ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll - True - - - - - - + @@ -247,7 +199,6 @@ - diff --git a/Net.Pokeshot.JiveSdk/Net.Pokeshot.JiveSdk.nuspec b/Net.Pokeshot.JiveSdk/Net.Pokeshot.JiveSdk.nuspec new file mode 100644 index 0000000..4c86381 --- /dev/null +++ b/Net.Pokeshot.JiveSdk/Net.Pokeshot.JiveSdk.nuspec @@ -0,0 +1,16 @@ + + + + JiveDotNetSDK + $version$ + $title$ + pokeshot, ryanrutan, cnorick, and doublerazr + ryanrutan + https://github.com/jivesoftware/JiveDotNetSDK + https://github.com/jivesoftware/JiveDotNetSDK + https://www.jivesoftware.com/wp-content/themes/jive2015/images/brand/favicon32.png + false + C# wrapper for Jive's Rest API + jive c# api + + \ No newline at end of file diff --git a/Net.Pokeshot.JiveSdk/Util/CultureHelper.cs b/Net.Pokeshot.JiveSdk/Util/CultureHelper.cs index bdcf5fa..5494e70 100644 --- a/Net.Pokeshot.JiveSdk/Util/CultureHelper.cs +++ b/Net.Pokeshot.JiveSdk/Util/CultureHelper.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Text; using System.Threading; -using System.Threading.Tasks; namespace Net.Pokeshot.JiveSdk.Util { diff --git a/Net.Pokeshot.JiveSdk/packages.config b/Net.Pokeshot.JiveSdk/packages.config index ca3d63f..4a8c5dc 100644 --- a/Net.Pokeshot.JiveSdk/packages.config +++ b/Net.Pokeshot.JiveSdk/packages.config @@ -1,15 +1,6 @@  - - - - - - - - - - + \ No newline at end of file