Skip to content

Commit

Permalink
Added method Decode(JwtParts) to Decoder (#234)
Browse files Browse the repository at this point in the history
* Added method Decode(JwtParts) to Decoder
* Using key from README in tests
* Bumped version to 6.0.0-beta2
  • Loading branch information
abatishchev authored Dec 2, 2019
1 parent f3d6e3f commit a6fd20e
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 122 deletions.
3 changes: 2 additions & 1 deletion JWT.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
.gitignore = .gitignore
JWT.sln.DotSettings = JWT.sln.DotSettings
LICENSE.txt = LICENSE.txt
LICENSE.md = LICENSE.md
NuGet.config = NuGet.config
README.md = README.md
EndProjectSection
EndProject
Expand Down
1 change: 1 addition & 0 deletions JWT.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ThisQualifier/INSTANCE_MEMBERS_QUALIFY_MEMBERS/@EntryValue">Property</s:String>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue"></s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=HMACSHA/@EntryIndexedValue">HMACSHA</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RS/@EntryIndexedValue">RS</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RSA/@EntryIndexedValue">RSA</s:String>
Expand Down
File renamed without changes.
9 changes: 7 additions & 2 deletions src/JWT/Algorithms/RS256Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ public RS256Algorithm(X509Certificate2 cert)
public bool IsAsymmetric => true;

/// <inheritdoc />
public byte[] Sign(byte[] key, byte[] bytesToSign) =>
_privateKey is object ? Sign(bytesToSign) : throw new InvalidOperationException("Can't sign data without private key");
public byte[] Sign(byte[] key, byte[] bytesToSign)
{
if (_privateKey is null)
throw new InvalidOperationException("Can't sign data without private key");

return Sign(bytesToSign);
}

/// <summary>
/// Signs the provided bytes.
Expand Down
20 changes: 10 additions & 10 deletions src/JWT/Builder/JwtBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private void TryCreateDecoder()

private void TryCreateValidator()
{
if (_validator != null)
if (_validator is object)
return;

if (_serializer is null)
Expand Down Expand Up @@ -331,20 +331,20 @@ private void EnsureCanDecode()
/// Checks whether enough dependencies were supplied to build a new token.
/// </summary>
private bool CanBuild() =>
_algorithm != null &&
_serializer != null &&
_urlEncoder != null &&
_jwt.Payload != null &&
_algorithm is object &&
_serializer is object &&
_urlEncoder is object &&
_jwt.Payload is object &&
(_algorithm.IsAsymmetric || HasOnlyOneSecret());

/// <summary>
/// Checks whether enough dependencies were supplied to decode a token.
/// </summary>
private bool CanDecode()
{
if (_serializer != null &&
_dateTimeProvider != null &&
_urlEncoder != null)
if (_serializer is object &&
_dateTimeProvider is object &&
_urlEncoder is object)
{
return !_verify || (_algorithm?.IsAsymmetric ?? true) || HasSecrets();
}
Expand All @@ -356,13 +356,13 @@ private bool CanDecode()
/// Checks if any secret was supplied to use in token decoding
/// </summary>
private bool HasSecrets() =>
_secrets != null && _secrets.Length > 0;
_secrets is object && _secrets.Length > 0;

/// <summary>
/// Checks if there is only one secret was supplied for token encoding
/// </summary>
/// <returns></returns>
private bool HasOnlyOneSecret() =>
_secrets != null && _secrets.Length == 1;
_secrets is object && _secrets.Length == 1;
}
}
7 changes: 7 additions & 0 deletions src/JWT/IJwtDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ public interface IJwtDecoder
{
#region Decode

/// <summary>
/// Given a JWT, decodes it and return the JSON payload.
/// </summary>
/// <param name="jwt">The JWT</param>
/// <returns>A string containing the JSON payload</returns>
string Decode(JwtParts jwt);

/// <summary>
/// Given a JWT, decodes it and return the JSON payload.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</PropertyGroup>

<PropertyGroup>
<Company>Public Domain</Company>
<Copyright>Public Domain</Copyright>
<PackageDescription>Jwt.Net, a JWT (JSON Web Token) implementation for .NET</PackageDescription>
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
Expand All @@ -29,9 +28,11 @@
<!-- Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>

<Version>6.0.0-beta1</Version>
<Version>6.0.0-beta2</Version>
<FileVersion>6.0.0.0</FileVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
17 changes: 15 additions & 2 deletions src/JWT/JwtDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ public JwtDecoder(IJsonSerializer jsonSerializer, IJwtValidator jwtValidator, IB
_algFactory = algFactory;
}

/// <inheritdoc />
/// <exception cref="ArgumentException" />
/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentOutOfRangeException" />
/// <exception cref="FormatException" />
public string Decode(JwtParts jwt)
{
var decoded = _urlEncoder.Decode(jwt.Payload);
return GetString(decoded);
}

/// <inheritdoc />
/// <exception cref="ArgumentException" />
/// <exception cref="ArgumentNullException" />
Expand Down Expand Up @@ -107,12 +118,14 @@ public string Decode(string token, byte[][] keys, bool verify)
if (keys.Length == 0 || !AllKeysHaveValues(keys))
throw new ArgumentOutOfRangeException(nameof(keys));

var jwt = new JwtParts(token);

if (verify)
{
Validate(new JwtParts(token), keys);
Validate(jwt, keys);
}

return Decode(token);
return Decode(jwt);
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion src/JWT/JwtEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public JwtEncoder(IJwtAlgorithm algorithm, IJsonSerializer jsonSerializer, IBase

/// <inheritdoc />
public string Encode(object payload, string key) =>
Encode(null, payload, key != null ? GetBytes(key) : null);
Encode(null, payload, key is object ? GetBytes(key) : null);

/// <inheritdoc />
public string Encode(object payload, byte[] key) =>
Expand Down
2 changes: 1 addition & 1 deletion src/JWT/JwtValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public JwtValidator(IJsonSerializer jsonSerializer, IDateTimeProvider dateTimePr
public void Validate(string payloadJson, string decodedCrypto, params string[] decodedSignatures)
{
var ex = GetValidationException(payloadJson, decodedCrypto, decodedSignatures);
if (ex != null)
if (ex is object)
throw ex;
}

Expand Down
19 changes: 0 additions & 19 deletions tests/JWT.Tests.Common/Internal/CustomerEqualityComparer.cs

This file was deleted.

25 changes: 0 additions & 25 deletions tests/JWT.Tests.Common/Internal/DictionaryEqualityComparer.cs

This file was deleted.

6 changes: 3 additions & 3 deletions tests/JWT.Tests.Common/JwtDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public void JwtData_With_Ctor_Params()

jwtData.Header
.Should()
.Contain(headers, "because the DTO's header must match the one provided");
.Contain(headers, "because header must match the one provided");

jwtData.Payload
.Should()
.Contain(payload, "because the DTO's payload must match the one provided");
.Contain(payload, "because payload must match the one provided");
}
}
}
}
Loading

0 comments on commit a6fd20e

Please sign in to comment.