Move from System.IdentityModel.Tokens.Jwt to Microsoft.IdentityModel.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
This commit is contained in:
Archi
2024-01-30 13:26:32 +01:00
parent 0384365315
commit 19d883e624
4 changed files with 24 additions and 42 deletions

View File

@@ -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" />
@@ -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" />

View File

@@ -25,7 +25,6 @@ using System.Collections.Frozen;
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;
@@ -40,6 +39,7 @@ using ArchiSteamFarm.Storage;
using Humanizer;
using Humanizer.Localisation;
using JetBrains.Annotations;
using Microsoft.IdentityModel.JsonWebTokens;
using SteamKit2;
using Zxcvbn;
@@ -178,41 +178,6 @@ public static class Utilities {
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);
@@ -281,6 +246,23 @@ public static class Utilities {
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);

View File

@@ -28,7 +28,6 @@ using System.Collections.Immutable;
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;
@@ -53,6 +52,7 @@ using ArchiSteamFarm.Steam.Storage;
using ArchiSteamFarm.Storage;
using ArchiSteamFarm.Web;
using JetBrains.Annotations;
using Microsoft.IdentityModel.JsonWebTokens;
using Newtonsoft.Json;
using SteamKit2;
using SteamKit2.Authentication;
@@ -203,7 +203,7 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
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;
@@ -2380,13 +2380,13 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
}
}
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;

View File

@@ -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" />
@@ -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" />