Skip to content

Commit 3db21bd

Browse files
committed
Use TimeProvider
1 parent e8cabc0 commit 3db21bd

File tree

13 files changed

+265
-85
lines changed

13 files changed

+265
-85
lines changed

src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.CreateToken.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,11 @@ internal static void WriteJwsPayload(
835835
// By default we set these three properties only if they haven't been detected before.
836836
if (setDefaultTimesOnTokenCreation && !(expSet && iatSet && nbfSet))
837837
{
838-
DateTime now = DateTime.UtcNow;
838+
DateTime now =
839+
#if SUPPORTS_TIME_PROVIDER
840+
tokenDescriptor.TimeProvider?.GetUtcNow().UtcDateTime ??
841+
#endif
842+
DateTime.UtcNow;
839843

840844
if (!expSet)
841845
{

src/Microsoft.IdentityModel.Protocols.OpenIdConnect/OpenIdConnectProtocolValidator.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,30 @@ public virtual string GenerateNonce()
8888
string nonce = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString() + Guid.NewGuid().ToString()));
8989
if (RequireTimeStampInNonce)
9090
{
91-
return DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture) + "." + nonce;
91+
DateTime utcNow =
92+
#if SUPPORTS_TIME_PROVIDER
93+
TimeProvider?.GetUtcNow().UtcDateTime ??
94+
#endif
95+
DateTime.UtcNow;
96+
97+
return utcNow.Ticks.ToString(CultureInfo.InvariantCulture) + "." + nonce;
9298
}
9399

94100
return nonce;
95101
}
96102

103+
#if SUPPORTS_TIME_PROVIDER
104+
#nullable enable
105+
/// <summary>
106+
/// Gets or sets the time provider.
107+
/// </summary>
108+
/// <remarks>
109+
/// If not set, fall back to using the <see cref="DateTime"/> class to obtain the current time.
110+
/// </remarks>
111+
public TimeProvider? TimeProvider { get; set; }
112+
#nullable restore
113+
#endif
114+
97115
/// <summary>
98116
/// Gets the algorithm mapping between OpenIdConnect and .Net for Hash algorithms.
99117
/// a <see cref="IDictionary{TKey, TValue}"/> that contains mappings from the JWT namespace <see href="https://datatracker.ietf.org/doc/html/rfc7518"/> to .NET.
@@ -658,7 +676,12 @@ protected virtual void ValidateNonce(OpenIdConnectProtocolValidationContext vali
658676
throw LogHelper.LogExceptionMessage(new OpenIdConnectProtocolInvalidNonceException(LogHelper.FormatInvariant(LogMessages.IDX21327, LogHelper.MarkAsNonPII(timestamp), LogHelper.MarkAsNonPII(DateTime.MinValue.Ticks.ToString(CultureInfo.InvariantCulture)), LogHelper.MarkAsNonPII(DateTime.MaxValue.Ticks.ToString(CultureInfo.InvariantCulture))), ex));
659677
}
660678

661-
DateTime utcNow = DateTime.UtcNow;
679+
DateTime utcNow =
680+
#if SUPPORTS_TIME_PROVIDER
681+
TimeProvider?.GetUtcNow().UtcDateTime ??
682+
#endif
683+
DateTime.UtcNow;
684+
662685
if (nonceTime + NonceLifetime < utcNow)
663686
throw LogHelper.LogExceptionMessage(new OpenIdConnectProtocolInvalidNonceException(LogHelper.FormatInvariant(LogMessages.IDX21324, nonceFoundInJwt, LogHelper.MarkAsNonPII(nonceTime.ToString(CultureInfo.InvariantCulture)), LogHelper.MarkAsNonPII(utcNow.ToString(CultureInfo.InvariantCulture)), LogHelper.MarkAsNonPII(NonceLifetime.ToString("c", CultureInfo.InvariantCulture)))));
664687
}

src/Microsoft.IdentityModel.Protocols.SignedHttpRequest/SignedHttpRequestCreationParameters.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class SignedHttpRequestCreationParameters
1414
/// Gets or sets a value indicating whether the <see cref="ConfirmationClaimTypes.Cnf"/> claim should be created and added or not.
1515
/// </summary>
1616
/// <remarks>
17-
/// <see cref="SignedHttpRequestDescriptor.CnfClaimValue"/> will be used as a "cnf" claim value, if set.
17+
/// <see cref="SignedHttpRequestDescriptor.CnfClaimValue"/> will be used as a "cnf" claim value, if set.
1818
/// Otherwise, a "cnf" claim value will be derived from <see cref="SignedHttpRequestDescriptor.SigningCredentials"/>.
1919
/// </remarks>
2020
public bool CreateCnf { get; set; } = true;
@@ -27,19 +27,19 @@ public class SignedHttpRequestCreationParameters
2727
/// <summary>
2828
/// Gets or sets a value indicating whether the <see cref="SignedHttpRequestClaimTypes.Ts"/> claim should be created and added or not.
2929
/// </summary>
30-
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
30+
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
3131
public bool CreateTs { get; set; } = true;
3232

3333
/// <summary>
3434
/// Gets or sets a value indicating whether the <see cref="SignedHttpRequestClaimTypes.M"/> claim should be created and added or not.
3535
/// </summary>
36-
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
36+
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
3737
public bool CreateM { get; set; } = true;
3838

3939
/// <summary>
4040
/// Gets or sets a value indicating whether the <see cref="SignedHttpRequestClaimTypes.U"/> claim should be created and added or not.
4141
/// </summary>
42-
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
42+
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
4343
public bool CreateU { get; set; } = true;
4444

4545
/// <summary>
@@ -51,19 +51,19 @@ public class SignedHttpRequestCreationParameters
5151
/// <summary>
5252
/// Gets or sets a value indicating whether the <see cref="SignedHttpRequestClaimTypes.Q"/> claim should be created and added or not.
5353
/// </summary>
54-
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
54+
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
5555
public bool CreateQ { get; set; }
5656

5757
/// <summary>
5858
/// Gets or sets a value indicating whether the <see cref="SignedHttpRequestClaimTypes.H"/> claim should be created and added or not.
5959
/// </summary>
60-
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
60+
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
6161
public bool CreateH { get; set; }
6262

6363
/// <summary>
6464
/// Gets or sets a value indicating whether the <see cref="SignedHttpRequestClaimTypes.B"/> claim should be created and added or not.
6565
/// </summary>
66-
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
66+
/// <remarks>https://datatracker.ietf.org/doc/html/draft-ietf-oauth-signed-http-request-03#section-3</remarks>
6767
public bool CreateB { get; set; }
6868

6969
/// <summary>
@@ -76,5 +76,17 @@ public class SignedHttpRequestCreationParameters
7676
/// </summary>
7777
/// <remarks>Allows for adjusting the local time so it matches a server time.</remarks>
7878
public TimeSpan TimeAdjustment { get; set; } = DefaultTimeAdjustment;
79+
80+
#if SUPPORTS_TIME_PROVIDER
81+
#nullable enable
82+
/// <summary>
83+
/// Gets or sets the time provider.
84+
/// </summary>
85+
/// <remarks>
86+
/// If not set, fall back to using the <see cref="DateTime"/> class to obtain the current time.
87+
/// </remarks>
88+
public TimeProvider? TimeProvider { get; set; }
89+
#nullable restore
90+
#endif
7991
}
8092
}

0 commit comments

Comments
 (0)