Skip to content

Commit 93f165b

Browse files
authored
Merge pull request #384 from microsoftgraph/po/authModuleCmdlets
Connect and Disconnect-Graph Aliases
2 parents 3d31022 + b6f23f4 commit 93f165b

File tree

7 files changed

+45
-14
lines changed

7 files changed

+45
-14
lines changed

src/Authentication/Authentication/Cmdlets/ConnectGraph.cs renamed to src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets
1717
using System.Net;
1818
using System.Globalization;
1919

20-
[Cmdlet(VerbsCommunications.Connect, "Graph", DefaultParameterSetName = Constants.UserParameterSet)]
21-
public class ConnectGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssemblyCleanup
20+
[Cmdlet(VerbsCommunications.Connect, "MgGraph", DefaultParameterSetName = Constants.UserParameterSet)]
21+
[Alias("Connect-Graph")]
22+
public class ConnectMgGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssemblyCleanup
2223
{
2324
[Parameter(ParameterSetName = Constants.UserParameterSet,
2425
Position = 1,
@@ -29,11 +30,13 @@ public class ConnectGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssembl
2930
Position = 1,
3031
Mandatory = true,
3132
HelpMessage = "The client id of your application.")]
33+
[Alias("AppId")]
3234
public string ClientId { get; set; }
3335

3436
[Parameter(ParameterSetName = Constants.AppParameterSet,
3537
Position = 2,
3638
HelpMessage = "The name of your certificate. The Certificate will be retrieved from the current user's certificate store.")]
39+
[Alias("CertificateSubject")]
3740
public string CertificateName { get; set; }
3841

3942
[Parameter(ParameterSetName = Constants.AppParameterSet,
@@ -85,7 +88,8 @@ protected override void ProcessRecord()
8588
TimeSpan authTimeout = new TimeSpan(0, 0, Constants.MaxDeviceCodeTimeOut);
8689
cancellationTokenSource = new CancellationTokenSource(authTimeout);
8790
authContext.AuthType = AuthenticationType.Delegated;
88-
authContext.Scopes = Scopes ?? new string[] { "User.Read" };
91+
string[] processedScopes = ProcessScopes(Scopes);
92+
authContext.Scopes = processedScopes.Length == 0 ? new string[] { "User.Read" } : processedScopes;
8993
// Default to CurrentUser but allow the customer to change this via `ContextScope` param.
9094
authContext.ContextScope = this.IsParameterBound(nameof(ContextScope)) ? ContextScope : ContextScope.CurrentUser;
9195
}
@@ -190,6 +194,31 @@ protected override void StopProcessing()
190194
base.StopProcessing();
191195
}
192196

197+
/// <summary>
198+
/// Processes user provided scopes by removing whitespace and splitting comma separated scopes.
199+
/// </summary>
200+
/// <param name="scopes">An array of scopes.</param>
201+
/// <returns>A formated array of scopes.</returns>
202+
private string[] ProcessScopes(string[] scopes)
203+
{
204+
if (scopes == null)
205+
{
206+
return new string[0];
207+
}
208+
209+
List<string> formatedScopes = new List<string>();
210+
foreach (string scope in scopes)
211+
{
212+
string[] cleanScopes = scope.Split(',')
213+
.Select(s => s.Trim())
214+
.Where(s => !string.IsNullOrWhiteSpace(s))
215+
.ToArray();
216+
217+
formatedScopes.AddRange(cleanScopes);
218+
}
219+
return formatedScopes.ToArray();
220+
}
221+
193222
private void ValidateParameters()
194223
{
195224
switch (ParameterSetName)

src/Authentication/Authentication/Cmdlets/DisconnectGraph.cs renamed to src/Authentication/Authentication/Cmdlets/DisconnectMgGraph.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets
66
using Microsoft.Graph.PowerShell.Authentication.Helpers;
77
using System;
88
using System.Management.Automation;
9-
[Cmdlet(VerbsCommunications.Disconnect, "Graph")]
10-
public class DisconnectGraph : PSCmdlet
9+
[Cmdlet(VerbsCommunications.Disconnect, "MgGraph")]
10+
[Alias("Disconnect-Graph")]
11+
public class DisconnectMgGraph : PSCmdlet
1112
{
1213
protected override void BeginProcessing()
1314
{

src/Authentication/Authentication/Cmdlets/InvokeGraphRequest.cs renamed to src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222

2323
namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets
2424
{
25-
[Cmdlet(VerbsLifecycle.Invoke, "GraphRequest", DefaultParameterSetName = Constants.UserParameterSet)]
26-
public class InvokeGraphRequest : PSCmdlet
25+
[Cmdlet(VerbsLifecycle.Invoke, "MgGraphRequest", DefaultParameterSetName = Constants.UserParameterSet)]
26+
[Alias("Invoke-GraphRequest")]
27+
public class InvokeMgGraphRequest : PSCmdlet
2728
{
2829
private readonly CancellationTokenSource _cancellationTokenSource;
2930
private readonly InvokeGraphRequestUserAgent _graphRequestUserAgent;
3031
private string _originalFilePath;
3132

32-
public InvokeGraphRequest()
33+
public InvokeMgGraphRequest()
3334
{
3435
_cancellationTokenSource = new CancellationTokenSource();
3536
_graphRequestUserAgent = new InvokeGraphRequestUserAgent(this);

src/Authentication/Authentication/Helpers/HttpHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers
1717
public static class HttpHelpers
1818
{
1919
/// The version for current assembly.
20-
private static AssemblyName AssemblyInfo = typeof(ConnectGraph).GetTypeInfo().Assembly.GetName();
20+
private static AssemblyName AssemblyInfo = typeof(ConnectMgGraph).GetTypeInfo().Assembly.GetName();
2121

2222
/// The value for the Auth module version header.
2323
private static string AuthModuleVersionHeaderValue =

src/Authentication/Authentication/Helpers/PathUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers
1313
/// </summary>
1414
internal static class PathUtils
1515
{
16-
public static string ResolveFilePath(string filePath, InvokeGraphRequest command, bool isLiteralPath)
16+
public static string ResolveFilePath(string filePath, InvokeMgGraphRequest command, bool isLiteralPath)
1717
{
1818
string path = null;
1919
try

src/Authentication/Authentication/Helpers/StreamHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal static string StreamToString(this Stream stream, Encoding encoding)
7979
}
8080

8181
internal static void SaveStreamToFile(this Stream baseResponseStream, string filePath,
82-
InvokeGraphRequest invokeGraphRequest, CancellationToken token)
82+
InvokeMgGraphRequest invokeGraphRequest, CancellationToken token)
8383
{
8484
// If the web cmdlet should resume, append the file instead of overwriting.
8585
const FileMode fileMode = FileMode.Create;

src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ FormatsToProcess = './Microsoft.Graph.Authentication.format.ps1xml'
7272
FunctionsToExport = @()
7373

7474
# 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.
75-
CmdletsToExport = 'Connect-Graph', 'Disconnect-Graph', 'Get-MgContext', 'Get-MgProfile',
76-
'Select-MgProfile', 'Invoke-GraphRequest'
75+
CmdletsToExport = 'Connect-MgGraph', 'Disconnect-MgGraph', 'Get-MgContext', 'Get-MgProfile',
76+
'Select-MgProfile', 'Invoke-MgGraphRequest'
7777

7878
# Variables to export from this module
7979
# VariablesToExport = @()
8080

8181
# 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.
82-
AliasesToExport = @()
82+
AliasesToExport = @('Connect-Graph', 'Disconnect-Graph', 'Invoke-GraphRequest')
8383

8484
# DSC resources to export from this module
8585
# DscResourcesToExport = @()

0 commit comments

Comments
 (0)