Skip to content

Commit 3e1e69e

Browse files
authored
Add nullable annotations to Authenticator.Core & Authentication.Cookies (#24307)
Contributes to #5680
1 parent 799014c commit 3e1e69e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+183
-151
lines changed

src/Http/Authentication.Abstractions/src/AuthenticateResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Security.Claims;
67

78
namespace Microsoft.AspNetCore.Authentication
@@ -19,6 +20,7 @@ protected AuthenticateResult() { }
1920
/// <summary>
2021
/// If a ticket was produced, authenticate was successful.
2122
/// </summary>
23+
[MemberNotNullWhen(true, nameof(Ticket))]
2224
public bool Succeeded => Ticket != null;
2325

2426
/// <summary>

src/Http/Authentication.Abstractions/src/AuthenticationHttpContextExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static Task<AuthenticateResult> AuthenticateAsync(this HttpContext contex
3636
/// <param name="context">The <see cref="HttpContext"/> context.</param>
3737
/// <param name="scheme">The name of the authentication scheme.</param>
3838
/// <returns>The result.</returns>
39-
public static Task ChallengeAsync(this HttpContext context, string scheme) =>
39+
public static Task ChallengeAsync(this HttpContext context, string? scheme) =>
4040
context.ChallengeAsync(scheme, properties: null);
4141

4242
/// <summary>
@@ -72,7 +72,7 @@ public static Task ChallengeAsync(this HttpContext context, string? scheme, Auth
7272
/// <param name="context">The <see cref="HttpContext"/> context.</param>
7373
/// <param name="scheme">The name of the authentication scheme.</param>
7474
/// <returns>The task.</returns>
75-
public static Task ForbidAsync(this HttpContext context, string scheme) =>
75+
public static Task ForbidAsync(this HttpContext context, string? scheme) =>
7676
context.ForbidAsync(scheme, properties: null);
7777

7878
/// <summary>
@@ -109,7 +109,7 @@ public static Task ForbidAsync(this HttpContext context, string? scheme, Authent
109109
/// <param name="scheme">The name of the authentication scheme.</param>
110110
/// <param name="principal">The user.</param>
111111
/// <returns>The task.</returns>
112-
public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal) =>
112+
public static Task SignInAsync(this HttpContext context, string? scheme, ClaimsPrincipal principal) =>
113113
context.SignInAsync(scheme, principal, properties: null);
114114

115115
/// <summary>

src/Http/Authentication.Abstractions/src/AuthenticationTicket.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class AuthenticationTicket
1717
/// <param name="principal">the <see cref="ClaimsPrincipal"/> that represents the authenticated user.</param>
1818
/// <param name="properties">additional properties that can be consumed by the user or runtime.</param>
1919
/// <param name="authenticationScheme">the authentication middleware that was responsible for this ticket.</param>
20-
public AuthenticationTicket(ClaimsPrincipal principal, AuthenticationProperties? properties, string? authenticationScheme)
20+
public AuthenticationTicket(ClaimsPrincipal principal, AuthenticationProperties? properties, string authenticationScheme)
2121
{
2222
if (principal == null)
2323
{
@@ -41,17 +41,17 @@ public AuthenticationTicket(ClaimsPrincipal principal, string authenticationSche
4141
/// <summary>
4242
/// Gets the authentication type.
4343
/// </summary>
44-
public string? AuthenticationScheme { get; private set; }
44+
public string AuthenticationScheme { get; }
4545

4646
/// <summary>
4747
/// Gets the claims-principal with authenticated user identities.
4848
/// </summary>
49-
public ClaimsPrincipal Principal { get; private set; }
49+
public ClaimsPrincipal Principal { get; }
5050

5151
/// <summary>
5252
/// Additional state values for the authentication session.
5353
/// </summary>
54-
public AuthenticationProperties Properties { get; private set; }
54+
public AuthenticationProperties Properties { get; }
5555

5656
/// <summary>
5757
/// Returns a copy of the ticket.

src/Http/Http.Abstractions/src/PathString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ public override int GetHashCode()
424424
/// <param name="left">The left parameter</param>
425425
/// <param name="right">The right parameter</param>
426426
/// <returns>The ToString combination of both values</returns>
427-
public static string operator +(PathString left, string right)
427+
public static string operator +(PathString left, string? right)
428428
{
429429
// This overload exists to prevent the implicit string<->PathString converter from
430430
// trying to call the PathString+PathString operator for things that are not path strings.

src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics;
56
using System.Linq;
67
using System.Security.Claims;
78
using System.Text.Encodings.Web;
@@ -27,9 +28,9 @@ public class CookieAuthenticationHandler : SignInAuthenticationHandler<CookieAut
2728

2829
private DateTimeOffset? _refreshIssuedUtc;
2930
private DateTimeOffset? _refreshExpiresUtc;
30-
private string _sessionKey;
31-
private Task<AuthenticateResult> _readCookieTask;
32-
private AuthenticationTicket _refreshTicket;
31+
private string? _sessionKey;
32+
private Task<AuthenticateResult>? _readCookieTask;
33+
private AuthenticationTicket? _refreshTicket;
3334

3435
public CookieAuthenticationHandler(IOptionsMonitor<CookieAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
3536
: base(options, logger, encoder, clock)
@@ -41,7 +42,7 @@ public CookieAuthenticationHandler(IOptionsMonitor<CookieAuthenticationOptions>
4142
/// </summary>
4243
protected new CookieAuthenticationEvents Events
4344
{
44-
get { return (CookieAuthenticationEvents)base.Events; }
45+
get { return (CookieAuthenticationEvents)base.Events!; }
4546
set { base.Events = value; }
4647
}
4748

@@ -86,7 +87,7 @@ private void CheckForRefresh(AuthenticationTicket ticket)
8687
}
8788
}
8889

89-
private void RequestRefresh(AuthenticationTicket ticket, ClaimsPrincipal replacedPrincipal = null)
90+
private void RequestRefresh(AuthenticationTicket ticket, ClaimsPrincipal? replacedPrincipal = null)
9091
{
9192
var issuedUtc = ticket.Properties.IssuedUtc;
9293
var expiresUtc = ticket.Properties.ExpiresUtc;
@@ -102,7 +103,7 @@ private void RequestRefresh(AuthenticationTicket ticket, ClaimsPrincipal replace
102103
}
103104
}
104105

105-
private AuthenticationTicket CloneTicket(AuthenticationTicket ticket, ClaimsPrincipal replacedPrincipal)
106+
private AuthenticationTicket CloneTicket(AuthenticationTicket ticket, ClaimsPrincipal? replacedPrincipal)
106107
{
107108
var principal = replacedPrincipal ?? ticket.Principal;
108109
var newPrincipal = new ClaimsPrincipal();
@@ -122,7 +123,7 @@ private AuthenticationTicket CloneTicket(AuthenticationTicket ticket, ClaimsPrin
122123

123124
private async Task<AuthenticateResult> ReadCookieTicket()
124125
{
125-
var cookie = Options.CookieManager.GetRequestCookie(Context, Options.Cookie.Name);
126+
var cookie = Options.CookieManager.GetRequestCookie(Context, Options.Cookie.Name!);
126127
if (string.IsNullOrEmpty(cookie))
127128
{
128129
return AuthenticateResult.NoResult();
@@ -157,7 +158,7 @@ private async Task<AuthenticateResult> ReadCookieTicket()
157158
{
158159
if (Options.SessionStore != null)
159160
{
160-
await Options.SessionStore.RemoveAsync(_sessionKey);
161+
await Options.SessionStore.RemoveAsync(_sessionKey!);
161162
}
162163
return AuthenticateResult.Fail("Ticket expired");
163164
}
@@ -176,6 +177,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
176177
return result;
177178
}
178179

180+
Debug.Assert(result.Ticket != null);
179181
var context = new CookieValidatePrincipalContext(Context, Scheme, Options, result.Ticket);
180182
await Events.ValidatePrincipal(context);
181183

@@ -244,15 +246,15 @@ protected virtual async Task FinishResponseAsync()
244246

245247
Options.CookieManager.AppendResponseCookie(
246248
Context,
247-
Options.Cookie.Name,
249+
Options.Cookie.Name!,
248250
cookieValue,
249251
cookieOptions);
250252

251253
await ApplyHeaders(shouldRedirectToReturnUrl: false, properties: properties);
252254
}
253255
}
254256

255-
protected async override Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)
257+
protected async override Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties? properties)
256258
{
257259
if (user == null)
258260
{
@@ -299,7 +301,7 @@ protected async override Task HandleSignInAsync(ClaimsPrincipal user, Authentica
299301
signInContext.CookieOptions.Expires = expiresUtc.ToUniversalTime();
300302
}
301303

302-
var ticket = new AuthenticationTicket(signInContext.Principal, signInContext.Properties, signInContext.Scheme.Name);
304+
var ticket = new AuthenticationTicket(signInContext.Principal!, signInContext.Properties, signInContext.Scheme.Name);
303305

304306
if (Options.SessionStore != null)
305307
{
@@ -324,14 +326,14 @@ protected async override Task HandleSignInAsync(ClaimsPrincipal user, Authentica
324326

325327
Options.CookieManager.AppendResponseCookie(
326328
Context,
327-
Options.Cookie.Name,
329+
Options.Cookie.Name!,
328330
cookieValue,
329331
signInContext.CookieOptions);
330332

331333
var signedInContext = new CookieSignedInContext(
332334
Context,
333335
Scheme,
334-
signInContext.Principal,
336+
signInContext.Principal!,
335337
signInContext.Properties,
336338
Options);
337339

@@ -344,7 +346,7 @@ protected async override Task HandleSignInAsync(ClaimsPrincipal user, Authentica
344346
Logger.AuthenticationSchemeSignedIn(Scheme.Name);
345347
}
346348

347-
protected async override Task HandleSignOutAsync(AuthenticationProperties properties)
349+
protected async override Task HandleSignOutAsync(AuthenticationProperties? properties)
348350
{
349351
properties = properties ?? new AuthenticationProperties();
350352

@@ -369,7 +371,7 @@ protected async override Task HandleSignOutAsync(AuthenticationProperties proper
369371

370372
Options.CookieManager.DeleteCookie(
371373
Context,
372-
Options.Cookie.Name,
374+
Options.Cookie.Name!,
373375
context.CookieOptions);
374376

375377
// Only redirect on the logout path
@@ -449,7 +451,7 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop
449451
await Events.RedirectToLogin(redirectContext);
450452
}
451453

452-
private string GetTlsTokenBinding()
454+
private string? GetTlsTokenBinding()
453455
{
454456
var binding = Context.Features.Get<ITlsTokenBindingFeature>()?.GetProvidedTokenBindingId();
455457
return binding == null ? null : Convert.ToBase64String(binding);

src/Security/Authentication/Cookies/src/CookieAuthenticationOptions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public CookieBuilder Cookie
7171
/// <summary>
7272
/// If set this will be used by the CookieAuthenticationHandler for data protection.
7373
/// </summary>
74-
public IDataProtectionProvider DataProtectionProvider { get; set; }
74+
public IDataProtectionProvider? DataProtectionProvider { get; set; }
7575

7676
/// <summary>
7777
/// The SlidingExpiration is set to true to instruct the handler to re-issue a new cookie with a new
@@ -111,28 +111,28 @@ public CookieBuilder Cookie
111111
/// </summary>
112112
public new CookieAuthenticationEvents Events
113113
{
114-
get => (CookieAuthenticationEvents)base.Events;
114+
get => (CookieAuthenticationEvents)base.Events!;
115115
set => base.Events = value;
116116
}
117117

118118
/// <summary>
119119
/// The TicketDataFormat is used to protect and unprotect the identity and other properties which are stored in the
120120
/// cookie value. If not provided one will be created using <see cref="DataProtectionProvider"/>.
121121
/// </summary>
122-
public ISecureDataFormat<AuthenticationTicket> TicketDataFormat { get; set; }
122+
public ISecureDataFormat<AuthenticationTicket> TicketDataFormat { get; set; } = default!;
123123

124124
/// <summary>
125125
/// The component used to get cookies from the request or set them on the response.
126126
///
127127
/// ChunkingCookieManager will be used by default.
128128
/// </summary>
129-
public ICookieManager CookieManager { get; set; }
129+
public ICookieManager CookieManager { get; set; } = default!;
130130

131131
/// <summary>
132132
/// An optional container in which to store the identity across requests. When used, only a session identifier is sent
133133
/// to the client. This can be used to mitigate potential problems with very large identities.
134134
/// </summary>
135-
public ITicketStore SessionStore { get; set; }
135+
public ITicketStore? SessionStore { get; set; }
136136

137137
/// <summary>
138138
/// <para>

src/Security/Authentication/Cookies/src/CookieExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder
1717
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme)
1818
=> builder.AddCookie(authenticationScheme, configureOptions: null);
1919

20-
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, Action<CookieAuthenticationOptions> configureOptions)
20+
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, Action<CookieAuthenticationOptions>? configureOptions)
2121
=> builder.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, configureOptions);
2222

23-
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, Action<CookieAuthenticationOptions> configureOptions)
23+
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, Action<CookieAuthenticationOptions>? configureOptions)
2424
=> builder.AddCookie(authenticationScheme, displayName: null, configureOptions: configureOptions);
2525

26-
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<CookieAuthenticationOptions> configureOptions)
26+
public static AuthenticationBuilder AddCookie(this AuthenticationBuilder builder, string authenticationScheme, string? displayName, Action<CookieAuthenticationOptions>? configureOptions)
2727
{
2828
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<CookieAuthenticationOptions>, PostConfigureCookieAuthenticationOptions>());
2929
builder.Services.AddOptions<CookieAuthenticationOptions>(authenticationScheme).Validate(o => o.Cookie.Expiration == null, "Cookie.Expiration is ignored, use ExpireTimeSpan instead.");

src/Security/Authentication/Cookies/src/CookieSignedInContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public CookieSignedInContext(
2323
HttpContext context,
2424
AuthenticationScheme scheme,
2525
ClaimsPrincipal principal,
26-
AuthenticationProperties properties,
26+
AuthenticationProperties? properties,
2727
CookieAuthenticationOptions options)
2828
: base(context, scheme, options, properties)
2929
{

src/Security/Authentication/Cookies/src/CookieSigningInContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public CookieSigningInContext(
2525
AuthenticationScheme scheme,
2626
CookieAuthenticationOptions options,
2727
ClaimsPrincipal principal,
28-
AuthenticationProperties properties,
28+
AuthenticationProperties? properties,
2929
CookieOptions cookieOptions)
3030
: base(context, scheme, options, properties)
3131
{

src/Security/Authentication/Cookies/src/CookieSigningOutContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public CookieSigningOutContext(
2222
HttpContext context,
2323
AuthenticationScheme scheme,
2424
CookieAuthenticationOptions options,
25-
AuthenticationProperties properties,
25+
AuthenticationProperties? properties,
2626
CookieOptions cookieOptions)
2727
: base(context, scheme, options, properties)
2828
=> CookieOptions = cookieOptions;

src/Security/Authentication/Cookies/src/ICookieManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface ICookieManager
1717
/// <param name="context"></param>
1818
/// <param name="key"></param>
1919
/// <returns></returns>
20-
string GetRequestCookie(HttpContext context, string key);
20+
string? GetRequestCookie(HttpContext context, string key);
2121

2222
/// <summary>
2323
/// Append the given cookie to the response.
@@ -26,7 +26,7 @@ public interface ICookieManager
2626
/// <param name="key"></param>
2727
/// <param name="value"></param>
2828
/// <param name="options"></param>
29-
void AppendResponseCookie(HttpContext context, string key, string value, CookieOptions options);
29+
void AppendResponseCookie(HttpContext context, string key, string? value, CookieOptions options);
3030

3131
/// <summary>
3232
/// Append a delete cookie to the response.

src/Security/Authentication/Cookies/src/LoggingExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace Microsoft.Extensions.Logging
77
{
88
internal static class LoggingExtensions
99
{
10-
private static Action<ILogger, string, Exception> _authenticationSchemeSignedIn;
11-
private static Action<ILogger, string, Exception> _authenticationSchemeSignedOut;
10+
private static Action<ILogger, string, Exception?> _authenticationSchemeSignedIn;
11+
private static Action<ILogger, string, Exception?> _authenticationSchemeSignedOut;
1212

1313
static LoggingExtensions()
1414
{

src/Security/Authentication/Cookies/src/Microsoft.AspNetCore.Authentication.Cookies.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>ASP.NET Core middleware that enables an application to use cookie based authentication.</Description>
@@ -9,6 +9,7 @@
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1010
<PackageTags>aspnetcore;authentication;security</PackageTags>
1111
<IsPackable>false</IsPackable>
12+
<Nullable>enable</Nullable>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

src/Security/Authentication/Cookies/src/PostConfigureCookieAuthenticationOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public PostConfigureCookieAuthenticationOptions(IDataProtectionProvider dataProt
2626
/// <param name="options">The options instance to configure.</param>
2727
public void PostConfigure(string name, CookieAuthenticationOptions options)
2828
{
29-
options.DataProtectionProvider = options.DataProtectionProvider ?? _dp;
29+
options.DataProtectionProvider ??= _dp;
3030

3131
if (string.IsNullOrEmpty(options.Cookie.Name))
3232
{

0 commit comments

Comments
 (0)