Skip to content

Commit

Permalink
Migrate to .NETStandard 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
henkmollema committed May 19, 2016
1 parent 84827df commit 7c84f2a
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 100 deletions.
15 changes: 8 additions & 7 deletions Jwt.NET.sln
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{27CE9B86-3C2B-4DC5-A4A4-9B7CE5B19FD1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EB12BB02-C15C-469F-B4A4-6DE0E187C8DB}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
NuGet.Config = NuGet.Config
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "JsonWebTokens", "src\JsonWebTokens\JsonWebTokens.xproj", "{046E9A92-AECE-49B2-B33C-78CB06A15EC7}"
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "JsonWebTokens", "src\JsonWebTokens\JsonWebTokens.xproj", "{BCDB458E-A10B-425D-81D1-A78930978394}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{046E9A92-AECE-49B2-B33C-78CB06A15EC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{046E9A92-AECE-49B2-B33C-78CB06A15EC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{046E9A92-AECE-49B2-B33C-78CB06A15EC7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{046E9A92-AECE-49B2-B33C-78CB06A15EC7}.Release|Any CPU.Build.0 = Release|Any CPU
{BCDB458E-A10B-425D-81D1-A78930978394}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BCDB458E-A10B-425D-81D1-A78930978394}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BCDB458E-A10B-425D-81D1-A78930978394}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BCDB458E-A10B-425D-81D1-A78930978394}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{046E9A92-AECE-49B2-B33C-78CB06A15EC7} = {27CE9B86-3C2B-4DC5-A4A4-9B7CE5B19FD1}
{BCDB458E-A10B-425D-81D1-A78930978394} = {27CE9B86-3C2B-4DC5-A4A4-9B7CE5B19FD1}
EndGlobalSection
EndGlobal
5 changes: 1 addition & 4 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-rc1-update1"
}
"projects": [ "src" ]
}
2 changes: 2 additions & 0 deletions src/JsonWebTokens/DefaultJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ namespace Jwt
/// </summary>
public class DefaultJsonSerializer : IJsonSerializer
{
/// <inheritdoc />
public string Serialize(object value)
{
return JsonConvert.SerializeObject(value);
}

/// <inheritdoc />
public T Deserialize<T>(string value)
{
return JsonConvert.DeserializeObject<T>(value);
Expand Down
11 changes: 2 additions & 9 deletions src/JsonWebTokens/JsonWebToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@

namespace Jwt
{
public enum JwtHashAlgorithm
{
HS256,
HS384,
HS512
}

/// <summary>
/// Provides methods for encoding and decoding JSON Web Tokens.
/// </summary>
Expand Down Expand Up @@ -303,7 +296,7 @@ private static JwtHashAlgorithm GetHashAlgorithm(string algorithm)
}
}

public static string Base64UrlEncode(byte[] input)
private static string Base64UrlEncode(byte[] input)
{
var output = Convert.ToBase64String(input);
output = output.Split('=')[0]; // Remove any trailing '='s
Expand All @@ -312,7 +305,7 @@ public static string Base64UrlEncode(byte[] input)
return output;
}

public static byte[] Base64UrlDecode(string input)
private static byte[] Base64UrlDecode(string input)
{
var output = input;
output = output.Replace('-', '+'); // 62nd char of encoding
Expand Down
14 changes: 7 additions & 7 deletions src/JsonWebTokens/JsonWebTokens.xproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="14.0.25123" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25123</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>046e9a92-aece-49b2-b33c-78cb06a15ec7</ProjectGuid>
<ProjectGuid>bcdb458e-a10b-425d-81d1-a78930978394</ProjectGuid>
<RootNamespace>Jwt</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
103 changes: 103 additions & 0 deletions src/JsonWebTokens/JwtBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Collections.Generic;

namespace Jwt
{
/// <summary>
/// Builder for <see cref="JwtData"/> objects.
/// </summary>
public class JwtBuilder
{
private readonly JwtData _data = new JwtData();

/// <summary>
/// Adds the specified payload to the JWT.
/// </summary>
/// <param name="payload">The payload.</param>
/// <returns>The <see cref="JwtBuilder"/> instance.</returns>
public JwtBuilder WithPayload(object payload)
{
_data.Payload = payload;
return this;
}

/// <summary>
/// Marks the specified payload as already serialized.
/// </summary>
/// <returns>The <see cref="JwtBuilder"/> instance.</returns>
public JwtBuilder IsSerialized()
{
_data.Serialized = true;
return this;
}

/// <summary>
/// Adds the specified key string to the JWT.
/// </summary>
/// <param name="key">The string representation of the key.</param>
/// <returns>The <see cref="JwtBuilder"/> instance.</returns>
public JwtBuilder WithKey(string key)
{
_data.Key = key;
return this;
}

/// <summary>
/// Adds the specified key bytes to the JWT.
/// </summary>
/// <param name="keyBytes">The bytes representation of the key.</param>
/// <returns>The <see cref="JwtBuilder"/> instance.</returns>
public JwtBuilder WithKey(byte[] keyBytes)
{
_data.KeyBytes = keyBytes;
return this;
}

/// <summary>
/// Specifies the algorithm being used for hashing the JWT.
/// </summary>
/// <param name="algorithm">The algorithm being used for hashing the JWT.</param>
/// <returns>The <see cref="JwtBuilder"/> instance.</returns>
public JwtBuilder WithAlgorithm(JwtHashAlgorithm algorithm)
{
_data.Algorithm = algorithm;
return this;
}

/// <summary>
/// Adds the specified key/value pair as header to the JWT.
/// </summary>
/// <param name="key">The key of the key/value pair.</param>
/// <param name="value">The value of the key/value pair.</param>
/// <returns>The <see cref="JwtBuilder"/> instance.</returns>
public JwtBuilder WithHeader(string key, object value)
{
if (_data.ExtraHeaders == null)
{
_data.ExtraHeaders = new Dictionary<string, object>();
}

_data.ExtraHeaders.Add(key, value);
return this;
}

/// <summary>
/// Adds the specified dictionary as headers to the JWT.
/// </summary>
/// <param name="dict">The dictionary with the headers of the JWT.</param>
/// <returns>The <see cref="JwtBuilder"/> instance.</returns>
public JwtBuilder WithHeaders(IDictionary<string, object> dict)
{
_data.ExtraHeaders = dict;
return this;
}

/// <summary>
/// Builds the data to a <see cref="JwtData"/> object.
/// </summary>
/// <returns>A <see cref="JwtData"/> object.</returns>
public JwtData Build()
{
return _data;
}
}
}
79 changes: 18 additions & 61 deletions src/JsonWebTokens/JwtData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,39 @@

namespace Jwt
{
/// <summary>
/// Represents a JSON Web Token.
/// </summary>
public class JwtData
{
/// <summary>
/// Gets or sets the bytes representing the key of the JWT.
/// </summary>
public byte[] KeyBytes { get; set; }

/// <summary>
/// Gets or sets a string representing the key of the JWT.
/// </summary>
public string Key { get; set; }

/// <summary>
/// Gets or sets the payload of the JWT.
/// </summary>
public object Payload { get; set; }

/// <summary>
/// Gets or sets the hashing algorithm being used for the JWT.
/// </summary>
public JwtHashAlgorithm Algorithm { get; set; } = JwtHashAlgorithm.HS256;

/// <summary>
/// Gets or sets a value whether the payload is already serialized to JSON.
/// Gets or sets a value indicating whether the payload is already serialized to JSON.
/// </summary>
public bool Serialized { get; set; }

public IDictionary<string, object> ExtraHeaders { get; set; }
}

public class JwtBuilder
{
private readonly JwtData _data = new JwtData();

public JwtBuilder WithPayload(object payload)
{
_data.Payload = payload;
return this;
}

public JwtBuilder IsSerialized()
{
_data.Serialized = true;
return this;
}

public JwtBuilder WithKey(string key)
{
_data.Key = key;
return this;
}

public JwtBuilder WithKey(byte[] keyBytes)
{
_data.KeyBytes = keyBytes;
return this;
}

public JwtBuilder WithAlgorithm(JwtHashAlgorithm algorithm)
{
_data.Algorithm = algorithm;
return this;
}

public JwtBuilder WithHeader(string key, object value)
{
if (_data.ExtraHeaders == null)
{
_data.ExtraHeaders = new Dictionary<string, object>();
}

_data.ExtraHeaders.Add(key, value);
return this;
}

public JwtBuilder WithHeaders(IDictionary<string, object> dict)
{
_data.ExtraHeaders = dict;
return this;
}

/// <summary>
/// Builds the data to a <see cref="JwtData"/> object.
/// Gets or sets a dictionary of extra heading to append to the JWT.
/// </summary>
/// <returns>A <see cref="JwtData"/> object.</returns>
public JwtData Build()
{
return _data;
}
public IDictionary<string, object> ExtraHeaders { get; set; }
}
}
23 changes: 23 additions & 0 deletions src/JsonWebTokens/JwtHashAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Jwt
{
/// <summary>
/// Represents the hashing algorithm being used used.
/// </summary>
public enum JwtHashAlgorithm
{
/// <summary>
/// Hash-based Message Authentication Code (HMAC) using SHA256.
/// </summary>
HS256,

/// <summary>
/// Hash-based Message Authentication Code (HMAC) using SHA256.
/// </summary>
HS384,

/// <summary>
/// Hash-based Message Authentication Code (HMAC) using SHA256.
/// </summary>
HS512
}
}
8 changes: 8 additions & 0 deletions src/JsonWebTokens/SignatureVerificationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

namespace Jwt
{
/// <summary>
/// Represents errors that occur during verification of the JWT signature.
/// </summary>
public class SignatureVerificationException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="SignatureVerificationException"/> class
/// with a specified error message.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public SignatureVerificationException(string message) : base(message)
{
}
Expand Down
Loading

0 comments on commit 7c84f2a

Please sign in to comment.