Skip to content
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

Style checking azure identity #7583

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ csharp_new_line_between_query_expression_clauses = true # vs-default: true
csharp_indent_block_contents = true # vs-default: true
csharp_indent_braces = false # vs-default: false
csharp_indent_case_contents = true # vs-default: true
csharp_indent_case_contents_when_block = true
csharp_indent_switch_labels = true # vs-default: true
csharp_indent_labels = one_less_than_current # vs-default: one_less_than_current

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

# avoid this. unless absolutely necessary
dotnet_style_qualification_for_field = false:suggestion # vs-default: false:none
dotnet_style_qualification_for_property = false:suggestion # vs-default: false:none
Expand Down Expand Up @@ -77,16 +81,32 @@ dotnet_naming_style.camel_case_underscore_style.required_prefix = _
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case

# Code style defaults
csharp_using_directive_placement = outside_namespace:suggestion
dotnet_sort_system_directives_first = true # vs-default: true
csharp_prefer_braces = true:refactoring
csharp_preserve_single_line_blocks = true # vs-default: true
csharp_preserve_single_line_statements = false # vs-default: true
csharp_prefer_static_local_function = true:suggestion
csharp_prefer_simple_using_statement = false:none
csharp_style_prefer_switch_expression = true:suggestion

# Code quality
dotnet_style_readonly_field = true:suggestion
dotnet_code_quality_unused_parameters = non_public:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion # vs-default: true:suggestion
dotnet_style_collection_initializer = true:suggestion # vs-default: true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion # vs-default: true:suggestion
dotnet_style_coalesce_expression = true:suggestion # vs-default: true:suggestion
dotnet_style_null_propagation = true:suggestion # vs-default: true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:refactoring
dotnet_style_prefer_conditional_expression_over_return = true:refactoring
csharp_prefer_simple_default_expression = true:suggestion

# Expression-bodied members
csharp_style_expression_bodied_methods = false:none # vs-default: false:none
Expand All @@ -95,6 +115,8 @@ csharp_style_expression_bodied_operators = false:none # vs-default: false:none
csharp_style_expression_bodied_properties = true:none # vs-default: true:none
csharp_style_expression_bodied_indexers = true:none # vs-default: true:none
csharp_style_expression_bodied_accessors = true:none # vs-default: true:none
csharp_style_expression_bodied_lambdas = true:refactoring
csharp_style_expression_bodied_local_functions = true:refactoring

# Pattern matching
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion # vs-default: true:suggestion
Expand All @@ -105,6 +127,11 @@ csharp_style_inlined_variable_declaration = true:suggestion # vs-default: true:s
csharp_style_throw_expression = true:suggestion # vs-default: true:suggestion
csharp_style_conditional_delegate_call = true:suggestion # vs-default: true:suggestion

# Other features
csharp_style_prefer_index_operator = false:none
csharp_style_prefer_range_operator = false:none
csharp_style_pattern_local_over_anonymous_function = false:none

# Space preferences
csharp_space_after_cast = false # vs-default: false
csharp_space_after_colon_in_inheritance_clause = true # vs-default: true
Expand All @@ -130,4 +157,29 @@ csharp_space_between_parentheses = false # vs-default: false
csharp_space_between_square_brackets = false # vs-default: false

# Require accessibility modifiers
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion # vs-default: for_non_interface_members:none
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion # vs-default: for_non_interface_members:none

# Analyzers
dotnet_code_quality.ca1802.api_surface = private, internal

# Xml project files
[*.{csproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
indent_size = 2

# Xml build files
[*.builds]
indent_size = 2

# Xml files
[*.{xml,stylecop,resx,ruleset}]
indent_size = 2

# Xml config files
[*.{props,targets,config,nuspec}]
indent_size = 2

# Shell scripts
[*.sh]
end_of_line = lf
[*.{cmd, bat}]
end_of_line = crlf
4 changes: 0 additions & 4 deletions sdk/identity/Azure.Identity/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Disables StyleCopAnalyzer, Remove this property to enable it -->
<PropertyGroup>
<EnableStyleCopAnalyzers>false</EnableStyleCopAnalyzers>
</PropertyGroup>
<!--
Add any shared properties you want for the projects under this package directory that need to be set before the auto imported Directory.Build.props
-->
Expand Down
10 changes: 5 additions & 5 deletions sdk/identity/Azure.Identity/src/AadIdentityClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Azure.Identity
{
internal class AadIdentityClient
{
private static Lazy<AadIdentityClient> s_sharedClient = new Lazy<AadIdentityClient>(() => new AadIdentityClient(null));
private static readonly Lazy<AadIdentityClient> s_sharedClient = new Lazy<AadIdentityClient>(() => new AadIdentityClient(null));

private readonly IdentityClientOptions _options;
private readonly HttpPipeline _pipeline;
Expand Down Expand Up @@ -142,11 +142,11 @@ public virtual AccessToken Authenticate(string tenantId, string clientId, X509Ce

private async Task<AccessToken> SendAuthRequestAsync(Request request, CancellationToken cancellationToken)
{
var response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);
Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);

if (response.Status == 200 || response.Status == 201)
{
var result = await DeserializeAsync(response.ContentStream, cancellationToken).ConfigureAwait(false);
AccessToken result = await DeserializeAsync(response.ContentStream, cancellationToken).ConfigureAwait(false);

return new Response<AccessToken>(response, result);
}
Expand All @@ -156,11 +156,11 @@ private async Task<AccessToken> SendAuthRequestAsync(Request request, Cancellati

private AccessToken SendAuthRequest(Request request, CancellationToken cancellationToken)
{
var response = _pipeline.SendRequest(request, cancellationToken);
Response response = _pipeline.SendRequest(request, cancellationToken);

if (response.Status == 200 || response.Status == 201)
{
var result = Deserialize(response.ContentStream);
AccessToken result = Deserialize(response.ContentStream);

return new Response<AccessToken>(response, result);
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/identity/Azure.Identity/src/Base64Url.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public static string HexToBase64Url(string hex)
return Base64Url.Encode(bytes);
}
}
}
}
18 changes: 12 additions & 6 deletions sdk/identity/Azure.Identity/src/ChainedTokenCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,30 @@
namespace Azure.Identity
{
/// <summary>
/// Provides a <see cref="TokenCredential"/> implementation which chains multiple <see cref="TokenCredential"/> implementations to be tried in order
/// Provides a <see cref="TokenCredential"/> implementation which chains multiple <see cref="TokenCredential"/> implementations to be tried in order
/// until one of the getToken methods returns a non-default <see cref="AccessToken"/>.
/// </summary>
public class ChainedTokenCredential : TokenCredential
{
private TokenCredential[] _sources;
private readonly TokenCredential[] _sources;

/// <summary>
/// Creates an instance with the specified <see cref="TokenCredential"/> sources.
/// </summary>
/// <param name="sources">The ordered chain of <see cref="TokenCredential"/> implementations to tried when calling <see cref="GetToken"/> or <see cref="GetTokenAsync"/></param>
public ChainedTokenCredential(params TokenCredential[] sources)
{
if (sources == null) throw new ArgumentNullException(nameof(sources));
if (sources == null)
{
throw new ArgumentNullException(nameof(sources));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we bracket these? I'm not a huge fan of having them on a new line without brackets.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule csharp_preserve_single_line_statements = false is responsible for that. I have fixed up the current instances but it will likely happen again when new single line statements are introduced.

}

if (sources.Length == 0) throw new ArgumentException("sources must not be empty", nameof(sources));
if (sources.Length == 0)
{
throw new ArgumentException("sources must not be empty", nameof(sources));
chidozieononiwu marked this conversation as resolved.
Show resolved Hide resolved
}

for(int i = 0; i < sources.Length; i++)
for (int i = 0; i < sources.Length; i++)
{
if (sources[i] == null)
{
Expand Down Expand Up @@ -66,7 +72,7 @@ public override async Task<AccessToken> GetTokenAsync(string[] scopes, Cancellat
{
AccessToken token = new AccessToken();

for(int i = 0; i < _sources.Length && token.Token == null; i++)
for (int i = 0; i < _sources.Length && token.Token == null; i++)
{
token = await _sources[i].GetTokenAsync(scopes, cancellationToken).ConfigureAwait(false);
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/identity/Azure.Identity/src/ClientSecretCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ClientSecretCredential(string tenantId, string clientId, string clientSec
/// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls.</returns>
public override async Task<AccessToken> GetTokenAsync(string[] scopes, CancellationToken cancellationToken = default)
{
return await this._client.AuthenticateAsync(TenantId, ClientId, ClientSecret, scopes, cancellationToken).ConfigureAwait(false);
return await _client.AuthenticateAsync(TenantId, ClientId, ClientSecret, scopes, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -78,7 +78,7 @@ public override async Task<AccessToken> GetTokenAsync(string[] scopes, Cancellat
/// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls.</returns>
public override AccessToken GetToken(string[] scopes, CancellationToken cancellationToken = default)
{
return this._client.Authenticate(TenantId, ClientId, ClientSecret, scopes, cancellationToken);
return _client.Authenticate(TenantId, ClientId, ClientSecret, scopes, cancellationToken);
}
}
}
12 changes: 6 additions & 6 deletions sdk/identity/Azure.Identity/src/DefaultAzureCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class DefaultAzureCredential : ChainedTokenCredential
{
// TODO: Currently this is piggybacking off the Azure CLI client ID, but needs to be switched once the Developer Sign On application is available
private const string DeveloperSignOnClientId = "04b07795-8ddb-461a-bbee-02f9e1bf7b46";
private static readonly string AzureUsername = Environment.GetEnvironmentVariable("AZURE_USERNAME");
private static readonly string AzurePassword = Environment.GetEnvironmentVariable("AZURE_PASSWORD");
private static TokenCredential[] s_defaultCredentialChain = GetDefaultAzureCredentialChain();
private static readonly string s_azureUsername = Environment.GetEnvironmentVariable("AZURE_USERNAME");
private static readonly string s_azurePassword = Environment.GetEnvironmentVariable("AZURE_PASSWORD");
private static readonly TokenCredential[] s_defaultCredentialChain = GetDefaultAzureCredentialChain();

/// <summary>
/// Creates an instance of the DefaultAzureCredential class.
Expand All @@ -35,14 +35,14 @@ public DefaultAzureCredential()

private static TokenCredential[] GetDefaultAzureCredentialChain()
{
// if only the username is specified via the enviornment (not password as well) and we're running on windows add the SharedTokenCacheCredential to the
// if only the username is specified via the enviornment (not password as well) and we're running on windows add the SharedTokenCacheCredential to the
// default credential to enable SSO
if (!string.IsNullOrEmpty(AzureUsername) && string.IsNullOrEmpty(AzurePassword) && (Environment.OSVersion.Platform == PlatformID.Win32NT))
if (!string.IsNullOrEmpty(s_azureUsername) && string.IsNullOrEmpty(s_azurePassword) && (Environment.OSVersion.Platform == PlatformID.Win32NT))
{
return new TokenCredential[] {
new EnvironmentCredential(),
new ManagedIdentityCredential(),
new SharedTokenCacheCredential(DeveloperSignOnClientId, AzureUsername),
new SharedTokenCacheCredential(DeveloperSignOnClientId, s_azureUsername),
new CredentialNotFoundGuard()
};
}
Expand Down
14 changes: 7 additions & 7 deletions sdk/identity/Azure.Identity/src/DeviceCodeCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
namespace Azure.Identity
{
/// <summary>
/// A <see cref="TokenCredential"/> implementation which authenticates a user using the device code flow, and provides access tokens for that user account.
/// A <see cref="TokenCredential"/> implementation which authenticates a user using the device code flow, and provides access tokens for that user account.
/// For more information on the device code authentication flow see https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Device-Code-Flow.
/// </summary>
public class DeviceCodeCredential : TokenCredential
{
private IPublicClientApplication _pubApp = null;
private HttpPipeline _pipeline = null;
private readonly IPublicClientApplication _pubApp = null;
private readonly HttpPipeline _pipeline = null;
private IAccount _account = null;
private IdentityClientOptions _options;
private string _clientId;
private Func<DeviceCodeInfo, CancellationToken, Task> _deviceCodeCallback;
private readonly IdentityClientOptions _options;
private readonly string _clientId;
private readonly Func<DeviceCodeInfo, CancellationToken, Task> _deviceCodeCallback;

/// <summary>
/// Protected constructor for mocking
Expand All @@ -43,7 +43,7 @@ protected DeviceCodeCredential()
public DeviceCodeCredential(string clientId, Func<DeviceCodeInfo, CancellationToken, Task> deviceCodeCallback)
: this(clientId, deviceCodeCallback, null)
{

}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion sdk/identity/Azure.Identity/src/DeviceCodeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal DeviceCodeInfo(DeviceCodeResult deviceCode)
public string DeviceCode { get; private set; }

#pragma warning disable CA1056 // Uri properties should not be strings

/// <summary>
/// Verification URL where the user must navigate to authenticate using the device code and credentials.
/// </summary>
Expand Down
12 changes: 6 additions & 6 deletions sdk/identity/Azure.Identity/src/EnvironmentCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace Azure.Identity
/// </summary>
public class EnvironmentCredential : TokenCredential
{
private TokenCredential _credential = null;
private readonly TokenCredential _credential = null;

/// <summary>
/// Creates an instance of the EnvironmentCredential class and reads client secret details from environment variables.
/// Creates an instance of the EnvironmentCredential class and reads client secret details from environment variables.
/// If the expected environment variables are not found at this time, the GetToken method will return the default <see cref="AccessToken"/> when invoked.
/// </summary>
public EnvironmentCredential()
Expand All @@ -32,7 +32,7 @@ public EnvironmentCredential()
}

/// <summary>
/// Creates an instance of the EnvironmentCredential class and reads client secret details from environment variables.
/// Creates an instance of the EnvironmentCredential class and reads client secret details from environment variables.
/// If the expected environment variables are not found at this time, the GetToken method will return the default <see cref="AccessToken"/> when invoked.
/// </summary>
/// <param name="options">Options that allow to configure the management of the requests sent to the Azure Active Directory service.</param>
Expand All @@ -49,7 +49,7 @@ public EnvironmentCredential(IdentityClientOptions options)
}

/// <summary>
/// Obtains a token from the Azure Active Directory service, using the specified client details specified in the environment variables
/// Obtains a token from the Azure Active Directory service, using the specified client details specified in the environment variables
/// AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET to authenticate.
/// </summary>
/// <remarks>
Expand All @@ -64,7 +64,7 @@ public override AccessToken GetToken(string[] scopes, CancellationToken cancella
}

/// <summary>
/// Obtains a token from the Azure Active Directory service, using the specified client details specified in the environment variables
/// Obtains a token from the Azure Active Directory service, using the specified client details specified in the environment variables
/// AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET to authenticate.
/// </summary>
/// <remarks>
Expand All @@ -73,7 +73,7 @@ public override AccessToken GetToken(string[] scopes, CancellationToken cancella
/// <param name="scopes">The list of scopes for which the token will have access.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
/// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls, or a default <see cref="AccessToken"/>.</returns>
public async override Task<AccessToken> GetTokenAsync(string[] scopes, CancellationToken cancellationToken = default)
public override async Task<AccessToken> GetTokenAsync(string[] scopes, CancellationToken cancellationToken = default)
{
return (_credential != null) ? await _credential.GetTokenAsync(scopes, cancellationToken).ConfigureAwait(false) : default;
}
Expand Down
13 changes: 7 additions & 6 deletions sdk/identity/Azure.Identity/src/HttpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static async Task<Request> ToPipelineRequestAsync(this HttpRequestMessage

pipelineRequest.Content = await request.Content.ToPipelineRequestContentAsync().ConfigureAwait(false);

foreach (var header in request.Headers)
foreach (System.Collections.Generic.KeyValuePair<string, System.Collections.Generic.IEnumerable<string>> header in request.Headers)
{
foreach (var value in header.Value)
{
Expand All @@ -47,13 +47,14 @@ private static void AddHeader(HttpResponseMessage request, HttpHeader header)

public static HttpResponseMessage ToHttpResponseMessage(this Response response)
{
HttpResponseMessage responseMessage = new HttpResponseMessage();

responseMessage.StatusCode = (HttpStatusCode)response.Status;
HttpResponseMessage responseMessage = new HttpResponseMessage
{
StatusCode = (HttpStatusCode)response.Status,

responseMessage.Content = new StreamContent(response.ContentStream);
Content = new StreamContent(response.ContentStream)
};

foreach (var header in response.Headers)
foreach (HttpHeader header in response.Headers)
{
if (!responseMessage.Headers.TryAddWithoutValidation(header.Name, header.Value))
{
Expand Down
Loading