Skip to content

Commit b8d3a16

Browse files
author
Hovsep
committed
Merge pull request Azure#2239 from hovsepm/dev
Code clean up
2 parents d83c3bb + 6aa1240 commit b8d3a16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+332
-232
lines changed

src/Common/Commands.Common.Authentication/Authentication/AccessTokenCredential.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public AccessTokenCredential(Guid subscriptionId, IAccessToken token)
3131
this.token = token;
3232
this.TenantID = token.TenantId;
3333
}
34-
34+
3535
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
3636
{
37-
token.AuthorizeRequest((tokenType, tokenValue) => {
37+
token.AuthorizeRequest((tokenType, tokenValue) =>
38+
{
3839
request.Headers.Authorization = new AuthenticationHeaderValue(tokenType, tokenValue);
3940
});
4041
return base.ProcessHttpRequestAsync(request, cancellationToken);

src/Common/Commands.Common.Authentication/Authentication/AdalConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class AdalConfiguration
2828
// These constants define the default values to use for AD authentication
2929
// against RDFE
3030
//
31-
public const string PowerShellClientId = "1950a258-227b-4e31-a9cf-717495945fc2";
31+
public const string PowerShellClientId = "1950a258-227b-4e31-a9cf-717495945fc2";
3232

3333
public static readonly Uri PowerShellRedirectUri = new Uri("urn:ietf:wg:oauth:2.0:oob");
3434

@@ -37,7 +37,7 @@ public class AdalConfiguration
3737
// login window. Also adding popup flag to handle overly large login windows.
3838
public const string EnableEbdMagicCookie = "site_id=501358&display=popup";
3939

40-
public string AdEndpoint { get;set; }
40+
public string AdEndpoint { get; set; }
4141

4242
public bool ValidateAuthority { get; set; }
4343

src/Common/Commands.Common.Authentication/Authentication/AdalTokenProvider.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ public AdalTokenProvider()
3737
public AdalTokenProvider(IWin32Window parentWindow)
3838
{
3939
this.userTokenProvider = new UserTokenProvider(parentWindow);
40-
servicePrincipalTokenProvider = new ServicePrincipalTokenProvider();
40+
this.servicePrincipalTokenProvider = new ServicePrincipalTokenProvider();
4141
}
4242

43-
public IAccessToken GetAccessToken(AdalConfiguration config, ShowDialog promptBehavior, string userId, SecureString password,
43+
public IAccessToken GetAccessToken(
44+
AdalConfiguration config,
45+
ShowDialog promptBehavior,
46+
string userId,
47+
SecureString password,
4448
AzureAccount.AccountType credentialType)
4549
{
4650
switch (credentialType)
@@ -54,7 +58,11 @@ public IAccessToken GetAccessToken(AdalConfiguration config, ShowDialog promptBe
5458
}
5559
}
5660

57-
public IAccessToken GetAccessTokenWithCertificate(AdalConfiguration config, string clientId, string certificate, AzureAccount.AccountType credentialType)
61+
public IAccessToken GetAccessTokenWithCertificate(
62+
AdalConfiguration config,
63+
string clientId,
64+
string certificate,
65+
AzureAccount.AccountType credentialType)
5866
{
5967
switch (credentialType)
6068
{

src/Common/Commands.Common.Authentication/Authentication/CertificateApplicationCredentialProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public CertificateApplicationCredentialProvider(string certificateThumbprint)
3535
{
3636
this._certificateThumbprint = certificateThumbprint;
3737
}
38-
38+
3939
/// <summary>
4040
/// Authenticate using certificate thumbprint from the datastore
4141
/// </summary>
@@ -47,7 +47,7 @@ public async Task<AuthenticationResult> AuthenticateAsync(string clientId, strin
4747
{
4848
var task = new Task<X509Certificate2>(() =>
4949
{
50-
return AzureSession.DataStore.GetCertificate(this._certificateThumbprint);
50+
return AzureSession.DataStore.GetCertificate(this._certificateThumbprint);
5151
});
5252
task.Start();
5353
var certificate = await task.ConfigureAwait(false);

src/Common/Commands.Common.Authentication/Authentication/CredStore.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal static class CredStore
2626
internal enum CredentialType
2727
{
2828
Generic = 1,
29-
}
29+
}
3030

3131
internal static class NativeMethods
3232
{
@@ -69,7 +69,10 @@ internal extern static bool CredFree(
6969
IntPtr pCredential
7070
);
7171

72-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1049:TypesThatOwnNativeResourcesShouldBeDisposable", Justification = "Wrapper for native struct")]
72+
[System.Diagnostics.CodeAnalysis.SuppressMessage(
73+
"Microsoft.Design",
74+
"CA1049:TypesThatOwnNativeResourcesShouldBeDisposable",
75+
Justification = "Wrapper for native struct")]
7376
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
7477
internal struct Credential
7578
{
@@ -108,7 +111,7 @@ public Credential(string userName, string key, string value)
108111
internal IntPtr attributes;
109112
internal string targetAlias;
110113
internal string userName;
111-
}
114+
}
112115
}
113116
}
114117
}

src/Common/Commands.Common.Authentication/Authentication/IAccessToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface IAccessToken
2525
string UserId { get; }
2626

2727
string TenantId { get; }
28-
28+
2929
LoginType LoginType { get; }
3030
}
3131
}

src/Common/Commands.Common.Authentication/Authentication/ITokenProvider.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ public interface ITokenProvider
3333
/// <param name="password">Secure strings with password/service principal key.</param>
3434
/// <param name="credentialType">Credential type.</param>
3535
/// <returns>An access token.</returns>
36-
IAccessToken GetAccessToken(AdalConfiguration config, ShowDialog promptBehavior, string userId,
37-
SecureString password, AzureAccount.AccountType credentialType);
36+
IAccessToken GetAccessToken(
37+
AdalConfiguration config,
38+
ShowDialog promptBehavior,
39+
string userId,
40+
SecureString password,
41+
AzureAccount.AccountType credentialType);
3842

3943
/// <summary>
4044
/// Get a new authentication token for the given environment
@@ -44,7 +48,10 @@ IAccessToken GetAccessToken(AdalConfiguration config, ShowDialog promptBehavior,
4448
/// <param name="certificateThumbprint">The certificate thumbprint for this user</param>
4549
/// <param name="credentialType">The account type</param>
4650
/// <returns>An access token, which can be renewed</returns>
47-
IAccessToken GetAccessTokenWithCertificate(AdalConfiguration config, string principalId, string certificateThumbprint,
51+
IAccessToken GetAccessTokenWithCertificate(
52+
AdalConfiguration config,
53+
string principalId,
54+
string certificateThumbprint,
4855
AzureAccount.AccountType credentialType);
4956
}
5057
}

src/Common/Commands.Common.Authentication/Authentication/KeyStoreApplicationCredentialProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public KeyStoreApplicationCredentialProvider(string tenant)
3434
{
3535
this._tenantId = tenant;
3636
}
37-
37+
3838
/// <summary>
3939
/// Authenticate using the secret for the specified client from the key store
4040
/// </summary>

src/Common/Commands.Common.Authentication/Authentication/LoginType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public enum LoginType
1919
/// <summary>
2020
/// User is logging in with orgid credentials
2121
/// </summary>
22-
OrgId,
22+
OrgId,
2323

2424
/// <summary>
2525
/// User is logging in with liveid credentials

src/Common/Commands.Common.Authentication/Authentication/ServicePrincipalKeyStore.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void SaveKey(string appId, string tenantId, SecureString serviceKe
3737
targetName = CreateKey(appId, tenantId),
3838
targetAlias = null,
3939
comment = null,
40-
lastWritten = new FILETIME {dwHighDateTime = 0, dwLowDateTime = 0},
40+
lastWritten = new FILETIME { dwHighDateTime = 0, dwLowDateTime = 0 },
4141
persist = 2, // persist on local machine
4242
attibuteCount = 0,
4343
attributes = IntPtr.Zero,
@@ -79,16 +79,16 @@ public static SecureString GetKey(string appId, string tenantId)
7979
out pCredential))
8080
{
8181
var credential = (CredStore.NativeMethods.Credential)
82-
Marshal.PtrToStructure(pCredential, typeof (CredStore.NativeMethods.Credential));
82+
Marshal.PtrToStructure(pCredential, typeof(CredStore.NativeMethods.Credential));
8383
unsafe
8484
{
85-
return new SecureString((char*) (credential.credentialBlob),
86-
(int)(credential.credentialBlobSize/Marshal.SystemDefaultCharSize));
85+
return new SecureString((char*)(credential.credentialBlob),
86+
(int)(credential.credentialBlobSize / Marshal.SystemDefaultCharSize));
8787
}
8888
}
8989
return null;
9090
}
91-
catch
91+
catch
9292
{
9393
// we could be running in an environment that does not have credentials store
9494
}
@@ -97,7 +97,7 @@ public static SecureString GetKey(string appId, string tenantId)
9797
if (pCredential != IntPtr.Zero)
9898
{
9999
CredStore.NativeMethods.CredFree(pCredential);
100-
}
100+
}
101101
}
102102

103103
return null;

src/Common/Commands.Common.Authentication/Authentication/ServicePrincipalTokenProvider.cs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ internal class ServicePrincipalTokenProvider : ITokenProvider
2626
{
2727
private static readonly TimeSpan expirationThreshold = TimeSpan.FromMinutes(5);
2828

29-
public IAccessToken GetAccessToken(AdalConfiguration config, ShowDialog promptBehavior, string userId, SecureString password,
29+
public IAccessToken GetAccessToken(
30+
AdalConfiguration config,
31+
ShowDialog promptBehavior,
32+
string userId,
33+
SecureString password,
3034
AzureAccount.AccountType credentialType)
3135
{
3236
if (credentialType == AzureAccount.AccountType.User)
@@ -36,13 +40,19 @@ public IAccessToken GetAccessToken(AdalConfiguration config, ShowDialog promptBe
3640
return new ServicePrincipalAccessToken(config, AcquireTokenWithSecret(config, userId, password), this.RenewWithSecret, userId);
3741
}
3842

39-
public IAccessToken GetAccessTokenWithCertificate(AdalConfiguration config, string clientId, string certificateThumbprint, AzureAccount.AccountType credentialType)
43+
public IAccessToken GetAccessTokenWithCertificate(
44+
AdalConfiguration config,
45+
string clientId,
46+
string certificateThumbprint,
47+
AzureAccount.AccountType credentialType)
4048
{
4149
if (credentialType == AzureAccount.AccountType.User)
4250
{
4351
throw new ArgumentException(string.Format(Resources.InvalidCredentialType, "User"), "credentialType");
4452
}
45-
return new ServicePrincipalAccessToken(config, AcquireTokenWithCertificate(config, clientId, certificateThumbprint),
53+
return new ServicePrincipalAccessToken(
54+
config,
55+
AcquireTokenWithCertificate(config, clientId, certificateThumbprint),
4656
(adalConfig, appId) => this.RenewWithCertificate(adalConfig, appId, certificateThumbprint), clientId);
4757
}
4858

@@ -51,7 +61,7 @@ private AuthenticationContext GetContext(AdalConfiguration config)
5161
string authority = config.AdEndpoint + config.AdDomain;
5262
return new AuthenticationContext(authority, config.ValidateAuthority, config.TokenCache);
5363
}
54-
64+
5565
private AuthenticationResult AcquireTokenWithSecret(AdalConfiguration config, string appId, SecureString appKey)
5666
{
5767
if (appKey == null)
@@ -65,7 +75,9 @@ private AuthenticationResult AcquireTokenWithSecret(AdalConfiguration config, st
6575
return context.AcquireToken(config.ResourceClientUri, credential);
6676
}
6777

68-
private AuthenticationResult AcquireTokenWithCertificate(AdalConfiguration config, string appId,
78+
private AuthenticationResult AcquireTokenWithCertificate(
79+
AdalConfiguration config,
80+
string appId,
6981
string thumbprint)
7082
{
7183
var certificate = AzureSession.DataStore.GetCertificate(thumbprint);
@@ -80,9 +92,9 @@ private AuthenticationResult AcquireTokenWithCertificate(AdalConfiguration confi
8092

8193
private AuthenticationResult RenewWithSecret(AdalConfiguration config, string appId)
8294
{
83-
TracingAdapter.Information(Resources.SPNRenewTokenTrace, appId, config.AdDomain, config.AdEndpoint,
95+
TracingAdapter.Information(Resources.SPNRenewTokenTrace, appId, config.AdDomain, config.AdEndpoint,
8496
config.ClientId, config.ClientRedirectUri);
85-
using (SecureString appKey = LoadAppKey(appId, config.AdDomain))
97+
using (SecureString appKey = LoadAppKey(appId, config.AdDomain))
8698
{
8799
if (appKey == null)
88100
{
@@ -92,11 +104,18 @@ private AuthenticationResult RenewWithSecret(AdalConfiguration config, string ap
92104
}
93105
}
94106

95-
private AuthenticationResult RenewWithCertificate(AdalConfiguration config, string appId,
107+
private AuthenticationResult RenewWithCertificate(
108+
AdalConfiguration config,
109+
string appId,
96110
string thumbprint)
97111
{
98-
TracingAdapter.Information(Resources.SPNRenewTokenTrace, appId, config.AdDomain, config.AdEndpoint,
99-
config.ClientId, config.ClientRedirectUri);
112+
TracingAdapter.Information(
113+
Resources.SPNRenewTokenTrace,
114+
appId,
115+
config.AdDomain,
116+
config.AdEndpoint,
117+
config.ClientId,
118+
config.ClientRedirectUri);
100119
return AcquireTokenWithCertificate(config, appId, thumbprint);
101120
}
102121

@@ -110,15 +129,18 @@ private void StoreAppKey(string appId, string tenantId, SecureString appKey)
110129
ServicePrincipalKeyStore.SaveKey(appId, tenantId, appKey);
111130
}
112131

113-
114132
private class ServicePrincipalAccessToken : IAccessToken
115133
{
116134
internal readonly AdalConfiguration Configuration;
117135
internal AuthenticationResult AuthResult;
118136
private readonly Func<AdalConfiguration, string, AuthenticationResult> tokenRenewer;
119137
private readonly string appId;
120138

121-
public ServicePrincipalAccessToken(AdalConfiguration configuration, AuthenticationResult authResult, Func<AdalConfiguration, string, AuthenticationResult> tokenRenewer, string appId)
139+
public ServicePrincipalAccessToken(
140+
AdalConfiguration configuration,
141+
AuthenticationResult authResult,
142+
Func<AdalConfiguration, string, AuthenticationResult> tokenRenewer,
143+
string appId)
122144
{
123145
Configuration = configuration;
124146
AuthResult = authResult;
@@ -137,8 +159,11 @@ public void AuthorizeRequest(Action<string, string> authTokenSetter)
137159
}
138160

139161
public string UserId { get { return appId; } }
162+
140163
public string AccessToken { get { return AuthResult.AccessToken; } }
164+
141165
public LoginType LoginType { get { return LoginType.OrgId; } }
166+
142167
public string TenantId { get { return this.Configuration.AdDomain; } }
143168

144169
private bool IsExpired
@@ -155,8 +180,12 @@ private bool IsExpired
155180
var expiration = AuthResult.ExpiresOn;
156181
var currentTime = DateTimeOffset.UtcNow;
157182
var timeUntilExpiration = expiration - currentTime;
158-
TracingAdapter.Information(Resources.SPNTokenExpirationCheckTrace, expiration, currentTime,
159-
expirationThreshold, timeUntilExpiration);
183+
TracingAdapter.Information(
184+
Resources.SPNTokenExpirationCheckTrace,
185+
expiration,
186+
currentTime,
187+
expirationThreshold,
188+
timeUntilExpiration);
160189
return timeUntilExpiration < expirationThreshold;
161190
}
162191
}

0 commit comments

Comments
 (0)