Skip to content

Commit 31b82d5

Browse files
Fix nullable errors
Fix compiler errors caused by new nullable annotations.
1 parent 1781067 commit 31b82d5

File tree

19 files changed

+36
-35
lines changed

19 files changed

+36
-35
lines changed

src/AspNet.Security.OAuth.Apple/AppleAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected virtual IEnumerable<Claim> ExtractClaimsFromUser([NotNull] JsonElement
176176

177177
if (user.TryGetProperty("email", out var email))
178178
{
179-
claims.Add(new Claim(ClaimTypes.Email, email.GetString(), ClaimValueTypes.String, ClaimsIssuer));
179+
claims.Add(new Claim(ClaimTypes.Email, email.GetString() ?? string.Empty, ClaimValueTypes.String, ClaimsIssuer));
180180
}
181181

182182
return claims;

src/AspNet.Security.OAuth.Bitbucket/BitbucketAuthenticationHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
6363
!identity.HasClaim(claim => claim.Type == ClaimTypes.Email) &&
6464
Options.Scope.Contains("email"))
6565
{
66-
string address = await GetEmailAsync(tokens);
66+
string? address = await GetEmailAsync(tokens);
6767

6868
if (!string.IsNullOrEmpty(address))
6969
{
70-
identity.AddClaim(new Claim(ClaimTypes.Email, address, ClaimValueTypes.String, Options.ClaimsIssuer));
70+
identity.AddClaim(new Claim(ClaimTypes.Email, address!, ClaimValueTypes.String, Options.ClaimsIssuer));
7171
}
7272
}
7373

7474
await Options.Events.CreatingTicket(context);
7575
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
7676
}
7777

78-
protected virtual async Task<string> GetEmailAsync([NotNull] OAuthTokenResponse tokens)
78+
protected virtual async Task<string?> GetEmailAsync([NotNull] OAuthTokenResponse tokens)
7979
{
8080
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserEmailsEndpoint);
8181
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

src/AspNet.Security.OAuth.Deezer/DeezerAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
129129
properties.Items.Add(OAuthConstants.CodeVerifierKey, codeVerifier);
130130

131131
using var sha256 = HashAlgorithm.Create("SHA256");
132-
byte[] challengeBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(codeVerifier));
132+
byte[] challengeBytes = sha256!.ComputeHash(Encoding.UTF8.GetBytes(codeVerifier));
133133
parameters[OAuthConstants.CodeChallengeKey] = WebEncoders.Base64UrlEncode(challengeBytes);
134134
parameters[OAuthConstants.CodeChallengeMethodKey] = OAuthConstants.CodeChallengeMethodS256;
135135
}

src/AspNet.Security.OAuth.Fitbit/FitbitAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
7676
["code"] = context.Code
7777
};
7878

79-
request.Content = new FormUrlEncodedContent(parameters);
79+
request.Content = new FormUrlEncodedContent(parameters!);
8080

8181
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
8282
if (!response.IsSuccessStatusCode)

src/AspNet.Security.OAuth.GitHub/GitHubAuthenticationHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
6363
!identity.HasClaim(claim => claim.Type == ClaimTypes.Email) &&
6464
Options.Scope.Contains("user:email"))
6565
{
66-
string address = await GetEmailAsync(tokens);
66+
string? address = await GetEmailAsync(tokens);
6767

6868
if (!string.IsNullOrEmpty(address))
6969
{
@@ -75,7 +75,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
7575
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
7676
}
7777

78-
protected virtual async Task<string> GetEmailAsync([NotNull] OAuthTokenResponse tokens)
78+
protected virtual async Task<string?> GetEmailAsync([NotNull] OAuthTokenResponse tokens)
7979
{
8080
// See https://developer.github.com/v3/users/emails/ for more information about the /user/emails endpoint.
8181
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserEmailsEndpoint);

src/AspNet.Security.OAuth.Gitee/GiteeAuthenticationHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public GiteeAuthenticationHandler(
2727
[NotNull] UrlEncoder encoder,
2828
[NotNull] ISystemClock clock)
2929
: base(options, logger, encoder, clock)
30-
{
31-
}
30+
{
31+
}
3232

3333
protected override async Task<AuthenticationTicket> CreateTicketAsync(
3434
[NotNull] ClaimsIdentity identity,
@@ -63,7 +63,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
6363
!identity.HasClaim(claim => claim.Type == ClaimTypes.Email) &&
6464
Options.Scope.Contains("emails"))
6565
{
66-
string address = await GetEmailAsync(tokens);
66+
string? address = await GetEmailAsync(tokens);
6767

6868
if (!string.IsNullOrEmpty(address))
6969
{
@@ -75,7 +75,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
7575
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
7676
}
7777

78-
protected async Task<string> GetEmailAsync([NotNull] OAuthTokenResponse tokens)
78+
protected async Task<string?> GetEmailAsync([NotNull] OAuthTokenResponse tokens)
7979
{
8080
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserEmailsEndpoint);
8181
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

src/AspNet.Security.OAuth.LinkedIn/LinkedInAuthenticationOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public LinkedInAuthenticationOptions()
7272
/// 3. Returns the first value.
7373
/// </summary>
7474
/// <see cref="DefaultMultiLocaleStringResolver(IReadOnlyDictionary{string, string}, string?)"/>
75-
public Func<IReadOnlyDictionary<string, string>, string?, string> MultiLocaleStringResolver { get; set; } = DefaultMultiLocaleStringResolver;
75+
public Func<IReadOnlyDictionary<string, string?>, string?, string?> MultiLocaleStringResolver { get; set; } = DefaultMultiLocaleStringResolver;
7676

7777
/// <summary>
7878
/// Gets the <c>MultiLocaleString</c> value using the configured resolver.
@@ -100,7 +100,7 @@ public LinkedInAuthenticationOptions()
100100
preferredLocaleKey = $"{preferredLocale.GetString("language")}_{preferredLocale.GetString("country")}";
101101
}
102102

103-
var preferredLocales = new Dictionary<string, string>();
103+
var preferredLocales = new Dictionary<string, string?>();
104104

105105
foreach (var element in propertyLocalized.EnumerateObject())
106106
{
@@ -164,7 +164,7 @@ private static IEnumerable<string> GetPictureUrls(JsonElement user)
164164
/// <param name="localizedValues">The localized values with culture keys.</param>
165165
/// <param name="preferredLocale">The preferred locale, if provided by LinkedIn.</param>
166166
/// <returns>The localized value.</returns>
167-
private static string DefaultMultiLocaleStringResolver(IReadOnlyDictionary<string, string> localizedValues, string? preferredLocale)
167+
private static string? DefaultMultiLocaleStringResolver(IReadOnlyDictionary<string, string?> localizedValues, string? preferredLocale)
168168
{
169169
if (!string.IsNullOrEmpty(preferredLocale) &&
170170
localizedValues.TryGetValue(preferredLocale, out string? preferredLocaleValue))

src/AspNet.Security.OAuth.Odnoklassniki/OdnoklassnikiAuthenticationHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
4040
[NotNull] OAuthTokenResponse tokens)
4141
{
4242
using var algorithm = HashAlgorithm.Create("MD5");
43-
string accessSecret = GetHash(algorithm, tokens.AccessToken + Options.ClientSecret);
44-
string sign = GetHash(algorithm, $"application_key={Options.PublicSecret}format=jsonmethod=users.getCurrentUser{accessSecret}");
43+
string accessSecret = GetHash(algorithm!, tokens.AccessToken + Options.ClientSecret);
44+
string sign = GetHash(algorithm!, $"application_key={Options.PublicSecret}format=jsonmethod=users.getCurrentUser{accessSecret}");
4545

4646
string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, new Dictionary<string, string>
4747
{

src/AspNet.Security.OAuth.Reddit/RedditAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
108108
["code"] = context.Code
109109
};
110110

111-
request.Content = new FormUrlEncodedContent(parameters);
111+
request.Content = new FormUrlEncodedContent(parameters!);
112112

113113
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
114114
if (!response.IsSuccessStatusCode)

src/AspNet.Security.OAuth.Shopify/ShopifyAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
186186
["code"] = context.Code
187187
};
188188

189-
request.Content = new FormUrlEncodedContent(parameters);
189+
request.Content = new FormUrlEncodedContent(parameters!);
190190

191191
using var response = await Backchannel.SendAsync(request, Context.RequestAborted);
192192

src/AspNet.Security.OAuth.StackExchange/StackExchangeAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
9696

9797
using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint)
9898
{
99-
Content = new FormUrlEncodedContent(parameters)
99+
Content = new FormUrlEncodedContent(parameters!)
100100
};
101101

102102
using var response = await Backchannel.SendAsync(request, Context.RequestAborted);

src/AspNet.Security.OAuth.VisualStudio/VisualStudioAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
7777
["client_assertion_type"] = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
7878
};
7979

80-
request.Content = new FormUrlEncodedContent(parameters);
80+
request.Content = new FormUrlEncodedContent(parameters!);
8181

8282
using var response = await Backchannel.SendAsync(request, Context.RequestAborted);
8383

src/AspNet.Security.OAuth.Weibo/WeiboAuthenticationHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
6666
!identity.HasClaim(claim => claim.Type == ClaimTypes.Email) &&
6767
Options.Scope.Contains("email"))
6868
{
69-
string email = await GetEmailAsync(tokens);
69+
string? email = await GetEmailAsync(tokens);
7070

7171
if (!string.IsNullOrEmpty(address))
7272
{
73-
identity.AddClaim(new Claim(ClaimTypes.Email, email, ClaimValueTypes.String, Options.ClaimsIssuer));
73+
identity.AddClaim(new Claim(ClaimTypes.Email, email!, ClaimValueTypes.String, Options.ClaimsIssuer));
7474
}
7575
}
7676

@@ -84,7 +84,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
8484

8585
protected override string FormatScope() => string.Join(",", Options.Scope);
8686

87-
protected virtual async Task<string> GetEmailAsync([NotNull] OAuthTokenResponse tokens)
87+
protected virtual async Task<string?> GetEmailAsync([NotNull] OAuthTokenResponse tokens)
8888
{
8989
// See http://open.weibo.com/wiki/2/account/profile/email for more information about the /account/profile/email.json endpoint.
9090
string address = QueryHelpers.AddQueryString(Options.UserEmailsEndpoint, "access_token", tokens.AccessToken);

src/AspNet.Security.OAuth.Yahoo/YahooAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
7676
["code"] = context.Code
7777
};
7878

79-
request.Content = new FormUrlEncodedContent(parameters);
79+
request.Content = new FormUrlEncodedContent(parameters!);
8080

8181
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
8282
if (!response.IsSuccessStatusCode)

src/AspNet.Security.OAuth.Yammer/YammerAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
7777
["grant_type"] = "authorization_code"
7878
};
7979

80-
request.Content = new FormUrlEncodedContent(parameters);
80+
request.Content = new FormUrlEncodedContent(parameters!);
8181

8282
using var response = await Backchannel.SendAsync(request, Context.RequestAborted);
8383
if (!response.IsSuccessStatusCode)

src/AspNet.Security.OAuth.Yandex/YandexAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
7676
["code"] = context.Code
7777
};
7878

79-
request.Content = new FormUrlEncodedContent(parameters);
79+
request.Content = new FormUrlEncodedContent(parameters!);
8080

8181
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
8282
if (!response.IsSuccessStatusCode)

test/AspNet.Security.OAuth.Providers.Tests/Infrastructure/ApplicationFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private static void ConfigureApplication<TOptions>(IApplicationBuilder app, OAut
102102
"/me",
103103
async context =>
104104
{
105-
if (context.User.Identity.IsAuthenticated)
105+
if (context.User.Identity?.IsAuthenticated == true)
106106
{
107107
string xml = IdentityToXmlString(context.User);
108108
byte[] buffer = Encoding.UTF8.GetBytes(xml);

test/AspNet.Security.OAuth.Providers.Tests/Infrastructure/LoopbackRedirectHandler.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
4040
if (RedirectMethod == HttpMethod.Post)
4141
{
4242
var queryString = HttpUtility.ParseQueryString(result.Headers.Location!.Query);
43-
string state = queryString["state"];
43+
string? state = queryString["state"];
4444

45-
var parameters = new Dictionary<string, string>()
45+
var parameters = new Dictionary<string, string?>()
4646
{
4747
["code"] = "a6ed8e7f-471f-44f1-903b-65946475f351",
4848
["state"] = state,
@@ -56,7 +56,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
5656
}
5757
}
5858

59-
content = new FormUrlEncodedContent(parameters);
59+
content = new FormUrlEncodedContent(parameters!);
6060
}
6161
else
6262
{
@@ -89,17 +89,17 @@ protected virtual Uri BuildLoopbackUri(HttpResponseMessage responseMessage)
8989
{
9090
// Rewrite the URI to loop back to the redirected URL to simulate the user having
9191
// successfully authenticated with the external login page they were redirected to.
92-
var queryString = HttpUtility.ParseQueryString(responseMessage.Headers.Location.Query);
92+
var queryString = HttpUtility.ParseQueryString(responseMessage.Headers.Location!.Query);
9393

9494
string? location = queryString["redirect_uri"] ?? RedirectUri;
95-
string state = queryString["state"];
95+
string? state = queryString["state"];
9696

9797
var builder = new UriBuilder(location!);
9898

9999
// Retain the _oauthstate parameter in redirect_uri for WeChat (see #262)
100100
const string OAuthStateKey = "_oauthstate";
101101
var redirectQuery = HttpUtility.ParseQueryString(builder.Query);
102-
string oauthState = redirectQuery[OAuthStateKey];
102+
string? oauthState = redirectQuery[OAuthStateKey];
103103

104104
// Remove any query string parameters we do not explictly need to retain
105105
queryString.Clear();

test/AspNet.Security.OAuth.Providers.Tests/OAuthTests`1.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ protected async Task<IDictionary<string, Claim>> AuthenticateUserAsync(WebApplic
170170

171171
// Assert
172172
result.StatusCode.ShouldBe(HttpStatusCode.OK);
173-
result.Content.Headers.ContentType.MediaType.ShouldBe("text/xml");
173+
result.Content.Headers.ContentType.ShouldNotBeNull();
174+
result.Content.Headers.ContentType!.MediaType.ShouldBe("text/xml");
174175

175176
string xml = await result.Content.ReadAsStringAsync();
176177

0 commit comments

Comments
 (0)