From 21a2c680985d784f8e81a4e85bcdac8197201874 Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Thu, 17 Sep 2020 10:59:32 -0700 Subject: [PATCH 1/4] Handle comma separated scopes. --- .../Authentication/Cmdlets/ConnectGraph.cs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Authentication/Authentication/Cmdlets/ConnectGraph.cs b/src/Authentication/Authentication/Cmdlets/ConnectGraph.cs index 5a224425a30..9310fabde29 100644 --- a/src/Authentication/Authentication/Cmdlets/ConnectGraph.cs +++ b/src/Authentication/Authentication/Cmdlets/ConnectGraph.cs @@ -85,7 +85,8 @@ protected override void ProcessRecord() TimeSpan authTimeout = new TimeSpan(0, 0, Constants.MaxDeviceCodeTimeOut); cancellationTokenSource = new CancellationTokenSource(authTimeout); authContext.AuthType = AuthenticationType.Delegated; - authContext.Scopes = Scopes ?? new string[] { "User.Read" }; + string[] processedScopes = ProcessScopes(Scopes); + authContext.Scopes = processedScopes.Length == 0 ? new string[] { "User.Read" } : processedScopes; // Default to CurrentUser but allow the customer to change this via `ContextScope` param. authContext.ContextScope = this.IsParameterBound(nameof(ContextScope)) ? ContextScope : ContextScope.CurrentUser; } @@ -190,6 +191,31 @@ protected override void StopProcessing() base.StopProcessing(); } + /// + /// Processes user provided scopes by removing whitespace and splitting comma separated scopes. + /// + /// An array of scopes. + /// A formated array of scopes. + private string[] ProcessScopes(string[] scopes) + { + if (scopes == null) + { + return new string[0]; + } + + List formatedScopes = new List(); + foreach (string scope in scopes) + { + string[] cleanScopes = scope.Split(',') + .Select(s => s.Trim()) + .Where(s => !string.IsNullOrWhiteSpace(s)) + .ToArray(); + + formatedScopes.AddRange(cleanScopes); + } + return formatedScopes.ToArray(); + } + private void ValidateParameters() { switch (ParameterSetName) From a85e3d62627affaaeee4da5b380c98936a608c8e Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Thu, 17 Sep 2020 17:08:05 -0700 Subject: [PATCH 2/4] Alias Connect & Disconnect-Graph --- .../Cmdlets/{ConnectGraph.cs => ConnectMgGraph.cs} | 5 +++-- .../Cmdlets/{DisconnectGraph.cs => DisconnectMgGraph.cs} | 5 +++-- src/Authentication/Authentication/Helpers/HttpHelpers.cs | 2 +- .../Authentication/Microsoft.Graph.Authentication.psd1 | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) rename src/Authentication/Authentication/Cmdlets/{ConnectGraph.cs => ConnectMgGraph.cs} (98%) rename src/Authentication/Authentication/Cmdlets/{DisconnectGraph.cs => DisconnectMgGraph.cs} (90%) diff --git a/src/Authentication/Authentication/Cmdlets/ConnectGraph.cs b/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs similarity index 98% rename from src/Authentication/Authentication/Cmdlets/ConnectGraph.cs rename to src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs index 9310fabde29..6173ee0c03a 100644 --- a/src/Authentication/Authentication/Cmdlets/ConnectGraph.cs +++ b/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs @@ -17,8 +17,9 @@ namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets using System.Net; using System.Globalization; - [Cmdlet(VerbsCommunications.Connect, "Graph", DefaultParameterSetName = Constants.UserParameterSet)] - public class ConnectGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssemblyCleanup + [Cmdlet(VerbsCommunications.Connect, "MgGraph", DefaultParameterSetName = Constants.UserParameterSet)] + [Alias("Connect-Graph")] + public class ConnectMgGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssemblyCleanup { [Parameter(ParameterSetName = Constants.UserParameterSet, Position = 1, diff --git a/src/Authentication/Authentication/Cmdlets/DisconnectGraph.cs b/src/Authentication/Authentication/Cmdlets/DisconnectMgGraph.cs similarity index 90% rename from src/Authentication/Authentication/Cmdlets/DisconnectGraph.cs rename to src/Authentication/Authentication/Cmdlets/DisconnectMgGraph.cs index 085c9b5f34e..8e38062da9b 100644 --- a/src/Authentication/Authentication/Cmdlets/DisconnectGraph.cs +++ b/src/Authentication/Authentication/Cmdlets/DisconnectMgGraph.cs @@ -6,8 +6,9 @@ namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets using Microsoft.Graph.PowerShell.Authentication.Helpers; using System; using System.Management.Automation; - [Cmdlet(VerbsCommunications.Disconnect, "Graph")] - public class DisconnectGraph : PSCmdlet + [Cmdlet(VerbsCommunications.Disconnect, "MgGraph")] + [Alias("Disconnect-Graph")] + public class DisconnectMgGraph : PSCmdlet { protected override void BeginProcessing() { diff --git a/src/Authentication/Authentication/Helpers/HttpHelpers.cs b/src/Authentication/Authentication/Helpers/HttpHelpers.cs index c030452fd68..3b29c5a4c2d 100644 --- a/src/Authentication/Authentication/Helpers/HttpHelpers.cs +++ b/src/Authentication/Authentication/Helpers/HttpHelpers.cs @@ -17,7 +17,7 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers public static class HttpHelpers { /// The version for current assembly. - private static AssemblyName AssemblyInfo = typeof(ConnectGraph).GetTypeInfo().Assembly.GetName(); + private static AssemblyName AssemblyInfo = typeof(ConnectMgGraph).GetTypeInfo().Assembly.GetName(); /// The value for the Auth module version header. private static string AuthModuleVersionHeaderValue = diff --git a/src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1 b/src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1 index fe97302236b..2f8acfa8e26 100644 --- a/src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1 +++ b/src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1 @@ -72,14 +72,14 @@ FormatsToProcess = './Microsoft.Graph.Authentication.format.ps1xml' FunctionsToExport = @() # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. -CmdletsToExport = 'Connect-Graph', 'Disconnect-Graph', 'Get-MgContext', 'Get-MgProfile', +CmdletsToExport = 'Connect-MgGraph', 'Disconnect-MgGraph', 'Get-MgContext', 'Get-MgProfile', 'Select-MgProfile', 'Invoke-GraphRequest' # Variables to export from this module # VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = @() +AliasesToExport = @('Connect-Graph', 'Disconnect-Graph') # DSC resources to export from this module # DscResourcesToExport = @() From 505c42f434bfd95b439b449dc82fee562ca9216d Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Fri, 18 Sep 2020 13:18:36 -0700 Subject: [PATCH 3/4] Alias ClientId and CertName to AppId and CertSubject respectively. --- src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs b/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs index 6173ee0c03a..e8bb9ea125b 100644 --- a/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs +++ b/src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs @@ -30,11 +30,13 @@ public class ConnectMgGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssem Position = 1, Mandatory = true, HelpMessage = "The client id of your application.")] + [Alias("AppId")] public string ClientId { get; set; } [Parameter(ParameterSetName = Constants.AppParameterSet, Position = 2, HelpMessage = "The name of your certificate. The Certificate will be retrieved from the current user's certificate store.")] + [Alias("CertificateSubject")] public string CertificateName { get; set; } [Parameter(ParameterSetName = Constants.AppParameterSet, From b6f23f4b8b61a5328a6f6ad39136abc0f6e429ed Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Fri, 18 Sep 2020 13:28:17 -0700 Subject: [PATCH 4/4] Alias Invoke-GraphRequest --- .../{InvokeGraphRequest.cs => InvokeMgGraphRequest.cs} | 7 ++++--- src/Authentication/Authentication/Helpers/PathUtils.cs | 2 +- src/Authentication/Authentication/Helpers/StreamHelper.cs | 2 +- .../Authentication/Microsoft.Graph.Authentication.psd1 | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) rename src/Authentication/Authentication/Cmdlets/{InvokeGraphRequest.cs => InvokeMgGraphRequest.cs} (99%) diff --git a/src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs similarity index 99% rename from src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs rename to src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs index 9f66df3db5b..7f4d0b3c9dc 100644 --- a/src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs +++ b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs @@ -22,14 +22,15 @@ namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets { - [Cmdlet(VerbsLifecycle.Invoke, "GraphRequest", DefaultParameterSetName = Constants.UserParameterSet)] - public class InvokeGraphRequest : PSCmdlet + [Cmdlet(VerbsLifecycle.Invoke, "MgGraphRequest", DefaultParameterSetName = Constants.UserParameterSet)] + [Alias("Invoke-GraphRequest")] + public class InvokeMgGraphRequest : PSCmdlet { private readonly CancellationTokenSource _cancellationTokenSource; private readonly InvokeGraphRequestUserAgent _graphRequestUserAgent; private string _originalFilePath; - public InvokeGraphRequest() + public InvokeMgGraphRequest() { _cancellationTokenSource = new CancellationTokenSource(); _graphRequestUserAgent = new InvokeGraphRequestUserAgent(this); diff --git a/src/Authentication/Authentication/Helpers/PathUtils.cs b/src/Authentication/Authentication/Helpers/PathUtils.cs index 716aa4cdd87..13d8c9ed449 100644 --- a/src/Authentication/Authentication/Helpers/PathUtils.cs +++ b/src/Authentication/Authentication/Helpers/PathUtils.cs @@ -13,7 +13,7 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers /// internal static class PathUtils { - public static string ResolveFilePath(string filePath, InvokeGraphRequest command, bool isLiteralPath) + public static string ResolveFilePath(string filePath, InvokeMgGraphRequest command, bool isLiteralPath) { string path = null; try diff --git a/src/Authentication/Authentication/Helpers/StreamHelper.cs b/src/Authentication/Authentication/Helpers/StreamHelper.cs index 7329861cdb3..0a0a042c278 100644 --- a/src/Authentication/Authentication/Helpers/StreamHelper.cs +++ b/src/Authentication/Authentication/Helpers/StreamHelper.cs @@ -79,7 +79,7 @@ internal static string StreamToString(this Stream stream, Encoding encoding) } internal static void SaveStreamToFile(this Stream baseResponseStream, string filePath, - InvokeGraphRequest invokeGraphRequest, CancellationToken token) + InvokeMgGraphRequest invokeGraphRequest, CancellationToken token) { // If the web cmdlet should resume, append the file instead of overwriting. const FileMode fileMode = FileMode.Create; diff --git a/src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1 b/src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1 index 2f8acfa8e26..c438de46e4a 100644 --- a/src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1 +++ b/src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1 @@ -73,13 +73,13 @@ FunctionsToExport = @() # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = 'Connect-MgGraph', 'Disconnect-MgGraph', 'Get-MgContext', 'Get-MgProfile', - 'Select-MgProfile', 'Invoke-GraphRequest' + 'Select-MgProfile', 'Invoke-MgGraphRequest' # Variables to export from this module # VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = @('Connect-Graph', 'Disconnect-Graph') +AliasesToExport = @('Connect-Graph', 'Disconnect-Graph', 'Invoke-GraphRequest') # DSC resources to export from this module # DscResourcesToExport = @()