Skip to content

Commit

Permalink
Style consistency
Browse files Browse the repository at this point in the history
Use trailing comma in dictionaries.
Use var instead of explicit type.
  • Loading branch information
martincostello committed Jan 6, 2022
1 parent 72bcab6 commit 51f4c00
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
[NotNull] OAuthTokenResponse tokens)
{
// Note: the ArcGIS API doesn't support content negotiation via headers.
string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, new Dictionary<string, string?>
var parameters = new Dictionary<string, string?>
{
["f"] = "json",
["token"] = tokens.AccessToken
});
["token"] = tokens.AccessToken,
};

var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, parameters!);

using var request = new HttpRequestMessage(HttpMethod.Get, address);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand Down
4 changes: 2 additions & 2 deletions src/AspNet.Security.OAuth.Ebay/EbayAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
{
["grant_type"] = "authorization_code",
["code"] = context.Code,
["redirect_uri"] = Options.RuName!
["redirect_uri"] = Options.RuName!,
};

// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
Expand Down Expand Up @@ -90,7 +90,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA

private AuthenticationHeaderValue CreateAuthorizationHeader()
{
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
string.Concat(
EscapeDataString(Options.ClientId),
":",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
{
["grant_type"] = "authorization_code",
["redirect_uri"] = context.RedirectUri,
["code"] = context.Code
["code"] = context.Code,
};

// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
Expand Down Expand Up @@ -97,7 +97,7 @@ private AuthenticationHeaderValue CreateAuthorizationHeader()
return Uri.EscapeDataString(value).Replace("%20", "+", StringComparison.Ordinal);
}

string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
string.Concat(
EscapeDataString(Options.ClientId),
":",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
{
// See https://developer.foursquare.com/overview/versioning
// for more information about the mandatory "v" and "m" parameters.
string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, new Dictionary<string, string?>
var parameters = new Dictionary<string, string?>
{
["m"] = "foursquare",
["v"] = !string.IsNullOrEmpty(Options.ApiVersion) ? Options.ApiVersion : FoursquareAuthenticationDefaults.ApiVersion,
["oauth_token"] = tokens.AccessToken,
});
};

var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, parameters);

using var request = new HttpRequestMessage(HttpMethod.Get, address);

Expand Down
12 changes: 6 additions & 6 deletions src/AspNet.Security.OAuth.Line/LineAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
["code"] = context.Code,
["redirect_uri"] = context.RedirectUri,
["client_id"] = Options.ClientId,
["client_secret"] = Options.ClientSecret
["client_secret"] = Options.ClientSecret,
};

// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
Expand Down Expand Up @@ -89,7 +89,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
// When the email address is not public, retrieve it from the emails endpoint if the user:email scope is specified.
if (!string.IsNullOrEmpty(Options.UserEmailsEndpoint) && Options.Scope.Contains("email"))
{
string? email = await GetEmailAsync(tokens);
var email = await GetEmailAsync(tokens);

if (!string.IsNullOrEmpty(email))
{
Expand All @@ -103,14 +103,14 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(

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

var parameters = new Dictionary<string, string?>
{
["id_token"] = tokens.Response?.RootElement.GetString("id_token") ?? string.Empty,
["client_id"] = Options.ClientId
["client_id"] = Options.ClientId,
};

using var request = new HttpRequestMessage(HttpMethod.Post, Options.UserEmailsEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Content = new FormUrlEncodedContent(parameters);

using var response = await Backchannel.SendAsync(request, Context.RequestAborted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public LinkedInAuthenticationOptions()

private string GetFullName(JsonElement user)
{
string?[] nameParts = new string?[]
var nameParts = new string?[]
{
GetMultiLocaleString(user, ProfileFields.FirstName),
GetMultiLocaleString(user, ProfileFields.LastName),
Expand Down Expand Up @@ -134,7 +134,7 @@ private static IEnumerable<string> GetPictureUrls(JsonElement user)
continue;
}

string? pictureUrl = imageIdentifier
var pictureUrl = imageIdentifier
.EnumerateArray()
.FirstOrDefault()
.GetString("identifier");
Expand All @@ -161,13 +161,13 @@ private static IEnumerable<string> GetPictureUrls(JsonElement user)
private static string? DefaultMultiLocaleStringResolver(IReadOnlyDictionary<string, string?> localizedValues, string? preferredLocale)
{
if (!string.IsNullOrEmpty(preferredLocale) &&
localizedValues.TryGetValue(preferredLocale, out string? preferredLocaleValue))
localizedValues.TryGetValue(preferredLocale, out var preferredLocaleValue))
{
return preferredLocaleValue;
}

string currentUIKey = Thread.CurrentThread.CurrentUICulture.ToString().Replace('-', '_');
if (localizedValues.TryGetValue(currentUIKey, out string? currentUIValue))
var currentUIKey = Thread.CurrentThread.CurrentUICulture.ToString().Replace('-', '_');
if (localizedValues.TryGetValue(currentUIKey, out var currentUIValue))
{
return currentUIValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override string BuildChallengeUrl([NotNull] AuthenticationProperties p
{
["client_id"] = Options.ClientId,
["scope"] = scope,
["response_type"] = "code"
["response_type"] = "code",
};

if (Options.UsePkce)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

byte[] byteArray = Encoding.ASCII.GetBytes(Options.ClientId + ":" + Options.ClientSecret);
var byteArray = Encoding.ASCII.GetBytes(Options.ClientId + ":" + Options.ClientSecret);
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
requestMessage.Content = requestContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
[NotNull] AuthenticationProperties properties,
[NotNull] OAuthTokenResponse tokens)
{
string accessSecret = GetMD5Hash(tokens.AccessToken + Options.ClientSecret);
string sign = GetMD5Hash($"application_key={Options.PublicSecret}format=jsonmethod=users.getCurrentUser{accessSecret}");
var accessSecret = GetMD5Hash(tokens.AccessToken + Options.ClientSecret);
var sign = GetMD5Hash($"application_key={Options.PublicSecret}format=jsonmethod=users.getCurrentUser{accessSecret}");

string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, new Dictionary<string, string?>
var parameters = new Dictionary<string, string?>
{
["application_key"] = Options.PublicSecret ?? string.Empty,
["format"] = "json",
["method"] = "users.getCurrentUser",
["sig"] = sign,
["access_token"] = tokens.AccessToken,
});
};

var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, parameters);

using var request = new HttpRequestMessage(HttpMethod.Get, address);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand Down Expand Up @@ -70,7 +72,7 @@ protected override string FormatScope([NotNull] IEnumerable<string> scopes)
private static string GetMD5Hash(string input)
{
#pragma warning disable CA5351
byte[] hash = MD5.HashData(Encoding.UTF8.GetBytes(input));
var hash = MD5.HashData(Encoding.UTF8.GetBytes(input));
#pragma warning restore CA5351

return Convert.ToHexString(hash).ToLowerInvariant();
Expand Down
18 changes: 10 additions & 8 deletions src/AspNet.Security.OAuth.QQ/QQAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
[NotNull] AuthenticationProperties properties,
[NotNull] OAuthTokenResponse tokens)
{
(int errorCode, string? openId, string? unionId) = await GetUserIdentifierAsync(tokens);
(var errorCode, var openId, var unionId) = await GetUserIdentifierAsync(tokens);

if (errorCode != 0 || string.IsNullOrEmpty(openId))
{
Expand All @@ -43,12 +43,14 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
identity.AddClaim(new Claim(QQAuthenticationConstants.Claims.UnionId, unionId, ClaimValueTypes.String, Options.ClaimsIssuer));
}

string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, new Dictionary<string, string?>(3)
var parameters = new Dictionary<string, string?>(3)
{
["oauth_consumer_key"] = Options.ClientId,
["access_token"] = tokens.AccessToken,
["openid"] = openId,
});
};

var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, parameters);

using var response = await Backchannel.GetAsync(address);
if (!response.IsSuccessStatusCode)
Expand All @@ -60,7 +62,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
using var stream = await response.Content.ReadAsStreamAsync(Context.RequestAborted);
using var payload = JsonDocument.Parse(stream);

int status = payload.RootElement.GetProperty("ret").GetInt32();
var status = payload.RootElement.GetProperty("ret").GetInt32();

if (status != 0)
{
Expand All @@ -86,7 +88,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
["redirect_uri"] = context.RedirectUri,
["code"] = context.Code,
["grant_type"] = "authorization_code",
["fmt"] = "json" // Return JSON instead of x-www-form-urlencoded which is default due to historical reasons
["fmt"] = "json", // Return JSON instead of x-www-form-urlencoded which is default due to historical reasons
};

// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
Expand All @@ -96,7 +98,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
context.Properties.Items.Remove(OAuthConstants.CodeVerifierKey);
}

string address = QueryHelpers.AddQueryString(Options.TokenEndpoint, tokenRequestParameters);
var address = QueryHelpers.AddQueryString(Options.TokenEndpoint, tokenRequestParameters);

using var request = new HttpRequestMessage(HttpMethod.Get, address);

Expand Down Expand Up @@ -127,7 +129,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
queryString.Add("unionid", "1");
}

string address = QueryHelpers.AddQueryString(Options.UserIdentificationEndpoint, queryString);
var address = QueryHelpers.AddQueryString(Options.UserIdentificationEndpoint, queryString);
using var request = new HttpRequestMessage(HttpMethod.Get, address);

using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
Expand All @@ -142,7 +144,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA

var payloadRoot = payload.RootElement;

int errorCode =
var errorCode =
payloadRoot.TryGetProperty("error", out var errorCodeElement) && errorCodeElement.ValueKind == JsonValueKind.Number ?
errorCodeElement.GetInt32() :
0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(

protected override string BuildChallengeUrl([NotNull] AuthenticationProperties properties, [NotNull] string redirectUri)
{
string challengeUrl = base.BuildChallengeUrl(properties, redirectUri);
var challengeUrl = base.BuildChallengeUrl(properties, redirectUri);

// Add duration=permanent to the authorization request to get an access token that doesn't expire after 1 hour.
// See https://github.com/reddit/reddit/wiki/OAuth2#authorization for more information.
Expand All @@ -87,7 +87,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
{
["grant_type"] = "authorization_code",
["redirect_uri"] = context.RedirectUri,
["code"] = context.Code
["code"] = context.Code,
};

// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
Expand Down Expand Up @@ -134,7 +134,7 @@ private AuthenticationHeaderValue CreateAuthorizationHeader()
return Uri.EscapeDataString(value).Replace("%20", "+", StringComparison.Ordinal);
}

string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(
string.Concat(
EscapeDataString(Options.ClientId),
":",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
{
["client_id"] = Options.ClientId,
["client_secret"] = Options.ClientSecret,
["code"] = context.Code
["code"] = context.Code,
};

// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public bool RequestPerUserToken
{
get
{
string? prop = GetProperty(ShopifyAuthenticationDefaults.GrantOptionsAuthenticationProperty);
var prop = GetProperty(ShopifyAuthenticationDefaults.GrantOptionsAuthenticationProperty);
return string.Equals(prop, ShopifyAuthenticationDefaults.PerUserAuthenticationPropertyValue, StringComparison.OrdinalIgnoreCase);
}

Expand All @@ -77,6 +77,6 @@ private void SetProperty(string propName, string? value)

private string? GetProperty(string propName)
{
return Items.TryGetValue(propName, out string? val) ? val : null;
return Items.TryGetValue(propName, out var val) ? val : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
queryArguments["key"] = Options.RequestKey;
}

string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, queryArguments);
var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, queryArguments);

using var request = new HttpRequestMessage(HttpMethod.Get, address);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Expand Down Expand Up @@ -79,7 +79,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
["redirect_uri"] = context.RedirectUri,
["client_secret"] = Options.ClientSecret,
["code"] = context.Code,
["grant_type"] = "authorization_code"
["grant_type"] = "authorization_code",
};

// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
Expand Down Expand Up @@ -107,7 +107,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA

var token = new JsonObject();

foreach ((string key, StringValues value) in content)
foreach ((var key, var value) in content)
{
token[key] = value.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
context.Properties.Items.Remove(OAuthConstants.CodeVerifierKey);
}

using var requestContent = new FormUrlEncodedContent(tokenRequestParameters);
var parameters = new Dictionary<string, string?>
{
["client_id"] = Options.ClientId,
["redirect_uri"] = context.RedirectUri,
["client_secret"] = Options.ClientSecret,
["code"] = context.Code,
};

string address = QueryHelpers.AddQueryString(Options.TokenEndpoint,
new Dictionary<string, string?>
{
["client_id"] = Options.ClientId,
["redirect_uri"] = context.RedirectUri,
["client_secret"] = Options.ClientSecret,
["code"] = context.Code
});
var address = QueryHelpers.AddQueryString(Options.TokenEndpoint, parameters);

using var requestContent = new FormUrlEncodedContent(tokenRequestParameters);
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, address);
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
requestMessage.Content = requestContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] OA
["client_assertion"] = Options.ClientSecret,
["assertion"] = context.Code,
["grant_type"] = "urn:ietf:params:oauth:grant-type:jwt-bearer",
["client_assertion_type"] = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
["client_assertion_type"] = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
};

// PKCE https://tools.ietf.org/html/rfc7636#section-4.5, see BuildChallengeUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
[NotNull] AuthenticationProperties properties,
[NotNull] OAuthTokenResponse tokens)
{
string address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, new Dictionary<string, string?>
var parameters = new Dictionary<string, string?>
{
["access_token"] = tokens.AccessToken,
["v"] = !string.IsNullOrEmpty(Options.ApiVersion) ? Options.ApiVersion : VkontakteAuthenticationDefaults.ApiVersion
});
["v"] = !string.IsNullOrEmpty(Options.ApiVersion) ? Options.ApiVersion : VkontakteAuthenticationDefaults.ApiVersion,
};

var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, parameters);

if (Options.Fields.Count != 0)
{
Expand Down
Loading

0 comments on commit 51f4c00

Please sign in to comment.