Skip to content

Commit 53ff96a

Browse files
Fix nullable errors
Fix compiler errors caused by new nullable annotations.
1 parent 8b25982 commit 53ff96a

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
@@ -69,7 +69,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7070
request.Headers.Authorization = CreateAuthorizationHeader();
7171

72-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
72+
request.Content = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string?, string?>>)new Dictionary<string, string>
7373
{
7474
["grant_type"] = "authorization_code",
7575
["redirect_uri"] = context.RedirectUri,

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
@@ -101,7 +101,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
101101
request.Headers.UserAgent.ParseAdd(Options.UserAgent);
102102
}
103103

104-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
104+
request.Content = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string?, string?>>)new Dictionary<string, string>
105105
{
106106
["grant_type"] = "authorization_code",
107107
["redirect_uri"] = context.RedirectUri,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
179179
using var request = new HttpRequestMessage(HttpMethod.Post, uri);
180180
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
181181

182-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
182+
request.Content = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string?, string?>>)new Dictionary<string, string>
183183
{
184184
["client_id"] = Options.ClientId,
185185
["client_secret"] = Options.ClientSecret,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
8787
{
8888
using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint)
8989
{
90-
Content = new FormUrlEncodedContent(new Dictionary<string, string>
90+
Content = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string?, string?>>)new Dictionary<string, string>
9191
{
9292
["client_id"] = Options.ClientId,
9393
["redirect_uri"] = context.RedirectUri,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6868
using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
7070

71-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
71+
request.Content = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string?, string?>>)new Dictionary<string, string>
7272
{
7373
["redirect_uri"] = context.RedirectUri,
7474
["client_assertion"] = Options.ClientSecret,

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
@@ -69,7 +69,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7070
request.Headers.Authorization = CreateAuthorizationHeader();
7171

72-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
72+
request.Content = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string?, string?>>)new Dictionary<string, string>
7373
{
7474
["grant_type"] = "authorization_code",
7575
["redirect_uri"] = context.RedirectUri,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6868
using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7070

71-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
71+
request.Content = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string?, string?>>)new Dictionary<string, string>
7272
{
7373
["client_id"] = Options.ClientId,
7474
["redirect_uri"] = context.RedirectUri,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
6969
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
7070
request.Headers.Authorization = CreateAuthorizationHeader();
7171

72-
request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
72+
request.Content = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string?, string?>>)new Dictionary<string, string>
7373
{
7474
["grant_type"] = "authorization_code",
7575
["redirect_uri"] = context.RedirectUri,

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
@@ -38,9 +38,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
3838
if (RedirectMethod == HttpMethod.Post)
3939
{
4040
var queryString = HttpUtility.ParseQueryString(result.Headers.Location!.Query);
41-
string state = queryString["state"];
41+
string? state = queryString["state"];
4242

43-
var parameters = new Dictionary<string, string>()
43+
var parameters = new Dictionary<string, string?>()
4444
{
4545
["code"] = "a6ed8e7f-471f-44f1-903b-65946475f351",
4646
["state"] = state,
@@ -54,7 +54,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
5454
}
5555
}
5656

57-
content = new FormUrlEncodedContent(parameters);
57+
content = new FormUrlEncodedContent((IEnumerable<KeyValuePair<string?, string?>>)parameters);
5858
}
5959
else
6060
{
@@ -87,17 +87,17 @@ protected virtual Uri BuildLoopbackUri(HttpResponseMessage responseMessage)
8787
{
8888
// Rewrite the URI to loop back to the redirected URL to simulate the user having
8989
// successfully authenticated with the external login page they were redirected to.
90-
var queryString = HttpUtility.ParseQueryString(responseMessage.Headers.Location.Query);
90+
var queryString = HttpUtility.ParseQueryString(responseMessage.Headers.Location!.Query);
9191

9292
string? location = queryString["redirect_uri"] ?? RedirectUri;
93-
string state = queryString["state"];
93+
string? state = queryString["state"];
9494

9595
var builder = new UriBuilder(location!);
9696

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

102102
// Remove any query string parameters we do not explictly need to retain
103103
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)