Skip to content

Commit 38e66e2

Browse files
Use var more
More usage of var for consistency, rather than using explicit types.
1 parent 9cf06a1 commit 38e66e2

File tree

7 files changed

+65
-53
lines changed

7 files changed

+65
-53
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ dotnet_naming_symbols.constant_fields.required_modifiers = const
8585
[*.cs]
8686

8787
# var preferences
88-
csharp_style_var_for_built_in_types = false:suggestion
88+
csharp_style_var_for_built_in_types = true:suggestion
8989
csharp_style_var_when_type_is_apparent = true:silent
9090
csharp_style_var_elsewhere = true:silent
9191

src/AspNet.Security.OAuth.Alipay/AlipayAuthenticationHandler.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
7272
context.Properties.Items.Remove(OAuthConstants.CodeVerifierKey);
7373
}
7474

75-
string address = QueryHelpers.AddQueryString(Options.TokenEndpoint, tokenRequestParameters);
75+
var address = QueryHelpers.AddQueryString(Options.TokenEndpoint, tokenRequestParameters);
7676

7777
using var response = await Backchannel.GetAsync(address, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
7878
if (!response.IsSuccessStatusCode)
@@ -85,7 +85,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
8585
using var document = JsonDocument.Parse(stream);
8686

8787
var mainElement = document.RootElement.GetProperty("alipay_system_oauth_token_response");
88-
if (!ValidateReturnCode(mainElement, out string code))
88+
if (!ValidateReturnCode(mainElement, out var code))
8989
{
9090
return OAuthTokenResponse.Failed(new Exception($"An error (Code:{code}) occurred while retrieving an access token."));
9191
}
@@ -113,7 +113,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
113113
};
114114
parameters.Add("sign", GetRSA2Signature(parameters));
115115

116-
string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, parameters);
116+
var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, parameters);
117117

118118
using var response = await Backchannel.GetAsync(address, Context.RequestAborted);
119119

@@ -129,11 +129,11 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
129129

130130
if (!rootElement.TryGetProperty("alipay_user_info_share_response", out JsonElement mainElement))
131131
{
132-
string errorCode = rootElement.GetProperty("error_response").GetProperty("code").GetString()!;
132+
var errorCode = rootElement.GetProperty("error_response").GetProperty("code").GetString()!;
133133
throw new Exception($"An error (Code:{errorCode}) occurred while retrieving user information.");
134134
}
135135

136-
if (!ValidateReturnCode(mainElement, out string code))
136+
if (!ValidateReturnCode(mainElement, out var code))
137137
{
138138
throw new Exception($"An error (Code:{code}) occurred while retrieving user information.");
139139
}
@@ -192,14 +192,14 @@ private string GetRSA2Signature([NotNull] SortedDictionary<string, string?> sort
192192
.Append('&');
193193
}
194194

195-
string plainText = builder.ToString();
196-
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText, 0, plainText.Length - 1); // Skip the last '&'
197-
byte[] privateKeyBytes = Convert.FromBase64String(Options.ClientSecret);
195+
var plainText = builder.ToString();
196+
var plainBytes = Encoding.UTF8.GetBytes(plainText, 0, plainText.Length - 1); // Skip the last '&'
197+
var privateKeyBytes = Convert.FromBase64String(Options.ClientSecret);
198198

199199
using var rsa = RSA.Create();
200200
rsa.ImportRSAPrivateKey(privateKeyBytes, out _);
201201

202-
byte[] encryptedBytes = rsa.SignData(plainBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
202+
var encryptedBytes = rsa.SignData(plainBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
203203

204204
return Convert.ToBase64String(encryptedBytes);
205205
}
@@ -220,13 +220,13 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
220220

221221
if (Options.UsePkce)
222222
{
223-
byte[] bytes = RandomNumberGenerator.GetBytes(256 / 8);
224-
string codeVerifier = WebEncoders.Base64UrlEncode(bytes);
223+
var bytes = RandomNumberGenerator.GetBytes(256 / 8);
224+
var codeVerifier = WebEncoders.Base64UrlEncode(bytes);
225225

226226
// Store this for use during the code redemption.
227227
properties.Items.Add(OAuthConstants.CodeVerifierKey, codeVerifier);
228228

229-
byte[] challengeBytes = SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier));
229+
var challengeBytes = SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier));
230230
parameters[OAuthConstants.CodeChallengeKey] = WebEncoders.Base64UrlEncode(challengeBytes);
231231
parameters[OAuthConstants.CodeChallengeMethodKey] = OAuthConstants.CodeChallengeMethodS256;
232232
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
3939
};
4040

4141
// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
42-
if (context.Properties.Items.TryGetValue(OAuthConstants.CodeVerifierKey, out string? codeVerifier))
42+
if (context.Properties.Items.TryGetValue(OAuthConstants.CodeVerifierKey, out var codeVerifier))
4343
{
4444
tokenRequestParameters.Add(OAuthConstants.CodeVerifierKey, codeVerifier);
4545
context.Properties.Items.Remove(OAuthConstants.CodeVerifierKey);
4646
}
4747

48-
string endpoint = QueryHelpers.AddQueryString(Options.TokenEndpoint, tokenRequestParameters);
48+
var endpoint = QueryHelpers.AddQueryString(Options.TokenEndpoint, tokenRequestParameters);
4949

5050
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, endpoint);
5151
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
@@ -65,7 +65,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
6565
[NotNull] AuthenticationProperties properties,
6666
[NotNull] OAuthTokenResponse tokens)
6767
{
68-
string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "access_token", tokens.AccessToken!);
68+
var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "access_token", tokens.AccessToken!);
6969

7070
using var request = new HttpRequestMessage(HttpMethod.Get, address);
7171
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
@@ -90,7 +90,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
9090
protected override string BuildChallengeUrl([NotNull] AuthenticationProperties properties, [NotNull] string redirectUri)
9191
{
9292
var scopeParameter = properties.GetParameter<ICollection<string>>(OAuthChallengeProperties.ScopeKey);
93-
string scopes = scopeParameter != null ? FormatScope(scopeParameter) : FormatScope();
93+
var scopes = scopeParameter != null ? FormatScope(scopeParameter) : FormatScope();
9494

9595
var parameters = new Dictionary<string, string?>
9696
{
@@ -101,13 +101,13 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
101101

102102
if (Options.UsePkce)
103103
{
104-
byte[] bytes = RandomNumberGenerator.GetBytes(256 / 8);
105-
string codeVerifier = WebEncoders.Base64UrlEncode(bytes);
104+
var bytes = RandomNumberGenerator.GetBytes(256 / 8);
105+
var codeVerifier = WebEncoders.Base64UrlEncode(bytes);
106106

107107
// Store this for use during the code redemption.
108108
properties.Items.Add(OAuthConstants.CodeVerifierKey, codeVerifier);
109109

110-
byte[] challengeBytes = SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier));
110+
var challengeBytes = SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier));
111111
parameters[OAuthConstants.CodeChallengeKey] = WebEncoders.Base64UrlEncode(challengeBytes);
112112
parameters[OAuthConstants.CodeChallengeMethodKey] = OAuthConstants.CodeChallengeMethodS256;
113113
}

src/AspNet.Security.OAuth.Mixcloud/MixcloudAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
8383
[NotNull] AuthenticationProperties properties,
8484
[NotNull] OAuthTokenResponse tokens)
8585
{
86-
string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "access_token", tokens.AccessToken!);
86+
var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "access_token", tokens.AccessToken!);
8787

8888
using var request = new HttpRequestMessage(HttpMethod.Get, address);
8989
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
3535
[NotNull] AuthenticationProperties properties,
3636
[NotNull] OAuthTokenResponse tokens)
3737
{
38-
string uri = string.Format(CultureInfo.InvariantCulture, ShopifyAuthenticationDefaults.UserInformationEndpointFormat, properties.Items[ShopifyAuthenticationDefaults.ShopNameAuthenticationProperty]);
38+
var uri = string.Format(
39+
CultureInfo.InvariantCulture,
40+
ShopifyAuthenticationDefaults.UserInformationEndpointFormat,
41+
properties.Items[ShopifyAuthenticationDefaults.ShopNameAuthenticationProperty]);
3942

4043
using var request = new HttpRequestMessage(HttpMethod.Get, uri);
4144
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
@@ -52,23 +55,23 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
5255

5356
// In Shopify, the customer can modify the scope given to the app. Apps should verify
5457
// that the customer is allowing the required scope.
55-
string actualScope = tokens.Response!.RootElement.GetString("scope") ?? string.Empty;
56-
bool isPersistent = true;
58+
var actualScope = tokens.Response!.RootElement.GetString("scope") ?? string.Empty;
59+
var isPersistent = true;
5760

5861
// If the request was for a "per-user" (i.e. no offline access)
5962
if (tokens.Response.RootElement.TryGetProperty("expires_in", out var expiresInProperty))
6063
{
6164
isPersistent = false;
6265

63-
if (expiresInProperty.TryGetInt32(out int expiresIn))
66+
if (expiresInProperty.TryGetInt32(out var expiresIn))
6467
{
6568
var expires = Clock.UtcNow.AddSeconds(expiresIn);
6669
identity.AddClaim(new Claim(ClaimTypes.Expiration, expires.ToString("O", CultureInfo.InvariantCulture), ClaimValueTypes.DateTime));
6770
}
6871

6972
actualScope = tokens.Response.RootElement.GetString("associated_user_scope") ?? string.Empty;
7073

71-
string userData = tokens.Response.RootElement.GetString("associated_user") ?? string.Empty;
74+
var userData = tokens.Response.RootElement.GetString("associated_user") ?? string.Empty;
7275
identity.AddClaim(new Claim(ClaimTypes.UserData, userData));
7376
}
7477

@@ -94,16 +97,16 @@ protected override string FormatScope([NotNull] IEnumerable<string> scopes)
9497
/// <inheritdoc />
9598
protected override string BuildChallengeUrl([NotNull] AuthenticationProperties properties, [NotNull] string redirectUri)
9699
{
97-
if (!properties.Items.TryGetValue(ShopifyAuthenticationDefaults.ShopNameAuthenticationProperty, out string? shopName))
100+
if (!properties.Items.TryGetValue(ShopifyAuthenticationDefaults.ShopNameAuthenticationProperty, out var shopName))
98101
{
99102
Log.ShopNameMissing(Logger);
100103
throw new InvalidOperationException("Shopify provider AuthenticationProperties must contain ShopNameAuthenticationProperty.");
101104
}
102105

103-
string authorizationEndpoint = string.Format(CultureInfo.InvariantCulture, Options.AuthorizationEndpoint, shopName);
106+
var authorizationEndpoint = string.Format(CultureInfo.InvariantCulture, Options.AuthorizationEndpoint, shopName);
104107

105108
// Get the permission scope, which can either be set in options or overridden in AuthenticationProperties.
106-
if (!properties.Items.TryGetValue(ShopifyAuthenticationDefaults.ShopScopeAuthenticationProperty, out string? scope))
109+
if (!properties.Items.TryGetValue(ShopifyAuthenticationDefaults.ShopScopeAuthenticationProperty, out var scope))
107110
{
108111
var scopeParameter = properties.GetParameter<ICollection<string>>(OAuthChallengeProperties.ScopeKey);
109112
scope = scopeParameter != null ? FormatScope(scopeParameter) : FormatScope();
@@ -118,23 +121,23 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
118121

119122
if (Options.UsePkce)
120123
{
121-
byte[] bytes = RandomNumberGenerator.GetBytes(256 / 8);
122-
string codeVerifier = WebEncoders.Base64UrlEncode(bytes);
124+
var bytes = RandomNumberGenerator.GetBytes(256 / 8);
125+
var codeVerifier = WebEncoders.Base64UrlEncode(bytes);
123126

124127
// Store this for use during the code redemption.
125128
properties.Items.Add(OAuthConstants.CodeVerifierKey, codeVerifier);
126129

127-
byte[] challengeBytes = SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier));
130+
var challengeBytes = SHA256.HashData(Encoding.UTF8.GetBytes(codeVerifier));
128131
parameters[OAuthConstants.CodeChallengeKey] = WebEncoders.Base64UrlEncode(challengeBytes);
129132
parameters[OAuthConstants.CodeChallengeMethodKey] = OAuthConstants.CodeChallengeMethodS256;
130133
}
131134

132135
parameters["state"] = Options.StateDataFormat.Protect(properties);
133136

134-
string challengeUrl = QueryHelpers.AddQueryString(authorizationEndpoint, parameters);
137+
var challengeUrl = QueryHelpers.AddQueryString(authorizationEndpoint, parameters);
135138

136139
// If we're requesting a per-user, online only, token, add the grant_options query param.
137-
if (properties.Items.TryGetValue(ShopifyAuthenticationDefaults.GrantOptionsAuthenticationProperty, out string? grantOptions) &&
140+
if (properties.Items.TryGetValue(ShopifyAuthenticationDefaults.GrantOptionsAuthenticationProperty, out var grantOptions) &&
138141
grantOptions == ShopifyAuthenticationDefaults.PerUserAuthenticationPropertyValue)
139142
{
140143
challengeUrl = QueryHelpers.AddQueryString(challengeUrl, "grant_options[]", ShopifyAuthenticationDefaults.PerUserAuthenticationPropertyValue);
@@ -167,7 +170,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
167170
var shopValue = Context.Request.Query["shop"];
168171
var stateValue = Context.Request.Query["state"];
169172

170-
string shop = shopValue.ToString();
173+
var shop = shopValue.ToString();
171174

172175
// Shop name must end with myshopify.com
173176
if (!shop.EndsWith(".myshopify.com", StringComparison.OrdinalIgnoreCase))
@@ -182,7 +185,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
182185
// request the token. This probably isn't necessary, but it's an easy extra verification.
183186
var authenticationProperties = Options.StateDataFormat.Unprotect(stateValue);
184187

185-
string? shopNamePropertyValue = authenticationProperties?.Items[ShopifyAuthenticationDefaults.ShopNameAuthenticationProperty];
188+
var shopNamePropertyValue = authenticationProperties?.Items[ShopifyAuthenticationDefaults.ShopNameAuthenticationProperty];
186189

187190
if (!string.Equals(shopNamePropertyValue, shopDns, StringComparison.OrdinalIgnoreCase))
188191
{
@@ -195,7 +198,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
195198
return OAuthTokenResponse.Failed(ex);
196199
}
197200

198-
string uri = string.Format(CultureInfo.InvariantCulture, Options.TokenEndpoint, shopDns);
201+
var uri = string.Format(CultureInfo.InvariantCulture, Options.TokenEndpoint, shopDns);
199202

200203
using var request = new HttpRequestMessage(HttpMethod.Post, uri);
201204
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

src/AspNet.Security.OAuth.Weixin/WeixinAuthenticationHandler.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
5454
[NotNull] AuthenticationProperties properties,
5555
[NotNull] OAuthTokenResponse tokens)
5656
{
57-
string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, new Dictionary<string, string?>
57+
var parameters = new Dictionary<string, string?>
5858
{
5959
["access_token"] = tokens.AccessToken,
6060
["openid"] = tokens.Response?.RootElement.GetString("openid")
61-
});
61+
};
62+
63+
var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, parameters);
6264

6365
using var response = await Backchannel.GetAsync(address);
6466
if (!response.IsSuccessStatusCode)
@@ -102,7 +104,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
102104
context.Properties.Items.Remove(OAuthConstants.CodeVerifierKey);
103105
}
104106

105-
string address = QueryHelpers.AddQueryString(Options.TokenEndpoint, tokenRequestParameters);
107+
var address = QueryHelpers.AddQueryString(Options.TokenEndpoint, tokenRequestParameters);
106108

107109
using var response = await Backchannel.GetAsync(address);
108110
if (!response.IsSuccessStatusCode)
@@ -150,8 +152,8 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
150152
parameters[OAuthConstants.CodeChallengeMethodKey] = OAuthConstants.CodeChallengeMethodS256;
151153
}
152154

153-
string state = Options.StateDataFormat.Protect(properties);
154-
bool addRedirectHash = false;
155+
var state = Options.StateDataFormat.Protect(properties);
156+
var addRedirectHash = false;
155157

156158
if (!IsWeixinAuthorizationEndpointInUse())
157159
{
@@ -163,7 +165,7 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
163165
parameters["redirect_uri"] = redirectUri;
164166
parameters[State] = addRedirectHash ? OauthState : state;
165167

166-
string challengeUrl = QueryHelpers.AddQueryString(Options.AuthorizationEndpoint, parameters);
168+
var challengeUrl = QueryHelpers.AddQueryString(Options.AuthorizationEndpoint, parameters);
167169

168170
if (addRedirectHash)
169171
{

0 commit comments

Comments
 (0)