-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add nullable annotations to Authentication.Core & Authentication.Cookies #24307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
|
@@ -27,9 +28,9 @@ public class CookieAuthenticationHandler : SignInAuthenticationHandler<CookieAut | |
|
||
private DateTimeOffset? _refreshIssuedUtc; | ||
private DateTimeOffset? _refreshExpiresUtc; | ||
private string _sessionKey; | ||
private Task<AuthenticateResult> _readCookieTask; | ||
private AuthenticationTicket _refreshTicket; | ||
private string? _sessionKey; | ||
private Task<AuthenticateResult>? _readCookieTask; | ||
private AuthenticationTicket? _refreshTicket; | ||
|
||
public CookieAuthenticationHandler(IOptionsMonitor<CookieAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) | ||
: base(options, logger, encoder, clock) | ||
|
@@ -41,7 +42,7 @@ public CookieAuthenticationHandler(IOptionsMonitor<CookieAuthenticationOptions> | |
/// </summary> | ||
protected new CookieAuthenticationEvents Events | ||
{ | ||
get { return (CookieAuthenticationEvents)base.Events; } | ||
get { return (CookieAuthenticationEvents)base.Events!; } | ||
set { base.Events = value; } | ||
} | ||
|
||
|
@@ -86,7 +87,7 @@ private void CheckForRefresh(AuthenticationTicket ticket) | |
} | ||
} | ||
|
||
private void RequestRefresh(AuthenticationTicket ticket, ClaimsPrincipal replacedPrincipal = null) | ||
private void RequestRefresh(AuthenticationTicket ticket, ClaimsPrincipal? replacedPrincipal = null) | ||
{ | ||
var issuedUtc = ticket.Properties.IssuedUtc; | ||
var expiresUtc = ticket.Properties.ExpiresUtc; | ||
|
@@ -102,7 +103,7 @@ private void RequestRefresh(AuthenticationTicket ticket, ClaimsPrincipal replace | |
} | ||
} | ||
|
||
private AuthenticationTicket CloneTicket(AuthenticationTicket ticket, ClaimsPrincipal replacedPrincipal) | ||
private AuthenticationTicket CloneTicket(AuthenticationTicket ticket, ClaimsPrincipal? replacedPrincipal) | ||
{ | ||
var principal = replacedPrincipal ?? ticket.Principal; | ||
var newPrincipal = new ClaimsPrincipal(); | ||
|
@@ -122,7 +123,7 @@ private AuthenticationTicket CloneTicket(AuthenticationTicket ticket, ClaimsPrin | |
|
||
private async Task<AuthenticateResult> ReadCookieTicket() | ||
{ | ||
var cookie = Options.CookieManager.GetRequestCookie(Context, Options.Cookie.Name); | ||
var cookie = Options.CookieManager.GetRequestCookie(Context, Options.Cookie.Name!); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if Options.Cookie.Name is null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the default case, this is guaranteed to be non-null. The option configures it to a value and you cannot assign a null value to it. |
||
if (string.IsNullOrEmpty(cookie)) | ||
{ | ||
return AuthenticateResult.NoResult(); | ||
|
@@ -157,7 +158,7 @@ private async Task<AuthenticateResult> ReadCookieTicket() | |
{ | ||
if (Options.SessionStore != null) | ||
{ | ||
await Options.SessionStore.RemoveAsync(_sessionKey); | ||
await Options.SessionStore.RemoveAsync(_sessionKey!); | ||
} | ||
return AuthenticateResult.Fail("Ticket expired"); | ||
} | ||
|
@@ -176,6 +177,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync() | |
return result; | ||
} | ||
|
||
Debug.Assert(result.Ticket != null); | ||
var context = new CookieValidatePrincipalContext(Context, Scheme, Options, result.Ticket); | ||
await Events.ValidatePrincipal(context); | ||
|
||
|
@@ -244,15 +246,15 @@ protected virtual async Task FinishResponseAsync() | |
|
||
Options.CookieManager.AppendResponseCookie( | ||
Context, | ||
Options.Cookie.Name, | ||
Options.Cookie.Name!, | ||
cookieValue, | ||
cookieOptions); | ||
|
||
await ApplyHeaders(shouldRedirectToReturnUrl: false, properties: properties); | ||
} | ||
} | ||
|
||
protected async override Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties properties) | ||
protected async override Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties? properties) | ||
{ | ||
if (user == null) | ||
{ | ||
|
@@ -299,7 +301,7 @@ protected async override Task HandleSignInAsync(ClaimsPrincipal user, Authentica | |
signInContext.CookieOptions.Expires = expiresUtc.ToUniversalTime(); | ||
} | ||
|
||
var ticket = new AuthenticationTicket(signInContext.Principal, signInContext.Properties, signInContext.Scheme.Name); | ||
var ticket = new AuthenticationTicket(signInContext.Principal!, signInContext.Properties, signInContext.Scheme.Name); | ||
|
||
if (Options.SessionStore != null) | ||
{ | ||
|
@@ -324,14 +326,14 @@ protected async override Task HandleSignInAsync(ClaimsPrincipal user, Authentica | |
|
||
Options.CookieManager.AppendResponseCookie( | ||
Context, | ||
Options.Cookie.Name, | ||
Options.Cookie.Name!, | ||
cookieValue, | ||
signInContext.CookieOptions); | ||
|
||
var signedInContext = new CookieSignedInContext( | ||
Context, | ||
Scheme, | ||
signInContext.Principal, | ||
signInContext.Principal!, | ||
signInContext.Properties, | ||
Options); | ||
|
||
|
@@ -344,7 +346,7 @@ protected async override Task HandleSignInAsync(ClaimsPrincipal user, Authentica | |
Logger.AuthenticationSchemeSignedIn(Scheme.Name); | ||
} | ||
|
||
protected async override Task HandleSignOutAsync(AuthenticationProperties properties) | ||
protected async override Task HandleSignOutAsync(AuthenticationProperties? properties) | ||
{ | ||
properties = properties ?? new AuthenticationProperties(); | ||
|
||
|
@@ -369,7 +371,7 @@ protected async override Task HandleSignOutAsync(AuthenticationProperties proper | |
|
||
Options.CookieManager.DeleteCookie( | ||
Context, | ||
Options.Cookie.Name, | ||
Options.Cookie.Name!, | ||
context.CookieOptions); | ||
|
||
// Only redirect on the logout path | ||
|
@@ -449,7 +451,7 @@ protected override async Task HandleChallengeAsync(AuthenticationProperties prop | |
await Events.RedirectToLogin(redirectContext); | ||
} | ||
|
||
private string GetTlsTokenBinding() | ||
private string? GetTlsTokenBinding() | ||
{ | ||
var binding = Context.Features.Get<ITlsTokenBindingFeature>()?.GetProvidedTokenBindingId(); | ||
return binding == null ? null : Convert.ToBase64String(binding); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should authenticationScheme throw ArgumentNullException if null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally haven't added explicit null-checks to our code to express the non-nullness. IIRC, there was one code path that would result in an arg-null \ null-ref if the scheme was null, but I wasn't a 100% certain if this was the right default