Skip to content

Commit

Permalink
Merge branch 'pr-25'
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin Rader committed May 15, 2015
2 parents 6091239 + 6dd9e05 commit 2f6ebc3
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 61 deletions.
74 changes: 53 additions & 21 deletions JWT.Tests/DecodeTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using FluentAssertions;

Expand All @@ -17,45 +14,80 @@ public class DecodeTests
string token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJCb2IiLCJBZ2UiOjM3fQ.cr0xw8c_HKzhFBMQrseSPGoJ0NPlRp_3BKzP96jwBdY";
Customer customer = new Customer() { FirstName = "Bob", Age = 37 };

private Dictionary<string, object> dictionaryPayload = new Dictionary<string, object>() {
{ "FirstName", "Bob" },
{ "Age", 37 }
};

[TestMethod]
public void Should_Decode_Token_To_Json_Encoded_String()
{
var expected_payload = jsonSerializer.Serialize(customer);
var expectedPayload = jsonSerializer.Serialize(customer);

string decoded_payload = JWT.JsonWebToken.Decode(token, "ABC", false);
string decodedPayload = JsonWebToken.Decode(token, "ABC", false);

Assert.AreEqual(expected_payload, decoded_payload);
Assert.AreEqual(expectedPayload, decodedPayload);
}

[TestMethod]
public void Should_Decode_Token_To_Dictionary()
{
Dictionary<string, object> expected_payload = new Dictionary<string, object>() {
{ "FirstName", "Bob" },
{ "Age", 37 }
};
object decodedPayload = JsonWebToken.DecodeToObject(token, "ABC", false);

decodedPayload.ShouldBeEquivalentTo(dictionaryPayload, options=>options.IncludingAllRuntimeProperties());
}

[TestMethod]
public void Should_Decode_Token_To_Dictionary_With_ServiceStack()
{
JsonWebToken.JsonSerializer = new ServiceStackJsonSerializer();

object decoded_payload = JWT.JsonWebToken.DecodeToObject(token, "ABC", false);
object decodedPayload = JsonWebToken.DecodeToObject(token, "ABC", false);

decoded_payload.ShouldBeEquivalentTo(expected_payload, options=>options.IncludingAllRuntimeProperties());
decodedPayload.ShouldBeEquivalentTo(dictionaryPayload, options => options.IncludingAllRuntimeProperties());
}

[TestMethod]
public void Should_Decode_Token_To_Dictionary_With_Newtonsoft() {
JsonWebToken.JsonSerializer = new NewtonJsonSerializer();

object decodedPayload = JsonWebToken.DecodeToObject(token, "ABC", false);

decodedPayload.ShouldBeEquivalentTo(dictionaryPayload, options => options.IncludingAllRuntimeProperties());
}

[TestMethod]
public void Should_Decode_Token_To_Generic_Type()
{
Customer expected_payload = customer;
Customer decodedPayload = JsonWebToken.DecodeToObject<Customer>(token, "ABC", false);

decodedPayload.ShouldBeEquivalentTo(customer);
}

[TestMethod]
public void Should_Decode_Token_To_Generic_Type_With_ServiceStack() {
JsonWebToken.JsonSerializer = new ServiceStackJsonSerializer();

Customer decodedPayload = JsonWebToken.DecodeToObject<Customer>(token, "ABC", false);

decodedPayload.ShouldBeEquivalentTo(customer);
}

[TestMethod]
public void Should_Decode_Token_To_Generic_Type_With_Newtonsoft() {
JsonWebToken.JsonSerializer = new NewtonJsonSerializer();

Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(token, "ABC", false);
Customer decodedPayload = JsonWebToken.DecodeToObject<Customer>(token, "ABC", false);

decoded_payload.ShouldBeEquivalentTo(expected_payload);
decodedPayload.ShouldBeEquivalentTo(customer);
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Should_Throw_On_Malformed_Token() {
string malformedtoken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9eyJGaXJzdE5hbWUiOiJCb2IiLCJBZ2UiOjM3fQ.cr0xw8c_HKzhFBMQrseSPGoJ0NPlRp_3BKzP96jwBdY";

Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(malformedtoken, "ABC", false);
JsonWebToken.DecodeToObject<Customer>(malformedtoken, "ABC", false);
}

[TestMethod]
Expand All @@ -64,16 +96,16 @@ public void Should_Throw_On_Invalid_Key()
{
string invalidkey = "XYZ";

Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(token, invalidkey, true);
JsonWebToken.DecodeToObject<Customer>(token, invalidkey, true);
}

[TestMethod]
[ExpectedException(typeof(SignatureVerificationException))]
public void Should_Throw_On_Invalid_Expiration_Claim()
{
var invalidexptoken = JWT.JsonWebToken.Encode(new { exp = "asdsad" }, "ABC", JwtHashAlgorithm.HS256);
var invalidexptoken = JsonWebToken.Encode(new { exp = "asdsad" }, "ABC", JwtHashAlgorithm.HS256);

Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(invalidexptoken, "ABC", true);
JsonWebToken.DecodeToObject<Customer>(invalidexptoken, "ABC", true);
}

[TestMethod]
Expand All @@ -83,9 +115,9 @@ public void Should_Throw_On_Expired_Token()
var anHourAgoUtc = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0));
Int32 unixTimestamp = (Int32)(anHourAgoUtc.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

var invalidexptoken = JWT.JsonWebToken.Encode(new { exp=unixTimestamp }, "ABC", JwtHashAlgorithm.HS256);
var invalidexptoken = JsonWebToken.Encode(new { exp=unixTimestamp }, "ABC", JwtHashAlgorithm.HS256);

Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(invalidexptoken, "ABC", true);
JsonWebToken.DecodeToObject<Customer>(invalidexptoken, "ABC", true);
}
}

Expand Down
45 changes: 40 additions & 5 deletions JWT.Tests/EncodeTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Script.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace JWT.Tests
Expand All @@ -15,7 +13,7 @@ public class EncodeTests
[TestMethod]
public void Should_Encode_Type()
{
string result = JWT.JsonWebToken.Encode(customer, "ABC", JwtHashAlgorithm.HS256);
string result = JsonWebToken.Encode(customer, "ABC", JwtHashAlgorithm.HS256);

Assert.AreEqual(token, result);
}
Expand All @@ -25,7 +23,44 @@ public void Should_Encode_Type_With_Extra_Headers()
{
var extraheaders = new Dictionary<string, object>() { {"foo", "bar"} };

string result = JWT.JsonWebToken.Encode(extraheaders, customer, "ABC", JwtHashAlgorithm.HS256);
string result = JsonWebToken.Encode(extraheaders, customer, "ABC", JwtHashAlgorithm.HS256);

Assert.AreEqual(extraheaderstoken, result);
}

[TestMethod]
public void Should_Encode_Type_With_ServiceStack()
{
JsonWebToken.JsonSerializer = new ServiceStackJsonSerializer();
string result = JsonWebToken.Encode(customer, "ABC", JwtHashAlgorithm.HS256);

Assert.AreEqual(token, result);
}

[TestMethod]
public void Should_Encode_Type_With_ServiceStack_And_Extra_Headers() {
JsonWebToken.JsonSerializer = new ServiceStackJsonSerializer();

var extraheaders = new Dictionary<string, object>() { { "foo", "bar" } };
string result = JsonWebToken.Encode(extraheaders, customer, "ABC", JwtHashAlgorithm.HS256);

Assert.AreEqual(extraheaderstoken, result);
}

[TestMethod]
public void Should_Encode_Type_With_Newtonsoft_Serializer() {
JsonWebToken.JsonSerializer = new NewtonJsonSerializer();
string result = JsonWebToken.Encode(customer, "ABC", JwtHashAlgorithm.HS256);

Assert.AreEqual(token, result);
}

[TestMethod]
public void Should_Encode_Type_With_Newtonsoft_Serializer_And_Extra_Headers() {
JsonWebToken.JsonSerializer = new NewtonJsonSerializer();

var extraheaders = new Dictionary<string, object>() { { "foo", "bar" } };
string result = JsonWebToken.Encode(extraheaders, customer, "ABC", JwtHashAlgorithm.HS256);

Assert.AreEqual(extraheaderstoken, result);
}
Expand Down
8 changes: 8 additions & 0 deletions JWT.Tests/JWT.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
<HintPath>..\packages\FluentAssertions.3.3.0\lib\net45\FluentAssertions.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="ServiceStack.Text">
<HintPath>..\packages\ServiceStack.Text.4.0.40\lib\net40\ServiceStack.Text.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml" />
Expand All @@ -64,6 +71,7 @@
<Compile Include="DecodeTests.cs" />
<Compile Include="EncodeTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Serializers.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JWT\JWT.csproj">
Expand Down
31 changes: 31 additions & 0 deletions JWT.Tests/Serializers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Newtonsoft.Json;
using JsonSerializer = ServiceStack.Text.JsonSerializer;

namespace JWT.Tests
{
public class ServiceStackJsonSerializer : IJsonSerializer
{
public string Serialize(object obj)
{
return JsonSerializer.SerializeToString(obj);
}

public T Deserialize<T>(string json)
{
return JsonSerializer.DeserializeFromString<T>(json);
}
}

public class NewtonJsonSerializer : IJsonSerializer
{
public string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj);
}

public T Deserialize<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
}
}
2 changes: 2 additions & 0 deletions JWT.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentAssertions" version="3.3.0" targetFramework="net451" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
<package id="ServiceStack.Text" version="4.0.40" targetFramework="net451" />
</packages>
33 changes: 33 additions & 0 deletions JWT/DefaultJsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Web.Script.Serialization;

namespace JWT
{
/// <summary>
/// JSON Serializer using JavaScriptSerializer
/// </summary>
public class DefaultJsonSerializer : IJsonSerializer
{
private readonly JavaScriptSerializer serializer = new JavaScriptSerializer();

/// <summary>
/// Serialize an object to JSON string
/// </summary>
/// <param name="obj">object</param>
/// <returns>JSON string</returns>
public string Serialize(object obj)
{
return serializer.Serialize(obj);
}

/// <summary>
/// Deserialize a JSON string to typed object.
/// </summary>
/// <typeparam name="T">type of object</typeparam>
/// <param name="json">JSON string</param>
/// <returns>typed object</returns>
public T Deserialize<T>(string json)
{
return serializer.Deserialize<T>(json);
}
}
}
23 changes: 23 additions & 0 deletions JWT/IJsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace JWT
{
/// <summary>
/// Provides JSON Serialize and Deserialize. Allows custom serializers used.
/// </summary>
public interface IJsonSerializer
{
/// <summary>
/// Serialize an object to JSON string
/// </summary>
/// <param name="obj">object</param>
/// <returns>JSON string</returns>
string Serialize(object obj);

/// <summary>
/// Deserialize a JSON string to typed object.
/// </summary>
/// <typeparam name="T">type of object</typeparam>
/// <param name="json">JSON string</param>
/// <returns>typed object</returns>
T Deserialize<T>(string json);
}
}
Loading

0 comments on commit 2f6ebc3

Please sign in to comment.