Skip to content

Moving Common.Authentication into the repo #1809

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

Merged
merged 23 commits into from
Feb 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
04e55cc
Removing authentication classes from common and refactoring SM classes
markcowl Feb 9, 2016
c9163f3
Changes to storage cmdlets profile usage
markcowl Feb 9, 2016
9b25d78
Fixing default profile for SM storage cmdlets
markcowl Feb 9, 2016
6aeea72
Moving common.authenticatio9n library into PowerShell
markcowl Feb 12, 2016
24c3e05
Update namespaces and usings for new common.authentication type locat…
markcowl Feb 12, 2016
ac41f7e
Merging with upstream
markcowl Feb 12, 2016
4a95983
Adding common.authentication project to individual solution files for…
markcowl Feb 12, 2016
89c07d9
Adding necessary settings to new project file
markcowl Feb 12, 2016
aaf437a
Common.Authentication changes for cmdlets not in the refactoring solu…
markcowl Feb 12, 2016
6175950
Updating wix file for changes
markcowl Feb 12, 2016
3379cf9
Remvoing config from logicapp
markcowl Feb 12, 2016
f7f553b
Merge branch 'dev' of github.com:azure/azure-powershell into refactor
markcowl Feb 12, 2016
9bf0a3f
removing bcl build warning from new assembly
markcowl Feb 12, 2016
b4d8ab6
Removing app.config so conveniently added by nuget
markcowl Feb 12, 2016
3b9ede7
Updating test framework for new clients
markcowl Feb 13, 2016
0d40180
Remove test with unimplemented functionality and update test framewor…
markcowl Feb 13, 2016
1cc3f5a
Fixing tests
markcowl Feb 13, 2016
f10e5b6
Fix Issue [#113662413] with Add-AzureRmAccount -Environment and add c…
markcowl Feb 17, 2016
61f9fe0
Update LogicApp client to use new runtime version
markcowl Feb 17, 2016
44df9d5
Remove unnecessary overrides for DatCmdlet
markcowl Feb 17, 2016
aaf889b
Merging with upstream changes
markcowl Feb 17, 2016
b9d10ce
Fixing bad merge
markcowl Feb 17, 2016
59d5f45
Adjusting to new project added to build
markcowl Feb 17, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13,200 changes: 6,616 additions & 6,584 deletions setup/azurecmdfiles.wxi

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;

namespace Microsoft.Azure.Commands.Common.Authentication
{
/// <summary>
/// Base class representing an exception that occurs when
/// authenticating against Azure Active Directory
/// </summary>
[Serializable]
public abstract class AadAuthenticationException : Exception
{
protected AadAuthenticationException()
{
}

protected AadAuthenticationException(string message) : base(message)
{
}

protected AadAuthenticationException(string message, Exception innerException) : base(message, innerException)
{
}
}

/// <summary>
/// Exception that gets thrown when the user explicitly
/// cancels an authentication operation.
/// </summary>
[Serializable]
public class AadAuthenticationCanceledException : AadAuthenticationException
{
public AadAuthenticationCanceledException(string message, Exception innerException) : base(message, innerException)
{
}
}

/// <summary>
/// Exception that gets thrown when the ADAL library
/// is unable to authenticate without a popup dialog.
/// </summary>
[Serializable]
public class AadAuthenticationFailedWithoutPopupException : AadAuthenticationException
{
public AadAuthenticationFailedWithoutPopupException(string message, Exception innerException)
: base(message, innerException)
{
}
}

/// <summary>
/// Exception that gets thrown if an authentication operation
/// fails on the server.
/// </summary>
[Serializable]
public class AadAuthenticationFailedException : AadAuthenticationException
{
public AadAuthenticationFailedException(string message, Exception innerException) : base(message, innerException)
{
}
}

/// <summary>
/// Exception thrown if a refresh token has expired.
/// </summary>
[Serializable]
public class AadAuthenticationCantRenewException : AadAuthenticationException
{
public AadAuthenticationCantRenewException()
{
}

public AadAuthenticationCantRenewException(string message) : base(message)
{
}

public AadAuthenticationCantRenewException(string message, Exception innerException) : base(message, innerException)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Authentication
{
public class AccessTokenCredential : SubscriptionCloudCredentials
{
private readonly Guid subscriptionId;
private readonly IAccessToken token;

public AccessTokenCredential(Guid subscriptionId, IAccessToken token)
{
this.subscriptionId = subscriptionId;
this.token = token;
this.TenantID = token.TenantId;
}

public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
token.AuthorizeRequest((tokenType, tokenValue) => {
request.Headers.Authorization = new AuthenticationHeaderValue(tokenType, tokenValue);
});
return base.ProcessHttpRequestAsync(request, cancellationToken);
}

public override string SubscriptionId
{
get { return subscriptionId.ToString(); }
}

public string TenantID { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;

namespace Microsoft.Azure.Commands.Common.Authentication
{
/// <summary>
/// Class storing the configuration information needed
/// for ADAL to request token from the right AD tenant
/// depending on environment.
/// </summary>
public class AdalConfiguration
{
//
// These constants define the default values to use for AD authentication
// against RDFE
//
public const string PowerShellClientId = "1950a258-227b-4e31-a9cf-717495945fc2";

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

// ID for site to pass to enable EBD (email-based differentiation)
// This gets passed in the call to get the azure branding on the
// login window. Also adding popup flag to handle overly large login windows.
public const string EnableEbdMagicCookie = "site_id=501358&display=popup";

public string AdEndpoint { get;set; }

public bool ValidateAuthority { get; set; }

public string AdDomain { get; set; }

public string ClientId { get; set; }

public Uri ClientRedirectUri { get; set; }

public string ResourceClientUri { get; set; }

public TokenCache TokenCache { get; set; }

public AdalConfiguration()
{
ClientId = PowerShellClientId;
ClientRedirectUri = PowerShellRedirectUri;
ValidateAuthority = true;
AdEndpoint = string.Empty;
ResourceClientUri = "https://management.core.windows.net/";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Commands.Common.Authentication.Properties;
using System;
using System.Security;
using System.Windows.Forms;

namespace Microsoft.Azure.Commands.Common.Authentication
{
/// <summary>
/// A token provider that uses ADAL to retrieve
/// tokens from Azure Active Directory
/// </summary>
public class AdalTokenProvider : ITokenProvider
{
private readonly ITokenProvider userTokenProvider;
private readonly ITokenProvider servicePrincipalTokenProvider;

public AdalTokenProvider()
: this(new ConsoleParentWindow())
{
}

public AdalTokenProvider(IWin32Window parentWindow)
{
this.userTokenProvider = new UserTokenProvider(parentWindow);
servicePrincipalTokenProvider = new ServicePrincipalTokenProvider();
}

public IAccessToken GetAccessToken(AdalConfiguration config, ShowDialog promptBehavior, string userId, SecureString password,
AzureAccount.AccountType credentialType)
{
switch (credentialType)
{
case AzureAccount.AccountType.User:
return userTokenProvider.GetAccessToken(config, promptBehavior, userId, password, credentialType);
case AzureAccount.AccountType.ServicePrincipal:
return servicePrincipalTokenProvider.GetAccessToken(config, promptBehavior, userId, password, credentialType);
default:
throw new ArgumentException(Resources.UnknownCredentialType, "credentialType");
}
}

public IAccessToken GetAccessTokenWithCertificate(AdalConfiguration config, string clientId, string certificate, AzureAccount.AccountType credentialType)
{
switch (credentialType)
{
case AzureAccount.AccountType.ServicePrincipal:
return servicePrincipalTokenProvider.GetAccessTokenWithCertificate(config, clientId, certificate, credentialType);
default:
throw new ArgumentException(string.Format(Resources.UnsupportedCredentialType, credentialType), "credentialType");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest.Azure.Authentication;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Authentication
{
/// <summary>
/// Interface to the certificate store for authentication
/// </summary>
internal sealed class CertificateApplicationCredentialProvider : IApplicationAuthenticationProvider
{
private string _certificateThumbprint;

/// <summary>
/// Create a certificate provider
/// </summary>
/// <param name="certificateThumbprint"></param>
public CertificateApplicationCredentialProvider(string certificateThumbprint)
{
this._certificateThumbprint = certificateThumbprint;
}

/// <summary>
/// Authenticate using certificate thumbprint from the datastore
/// </summary>
/// <param name="clientId">The active directory client id for the application.</param>
/// <param name="audience">The intended audience for authentication</param>
/// <param name="context">The AD AuthenticationContext to use</param>
/// <returns></returns>
public async Task<AuthenticationResult> AuthenticateAsync(string clientId, string audience, AuthenticationContext context)
{
var task = new Task<X509Certificate2>(() =>
{
return AzureSession.DataStore.GetCertificate(this._certificateThumbprint);
});
task.Start();
var certificate = await task.ConfigureAwait(false);

return await context.AcquireTokenAsync(
audience,
new ClientAssertionCertificate(clientId, certificate));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Microsoft.Azure.Commands.Common.Authentication
{
/// <summary>
/// An implementation of <see cref="IWin32Window"/> that gives the
/// windows handle for the current console window.
/// </summary>
public class ConsoleParentWindow : IWin32Window
{
public IntPtr Handle { get { return NativeMethods.GetConsoleWindow(); } }

static class NativeMethods
{
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
}
}
}
Loading