Skip to content

Commit 956d0ca

Browse files
committed
Merge pull request Azure#1809 from markcowl/refactor
Moving Common.Authentication into the repo
2 parents 141a19b + 59d5f45 commit 956d0ca

File tree

840 files changed

+23754
-8997
lines changed

Some content is hidden

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

840 files changed

+23754
-8997
lines changed

setup/azurecmdfiles.wxi

Lines changed: 6616 additions & 6584 deletions
Large diffs are not rendered by default.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.Azure.Commands.Common.Authentication
18+
{
19+
/// <summary>
20+
/// Base class representing an exception that occurs when
21+
/// authenticating against Azure Active Directory
22+
/// </summary>
23+
[Serializable]
24+
public abstract class AadAuthenticationException : Exception
25+
{
26+
protected AadAuthenticationException()
27+
{
28+
}
29+
30+
protected AadAuthenticationException(string message) : base(message)
31+
{
32+
}
33+
34+
protected AadAuthenticationException(string message, Exception innerException) : base(message, innerException)
35+
{
36+
}
37+
}
38+
39+
/// <summary>
40+
/// Exception that gets thrown when the user explicitly
41+
/// cancels an authentication operation.
42+
/// </summary>
43+
[Serializable]
44+
public class AadAuthenticationCanceledException : AadAuthenticationException
45+
{
46+
public AadAuthenticationCanceledException(string message, Exception innerException) : base(message, innerException)
47+
{
48+
}
49+
}
50+
51+
/// <summary>
52+
/// Exception that gets thrown when the ADAL library
53+
/// is unable to authenticate without a popup dialog.
54+
/// </summary>
55+
[Serializable]
56+
public class AadAuthenticationFailedWithoutPopupException : AadAuthenticationException
57+
{
58+
public AadAuthenticationFailedWithoutPopupException(string message, Exception innerException)
59+
: base(message, innerException)
60+
{
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Exception that gets thrown if an authentication operation
66+
/// fails on the server.
67+
/// </summary>
68+
[Serializable]
69+
public class AadAuthenticationFailedException : AadAuthenticationException
70+
{
71+
public AadAuthenticationFailedException(string message, Exception innerException) : base(message, innerException)
72+
{
73+
}
74+
}
75+
76+
/// <summary>
77+
/// Exception thrown if a refresh token has expired.
78+
/// </summary>
79+
[Serializable]
80+
public class AadAuthenticationCantRenewException : AadAuthenticationException
81+
{
82+
public AadAuthenticationCantRenewException()
83+
{
84+
}
85+
86+
public AadAuthenticationCantRenewException(string message) : base(message)
87+
{
88+
}
89+
90+
public AadAuthenticationCantRenewException(string message, Exception innerException) : base(message, innerException)
91+
{
92+
}
93+
}
94+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Net.Http;
17+
using System.Net.Http.Headers;
18+
using System.Threading;
19+
using System.Threading.Tasks;
20+
21+
namespace Microsoft.Azure.Commands.Common.Authentication
22+
{
23+
public class AccessTokenCredential : SubscriptionCloudCredentials
24+
{
25+
private readonly Guid subscriptionId;
26+
private readonly IAccessToken token;
27+
28+
public AccessTokenCredential(Guid subscriptionId, IAccessToken token)
29+
{
30+
this.subscriptionId = subscriptionId;
31+
this.token = token;
32+
this.TenantID = token.TenantId;
33+
}
34+
35+
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
36+
{
37+
token.AuthorizeRequest((tokenType, tokenValue) => {
38+
request.Headers.Authorization = new AuthenticationHeaderValue(tokenType, tokenValue);
39+
});
40+
return base.ProcessHttpRequestAsync(request, cancellationToken);
41+
}
42+
43+
public override string SubscriptionId
44+
{
45+
get { return subscriptionId.ToString(); }
46+
}
47+
48+
public string TenantID { get; set; }
49+
}
50+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
16+
using System;
17+
18+
namespace Microsoft.Azure.Commands.Common.Authentication
19+
{
20+
/// <summary>
21+
/// Class storing the configuration information needed
22+
/// for ADAL to request token from the right AD tenant
23+
/// depending on environment.
24+
/// </summary>
25+
public class AdalConfiguration
26+
{
27+
//
28+
// These constants define the default values to use for AD authentication
29+
// against RDFE
30+
//
31+
public const string PowerShellClientId = "1950a258-227b-4e31-a9cf-717495945fc2";
32+
33+
public static readonly Uri PowerShellRedirectUri = new Uri("urn:ietf:wg:oauth:2.0:oob");
34+
35+
// ID for site to pass to enable EBD (email-based differentiation)
36+
// This gets passed in the call to get the azure branding on the
37+
// login window. Also adding popup flag to handle overly large login windows.
38+
public const string EnableEbdMagicCookie = "site_id=501358&display=popup";
39+
40+
public string AdEndpoint { get;set; }
41+
42+
public bool ValidateAuthority { get; set; }
43+
44+
public string AdDomain { get; set; }
45+
46+
public string ClientId { get; set; }
47+
48+
public Uri ClientRedirectUri { get; set; }
49+
50+
public string ResourceClientUri { get; set; }
51+
52+
public TokenCache TokenCache { get; set; }
53+
54+
public AdalConfiguration()
55+
{
56+
ClientId = PowerShellClientId;
57+
ClientRedirectUri = PowerShellRedirectUri;
58+
ValidateAuthority = true;
59+
AdEndpoint = string.Empty;
60+
ResourceClientUri = "https://management.core.windows.net/";
61+
}
62+
}
63+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Common.Authentication.Models;
16+
using Microsoft.Azure.Commands.Common.Authentication.Properties;
17+
using System;
18+
using System.Security;
19+
using System.Windows.Forms;
20+
21+
namespace Microsoft.Azure.Commands.Common.Authentication
22+
{
23+
/// <summary>
24+
/// A token provider that uses ADAL to retrieve
25+
/// tokens from Azure Active Directory
26+
/// </summary>
27+
public class AdalTokenProvider : ITokenProvider
28+
{
29+
private readonly ITokenProvider userTokenProvider;
30+
private readonly ITokenProvider servicePrincipalTokenProvider;
31+
32+
public AdalTokenProvider()
33+
: this(new ConsoleParentWindow())
34+
{
35+
}
36+
37+
public AdalTokenProvider(IWin32Window parentWindow)
38+
{
39+
this.userTokenProvider = new UserTokenProvider(parentWindow);
40+
servicePrincipalTokenProvider = new ServicePrincipalTokenProvider();
41+
}
42+
43+
public IAccessToken GetAccessToken(AdalConfiguration config, ShowDialog promptBehavior, string userId, SecureString password,
44+
AzureAccount.AccountType credentialType)
45+
{
46+
switch (credentialType)
47+
{
48+
case AzureAccount.AccountType.User:
49+
return userTokenProvider.GetAccessToken(config, promptBehavior, userId, password, credentialType);
50+
case AzureAccount.AccountType.ServicePrincipal:
51+
return servicePrincipalTokenProvider.GetAccessToken(config, promptBehavior, userId, password, credentialType);
52+
default:
53+
throw new ArgumentException(Resources.UnknownCredentialType, "credentialType");
54+
}
55+
}
56+
57+
public IAccessToken GetAccessTokenWithCertificate(AdalConfiguration config, string clientId, string certificate, AzureAccount.AccountType credentialType)
58+
{
59+
switch (credentialType)
60+
{
61+
case AzureAccount.AccountType.ServicePrincipal:
62+
return servicePrincipalTokenProvider.GetAccessTokenWithCertificate(config, clientId, certificate, credentialType);
63+
default:
64+
throw new ArgumentException(string.Format(Resources.UnsupportedCredentialType, credentialType), "credentialType");
65+
}
66+
}
67+
}
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
16+
using Microsoft.Rest.Azure.Authentication;
17+
using System.Security;
18+
using System.Security.Cryptography.X509Certificates;
19+
using System.Threading.Tasks;
20+
21+
namespace Microsoft.Azure.Commands.Common.Authentication
22+
{
23+
/// <summary>
24+
/// Interface to the certificate store for authentication
25+
/// </summary>
26+
internal sealed class CertificateApplicationCredentialProvider : IApplicationAuthenticationProvider
27+
{
28+
private string _certificateThumbprint;
29+
30+
/// <summary>
31+
/// Create a certificate provider
32+
/// </summary>
33+
/// <param name="certificateThumbprint"></param>
34+
public CertificateApplicationCredentialProvider(string certificateThumbprint)
35+
{
36+
this._certificateThumbprint = certificateThumbprint;
37+
}
38+
39+
/// <summary>
40+
/// Authenticate using certificate thumbprint from the datastore
41+
/// </summary>
42+
/// <param name="clientId">The active directory client id for the application.</param>
43+
/// <param name="audience">The intended audience for authentication</param>
44+
/// <param name="context">The AD AuthenticationContext to use</param>
45+
/// <returns></returns>
46+
public async Task<AuthenticationResult> AuthenticateAsync(string clientId, string audience, AuthenticationContext context)
47+
{
48+
var task = new Task<X509Certificate2>(() =>
49+
{
50+
return AzureSession.DataStore.GetCertificate(this._certificateThumbprint);
51+
});
52+
task.Start();
53+
var certificate = await task.ConfigureAwait(false);
54+
55+
return await context.AcquireTokenAsync(
56+
audience,
57+
new ClientAssertionCertificate(clientId, certificate));
58+
}
59+
}
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Runtime.InteropServices;
17+
using System.Windows.Forms;
18+
19+
namespace Microsoft.Azure.Commands.Common.Authentication
20+
{
21+
/// <summary>
22+
/// An implementation of <see cref="IWin32Window"/> that gives the
23+
/// windows handle for the current console window.
24+
/// </summary>
25+
public class ConsoleParentWindow : IWin32Window
26+
{
27+
public IntPtr Handle { get { return NativeMethods.GetConsoleWindow(); } }
28+
29+
static class NativeMethods
30+
{
31+
[DllImport("kernel32.dll")]
32+
public static extern IntPtr GetConsoleWindow();
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)