Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit 80f4951

Browse files
committed
Added support for different Azure Environments
1 parent fb53d9c commit 80f4951

14 files changed

+181
-83
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
99

1010
### Added
1111
- Added -AzureEnvironment parameter to `Initialize-PnPPowerShellAuthentication` to create an Azure AD App in other Azure environments than the default one.
12+
- Added -AzureEnvironment parameter to all Connect-PnPOnline options which result in an OAuth based authentication connection, supporting the various Azure Environments available.
1213

1314
### Changed
1415
- Fixed issue with Submit-PnPTeamsChannelMessage not posting HTML message when setting the content type to Html.
1516
- The content type that Submit-PnPTeamsChannelMessage uses defaults now to HTML.
17+
- Fixed an issue with the PnP Provisioning Engine not being able to correctly acquire a token for the Microsoft Graph when provisioning a tenant template containing a Team.
1618

1719
### Contributors
1820

Commands/Base/ConnectOnline.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ public class ConnectOnline : BasePSCmdlet
522522
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_APPONLYAADCER, HelpMessage = "The Azure environment to use for authentication, the defaults to 'Production' which is the main Azure environment.")]
523523
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_TOKEN, HelpMessage = "The Azure environment to use for authentication, the defaults to 'Production' which is the main Azure environment.")]
524524
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_APPONLYCLIENTIDCLIENTSECRETURL, HelpMessage = "The Azure environment to use for authentication, the defaults to 'Production' which is the main Azure environment.")]
525+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_AADWITHSCOPE, HelpMessage = "The Azure environment to use for authentication, the defaults to 'Production' which is the main Azure environment.")]
526+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_DEVICELOGIN, HelpMessage = "The Azure environment to use for authentication, the defaults to 'Production' which is the main Azure environment.")]
527+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_GRAPHDEVICELOGIN, HelpMessage = "The Azure environment to use for authentication, the defaults to 'Production' which is the main Azure environment.")]
525528
public AzureEnvironment AzureEnvironment = AzureEnvironment.Production;
526529

527530
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_AADWITHSCOPE, HelpMessage = "The array of permission scopes to request from Azure Active Directory")]
@@ -710,7 +713,7 @@ protected void Connect()
710713
break;
711714

712715
case ParameterSet_AADWITHSCOPE:
713-
connection = ConnectAadWithScope(credentials);
716+
connection = ConnectAadWithScope(credentials, AzureEnvironment);
714717
break;
715718
case ParameterSet_ACCESSTOKEN:
716719
connection = ConnectAccessToken();
@@ -889,7 +892,7 @@ private PnPConnection ConnectDeviceLogin()
889892
{
890893
Url += "/";
891894
}
892-
var connection = PnPConnectionHelper.InstantiateDeviceLoginConnection(Url, LaunchBrowser, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, TenantAdminUrl, Host, NoTelemetry);
895+
var connection = PnPConnectionHelper.InstantiateDeviceLoginConnection(Url, LaunchBrowser, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, TenantAdminUrl, Host, NoTelemetry, AzureEnvironment);
893896

894897
if (Host.Name == "ConsoleHost")
895898
{
@@ -939,7 +942,7 @@ private PnPConnection ConnectGraphDeviceLogin(string accessToken)
939942
}
940943
}
941944
return false;
942-
}, Host, NoTelemetry);
945+
}, Host, NoTelemetry, AzureEnvironment);
943946
if (Host.Name == "ConsoleHost")
944947
{
945948
Console.TreatControlCAsInput = ctrlCAsInput;
@@ -1076,7 +1079,7 @@ private PnPConnection ConnectAppOnlyAadCer()
10761079
/// </summary>
10771080
/// <param name="credentials">Credentials to authenticate with for delegated access or NULL for application permissions</param>
10781081
/// <returns>PnPConnection based on the parameters provided in the parameter set</returns>
1079-
private PnPConnection ConnectAadWithScope(PSCredential credentials)
1082+
private PnPConnection ConnectAadWithScope(PSCredential credentials, AzureEnvironment azureEnvironment)
10801083
{
10811084
#if !ONPREMISES
10821085
// Filter out the scopes for the Microsoft Office 365 Management API
@@ -1100,7 +1103,7 @@ private PnPConnection ConnectAadWithScope(PSCredential credentials)
11001103
// TokenManager.InitializeAsync(TokenManager.CLIENTID_PNPMANAGEMENTSHELL, officeManagementApiScopes.Select(s => $"https://manage.office.com/{s}").ToArray(), credentials.UserName, credentials.Password, cacheIdentifierName: "OfficeManagementApi").GetAwaiter().GetResult();
11011104
//}
11021105

1103-
var officeManagementApiToken = credentials == null ? OfficeManagementApiToken.AcquireApplicationTokenInteractive(PnPConnection.PnPManagementShellClientId, officeManagementApiScopes) : OfficeManagementApiToken.AcquireDelegatedTokenWithCredentials(PnPConnection.PnPManagementShellClientId, officeManagementApiScopes, credentials.UserName, credentials.Password);
1106+
var officeManagementApiToken = credentials == null ? OfficeManagementApiToken.AcquireApplicationTokenInteractive(PnPConnection.PnPManagementShellClientId, officeManagementApiScopes, azureEnvironment) : OfficeManagementApiToken.AcquireDelegatedTokenWithCredentials(PnPConnection.PnPManagementShellClientId, officeManagementApiScopes, credentials.UserName, credentials.Password, azureEnvironment);
11041107
#else
11051108
var officeManagementApiToken = credentials == null ? OfficeManagementApiToken.AcquireApplicationTokenDeviceLogin(PnPConnection.PnPManagementShellClientId, officeManagementApiScopes, PnPConnection.DeviceLoginCallback(this.Host, true)) : OfficeManagementApiToken.AcquireDelegatedTokenWithCredentials(PnPConnection.PnPManagementShellClientId, officeManagementApiScopes, credentials.UserName, credentials.Password);
11061109
#endif
@@ -1111,7 +1114,7 @@ private PnPConnection ConnectAadWithScope(PSCredential credentials)
11111114
if (graphScopes.Length > 0)
11121115
{
11131116
#if !PNPPSCORE
1114-
var graphToken = credentials == null ? GraphToken.AcquireApplicationTokenInteractive(PnPConnection.PnPManagementShellClientId, graphScopes) : GraphToken.AcquireDelegatedTokenWithCredentials(PnPConnection.PnPManagementShellClientId, graphScopes, credentials.UserName, credentials.Password);
1117+
var graphToken = credentials == null ? GraphToken.AcquireApplicationTokenInteractive(PnPConnection.PnPManagementShellClientId, graphScopes, azureEnvironment) : GraphToken.AcquireDelegatedTokenWithCredentials(PnPConnection.PnPManagementShellClientId, graphScopes, credentials.UserName, credentials.Password, azureEnvironment);
11151118
#else
11161119
var graphToken = credentials == null ? GraphToken.AcquireApplicationTokenDeviceLogin(PnPConnection.PnPManagementShellClientId, graphScopes, PnPConnection.DeviceLoginCallback(this.Host, true)) : GraphToken.AcquireDelegatedTokenWithCredentials(PnPConnection.PnPManagementShellClientId, graphScopes, credentials.UserName, credentials.Password);
11171120
#endif
@@ -1341,6 +1344,7 @@ private PnPConnection ConnectCredentials(PSCredential credentials)
13411344
RequestTimeout,
13421345
TenantAdminUrl,
13431346
NoTelemetry,
1347+
AzureEnvironment,
13441348
SkipTenantAdminCheck,
13451349
AuthenticationMode);
13461350
#else

Commands/Base/PnPConnection.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Web;
1919
using TextCopy;
2020
using PnP.PowerShell.CmdletHelpAttributes;
21+
using OfficeDevPnP.Core;
2122

2223
namespace PnP.PowerShell.Commands.Base
2324
{
@@ -116,6 +117,8 @@ public HttpClient HttpClient
116117
/// </summary>
117118
public string Tenant { get; set; }
118119

120+
public AzureEnvironment AzureEnvironment { get; set; } = AzureEnvironment.Production;
121+
119122
#endregion
120123

121124
#region Fields
@@ -137,7 +140,7 @@ public HttpClient HttpClient
137140
/// <returns>AccessToken for the audience or NULL if unable to retrieve an access token for the audience on the current connection</returns>
138141
internal string TryGetAccessToken(TokenAudience tokenAudience, string[] roles = null)
139142
{
140-
return TryGetToken(tokenAudience, roles)?.AccessToken;
143+
return TryGetToken(tokenAudience, AzureEnvironment, roles)?.AccessToken;
141144
}
142145

143146
internal static Action<DeviceCodeResult> DeviceLoginCallback(PSHost host, bool launchBrowser)
@@ -163,7 +166,7 @@ internal static Action<DeviceCodeResult> DeviceLoginCallback(PSHost host, bool l
163166
/// <param name="tokenAudience">Audience to try to get a token for</param>
164167
/// <param name="orRoles">The specific roles to request access to (i.e. Group.ReadWrite.All). Optional, will use default groups assigned to clientId if not specified.</param>
165168
/// <returns><see cref="GenericToken"/> for the audience or NULL if unable to retrieve a token for the audience on the current connection</returns>
166-
internal GenericToken TryGetToken(TokenAudience tokenAudience, string[] orRoles = null, string[] andRoles = null, TokenType tokenType = TokenType.All)
169+
internal GenericToken TryGetToken(TokenAudience tokenAudience, AzureEnvironment azureEnvironment, string[] orRoles = null, string[] andRoles = null, TokenType tokenType = TokenType.All)
167170
{
168171
GenericToken token = null;
169172

@@ -176,19 +179,19 @@ internal GenericToken TryGetToken(TokenAudience tokenAudience, string[] orRoles
176179
var officeManagementApiScopes = Enum.GetNames(typeof(OfficeManagementApiPermission)).Select(s => s.Replace("_", ".")).Intersect(Scopes).ToArray();
177180
// Take the remaining scopes and try requesting them from the Microsoft Graph API
178181
var scopes = Scopes.Except(officeManagementApiScopes).ToArray();
179-
token = GraphToken.AcquireApplicationTokenDeviceLogin(PnPConnection.PnPManagementShellClientId, scopes, DeviceLoginCallback(null, false));
182+
token = GraphToken.AcquireApplicationTokenDeviceLogin(PnPConnection.PnPManagementShellClientId, scopes, DeviceLoginCallback(null, false), AzureEnvironment);
180183
}
181184
else
182185
{
183186
if (!string.IsNullOrEmpty(Tenant))
184187
{
185188
if (Certificate != null)
186189
{
187-
token = GraphToken.AcquireApplicationToken(Tenant, ClientId, Certificate);
190+
token = GraphToken.AcquireApplicationToken(Tenant, ClientId, Certificate, AzureEnvironment);
188191
}
189192
else if (ClientSecret != null)
190193
{
191-
token = GraphToken.AcquireApplicationToken(Tenant, ClientId, ClientSecret);
194+
token = GraphToken.AcquireApplicationToken(Tenant, ClientId, ClientSecret, AzureEnvironment);
192195
}
193196
else if (Scopes != null)
194197
{
@@ -197,7 +200,7 @@ internal GenericToken TryGetToken(TokenAudience tokenAudience, string[] orRoles
197200
var scopes = Scopes.Except(officeManagementApiScopes).ToArray();
198201
if (scopes.Length > 0)
199202
{
200-
token = PSCredential == null ? GraphToken.AcquireApplicationTokenInteractive(PnPManagementShellClientId, scopes) : GraphToken.AcquireDelegatedTokenWithCredentials(PnPManagementShellClientId, scopes, PSCredential.UserName, PSCredential.Password);
203+
token = PSCredential == null ? GraphToken.AcquireApplicationTokenInteractive(PnPManagementShellClientId, scopes, azureEnvironment) : GraphToken.AcquireDelegatedTokenWithCredentials(PnPManagementShellClientId, scopes, PSCredential.UserName, PSCredential.Password, azureEnvironment);
201204
}
202205
else
203206
{
@@ -213,19 +216,19 @@ internal GenericToken TryGetToken(TokenAudience tokenAudience, string[] orRoles
213216
{
214217
if (Certificate != null)
215218
{
216-
token = OfficeManagementApiToken.AcquireApplicationToken(Tenant, ClientId, Certificate);
219+
token = OfficeManagementApiToken.AcquireApplicationToken(Tenant, ClientId, Certificate, AzureEnvironment);
217220
}
218221
else if (ClientSecret != null)
219222
{
220-
token = OfficeManagementApiToken.AcquireApplicationToken(Tenant, ClientId, ClientSecret);
223+
token = OfficeManagementApiToken.AcquireApplicationToken(Tenant, ClientId, ClientSecret, AzureEnvironment);
221224
}
222225
else if (Scopes != null)
223226
{
224227
var scopes = Enum.GetNames(typeof(OfficeManagementApiPermission)).Select(s => s.Replace("_", ".")).Intersect(Scopes).ToArray();
225228
// Take the remaining scopes and try requesting them from the Microsoft Graph API
226229
if (scopes.Length > 0)
227230
{
228-
token = PSCredential == null ? OfficeManagementApiToken.AcquireApplicationTokenInteractive(PnPManagementShellClientId, scopes) : OfficeManagementApiToken.AcquireDelegatedTokenWithCredentials(PnPManagementShellClientId, scopes, PSCredential.UserName, PSCredential.Password);
231+
token = PSCredential == null ? OfficeManagementApiToken.AcquireApplicationTokenInteractive(PnPManagementShellClientId, scopes, azureEnvironment) : OfficeManagementApiToken.AcquireDelegatedTokenWithCredentials(PnPManagementShellClientId, scopes, PSCredential.UserName, PSCredential.Password, azureEnvironment);
229232
}
230233
else
231234
{
@@ -514,13 +517,15 @@ public static PnPConnection GetConnectionWithToken(GenericToken token,
514517
ClientContext clientContext = null,
515518
int? minimalHealthScore = null,
516519
string pnpVersionTag = null,
517-
bool disableTelemetry = false)
520+
bool disableTelemetry = false,
521+
AzureEnvironment azureEnvironment = AzureEnvironment.Production)
518522
{
519523
var connection = new PnPConnection(host, initializationType, url, clientContext, new Dictionary<TokenAudience, GenericToken>(1) { { tokenAudience, token } }, minimalHealthScore, pnpVersionTag, disableTelemetry)
520524
{
521525
ConnectionMethod = ConnectionMethod.AccessToken,
522526
Tenant = token.ParsedToken.Claims.FirstOrDefault(c => c.Type.Equals("tid", StringComparison.InvariantCultureIgnoreCase))?.Value,
523-
ClientId = token.ParsedToken.Claims.FirstOrDefault(c => c.Type.Equals("appid", StringComparison.InvariantCultureIgnoreCase))?.Value
527+
ClientId = token.ParsedToken.Claims.FirstOrDefault(c => c.Type.Equals("appid", StringComparison.InvariantCultureIgnoreCase))?.Value,
528+
AzureEnvironment = azureEnvironment
524529
};
525530
connection.PSCredential = credentials;
526531
return connection;

0 commit comments

Comments
 (0)