Skip to content

Commit

Permalink
Calling Encoding methods from EncodingHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
abatishchev committed Feb 19, 2019
1 parent 41eeeca commit c772518
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/JWT/Builder/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ internal static class EnumExtensions
public static string GetPublicClaimName(this ClaimName value) => GetDescription(value);

/// <summary>
/// Gets the value of the Describtion Attribute from the object.
/// Gets the value of the <see cref="DescriptionAttribute" /> from the object.
/// </summary>
/// <param name="value">An object that is decorated with <see cref="DescriptionAttribute"/></param>
private static string GetDescription(object value) => value.GetType()
.GetField(value.ToString())
.GetCustomAttribute<DescriptionAttribute>()?.Description ?? value.ToString();
Expand Down
7 changes: 5 additions & 2 deletions src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Linq;
using JWT.Algorithms;
using JWT.Serializers;

using static JWT.Internal.EncodingHelper;

namespace JWT.Builder
{
/// <summary>
Expand All @@ -20,7 +23,7 @@ public sealed class JwtBuilder
private IDateTimeProvider _dateTimeProvider = new UtcDateTimeProvider();

private IJwtAlgorithm _algorithm;
private string[] _secrets;
private byte[][] _secrets;
private bool _verify;

/// <summary>
Expand Down Expand Up @@ -157,7 +160,7 @@ public JwtBuilder WithAlgorithm(IJwtAlgorithm algorithm)
/// <returns>Current builder instance</returns>
public JwtBuilder WithSecret(params string[] secrets)
{
_secrets = secrets;
_secrets = secrets.Select(s => GetBytes(s)).ToArray();
return this;
}

Expand Down
18 changes: 18 additions & 0 deletions src/JWT/Internal/EncodingHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JWT.Internal
{
internal static class EncodingHelper
{
internal static byte[] GetBytes(string input) =>
Encoding.UTF8.GetBytes(input);

internal static byte[][] GetBytes(IEnumerable<string> input) =>
input.Select(GetBytes).ToArray();

internal static string GetString(byte[] bytes) =>
Encoding.UTF8.GetString(bytes);
}
}
4 changes: 2 additions & 2 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<Description>Jwt.Net, a JWT (JSON Web Token) implementation for .NET</Description>
<RepositoryUrl>https://github.com/jwt-dotnet/jwt</RepositoryUrl>
<PackageProjectUrl>https://github.com/jwt-dotnet/jwt</PackageProjectUrl>
<Authors>John Sheehan, Michael Lehenbauer, Alexander Batishchev</Authors>
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageLicenseUrl>https://creativecommons.org/publicdomain/zero/1.0/</PackageLicenseUrl>
<Version>5.0.0</Version>
<Version>5.0.1</Version>
<PackageTags>jwt json</PackageTags>
<FileVersion>5.0.0.0</FileVersion>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
Expand Down
12 changes: 3 additions & 9 deletions src/JWT/JwtDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JWT.Algorithms;

using static JWT.Internal.EncodingHelper;

namespace JWT
{
/// <summary>
Expand Down Expand Up @@ -273,14 +274,7 @@ public void Validate(JwtParts jwt, IReadOnlyCollection<byte[]> keys)
_jwtValidator.Validate(payloadJson, decodedCrypto, decodedSignatures);
}

private static byte[] GetBytes(string input) => Encoding.UTF8.GetBytes(input);

private static string GetString(byte[] bytes) => Encoding.UTF8.GetString(bytes);

private static IReadOnlyCollection<byte[]> GetBytes(IEnumerable<string> input) =>
input.Select(key => GetBytes(key)).ToArray();

private static bool AllKeysHaveValues(IReadOnlyCollection<byte[]> keys) =>
private static bool AllKeysHaveValues(IEnumerable<byte[]> keys) =>
keys.All(key => key.Length != 0);
}
}
5 changes: 2 additions & 3 deletions src/JWT/JwtEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;
using JWT.Algorithms;

using static JWT.Internal.EncodingHelper;

namespace JWT
{
/// <summary>
Expand Down Expand Up @@ -63,7 +64,5 @@ public string Encode(IDictionary<string, object> extraHeaders, object payload, b

return String.Join(".", segments.ToArray());
}

private static byte[] GetBytes(string input) => Encoding.UTF8.GetBytes(input);
}
}
7 changes: 2 additions & 5 deletions src/JWT/JwtValidator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using static JWT.Internal.EncodingHelper;

namespace JWT
{
Expand Down Expand Up @@ -206,9 +207,5 @@ private static Exception ValidateNbfClaim(IReadOnlyDictionary<string, object> pa

return null;
}

internal static byte[] GetBytes(string input) => Encoding.UTF8.GetBytes(input);

internal static string GetString(byte[] bytes) => Encoding.UTF8.GetString(bytes);
}
}
26 changes: 14 additions & 12 deletions tests/JWT.Tests.Common/JwtValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using JWT.Tests.Common.Models;
using Xunit;

using static JWT.Internal.EncodingHelper;

namespace JWT.Tests.Common
{
public class JwtValidatorTest
Expand All @@ -29,14 +31,14 @@ public void Validate_Should_Throw_Exception_When_Crypto_Does_Not_Match_Signature

var jwt = new JwtParts(TestData.Token);

var payloadJson = JwtValidator.GetString(urlEncoder.Decode(jwt.Payload));
var payloadJson = GetString(urlEncoder.Decode(jwt.Payload));

var crypto = urlEncoder.Decode(jwt.Signature);
var decodedCrypto = Convert.ToBase64String(crypto);

var alg = new HMACSHA256Algorithm();
var bytesToSign = JwtValidator.GetBytes(String.Concat(jwt.Header, ".", jwt.Payload));
var signatureData = alg.Sign(JwtValidator.GetBytes("ABC"), bytesToSign);
var bytesToSign = GetBytes(String.Concat(jwt.Header, ".", jwt.Payload));
var signatureData = alg.Sign(GetBytes("ABC"), bytesToSign);
signatureData[0]++; // malformed signature
var decodedSignature = Convert.ToBase64String(signatureData);

Expand All @@ -53,14 +55,14 @@ public void Validate_Should_Not_Throw_Exception_When_Crypto_Matches_Signature()

var jwt = new JwtParts(TestData.Token);

var payloadJson = JwtValidator.GetString(urlEncoder.Decode(jwt.Payload));
var payloadJson = GetString(urlEncoder.Decode(jwt.Payload));

var crypto = urlEncoder.Decode(jwt.Signature);
var decodedCrypto = Convert.ToBase64String(crypto);

var alg = new HMACSHA256Algorithm();
var bytesToSign = JwtValidator.GetBytes(String.Concat(jwt.Header, ".", jwt.Payload));
var signatureData = alg.Sign(JwtValidator.GetBytes("ABC"), bytesToSign);
var bytesToSign = GetBytes(String.Concat(jwt.Header, ".", jwt.Payload));
var signatureData = alg.Sign(GetBytes("ABC"), bytesToSign);
var decodedSignature = Convert.ToBase64String(signatureData);

var jwtValidator = new JwtValidator(jsonNetSerializer, utcDateTimeProvider);
Expand Down Expand Up @@ -90,14 +92,14 @@ public void TryValidate_Should_Return_False_And_Exception_Not_Null_When_Crypto_M

var jwt = new JwtParts(TestData.Token);

var payloadJson = JwtValidator.GetString(urlEncoder.Decode(jwt.Payload));
var payloadJson = GetString(urlEncoder.Decode(jwt.Payload));

var crypto = urlEncoder.Decode(jwt.Signature);
var decodedCrypto = Convert.ToBase64String(crypto);

var alg = new HMACSHA256Algorithm();
var bytesToSign = JwtValidator.GetBytes(String.Concat(jwt.Header, ".", jwt.Payload));
var signatureData = alg.Sign(JwtValidator.GetBytes("ABC"), bytesToSign);
var bytesToSign = GetBytes(String.Concat(jwt.Header, ".", jwt.Payload));
var signatureData = alg.Sign(GetBytes("ABC"), bytesToSign);
signatureData[0]++; // malformed signature
var decodedSignature = Convert.ToBase64String(signatureData);

Expand All @@ -117,14 +119,14 @@ public void TryValidate_Should_Return_True_And_Exception_Null_When_Crypto_Matche

var jwt = new JwtParts(TestData.Token);

var payloadJson = JwtValidator.GetString(urlEncoder.Decode(jwt.Payload));
var payloadJson = GetString(urlEncoder.Decode(jwt.Payload));

var crypto = urlEncoder.Decode(jwt.Signature);
var decodedCrypto = Convert.ToBase64String(crypto);

var alg = new HMACSHA256Algorithm();
var bytesToSign = JwtValidator.GetBytes(String.Concat(jwt.Header, ".", jwt.Payload));
var signatureData = alg.Sign(JwtValidator.GetBytes("ABC"), bytesToSign);
var bytesToSign = GetBytes(String.Concat(jwt.Header, ".", jwt.Payload));
var signatureData = alg.Sign(GetBytes("ABC"), bytesToSign);
var decodedSignature = Convert.ToBase64String(signatureData);

var jwtValidator = new JwtValidator(jsonNetSerializer, utcDateTimeProvider);
Expand Down

0 comments on commit c772518

Please sign in to comment.