Skip to content

Commit 9f322b2

Browse files
authored
Remove jwt token dependency (#1897)
* Remove jwt token dependency
1 parent 9368179 commit 9f322b2

File tree

17 files changed

+190
-16
lines changed

17 files changed

+190
-16
lines changed

build/dependencies.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<SystemBuffersPackageVersion>4.5.1</SystemBuffersPackageVersion>
2222
<SystemMemoryPackageVersion>4.5.4</SystemMemoryPackageVersion>
2323
<SystemRuntimeCompilerServicesUnsafePackageVersion>6.0.0</SystemRuntimeCompilerServicesUnsafePackageVersion>
24-
<SystemIdentityModelTokensJwtPackageVersion>5.7.0</SystemIdentityModelTokensJwtPackageVersion>
2524
<AzureIdentityPackageVersion>1.10.4</AzureIdentityPackageVersion>
2625
<MicrosoftExtensionsLoggingPackageVersion>2.1.0</MicrosoftExtensionsLoggingPackageVersion>
2726
<MicrosoftExtensionsPrimitivesPackageVersion>2.1.1</MicrosoftExtensionsPrimitivesPackageVersion>
@@ -72,6 +71,7 @@
7271
<EmulatorMicrosoftPackageVersion>6.0.9</EmulatorMicrosoftPackageVersion>
7372

7473
<!--Testing -->
74+
<SystemIdentityModelTokensJwtPackageVersion>5.7.0</SystemIdentityModelTokensJwtPackageVersion>
7575
<MicrosoftAspNetCoreHttpConnectionsClientPackageVersion>1.0.0</MicrosoftAspNetCoreHttpConnectionsClientPackageVersion>
7676
<MessagePackPackage3_1Version>1.9.11</MessagePackPackage3_1Version>
7777
<MicrosoftAspNetCoreSignalRProtocolsMessagePackPackageVersion>3.1.24</MicrosoftAspNetCoreSignalRProtocolsMessagePackPackageVersion>

src/Directory.Build.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<PackageReference Include="Microsoft.Extensions.Http" Version="$(MicrosoftExtensionHttpVersion)" />
2929
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="$(MicrosoftRestClientRuntimePackageVersion)" />
3030
<PackageReference Include="Azure.Identity" Version="$(AzureIdentityPackageVersion)" />
31-
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="$(SystemIdentityModelTokensJwtPackageVersion)" />
3231
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
3332
</ItemGroup>
3433

src/Microsoft.Azure.SignalR.AspNet/ServiceOptions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Configuration;
88
using System.Net;
99
using System.Security.Claims;
10-
using Microsoft.IdentityModel.Tokens;
1110
using Microsoft.Owin;
1211

1312
namespace Microsoft.Azure.SignalR.AspNet
@@ -76,8 +75,8 @@ public int ConnectionCount
7675
public TimeSpan AccessTokenLifetime { get; set; } = Constants.Periods.DefaultAccessTokenLifetime;
7776

7877
/// <summary>
79-
/// Gets or sets the access token generate algorithm, supports <see cref="SecurityAlgorithms.HmacSha256"/> or <see cref="SecurityAlgorithms.HmacSha512"/>
80-
/// Default value is <see cref="SecurityAlgorithms.HmacSha256"/>
78+
/// Gets or sets the access token generate algorithm, supports HmacSha256 or HmacSha512
79+
/// Default value is HmacSha256
8180
/// </summary>
8281
public AccessTokenAlgorithm AccessTokenAlgorithm { get; set; } = AccessTokenAlgorithm.HS256;
8382

src/Microsoft.Azure.SignalR.Common/Auth/Base64UrlEncoder.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
using System;
1212
using System.Text;
13-
using Microsoft.IdentityModel.Logging;
1413

1514
namespace Microsoft.Azure.SignalR
1615
{
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*------------------------------------------------------------------------------
2+
* Copied from https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/6.22.0/src/Microsoft.IdentityModel.Tokens/DateTimeUtil.cs
3+
------------------------------------------------------------------------------*/
4+
5+
using System;
6+
7+
namespace Microsoft.Azure.SignalR
8+
{
9+
internal static class DateTimeUtil
10+
{
11+
/// <summary>
12+
/// Add a DateTime and a TimeSpan.
13+
/// The maximum time is DateTime.MaxTime. It is not an error if time + timespan > MaxTime.
14+
/// Just return MaxTime.
15+
/// </summary>
16+
/// <param name="time">Initial <see cref="DateTime"/> value.</param>
17+
/// <param name="timespan"><see cref="TimeSpan"/> to add.</param>
18+
/// <returns><see cref="DateTime"/> as the sum of time and timespan.</returns>
19+
public static DateTime Add(DateTime time, TimeSpan timespan)
20+
{
21+
if (timespan == TimeSpan.Zero)
22+
{
23+
return time;
24+
}
25+
26+
if (timespan > TimeSpan.Zero && DateTime.MaxValue - time <= timespan)
27+
{
28+
return GetMaxValue(time.Kind);
29+
}
30+
31+
if (timespan < TimeSpan.Zero && DateTime.MinValue - time >= timespan)
32+
{
33+
return GetMinValue(time.Kind);
34+
}
35+
36+
return time + timespan;
37+
}
38+
39+
/// <summary>
40+
/// Gets the Maximum value for a DateTime specifying kind.
41+
/// </summary>
42+
/// <param name="kind">DateTimeKind to use.</param>
43+
/// <returns>DateTime of specified kind.</returns>
44+
public static DateTime GetMaxValue(DateTimeKind kind)
45+
{
46+
if (kind == DateTimeKind.Unspecified)
47+
return new DateTime(DateTime.MaxValue.Ticks, DateTimeKind.Utc);
48+
49+
return new DateTime(DateTime.MaxValue.Ticks, kind);
50+
}
51+
52+
/// <summary>
53+
/// Gets the Minimum value for a DateTime specifying kind.
54+
/// </summary>
55+
/// <param name="kind">DateTimeKind to use.</param>
56+
/// <returns>DateTime of specified kind.</returns>
57+
public static DateTime GetMinValue(DateTimeKind kind)
58+
{
59+
if (kind == DateTimeKind.Unspecified)
60+
return new DateTime(DateTime.MinValue.Ticks, DateTimeKind.Utc);
61+
62+
return new DateTime(DateTime.MinValue.Ticks, kind);
63+
}
64+
65+
/// <summary>
66+
/// Ensures that DataTime is UTC.
67+
/// </summary>
68+
/// <param name="value"><see cref="DateTime"/>to convert.</param>
69+
/// <returns></returns>
70+
public static DateTime? ToUniversalTime(DateTime? value)
71+
{
72+
if (value == null || value.Value.Kind == DateTimeKind.Utc)
73+
return value;
74+
75+
return ToUniversalTime(value.Value);
76+
}
77+
78+
/// <summary>
79+
/// Ensures that DateTime is UTC.
80+
/// </summary>
81+
/// <param name="value"><see cref="DateTime"/>to convert.</param>
82+
/// <returns></returns>
83+
public static DateTime ToUniversalTime(DateTime value)
84+
{
85+
86+
if (value.Kind == DateTimeKind.Utc)
87+
return value;
88+
89+
return value.ToUniversalTime();
90+
}
91+
}
92+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*------------------------------------------------------------------------------
2+
* Copied from https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/v6.33.0/src/Microsoft.IdentityModel.Tokens/EpochTime.cs
3+
------------------------------------------------------------------------------*/
4+
5+
using System;
6+
7+
namespace Microsoft.Azure.SignalR
8+
{
9+
internal static class EpochTime
10+
{
11+
/// <summary>
12+
/// DateTime as UTV for UnixEpoch
13+
/// </summary>
14+
public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
15+
16+
/// <summary>
17+
/// Per JWT spec:
18+
/// Gets the number of seconds from 1970-01-01T0:0:0Z as measured in UTC until the desired date/time.
19+
/// </summary>
20+
/// <param name="datetime">The DateTime to convert to seconds.</param>
21+
/// <remarks>if dateTimeUtc less than UnixEpoch, return 0</remarks>
22+
/// <returns>the number of seconds since Unix Epoch.</returns>
23+
public static long GetIntDate(DateTime datetime)
24+
{
25+
DateTime dateTimeUtc = datetime;
26+
if (datetime.Kind != DateTimeKind.Utc)
27+
{
28+
dateTimeUtc = datetime.ToUniversalTime();
29+
}
30+
31+
if (dateTimeUtc.ToUniversalTime() <= UnixEpoch)
32+
{
33+
return 0;
34+
}
35+
36+
return (long)(dateTimeUtc - UnixEpoch).TotalSeconds;
37+
}
38+
39+
/// <summary>
40+
/// Creates a DateTime from epoch time.
41+
/// </summary>
42+
/// <param name="secondsSinceUnixEpoch">Number of seconds.</param>
43+
/// <returns>The DateTime in UTC.</returns>
44+
public static DateTime DateTime(long secondsSinceUnixEpoch)
45+
{
46+
if (secondsSinceUnixEpoch <= 0)
47+
{
48+
return UnixEpoch;
49+
}
50+
51+
if (secondsSinceUnixEpoch > TimeSpan.MaxValue.TotalSeconds)
52+
return DateTimeUtil.Add(UnixEpoch, TimeSpan.MaxValue).ToUniversalTime();
53+
54+
return DateTimeUtil.Add(UnixEpoch, TimeSpan.FromSeconds(secondsSinceUnixEpoch)).ToUniversalTime();
55+
}
56+
}
57+
}

src/Microsoft.Azure.SignalR.Common/Auth/JwtHeader.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using System.Collections.Generic;
1212
using Newtonsoft.Json;
1313
using System.Text;
14-
using Microsoft.IdentityModel.Logging;
1514

1615
namespace Microsoft.Azure.SignalR
1716
{

src/Microsoft.Azure.SignalR.Common/Auth/JwtPayload.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
using System.Collections.Generic;
1111
using System.Text;
1212
using System.Security.Claims;
13-
using Microsoft.IdentityModel.Logging;
14-
using Microsoft.IdentityModel.Tokens;
1513
using Newtonsoft.Json;
1614

1715
namespace Microsoft.Azure.SignalR
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*------------------------------------------------------------------------------
2+
* A simplified version of LogHelper
3+
------------------------------------------------------------------------------*/
4+
5+
using System;
6+
using System.Globalization;
7+
8+
internal class LogHelper
9+
{
10+
public static ArgumentNullException LogArgumentNullException(string name)
11+
{
12+
return new ArgumentNullException(name);
13+
}
14+
15+
public static Exception LogExceptionMessage(Exception exception)
16+
{
17+
return exception;
18+
}
19+
20+
public static string FormatInvariant(string format, params object[] args)
21+
{
22+
if (format == null)
23+
return string.Empty;
24+
25+
if (args == null)
26+
return format;
27+
28+
return string.Format(CultureInfo.InvariantCulture, format, args);
29+
}
30+
}

src/Microsoft.Azure.SignalR.Common/Auth/SignalRJwtSecurityTokenHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using System.Security.Claims;
1616
using System.Text;
1717
using System.Security.Cryptography;
18-
using Microsoft.IdentityModel.Logging;
1918

2019
namespace Microsoft.Azure.SignalR
2120
{
@@ -51,7 +50,6 @@ public string CreateJwtSecurityToken(
5150
if (!notBefore.HasValue)
5251
notBefore = now;
5352
}
54-
LogHelper.LogVerbose("IDX12721: Creating JwtSecurityToken: Issuer: '{0}', Audience: '{1}'", (audience ?? "null"), (issuer ?? "null"));
5553

5654
JwtPayload payload = new JwtPayload(issuer, audience, (subject == null ? null : OutboundClaimTypeTransform(subject.Claims)), notBefore, expires, issuedAt);
5755
JwtHeader header = new JwtHeader(kid, algorithm);

0 commit comments

Comments
 (0)