diff --git a/Jwt.NET.sln b/Jwt.NET.sln index f874c89..c59dcc0 100644 --- a/Jwt.NET.sln +++ b/Jwt.NET.sln @@ -1,16 +1,17 @@  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 @@ -18,15 +19,15 @@ Global 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 diff --git a/global.json b/global.json index 5ba986f..7d8f1bf 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,3 @@ { - "projects": [ "src", "test" ], - "sdk": { - "version": "1.0.0-rc1-update1" - } + "projects": [ "src" ] } diff --git a/src/JsonWebTokens/DefaultJsonSerializer.cs b/src/JsonWebTokens/DefaultJsonSerializer.cs index 2a7ffe3..b5db578 100644 --- a/src/JsonWebTokens/DefaultJsonSerializer.cs +++ b/src/JsonWebTokens/DefaultJsonSerializer.cs @@ -7,11 +7,13 @@ namespace Jwt /// public class DefaultJsonSerializer : IJsonSerializer { + /// public string Serialize(object value) { return JsonConvert.SerializeObject(value); } + /// public T Deserialize(string value) { return JsonConvert.DeserializeObject(value); diff --git a/src/JsonWebTokens/JsonWebToken.cs b/src/JsonWebTokens/JsonWebToken.cs index 99d76d4..ac00d6f 100644 --- a/src/JsonWebTokens/JsonWebToken.cs +++ b/src/JsonWebTokens/JsonWebToken.cs @@ -5,13 +5,6 @@ namespace Jwt { - public enum JwtHashAlgorithm - { - HS256, - HS384, - HS512 - } - /// /// Provides methods for encoding and decoding JSON Web Tokens. /// @@ -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 @@ -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 diff --git a/src/JsonWebTokens/JsonWebTokens.xproj b/src/JsonWebTokens/JsonWebTokens.xproj index 8edfc48..68e673d 100644 --- a/src/JsonWebTokens/JsonWebTokens.xproj +++ b/src/JsonWebTokens/JsonWebTokens.xproj @@ -1,18 +1,18 @@  - + - 14.0 + 14.0.25123 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - + - 046e9a92-aece-49b2-b33c-78cb06a15ec7 + bcdb458e-a10b-425d-81d1-a78930978394 Jwt - ..\..\artifacts\obj\$(MSBuildProjectName) - ..\..\artifacts\bin\$(MSBuildProjectName)\ + .\obj + .\bin\ 2.0 - + \ No newline at end of file diff --git a/src/JsonWebTokens/JwtBuilder.cs b/src/JsonWebTokens/JwtBuilder.cs new file mode 100644 index 0000000..d711f35 --- /dev/null +++ b/src/JsonWebTokens/JwtBuilder.cs @@ -0,0 +1,103 @@ +using System.Collections.Generic; + +namespace Jwt +{ + /// + /// Builder for objects. + /// + public class JwtBuilder + { + private readonly JwtData _data = new JwtData(); + + /// + /// Adds the specified payload to the JWT. + /// + /// The payload. + /// The instance. + public JwtBuilder WithPayload(object payload) + { + _data.Payload = payload; + return this; + } + + /// + /// Marks the specified payload as already serialized. + /// + /// The instance. + public JwtBuilder IsSerialized() + { + _data.Serialized = true; + return this; + } + + /// + /// Adds the specified key string to the JWT. + /// + /// The string representation of the key. + /// The instance. + public JwtBuilder WithKey(string key) + { + _data.Key = key; + return this; + } + + /// + /// Adds the specified key bytes to the JWT. + /// + /// The bytes representation of the key. + /// The instance. + public JwtBuilder WithKey(byte[] keyBytes) + { + _data.KeyBytes = keyBytes; + return this; + } + + /// + /// Specifies the algorithm being used for hashing the JWT. + /// + /// The algorithm being used for hashing the JWT. + /// The instance. + public JwtBuilder WithAlgorithm(JwtHashAlgorithm algorithm) + { + _data.Algorithm = algorithm; + return this; + } + + /// + /// Adds the specified key/value pair as header to the JWT. + /// + /// The key of the key/value pair. + /// The value of the key/value pair. + /// The instance. + public JwtBuilder WithHeader(string key, object value) + { + if (_data.ExtraHeaders == null) + { + _data.ExtraHeaders = new Dictionary(); + } + + _data.ExtraHeaders.Add(key, value); + return this; + } + + /// + /// Adds the specified dictionary as headers to the JWT. + /// + /// The dictionary with the headers of the JWT. + /// The instance. + public JwtBuilder WithHeaders(IDictionary dict) + { + _data.ExtraHeaders = dict; + return this; + } + + /// + /// Builds the data to a object. + /// + /// A object. + public JwtData Build() + { + return _data; + } + } +} \ No newline at end of file diff --git a/src/JsonWebTokens/JwtData.cs b/src/JsonWebTokens/JwtData.cs index b141cfd..6208918 100644 --- a/src/JsonWebTokens/JwtData.cs +++ b/src/JsonWebTokens/JwtData.cs @@ -2,82 +2,39 @@ namespace Jwt { + /// + /// Represents a JSON Web Token. + /// public class JwtData { + /// + /// Gets or sets the bytes representing the key of the JWT. + /// public byte[] KeyBytes { get; set; } + /// + /// Gets or sets a string representing the key of the JWT. + /// public string Key { get; set; } + /// + /// Gets or sets the payload of the JWT. + /// public object Payload { get; set; } + /// + /// Gets or sets the hashing algorithm being used for the JWT. + /// public JwtHashAlgorithm Algorithm { get; set; } = JwtHashAlgorithm.HS256; /// - /// 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. /// public bool Serialized { get; set; } - public IDictionary 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(); - } - - _data.ExtraHeaders.Add(key, value); - return this; - } - - public JwtBuilder WithHeaders(IDictionary dict) - { - _data.ExtraHeaders = dict; - return this; - } - /// - /// Builds the data to a object. + /// Gets or sets a dictionary of extra heading to append to the JWT. /// - /// A object. - public JwtData Build() - { - return _data; - } + public IDictionary ExtraHeaders { get; set; } } } diff --git a/src/JsonWebTokens/JwtHashAlgorithm.cs b/src/JsonWebTokens/JwtHashAlgorithm.cs new file mode 100644 index 0000000..907f989 --- /dev/null +++ b/src/JsonWebTokens/JwtHashAlgorithm.cs @@ -0,0 +1,23 @@ +namespace Jwt +{ + /// + /// Represents the hashing algorithm being used used. + /// + public enum JwtHashAlgorithm + { + /// + /// Hash-based Message Authentication Code (HMAC) using SHA256. + /// + HS256, + + /// + /// Hash-based Message Authentication Code (HMAC) using SHA256. + /// + HS384, + + /// + /// Hash-based Message Authentication Code (HMAC) using SHA256. + /// + HS512 + } +} \ No newline at end of file diff --git a/src/JsonWebTokens/SignatureVerificationException.cs b/src/JsonWebTokens/SignatureVerificationException.cs index 7c1af0b..66a230e 100644 --- a/src/JsonWebTokens/SignatureVerificationException.cs +++ b/src/JsonWebTokens/SignatureVerificationException.cs @@ -2,8 +2,16 @@ namespace Jwt { + /// + /// Represents errors that occur during verification of the JWT signature. + /// public class SignatureVerificationException : Exception { + /// + /// Initializes a new instance of the class + /// with a specified error message. + /// + /// The message that describes the error. public SignatureVerificationException(string message) : base(message) { } diff --git a/src/JsonWebTokens/project.json b/src/JsonWebTokens/project.json index 43e63af..04472b5 100644 --- a/src/JsonWebTokens/project.json +++ b/src/JsonWebTokens/project.json @@ -1,28 +1,41 @@ { - "version": "1.0.1", + "version": "1.1.0", "title": "Jwt.NET", - "description": "JSON Web Tokens implementation for .NET.", "authors": [ "Henk Mollema" ], - "owners": [ "Henk Mollema" ], - "tags": [ "jwt", "tokens", ".net" ], - "projectUrl": "https://github.com/henkmollema/Jwt.NET", - "licenseUrl": "https://github.com/henkmollema/Jwt.NET/blob/master/LICENSE", "copyright": "Copyright 2016 Henk Mollema", + "description": "JSON Web Tokens implementation for .NET Core.", + "packOptions": { + "owners": [ "Henk Mollema" ], + "tags": [ "jwt", "tokens", ".net" ], + "projectUrl": "https://github.com/henkmollema/Jwt.NET", + "licenseUrl": "https://github.com/henkmollema/Jwt.NET/blob/master/LICENSE", + "repository": { + "type": "git", + "url": "git://github.com/henkmollema/Jwt.NET" + } + }, + + "buildOptions": { + "xmlDoc": true + }, "dependencies": { - "Newtonsoft.Json": "7.0.1" + "Newtonsoft.Json": "8.0.4-beta1" }, "frameworks": { "net40": { }, "net45": { }, "net451": { }, - "dotnet5.4": { + "netstandard1.3": { "dependencies": { - "Microsoft.CSharp": "4.0.1-beta-23516", - "System.Runtime": "4.0.21-beta-23516", - "System.Security.Cryptography.Algorithms": "4.0.0-beta-23516" - } + "Microsoft.CSharp": "4.0.1-*", + "System.Security.Cryptography.Algorithms": "4.1.0-*" + }, + "imports": [ + "dotnet5.4", + "portable-net451+win8" + ] } } }