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

Commit f4cc68d

Browse files
Merge pull request #1439 from erwinvanhunen/dev
Updated graph cmdlets
2 parents 6d8d3ca + 57388a7 commit f4cc68d

File tree

9 files changed

+117
-28
lines changed

9 files changed

+117
-28
lines changed

Commands/Base/ConnectOnline.cs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
using File = System.IO.File;
1212
using System.Net;
1313
using Microsoft.Identity.Client;
14+
#if NETSTANDARD2_0
15+
using System.IdentityModel.Tokens.Jwt;
16+
#endif
1417
#if !ONPREMISES
1518
using Microsoft.SharePoint.Client.CompliancePolicy;
1619
#endif
@@ -167,7 +170,7 @@ public class ConnectOnline : PSCmdlet
167170
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_APPONLYAAD, ValueFromPipeline = true, HelpMessage = "The Url of the site collection to connect to.")]
168171
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_APPONLYAADPEM, ValueFromPipeline = true, HelpMessage = "The Url of the site collection to connect to.")]
169172
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_SPOMANAGEMENT, ValueFromPipeline = true, HelpMessage = "The Url of the site collection to connect to.")]
170-
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_ACCESSTOKEN, ValueFromPipeline = true, HelpMessage = "The Url of the site collection to connect to.")]
173+
[Parameter(Mandatory = false, Position = 0, ParameterSetName = ParameterSet_ACCESSTOKEN, ValueFromPipeline = true, HelpMessage = "The Url of the site collection to connect to.")]
171174
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_DEVICELOGIN, ValueFromPipeline = true, HelpMessage = "The Url of the site collection to connect to.")]
172175
#endif
173176
#if ONPREMISES
@@ -506,11 +509,11 @@ protected override void ProcessRecord()
506509
}
507510
else if (ParameterSetName == ParameterSet_DEVICELOGIN)
508511
{
509-
connection = ConnectDeviceLogin();
512+
connection = ConnectDeviceLogin();
510513
}
511514
else if (ParameterSetName == ParameterSet_GRAPHDEVICELOGIN)
512515
{
513-
connection = ConnectGraphDeviceLogin();
516+
connection = ConnectGraphDeviceLogin(null);
514517
}
515518
else if (ParameterSetName == ParameterSet_NATIVEAAD)
516519
{
@@ -545,10 +548,27 @@ protected override void ProcessRecord()
545548
else if (ParameterSetName == ParameterSet_ACCESSTOKEN)
546549
{
547550
#if !NETSTANDARD2_0
548-
connection = SPOnlineConnectionHelper.InitiateAccessTokenConnection(new Uri(Url), AccessToken, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, TenantAdminUrl, SkipTenantAdminCheck, AzureEnvironment);
551+
var jwtToken = new System.IdentityModel.Tokens.JwtSecurityToken(AccessToken);
549552
#else
550-
throw new NotImplementedException();
553+
var jwtToken = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(AccessToken);
551554
#endif
555+
var aud = jwtToken.Audiences.FirstOrDefault();
556+
if (aud != null)
557+
{
558+
Url = aud;
559+
}
560+
if (Url.ToLower() == "https://graph.microsoft.com")
561+
{
562+
connection = ConnectGraphDeviceLogin(AccessToken);
563+
}
564+
else
565+
{
566+
//#if !NETSTANDARD2_0
567+
connection = SPOnlineConnectionHelper.InitiateAccessTokenConnection(new Uri(Url), AccessToken, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, TenantAdminUrl, SkipTenantAdminCheck, AzureEnvironment);
568+
//#else
569+
//throw new NotImplementedException();
570+
//#endif
571+
}
552572
}
553573
#endif
554574
#if ONPREMISES
@@ -599,9 +619,17 @@ protected override void ProcessRecord()
599619
}
600620
if (SPOnlineConnection.CurrentConnection != null)
601621
{
602-
var hostUri = new Uri(SPOnlineConnection.CurrentConnection.Url);
603-
Environment.SetEnvironmentVariable("PNPPSHOST", hostUri.Host);
604-
Environment.SetEnvironmentVariable("PNPPSSITE", hostUri.LocalPath);
622+
if (SPOnlineConnection.CurrentConnection.ConnectionMethod != Model.ConnectionMethod.GraphDeviceLogin)
623+
{
624+
var hostUri = new Uri(SPOnlineConnection.CurrentConnection.Url);
625+
Environment.SetEnvironmentVariable("PNPPSHOST", hostUri.Host);
626+
Environment.SetEnvironmentVariable("PNPPSSITE", hostUri.LocalPath);
627+
}
628+
else
629+
{
630+
Environment.SetEnvironmentVariable("PNPPSHOST", "GRAPH");
631+
Environment.SetEnvironmentVariable("PNPPSSITE", "GRAPH");
632+
}
605633
}
606634
if (ReturnConnection)
607635
{
@@ -659,16 +687,23 @@ private SPOnlineConnection ConnectDeviceLogin()
659687
});
660688
}
661689

662-
private SPOnlineConnection ConnectGraphDeviceLogin()
690+
private SPOnlineConnection ConnectGraphDeviceLogin(string accessToken)
663691
{
664-
return SPOnlineConnectionHelper.InstantiateGraphDeviceLoginConnection(LaunchBrowser, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, (message) =>
692+
if (string.IsNullOrEmpty(accessToken))
665693
{
666-
WriteWarning(message);
667-
},
668-
(progress) =>
694+
return SPOnlineConnectionHelper.InstantiateGraphDeviceLoginConnection(LaunchBrowser, MinimalHealthScore, RetryCount, RetryWait, RequestTimeout, (message) =>
695+
{
696+
WriteWarning(message);
697+
},
698+
(progress) =>
699+
{
700+
Host.UI.Write(progress);
701+
});
702+
}
703+
else
669704
{
670-
Host.UI.Write(progress);
671-
});
705+
return SPOnlineConnectionHelper.InstantiateGraphAccessTokenConnection(accessToken);
706+
}
672707
}
673708

674709
private void ConnectGraphAAD()
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
using SharePointPnP.PowerShell.CmdletHelpAttributes;
22
using System;
33
using System.Collections.Generic;
4+
#if NETSTANDARD2_0
5+
using System.IdentityModel.Tokens.Jwt;
6+
#else
7+
using System.IdentityModel.Tokens;
8+
#endif
49
using System.Linq;
510
using System.Management.Automation;
611
using System.Text;
@@ -18,9 +23,19 @@ namespace SharePointPnP.PowerShell.Commands.Base
1823
SortOrder = 1)]
1924
public class GetPnPAccessToken : PnPGraphCmdlet
2025
{
26+
[Parameter(Mandatory = false, HelpMessage = "Returns the access token in a decoded manner")]
27+
public SwitchParameter Decoded;
2128
protected override void ExecuteCmdlet()
2229
{
23-
WriteObject(AccessToken);
30+
if (Decoded.IsPresent)
31+
{
32+
var decodedToken = new JwtSecurityToken(AccessToken);
33+
WriteObject(decodedToken);
34+
}
35+
else
36+
{
37+
WriteObject(AccessToken);
38+
}
2439
}
2540
}
2641
}

Commands/Base/PnPGraphCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected override void BeginProcessing()
7272
}
7373
}
7474
#else
75-
if (SPOnlineConnection.CurrentConnection != null && SPOnlineConnection.CurrentConnection.ConnectionMethod == Model.ConnectionMethod.GraphDeviceLogin)
75+
if (SPOnlineConnection.CurrentConnection != null && (SPOnlineConnection.CurrentConnection.ConnectionMethod == Model.ConnectionMethod.GraphDeviceLogin || SPOnlineConnection.CurrentConnection.ConnectionMethod == Model.ConnectionMethod.AccessToken))
7676
{
7777
// Graph Connection
7878
if (string.IsNullOrEmpty(SPOnlineConnection.CurrentConnection.AccessToken))

Commands/Base/SPOnlineConnection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public SPOnlineConnection(ClientContext context, TokenResult tokenResult, Connec
113113
};
114114
}
115115

116-
public SPOnlineConnection(TokenResult tokenResult, ConnectionType connectionType, int minimalHealthScore, int retryCount, int retryWait, string pnpVersionTag)
116+
public SPOnlineConnection(TokenResult tokenResult, ConnectionMethod connectionMethod, ConnectionType connectionType, int minimalHealthScore, int retryCount, int retryWait, string pnpVersionTag)
117117
{
118118
TokenResult = tokenResult;
119119
var coreAssembly = Assembly.GetExecutingAssembly();
@@ -123,7 +123,7 @@ public SPOnlineConnection(TokenResult tokenResult, ConnectionType connectionType
123123
RetryCount = retryCount;
124124
RetryWait = retryWait;
125125
PnPVersionTag = pnpVersionTag;
126-
ConnectionMethod = ConnectionMethod.GraphDeviceLogin;
126+
ConnectionMethod = ConnectionMethod;
127127
}
128128

129129

Commands/Base/SPOnlineConnectionHelper.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ internal static SPOnlineConnection InstantiateDeviceLoginConnection(string url,
146146
});
147147
#else
148148
OpenBrowser(returnData["verification_url"]);
149+
messageCallback(returnData["message"]);
150+
151+
var tokenResult = GetTokenResult(connectionUri, returnData, messageCallback, progressCallback);
152+
153+
if (tokenResult != null)
154+
{
155+
progressCallback("Token received");
156+
spoConnection = new SPOnlineConnection(context, tokenResult, ConnectionType.O365, minimalHealthScore, retryCount, retryWait, null, url.ToString(), tenantAdminUrl, PnPPSVersionTag);
157+
}
158+
else
159+
{
160+
progressCallback("No token received.");
161+
}
149162
#endif
150163
}
151164
else
@@ -155,11 +168,28 @@ internal static SPOnlineConnection InstantiateDeviceLoginConnection(string url,
155168
{
156169
progressCallback("Token received");
157170
spoConnection = new SPOnlineConnection(context, tokenResult, ConnectionType.O365, minimalHealthScore, retryCount, retryWait, null, url.ToString(), tenantAdminUrl, PnPPSVersionTag);
158-
} else
171+
}
172+
else
159173
{
160174
progressCallback("No token received.");
161175
}
162176
}
177+
spoConnection.ConnectionMethod = ConnectionMethod.DeviceLogin;
178+
return spoConnection;
179+
}
180+
181+
internal static SPOnlineConnection InstantiateGraphAccessTokenConnection(string accessToken)
182+
{
183+
#if NETSTANDARD2_0
184+
var jwtToken = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(accessToken);
185+
#else
186+
var jwtToken = new System.IdentityModel.Tokens.JwtSecurityToken(accessToken);
187+
#endif
188+
var tokenResult = new TokenResult();
189+
tokenResult.AccessToken = accessToken;
190+
tokenResult.ExpiresOn = jwtToken.ValidTo;
191+
var spoConnection = new SPOnlineConnection(tokenResult, ConnectionMethod.AccessToken, ConnectionType.O365, 0, 0, 0, PnPPSVersionTag);
192+
spoConnection.ConnectionMethod = ConnectionMethod.GraphDeviceLogin;
163193
return spoConnection;
164194
}
165195

@@ -185,7 +215,7 @@ internal static SPOnlineConnection InstantiateGraphDeviceLoginConnection(bool la
185215
if (tokenResult != null)
186216
{
187217
progressCallback("Token received");
188-
spoConnection = new SPOnlineConnection(tokenResult, ConnectionType.O365, minimalHealthScore, retryCount, retryWait, PnPPSVersionTag);
218+
spoConnection = new SPOnlineConnection(tokenResult, ConnectionMethod.GraphDeviceLogin, ConnectionType.O365, minimalHealthScore, retryCount, retryWait, PnPPSVersionTag);
189219
}
190220
else
191221
{
@@ -202,7 +232,7 @@ internal static SPOnlineConnection InstantiateGraphDeviceLoginConnection(bool la
202232
if (tokenResult != null)
203233
{
204234
progressCallback("Token received");
205-
spoConnection = new SPOnlineConnection(tokenResult, ConnectionType.O365, minimalHealthScore, retryCount, retryWait, PnPPSVersionTag);
235+
spoConnection = new SPOnlineConnection(tokenResult, ConnectionMethod.GraphDeviceLogin, ConnectionType.O365, minimalHealthScore, retryCount, retryWait, PnPPSVersionTag);
206236
}
207237
else
208238
{
@@ -220,13 +250,14 @@ internal static SPOnlineConnection InstantiateGraphDeviceLoginConnection(bool la
220250
if (tokenResult != null)
221251
{
222252
progressCallback("Token received");
223-
spoConnection = new SPOnlineConnection(tokenResult, ConnectionType.O365, minimalHealthScore, retryCount, retryWait, PnPPSVersionTag);
253+
spoConnection = new SPOnlineConnection(tokenResult, ConnectionMethod.GraphDeviceLogin, ConnectionType.O365, minimalHealthScore, retryCount, retryWait, PnPPSVersionTag);
224254
}
225255
else
226256
{
227257
progressCallback("No token received.");
228258
}
229259
}
260+
spoConnection.ConnectionMethod = ConnectionMethod.GraphDeviceLogin;
230261
return spoConnection;
231262
}
232263

@@ -363,7 +394,9 @@ internal static SPOnlineConnection InitiateAzureADAppOnlyConnection(Uri url, str
363394
}
364395
return new SPOnlineConnection(context, connectionType, minimalHealthScore, retryCount, retryWait, null, url.ToString(), tenantAdminUrl, PnPPSVersionTag);
365396
}
366-
397+
#endif
398+
#endif
399+
#if !ONPREMISES
367400
internal static SPOnlineConnection InitiateAccessTokenConnection(Uri url, string accessToken, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, bool skipAdminCheck = false, AzureEnvironment azureEnvironment = AzureEnvironment.Production)
368401
{
369402
var authManager = new OfficeDevPnP.Core.AuthenticationManager();
@@ -381,7 +414,6 @@ internal static SPOnlineConnection InitiateAccessTokenConnection(Uri url, string
381414
return spoConnection;
382415
}
383416
#endif
384-
#endif
385417

386418
#if !NETSTANDARD2_0
387419
internal static SPOnlineConnection InstantiateWebloginConnection(Uri url, int minimalHealthScore, int retryCount, int retryWait, int requestTimeout, string tenantAdminUrl, bool skipAdminCheck = false)

Commands/Graph/NewUnifiedGroup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class NewPnPUnifiedGroup : PnPGraphCmdlet
4141
[Parameter(Mandatory = true, HelpMessage = "The Description of the Office 365 Group.")]
4242
public String Description;
4343

44-
[Parameter(Mandatory = true, HelpMessage = "The Mail Nickname of the Office 365 Group.")]
44+
[Parameter(Mandatory = true, HelpMessage = "The Mail Nickname of the Office 365 Group. Cannot contain spaces.")]
4545
public String MailNickname;
4646

4747
[Parameter(Mandatory = false, HelpMessage = "The array UPN values of the group's owners.")]
@@ -61,6 +61,10 @@ public class NewPnPUnifiedGroup : PnPGraphCmdlet
6161

6262
protected override void ExecuteCmdlet()
6363
{
64+
if(MailNickname.Contains(" "))
65+
{
66+
throw new ArgumentException("MailNickname cannot contain spaces.");
67+
}
6468
bool forceCreation;
6569

6670
if (!Force)

Commands/Model/ConnectionMethod.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ namespace SharePointPnP.PowerShell.Commands.Model
88
{
99
public enum ConnectionMethod
1010
{
11+
Unspecified,
1112
WebLogin,
1213
Credentials,
1314
AccessToken,
1415
AzureADAppOnly,
1516
AzureADNativeApplication,
1617
ADFS,
17-
GraphDeviceLogin
18+
GraphDeviceLogin,
19+
DeviceLogin
1820
}
1921
}

Commands/PnPPowerShell.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<PackageReference Include="Microsoft.Identity.Client" Version="1.1.2-preview0008" />
2020
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
2121
<PackageReference Include="PowerShellStandard.Library" Version="3.0.0-preview-01" />
22+
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.2.1" />
2223
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
2324
</ItemGroup>
2425

Commands/SharePointPnP.PowerShell.Commands.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@
435435
<Compile Include="Admin\RemoveStorageEntity.cs" />
436436
<Compile Include="Admin\RemoveTenantTheme.cs" />
437437
<Compile Include="Base\AddStoredCredential.cs" />
438+
<Compile Include="Base\GetAccessToken.cs" />
438439
<Compile Include="Base\GetAzureCertificate.cs" />
439440
<Compile Include="Base\NewAzureCertificate.cs" />
440441
<Compile Include="Base\RemoveStoredCredential.cs" />
@@ -539,7 +540,6 @@
539540
<SubType>Code</SubType>
540541
</Compile>
541542
<Compile Include="Apps\UninstallAppInstance.cs" />
542-
<Compile Include="Base\GetPnPAccessToken.cs" />
543543
<Compile Include="Base\GetProperty.cs" />
544544
<Compile Include="Base\GetAuthenticationRealm.cs" />
545545
<Compile Include="Base\Constants.cs" />

0 commit comments

Comments
 (0)