Skip to content

Commit

Permalink
chore: remove Newtonsoft.Json dependency, closes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddesmet committed Jun 20, 2024
1 parent ec1b477 commit fac4979
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 105 deletions.
143 changes: 71 additions & 72 deletions src/Paseto/Paseto.csproj
Original file line number Diff line number Diff line change
@@ -1,72 +1,71 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Label="Build">
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Label="Package">
<Authors>David De Smet</Authors>
<Company />
<Description>A Paseto (Platform-Agnostic Security Tokens) implementation for .NET</Description>
<Copyright>Copyright © 2018-2024 David De Smet</Copyright>
<PackageId>Paseto.Core</PackageId>
<PackageTags>paseto paserk tokens cryptography dotnet dotnet-core netstandard</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/daviddesmet/paseto-dotnet</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>https://github.com/daviddesmet/paseto-dotnet/releases</PackageReleaseNotes>
<RepositoryUrl>https://github.com/daviddesmet/paseto-dotnet.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<RepositoryBranch>master</RepositoryBranch>
</PropertyGroup>

<PropertyGroup Label="Signing">
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../../Key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<PropertyGroup Label="Source Link">
<!-- Optional: Declare that the Repository URL can be published to NuSpec -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- Optional: Embed source files that are not tracked by the source control manager to the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- Optional: Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<!-- Deterministic -->
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<None Include="../../README.md" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

<ItemGroup Label="Package References">
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" Version="8.0.0" />
<PackageReference Include="MinVer" Version="5.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NaCl.Core" Version="2.0.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
</ItemGroup>

<ItemGroup Label="Package References for Windows" Condition=" '$(TargetFramework)' == 'net48' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

<Target Name="Versioning" BeforeTargets="MinVer">
<PropertyGroup Label="Build">
<AssemblyVersion>$(MinVerMajor).$(MinVerMinor).$(MinVerPatch).0</AssemblyVersion>
<MinVerVerbosity>normal</MinVerVerbosity>
</PropertyGroup>
</Target>

<Target Name="LogDebugInfo">
<Message Text="Building for $(TargetFramework) on $(OS)" Importance="High" />
</Target>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Label="Build">
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Label="Package">
<Authors>David De Smet</Authors>
<Company />
<Description>A Paseto (Platform-Agnostic Security Tokens) implementation for .NET</Description>
<Copyright>Copyright © 2018-2024 David De Smet</Copyright>
<PackageId>Paseto.Core</PackageId>
<PackageTags>paseto paserk tokens cryptography dotnet dotnet-core netstandard</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/daviddesmet/paseto-dotnet</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>https://github.com/daviddesmet/paseto-dotnet/releases</PackageReleaseNotes>
<RepositoryUrl>https://github.com/daviddesmet/paseto-dotnet.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<RepositoryBranch>master</RepositoryBranch>
</PropertyGroup>

<PropertyGroup Label="Signing">
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../../Key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<PropertyGroup Label="Source Link">
<!-- Optional: Declare that the Repository URL can be published to NuSpec -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- Optional: Embed source files that are not tracked by the source control manager to the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- Optional: Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<!-- Deterministic -->
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<None Include="../../README.md" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

<ItemGroup Label="Package References">
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" Version="8.0.0" />
<PackageReference Include="MinVer" Version="5.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NaCl.Core" Version="2.0.5" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
</ItemGroup>

<ItemGroup Label="Package References for Windows" Condition=" '$(TargetFramework)' == 'net48' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

<Target Name="Versioning" BeforeTargets="MinVer">
<PropertyGroup Label="Build">
<AssemblyVersion>$(MinVerMajor).$(MinVerMinor).$(MinVerPatch).0</AssemblyVersion>
<MinVerVerbosity>normal</MinVerVerbosity>
</PropertyGroup>
</Target>

<Target Name="LogDebugInfo">
<Message Text="Building for $(TargetFramework) on $(OS)" Importance="High" />
</Target>

</Project>
2 changes: 1 addition & 1 deletion src/Paseto/PasetoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public PasetoToken(string token, string payload)
if (string.IsNullOrWhiteSpace(token))
throw new ArgumentNullException(nameof(token));

var parts = token.Split(new[] { '.' }, MAX_SEGMENT_LEN + 1);
var parts = token.Split(['.'], MAX_SEGMENT_LEN + 1);
if (parts.Length != 3 && parts.Length != 4)
throw new PasetoInvalidException("The specified token has an invalid number of segments");

Expand Down
16 changes: 10 additions & 6 deletions src/Paseto/Serializers/IJsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Paseto.Serializers;

using System.Text.Json;

/// <summary>
/// Provides JSON Serialize and Deserialize. Allows custom serializers used.
/// </summary>
Expand All @@ -8,15 +10,17 @@ public interface IJsonSerializer
/// <summary>
/// Serialize an object to JSON string
/// </summary>
/// <param name="obj">object</param>
/// <param name="obj">The object to serialize</param>
/// <param name="options">Provides options to use with <see cref="JsonSerializer" /></param>
/// <returns>JSON string</returns>
string Serialize(object obj);
string Serialize(object obj, JsonSerializerOptions options = null);

/// <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);
/// <typeparam name="T">The type of object</typeparam>
/// <param name="json">The JSON string</param>
/// <param name="options">Provides options to use with <see cref="JsonSerializer" /></param>
/// <returns>A typed object</returns>
T Deserialize<T>(string json, JsonSerializerOptions options = null);
}
25 changes: 4 additions & 21 deletions src/Paseto/Serializers/JsonNetSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
namespace Paseto.Serializers;

using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;

/// <summary>
/// JSON serializer using Newtonsoft.Json implementation.
/// JSON serializer using System.Text.Json implementation.
/// </summary>
public sealed class JsonNetSerializer : IJsonSerializer
{
private readonly JsonSerializer _serializer;

/// <summary>
/// Creates a new instance of <see cref="JsonNetSerializer" />.
/// </summary>
/// <remarks>Uses <see cref="JsonSerializer.CreateDefault()" /> as internal serializer.</remarks>
public JsonNetSerializer() : this(JsonSerializer.CreateDefault()) { }

/// <summary>
/// Creates a new instance of <see cref="JsonNetSerializer" />.
/// </summary>
/// <param name="serializer">Internal <see cref="JsonSerializer" /> to use for serialization.</param>
public JsonNetSerializer(JsonSerializer serializer) => _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));

/// <inheritdoc />
public string Serialize(object obj) => JObject.FromObject(obj, _serializer).ToString(_serializer.Formatting, _serializer.Converters.ToArray());
public string Serialize(object obj, JsonSerializerOptions options = null) => JsonSerializer.Serialize(obj, options);

/// <inheritdoc />
public T Deserialize<T>(string json) => JObject.Parse(json).ToObject<T>(_serializer);
public T Deserialize<T>(string json, JsonSerializerOptions options = null) => JsonSerializer.Deserialize<T>(json, options);
}
15 changes: 14 additions & 1 deletion src/Paseto/Validators/EqualValidator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Paseto.Validators;

using System;
using System.Text.Json;
using Internal;

/// <summary>
Expand All @@ -13,6 +14,7 @@ public sealed class EqualValidator : BaseValidator
/// Initializes a new instance of the <see cref="EqualValidator"/> class.
/// </summary>
/// <param name="payload">The payload.</param>
/// <param name="claim">The claim.</param>
public EqualValidator(PasetoPayload payload, string claim) : base(payload)
{
if (string.IsNullOrWhiteSpace(claim))
Expand All @@ -34,7 +36,7 @@ public EqualValidator(PasetoPayload payload, string claim) : base(payload)
/// <exception cref="PasetoTokenValidationException">
/// Token is not yet valid.
/// </exception>
public override void Validate(IComparable expected)
public override void Validate(IComparable expected = null)
{
if (!Payload.TryGetValue(ClaimName, out var value))
throw new PasetoTokenValidationException($"Claim '{ClaimName}' not found.");
Expand All @@ -46,6 +48,9 @@ public override void Validate(IComparable expected)
}
else
{
if (value is JsonElement json)
value = GetValueFromJsonElement(json);

if (Equals(value, expected))
return;
}
Expand Down Expand Up @@ -74,4 +79,12 @@ public override bool IsValid(IComparable expected = null)
return false;
}
}

private static object GetValueFromJsonElement(JsonElement element) => element.ValueKind switch
{
JsonValueKind.Number => element.GetDouble(),
JsonValueKind.String => element.GetString(),
JsonValueKind.True or JsonValueKind.False => element.GetBoolean(),
_ => element.GetRawText().Trim('"')
};
}
3 changes: 1 addition & 2 deletions src/Paseto/Validators/IssuedAtValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public override void Validate(IComparable expected = null)
throw new PasetoTokenValidationException($"Claim '{ClaimName}' must be a DateTime");
}

if (expected is null)
expected = DateTime.UtcNow;
expected ??= DateTime.UtcNow;

if (Comparer.GetComparisonResult(iat, expected) > 0) // expected >= iat
throw new PasetoTokenValidationException("Token is not yet valid");
Expand Down
3 changes: 1 addition & 2 deletions src/Paseto/Validators/NotBeforeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public override void Validate(IComparable expected = null)
throw new PasetoTokenValidationException($"Claim '{ClaimName}' must be a DateTime");
}

if (expected is null)
expected = DateTime.UtcNow;
expected ??= DateTime.UtcNow;

if (Comparer.GetComparisonResult(nbf, expected) >= 0) // expected <= nbf
throw new PasetoTokenValidationException("Token is not yet valid");
Expand Down

0 comments on commit fac4979

Please sign in to comment.