Skip to content

Commit

Permalink
Move from System.IdentityModel.Tokens.Jwt to Microsoft.IdentityModel.…
Browse files Browse the repository at this point in the history
…JsonWebTokens

> As of IdentityModel 7x, this is a legacy tool that should be replaced with Microsoft.IdentityModel.JsonWebTokens.

> This is a newer, faster version of System.IdentityModel.Tokens.Jwt that has additional functionality
  • Loading branch information
JustArchi committed Jan 30, 2024
1 parent 0384365 commit 716b253
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 42 deletions.
2 changes: 1 addition & 1 deletion ArchiSteamFarm/ArchiSteamFarm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Humanizer" />
<PackageReference Include="JetBrains.Annotations" PrivateAssets="all" />
<PackageReference Include="Markdig.Signed" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Nito.AsyncEx.Coordination" />
<PackageReference Include="NLog.Web.AspNetCore" />
Expand All @@ -24,7 +25,6 @@
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" />
<PackageReference Include="System.Composition" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
<PackageReference Include="System.Linq.Async" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" />
<PackageReference Include="zxcvbn-core" />
Expand Down
54 changes: 18 additions & 36 deletions ArchiSteamFarm/Core/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -40,6 +39,7 @@
using Humanizer;
using Humanizer.Localisation;
using JetBrains.Annotations;
using Microsoft.IdentityModel.JsonWebTokens;
using SteamKit2;
using Zxcvbn;

Expand Down Expand Up @@ -178,41 +178,6 @@ public static bool IsValidHexadecimalText(string text) {
return (text.Length % 2 == 0) && text.All(Uri.IsHexDigit);
}

[Obsolete($"Use {nameof(TryReadJwtToken)} instead, this function will be removed in the next version.")]
[PublicAPI]
public static JwtSecurityToken? ReadJwtToken(string token) {
ArgumentException.ThrowIfNullOrEmpty(token);

JwtSecurityTokenHandler jwtSecurityTokenHandler = new();

try {
return jwtSecurityTokenHandler.ReadJwtToken(token);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericDebuggingException(e);

return null;
}
}

[PublicAPI]
public static bool TryReadJwtToken(string token, [NotNullWhen(true)] out JwtSecurityToken? result) {
ArgumentException.ThrowIfNullOrEmpty(token);

JwtSecurityTokenHandler jwtSecurityTokenHandler = new();

try {
result = jwtSecurityTokenHandler.ReadJwtToken(token);

return true;
} catch (Exception e) {
ASF.ArchiLogger.LogGenericDebuggingException(e);

result = null;

return false;
}
}

[PublicAPI]
public static IList<INode> SelectNodes(this IDocument document, string xpath) {
ArgumentNullException.ThrowIfNull(document);
Expand Down Expand Up @@ -281,6 +246,23 @@ public static Task<T> ToLongRunningTask<T>(this AsyncJob<T> job) where T : Callb
return job.ToTask();
}

[PublicAPI]
public static bool TryReadJsonWebToken(string token, [NotNullWhen(true)] out JsonWebToken? result) {
ArgumentException.ThrowIfNullOrEmpty(token);

try {
result = new JsonWebToken(token);
} catch (Exception e) {
ASF.ArchiLogger.LogGenericDebuggingException(e);

result = null;

return false;
}

return true;
}

internal static void DeleteEmptyDirectoriesRecursively(string directory) {
ArgumentException.ThrowIfNullOrEmpty(directory);

Expand Down
8 changes: 4 additions & 4 deletions ArchiSteamFarm/Steam/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand All @@ -53,6 +52,7 @@
using ArchiSteamFarm.Storage;
using ArchiSteamFarm.Web;
using JetBrains.Annotations;
using Microsoft.IdentityModel.JsonWebTokens;
using Newtonsoft.Json;
using SteamKit2;
using SteamKit2.Authentication;
Expand Down Expand Up @@ -203,7 +203,7 @@ private set {
return;
}

if (!Utilities.TryReadJwtToken(value, out JwtSecurityToken? accessToken)) {
if (!Utilities.TryReadJsonWebToken(value, out JsonWebToken? accessToken)) {
ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(accessToken)));

return;
Expand Down Expand Up @@ -2380,13 +2380,13 @@ private async Task InitModules() {
}
}

if (!string.IsNullOrEmpty(accessTokenText) && Utilities.TryReadJwtToken(accessTokenText, out JwtSecurityToken? accessToken) && ((accessToken.ValidTo == DateTime.MinValue) || (accessToken.ValidTo >= DateTime.UtcNow))) {
if (!string.IsNullOrEmpty(accessTokenText) && Utilities.TryReadJsonWebToken(accessTokenText, out JsonWebToken? accessToken) && ((accessToken.ValidTo == DateTime.MinValue) || (accessToken.ValidTo >= DateTime.UtcNow))) {
AccessToken = accessTokenText;
} else {
AccessToken = null;
}

if (!string.IsNullOrEmpty(refreshTokenText) && Utilities.TryReadJwtToken(refreshTokenText, out JwtSecurityToken? refreshToken) && ((refreshToken.ValidTo == DateTime.MinValue) || (refreshToken.ValidTo >= DateTime.UtcNow))) {
if (!string.IsNullOrEmpty(refreshTokenText) && Utilities.TryReadJsonWebToken(refreshTokenText, out JsonWebToken? refreshToken) && ((refreshToken.ValidTo == DateTime.MinValue) || (refreshToken.ValidTo >= DateTime.UtcNow))) {
RefreshToken = refreshTokenText;
} else {
RefreshToken = null;
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<PackageVersion Include="Humanizer" Version="2.14.1" />
<PackageVersion Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageVersion Include="Markdig.Signed" Version="0.34.0" />
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.3.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.2.0" />
<PackageVersion Include="MSTest.TestFramework" Version="3.2.0" />
Expand All @@ -18,7 +19,6 @@
<PackageVersion Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0" />
<PackageVersion Include="System.Composition" Version="8.0.0" />
<PackageVersion Include="System.Composition.AttributedModel" Version="8.0.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.3.0" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Security.Cryptography.ProtectedData" Version="8.0.0" />
<PackageVersion Include="zxcvbn-core" Version="7.0.92" />
Expand Down

0 comments on commit 716b253

Please sign in to comment.